Krita Source Code Documentation
Loading...
Searching...
No Matches
KisAllResourcesModel Class Reference

The KisAllresourcesModel class provides access to the cache database for a particular resource type. Instances should be retrieved using KisResourceModelProvider. All resources are part of this model, active and inactive, from all storages, active and inactive. More...

#include <KisResourceModel.h>

+ Inheritance diagram for KisAllResourcesModel:

Classes

struct  Private
 

Public Member Functions

bool addResource (KoResourceSP resource, const QString &storageId=QString("")) override
 addResource adds the given resource to the database and storage. If the resource already exists in the given storage with md5, filename or name, the existing resource will be updated instead. If the existing resource was inactive, it will be active (undeleted).
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role) const override
 
bool exportResource (KoResourceSP resource, QIODevice *device) override
 exportResource exports a resource into a QIODevice
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
KoResourceSP importResource (const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId=QString("")) override
 importResource imports a resource from a QIODevice
 
KoResourceSP importResourceFile (const QString &filename, const bool allowOverwrite, const QString &storageId=QString("")) override
 importResourceFile
 
bool importWillOverwriteResource (const QString &fileName, const QString &storageLocation=QString()) const override
 importWillOverwriteResource checks is importing a resource with this filename will overwrite anything
 
QModelIndex indexForResource (KoResourceSP resource) const override
 indexFromResource
 
QModelIndex indexForResourceId (int resourceId) const override
 indexFromResource
 
bool reloadResource (KoResourceSP resource) override
 reloadResource
 
bool renameResource (KoResourceSP resource, const QString &name) override
 renameResource name the given resource. The resource will have its name field reset, will be saved to the storage and there will be a new version created in the database.
 
bool resourceExists (const QString &md5, const QString &filename, const QString &name)
 resourceExists checks whether there is a resource with, in order, the given md5, the filename or the resource name.
 
KoResourceSP resourceForId (int id) const
 
KoResourceSP resourceForIndex (QModelIndex index=QModelIndex()) const override
 resourceForIndex returns a properly versioned and id'ed resource object
 
QVector< KoResourceSPresourcesForFilename (QString filename) const
 
QVector< KoResourceSPresourcesForMD5 (const QString &md5sum) const
 
QVector< KoResourceSPresourcesForName (const QString &name) const
 
QHash< int, QByteArray > roleNames () const override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 
bool setResourceActive (const QModelIndex &index, bool value) override
 setResourceActive changes 'active' state of the resource
 
bool setResourceMetaData (KoResourceSP resource, QMap< QString, QVariant > metadata) override
 setResourceMetaData
 
QVector< KisTagSPtagsForResource (int resourceId) const
 
bool updateResource (KoResourceSP resource) override
 updateResource creates a new version of the resource in the storage and in the database. This will also set the resource to active if it was inactive.
 
 ~KisAllResourcesModel () override
 
- Public Member Functions inherited from KisAbstractResourceModel
bool setResourceInactive (const QModelIndex &index)
 
virtual ~KisAbstractResourceModel ()
 

Private Slots

void beginExternalResourceImport (const QString &resourceType, int numResources)
 
void beginExternalResourceRemove (const QString &resourceType, const QVector< int > &resourceId)
 
void endExternalResourceImport (const QString &resourceType)
 
void endExternalResourceRemove (const QString &resourceType)
 
void slotResourceActiveStateChanged (const QString &resourceType, int resourceId)
 
void storageActiveStateChanged (const QString &location)
 
void storageResynchronized (const QString &storage, bool isBulkResynchronization)
 
void storagesBulkSynchronizationFinished ()
 

Private Member Functions

void closeQuery ()
 
 KisAllResourcesModel (const QString &resourceType, QObject *parent=0)
 
bool prepareQuery ()
 
bool resetQuery ()
 

Private Attributes

Private *const d
 

Friends

class KisResourceModel
 
class KisResourceModelProvider
 
class KisResourceQueryMapper
 

Additional Inherited Members

- Public Types inherited from KisAbstractResourceModel
enum  Columns {
  Id = 0 , StorageId , Name , Filename ,
  Tooltip , Thumbnail , Status , Location ,
  ResourceType , Tags , MD5 , LargeThumbnail ,
  Dirty , MetaData , ResourceActive , StorageActive ,
  BrokenStatus , BrokenStatusMessage
}
 The Columns enum indexes the columns in the model. To get the thumbnail for a particular resource, create the index with QModelIndex(row, Thumbnail). More...
 

Detailed Description

The KisAllresourcesModel class provides access to the cache database for a particular resource type. Instances should be retrieved using KisResourceModelProvider. All resources are part of this model, active and inactive, from all storages, active and inactive.

Definition at line 236 of file KisResourceModel.h.

Constructor & Destructor Documentation

◆ KisAllResourcesModel()

KisAllResourcesModel::KisAllResourcesModel ( const QString & resourceType,
QObject * parent = 0 )
private

we don't handle KisResourceLocator::storage{Added,Removed} signals here, we use per-resource notifications from KisResourceLocator instead

Definition at line 33 of file KisResourceModel.cpp.

34 : QAbstractTableModel(parent)
35 , d(new Private)
36{
37
40
45
51
52 d->resourceType = resourceType;
53
55 resetQuery();
56}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void endExternalResourceImport(const QString &resourceType)
void slotResourceActiveStateChanged(const QString &resourceType, int resourceId)
void beginExternalResourceImport(const QString &resourceType, int numResources)
void beginExternalResourceRemove(const QString &resourceType, const QVector< int > &resourceId)
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
void endExternalResourceRemove(const QString &resourceType)
void storageActiveStateChanged(const QString &location)
void endExternalResourceRemove(const QString &resourceType)
Emitted when the locator finished importing the embedded resource.
void beginExternalResourceImport(const QString &resourceType, int numResources)
Emitted when the locator needs to add an embedded resource.
void beginExternalResourceRemove(const QString &resourceType, const QVector< int > resourceIds)
Emitted when the locator needs to add an embedded resource.
void endExternalResourceImport(const QString &resourceType)
Emitted when the locator finished importing the embedded resource.
void resourceActiveStateChanged(const QString &resourceType, int resourceId)
Emitted when a resource changes its active state.
static KisResourceLocator * instance()
static KisStorageModel * instance()
void storageDisabled(const QString &storage)
void storageEnabled(const QString &storage)
void storageResynchronized(const QString &storage, bool isBulkResynchronization)
Emitted when an individual storage is initialized.
void storagesBulkSynchronizationFinished()
Emitted on loading when all the storages are finished initialization.

References KisResourceLocator::beginExternalResourceImport(), beginExternalResourceImport(), beginExternalResourceRemove(), KisResourceLocator::beginExternalResourceRemove(), connect(), d, KisResourceLocator::endExternalResourceImport(), endExternalResourceImport(), KisResourceLocator::endExternalResourceRemove(), endExternalResourceRemove(), KisResourceLocator::instance(), KisStorageModel::instance(), prepareQuery(), resetQuery(), KisResourceLocator::resourceActiveStateChanged(), KisAllResourcesModel::Private::resourceType, slotResourceActiveStateChanged(), storageActiveStateChanged(), KisStorageModel::storageDisabled(), KisStorageModel::storageEnabled(), storageResynchronized(), KisStorageModel::storageResynchronized(), storagesBulkSynchronizationFinished(), and KisStorageModel::storagesBulkSynchronizationFinished().

◆ ~KisAllResourcesModel()

KisAllResourcesModel::~KisAllResourcesModel ( )
override

Definition at line 58 of file KisResourceModel.cpp.

59{
60 delete d;
61}

References d.

Member Function Documentation

◆ addResource()

bool KisAllResourcesModel::addResource ( KoResourceSP resource,
const QString & storageId = QString("") )
overridevirtual

addResource adds the given resource to the database and storage. If the resource already exists in the given storage with md5, filename or name, the existing resource will be updated instead. If the existing resource was inactive, it will be active (undeleted).

Parameters
resourcethe resource itself
storageIdthe id of the storage (could be "memory" for temporary resources, the document's storage id for document storages or empty to save to the default resources folder
Returns
true if adding the resource succeeded.

Implements KisAbstractResourceModel.

Definition at line 468 of file KisResourceModel.cpp.

469{
470 if (!resource || !resource->valid()) {
471 qWarning() << "Cannot add resource. Resource is null or not valid";
472 return false;
473 }
474
475 bool r = true;
476 beginInsertRows(QModelIndex(), rowCount(), rowCount());
477 if (!KisResourceLocator::instance()->addResource(d->resourceType, resource, storageId)) {
478 qWarning() << "Failed to add resource" << resource->name();
479 r = false;
480 }
481 resetQuery();
482 endInsertRows();
483
484 return r;
485}
bool addResource(KoResourceSP resource, const QString &storageId=QString("")) override
addResource adds the given resource to the database and storage. If the resource already exists in th...
int rowCount(const QModelIndex &parent=QModelIndex()) const override

References addResource(), d, KisResourceLocator::instance(), resetQuery(), KisAllResourcesModel::Private::resourceType, and rowCount().

◆ beginExternalResourceImport

void KisAllResourcesModel::beginExternalResourceImport ( const QString & resourceType,
int numResources )
privateslot

A special connection for KisResourceLocator, which can import a resource on its own (all other places are supposed to do that via KisResourceModel). This call is needed to make sure the internal query in the model is reset.

WARNING: the resource is expected to be added to the end of the model, that is, its resourceId is expected to be greater than any existing resource.

Definition at line 721 of file KisResourceModel.cpp.

722{
723 if (resourceType != d->resourceType) return;
724
725 beginInsertRows(QModelIndex(), rowCount(), rowCount() + numResources - 1);
726}

References d, KisAllResourcesModel::Private::resourceType, and rowCount().

◆ beginExternalResourceRemove

void KisAllResourcesModel::beginExternalResourceRemove ( const QString & resourceType,
const QVector< int > & resourceId )
privateslot

A special connection for KisResourceLocator, which can remove the resource while importing something with overwrite. In such a case the locator will Q_EMIT both, remove and insert signals for both the resources.

Definition at line 736 of file KisResourceModel.cpp.

737{
738 if (resourceType != d->resourceType) return;
739
740 Q_FOREACH (int resourceId, resourceIds) {
741 const QModelIndex index = indexForResourceId(resourceId);
742 if (index.isValid()) {
743 beginRemoveRows(QModelIndex(), index.row(), index.row());
745 } else {
746 // it's fine if the index is invalid; it probably means it's one of the duplicates (another resource with the same type and content was already in the database)
747 dbgResources << "KisAllResourcesModel::beginExternalResourceRemove got invalid index" << index << "for resourceId" << resourceId
748 << "of type" << resourceType << "(possibly the resource was deduplicated via sql query and that's why it doesn't appear in the model)";
749 }
750 }
751}
QModelIndex indexForResourceId(int resourceId) const override
indexFromResource
#define dbgResources
Definition kis_debug.h:43

References d, dbgResources, KisAllResourcesModel::Private::externalResourcesRemovedCount, indexForResourceId(), and KisAllResourcesModel::Private::resourceType.

◆ closeQuery()

void KisAllResourcesModel::closeQuery ( )
private

Definition at line 585 of file KisResourceModel.cpp.

586{
587 d->resourcesQuery.clear();
588}

References d, and KisAllResourcesModel::Private::resourcesQuery.

◆ columnCount()

int KisAllResourcesModel::columnCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 63 of file KisResourceModel.cpp.

64{
65 if (parent.isValid()) {
66 return 0;
67 }
68
69 return d->columnCount;
70}
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References KisAllResourcesModel::Private::columnCount, and d.

◆ data()

QVariant KisAllResourcesModel::data ( const QModelIndex & index,
int role ) const
override

Definition at line 72 of file KisResourceModel.cpp.

73{
74
75 QVariant v;
76 if (!index.isValid()) return v;
77
78 if (index.row() > rowCount()) return v;
79 if (index.column() > d->columnCount) return v;
80
81 bool pos = const_cast<KisAllResourcesModel*>(this)->d->resourcesQuery.seek(index.row());
82
83 if (pos) {
85 }
86
87 return v;
88}
qreal v
The KisAllresourcesModel class provides access to the cache database for a particular resource type....
static QVariant variantFromResourceQuery(const QSqlQuery &query, int column, int role, bool useResourcePrefix)
variantFromResourceQuery returns a QVariant for the given column and or role

References KisAllResourcesModel::Private::columnCount, d, KisAllResourcesModel::Private::resourcesQuery, rowCount(), v, and KisResourceQueryMapper::variantFromResourceQuery().

◆ endExternalResourceImport

void KisAllResourcesModel::endExternalResourceImport ( const QString & resourceType)
privateslot
See also
beginExternalResourceImport

Definition at line 728 of file KisResourceModel.cpp.

729{
730 if (resourceType != d->resourceType) return;
731
732 resetQuery();
733 endInsertRows();
734}

References d, resetQuery(), and KisAllResourcesModel::Private::resourceType.

◆ endExternalResourceRemove

void KisAllResourcesModel::endExternalResourceRemove ( const QString & resourceType)
privateslot
See also
beginExternalResourceRemove

Definition at line 753 of file KisResourceModel.cpp.

754{
755 if (resourceType != d->resourceType) return;
756
758 resetQuery();
759 }
760 for (int i = 0; i < d->externalResourcesRemovedCount; i++) {
761 endRemoveRows();
762 }
763
765}

References d, KisAllResourcesModel::Private::externalResourcesRemovedCount, resetQuery(), and KisAllResourcesModel::Private::resourceType.

◆ exportResource()

bool KisAllResourcesModel::exportResource ( KoResourceSP resource,
QIODevice * device )
overridevirtual

exportResource exports a resource into a QIODevice

Exporting a resource as a binary blob is the only way to guarantee that its MD5 checksum is kept persistent. The underlying storage will just copy bytes into the device without doing any conversions

Parameters
resourcethe resource to be exported
devicedevice where the resource should be written to
Returns
true if export operation has been successful

Implements KisAbstractResourceModel.

Definition at line 459 of file KisResourceModel.cpp.

460{
461 bool res = KisResourceLocator::instance()->exportResource(resource, device);
462 if (!res) {
463 qWarning() << "Failed to export resource" << resource->signature();
464 }
465 return res;
466}
bool exportResource(KoResourceSP resource, QIODevice *device)
exportResource

References KisResourceLocator::exportResource(), and KisResourceLocator::instance().

◆ flags()

Qt::ItemFlags KisAllResourcesModel::flags ( const QModelIndex & index) const
override

Definition at line 151 of file KisResourceModel.cpp.

152{
153 if (!index.isValid()) {
154 return Qt::NoItemFlags;
155 }
156 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemNeverHasChildren;
157}

◆ headerData()

QVariant KisAllResourcesModel::headerData ( int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole ) const
override

Definition at line 90 of file KisResourceModel.cpp.

91{
92 if (role != Qt::DisplayRole) {
93 return {};
94 }
95 if (orientation == Qt::Horizontal) {
96 switch (section) {
97 case Id:
98 return i18n("Id");
99 case StorageId:
100 return i18n("Storage ID");
101 case Name:
102 return i18n("Name");
103 case Filename:
104 return i18n("File Name");
105 case Tooltip:
106 return i18n("Tooltip");
107 case Thumbnail:
108 return i18n("Image");
109 case Status:
110 return i18n("Status");
111 case Location:
112 return i18n("Location");
113 case ResourceType:
114 return i18n("Resource Type");
115 case ResourceActive:
116 return i18n("Active");
117 case StorageActive:
118 return i18n("Storage Active");
119 case MD5:
120 return i18n("md5sum");
121 case Tags:
122 return i18n("Tags");
123 case LargeThumbnail:
124 return i18n("Large Thumbnail");
125 case Dirty:
126 return i18n("Dirty");
127 case MetaData:
128 return i18n("Metadata");
129 case BrokenStatus:
130 return i18n("Broken Status");
132 return i18n("Broken Status Message");
133 default:
134 return QString::number(section);
135 }
136 }
137 return {};
138}
@ Status
Whether the resource is active. Duplicate of ResourceActive.
@ ResourceActive
Whether the current resource is active.
@ Dirty
A dirty resource is one that has been modified locally but not saved.
@ LargeThumbnail
A larger thumbnail for displaying in a tooltip. 200x200 or so.
@ MetaData
MetaData is a map of key, value pairs that is associated with this resource.
@ BrokenStatus
Whether the resource is broken (bool)
@ StorageActive
Whether the current resource's storage is active.

References KisAbstractResourceModel::BrokenStatus, KisAbstractResourceModel::BrokenStatusMessage, KisAbstractResourceModel::Dirty, KisAbstractResourceModel::Filename, KisAbstractResourceModel::Id, KisAbstractResourceModel::LargeThumbnail, KisAbstractResourceModel::Location, KisAbstractResourceModel::MD5, KisAbstractResourceModel::MetaData, KisAbstractResourceModel::Name, KisAbstractResourceModel::ResourceActive, KisAbstractResourceModel::ResourceType, KisAbstractResourceModel::Status, KisAbstractResourceModel::StorageActive, KisAbstractResourceModel::StorageId, KisAbstractResourceModel::Tags, KisAbstractResourceModel::Thumbnail, and KisAbstractResourceModel::Tooltip.

◆ importResource()

KoResourceSP KisAllResourcesModel::importResource ( const QString & filename,
QIODevice * device,
const bool allowOverwrite,
const QString & storageId = QString("") )
overridevirtual

importResource imports a resource from a QIODevice

Importing a resource from a binary blob is the only way to guarantee that its MD5 checksum is kept persistent. The underlying storage will just copy bytes into its location.

Parameters
filenamefile name of the resource if preset. File name may be used for addressing the resource, so it is usually preferred to preserve it.
devicedevice where the resource should be read from
Returns
the loaded resource object

Implements KisAbstractResourceModel.

Definition at line 442 of file KisResourceModel.cpp.

443{
444 KoResourceSP importedResource = KisResourceLocator::instance()->importResource(d->resourceType, filename, device, allowOverwrite, storageId);
445
446 if (!importedResource) {
447 qWarning() << "Failed to import resource" << filename;
448 }
449 resetQuery();
450
451 return importedResource;
452}
KoResourceSP importResource(const QString &resourceType, const QString &fileName, QIODevice *device, const bool allowOverwrite, const QString &storageLocation=QString())
importResource

References d, KisResourceLocator::importResource(), KisResourceLocator::instance(), resetQuery(), and KisAllResourcesModel::Private::resourceType.

◆ importResourceFile()

KoResourceSP KisAllResourcesModel::importResourceFile ( const QString & filename,
const bool allowOverwrite,
const QString & storageId = QString("") )
overridevirtual

importResourceFile

Parameters
filename
Returns

Implements KisAbstractResourceModel.

Definition at line 430 of file KisResourceModel.cpp.

431{
432 KoResourceSP importedResource = KisResourceLocator::instance()->importResourceFromFile(d->resourceType, filename, allowOverwrite, storageId);
433
434 if (!importedResource) {
435 qWarning() << "Failed to import resource" << filename;
436 }
437 resetQuery();
438
439 return importedResource;
440}
KoResourceSP importResourceFromFile(const QString &resourceType, const QString &fileName, const bool allowOverwrite, const QString &storageLocation=QString())
importResourceFromFile

References d, KisResourceLocator::importResourceFromFile(), KisResourceLocator::instance(), resetQuery(), and KisAllResourcesModel::Private::resourceType.

◆ importWillOverwriteResource()

bool KisAllResourcesModel::importWillOverwriteResource ( const QString & fileName,
const QString & storageLocation = QString() ) const
overridevirtual

importWillOverwriteResource checks is importing a resource with this filename will overwrite anything

If this function returns true, then importResource() is guaranteed to fail with 'allowOverwrite' set to false.

Parameters
filenamefile name of the resource if preset. File name may be used for addressing the resource, so it is usually preferred to preserve it.
Returns
true if the some existing will be overwritten while importing

Implements KisAbstractResourceModel.

Definition at line 454 of file KisResourceModel.cpp.

455{
456 return KisResourceLocator::instance()->importWillOverwriteResource(d->resourceType, fileName, storageLocation);
457}
bool importWillOverwriteResource(const QString &resourceType, const QString &fileName, const QString &storageLocation=QString()) const
return whether importing will overwrite some existing resource

References d, KisResourceLocator::importWillOverwriteResource(), KisResourceLocator::instance(), and KisAllResourcesModel::Private::resourceType.

◆ indexForResource()

QModelIndex KisAllResourcesModel::indexForResource ( KoResourceSP resource) const
overridevirtual

indexFromResource

Parameters
resource
Returns

Implements KisAbstractResourceModel.

Definition at line 392 of file KisResourceModel.cpp.

393{
394 if (!resource || !resource->valid() || resource->resourceId() < 0) return QModelIndex();
395
396 // For now a linear seek to find the first resource with the right id
397 return indexForResourceId(resource->resourceId());
398}

References indexForResourceId().

◆ indexForResourceId()

QModelIndex KisAllResourcesModel::indexForResourceId ( int resourceId) const
overridevirtual

indexFromResource

Parameters
resourceIdresource id for which we want to get an index
Returns

Implements KisAbstractResourceModel.

Definition at line 400 of file KisResourceModel.cpp.

401{
402 if (!d->resourcesQuery.first()) {
403 return QModelIndex();
404 }
405
406 do {
407 if (d->resourcesQuery.value("id").toInt() == resourceId) {
408 return index(d->resourcesQuery.at(), 0);
409 }
410 } while (d->resourcesQuery.next());
411
412 return QModelIndex();
413}

References d, and KisAllResourcesModel::Private::resourcesQuery.

◆ prepareQuery()

bool KisAllResourcesModel::prepareQuery ( )
private

Definition at line 551 of file KisResourceModel.cpp.

552{
553 bool r = d->resourcesQuery.prepare(
554 "SELECT resources.id\n"
555 ", resources.storage_id\n"
556 ", resources.name\n"
557 ", resources.filename\n"
558 ", resources.tooltip\n"
559 ", resources.status\n"
560 ", resources.md5sum\n"
561 ", storages.location\n"
562 ", resource_types.name as resource_type\n"
563 ", resources.status as resource_active\n"
564 ", storages.active as storage_active\n"
565 "FROM resources\n"
566 ", resource_types\n"
567 ", storages\n"
568 "WHERE resources.resource_type_id = resource_types.id\n"
569 "AND resources.storage_id = storages.id\n"
570 "AND resource_types.name = :resource_type\n"
571 "GROUP BY resources.name\n"
572 ", resources.filename\n"
573 ", resources.md5sum\n"
574 "ORDER BY resources.id");
575
576 if (!r) {
577 qWarning() << "Could not prepare KisAllResourcesModel query" << d->resourcesQuery.lastError();
578 return false;
579 }
580
581 d->resourcesQuery.bindValue(":resource_type", d->resourceType);
582 return true;
583}

References d, KisAllResourcesModel::Private::resourcesQuery, and KisAllResourcesModel::Private::resourceType.

◆ reloadResource()

bool KisAllResourcesModel::reloadResource ( KoResourceSP resource)
overridevirtual

reloadResource

Parameters
resource
Returns

We don't have to call reset query here, because reloading a resource doesn't change any database content.

Implements KisAbstractResourceModel.

Definition at line 504 of file KisResourceModel.cpp.

505{
506 if (!resource || !resource->valid()) {
507 qWarning() << "Cannot reload resource. Resource is null or not valid";
508 return false;
509 }
510
512 qWarning() << "Failed to reload resource" << resource;
513 return false;
514 }
515
521 QModelIndex index = indexForResource(resource);
522 Q_EMIT dataChanged(index, index);
523 return true;
524}
QModelIndex indexForResource(KoResourceSP resource) const override
indexFromResource
bool reloadResource(KoResourceSP resource) override
reloadResource

References d, indexForResource(), KisResourceLocator::instance(), reloadResource(), and KisAllResourcesModel::Private::resourceType.

◆ renameResource()

bool KisAllResourcesModel::renameResource ( KoResourceSP resource,
const QString & name )
overridevirtual

renameResource name the given resource. The resource will have its name field reset, will be saved to the storage and there will be a new version created in the database.

Parameters
resourceThe resource to rename
nameThe new name
Returns
true if the operation succeeded.

Implements KisAbstractResourceModel.

Definition at line 526 of file KisResourceModel.cpp.

527{
528 if (!resource || !resource->valid() || name.isEmpty()) {
529 qWarning() << "Cannot rename resources. Resource is NULL or not valid or name is empty";
530 return false;
531 }
532 resource->setName(name);
534 qWarning() << "Failed to rename resource" << resource << name;
535 return false;
536 }
537 bool r = resetQuery();
538 QModelIndex index = indexForResource(resource);
539 Q_EMIT dataChanged(index, index);
540 return r;
541}
bool updateResource(KoResourceSP resource) override
updateResource creates a new version of the resource in the storage and in the database....
const char * name(StandardAction id)

References d, indexForResource(), KisResourceLocator::instance(), resetQuery(), KisAllResourcesModel::Private::resourceType, and updateResource().

◆ resetQuery()

bool KisAllResourcesModel::resetQuery ( )
private

In case the query has been previously closed, try to restart it (used in unittests mostly)

Definition at line 590 of file KisResourceModel.cpp.

591{
596 if (!d->resourcesQuery.isValid()) {
597 d->resourcesQuery.clear();
598 prepareQuery();
599 }
600
601 bool r = d->resourcesQuery.exec();
602 if (!r) {
603 qWarning() << "Could not select" << d->resourceType << "resources" << d->resourcesQuery.lastError() << d->resourcesQuery.boundValues();
604 }
605 d->cachedRowCount = -1;
606
607 return r;
608}

References KisAllResourcesModel::Private::cachedRowCount, d, prepareQuery(), KisAllResourcesModel::Private::resourcesQuery, and KisAllResourcesModel::Private::resourceType.

◆ resourceExists()

bool KisAllResourcesModel::resourceExists ( const QString & md5,
const QString & filename,
const QString & name )

resourceExists checks whether there is a resource with, in order, the given md5, the filename or the resource name.

Definition at line 205 of file KisResourceModel.cpp.

206{
207 QSqlQuery q;
208
209 // md5
210
211 if (!md5.isEmpty()) {
212
213 bool r = q.prepare("SELECT resources.id AS id\n"
214 "FROM resources\n"
215 "WHERE md5sum = :md5sum");
216 if (!r) {
217 qWarning() << "Could not prepare find resourceExists by md5 query" << q.lastError();
218 }
219
220 q.bindValue(":mdsum", md5);
221
222 r = q.exec();
223
224 if (!r) {
225 qWarning() << "Could not execute resourceExists by md5 query" << q.lastError();
226 }
227
228 if (q.first()) {
229 return true;
230 }
231 }
232
233 // filename
234
235 if (!filename.isEmpty()) {
236
237 bool r = q.prepare("SELECT resources.id AS id\n"
238 "FROM resources\n"
239 "WHERE filename = :filename");
240 if (!r) {
241 qWarning() << "Could not prepare find resourceExists by filename query" << q.lastError();
242 }
243
244 q.bindValue(":filename", filename);
245
246 r = q.exec();
247
248 if (!r) {
249 qWarning() << "Could not execute resourceExists by filename query" << q.lastError();
250 }
251
252 if (q.first()) {
253 return true;
254 }
255 }
256
257 // name
258
259 if (!name.isEmpty()) {
260
261 bool r = q.prepare("SELECT resources.id AS id\n"
262 "FROM resources\n"
263 "WHERE name = :name");
264 if (!r) {
265 qWarning() << "Could not prepare find resourceExists by name query" << q.lastError();
266 }
267
268 q.bindValue(":name", name);
269
270 r = q.exec();
271 if (!r) {
272 qWarning() << "Could not execute resourceExists by name query" << q.lastError();
273 }
274
275 if (q.first()) {
276 return true;
277 }
278 }
279
280 // failure
281
282 return false;
283}

◆ resourceForId()

KoResourceSP KisAllResourcesModel::resourceForId ( int id) const

Definition at line 200 of file KisResourceModel.cpp.

201{
203}
KoResourceSP resourceForId(int resourceId)
resourceForId returns the resource with the given id, or 0 if no such resource exists....

References KisResourceLocator::instance(), and KisResourceLocator::resourceForId().

◆ resourceForIndex()

KoResourceSP KisAllResourcesModel::resourceForIndex ( QModelIndex index = QModelIndex()) const
overridevirtual

resourceForIndex returns a properly versioned and id'ed resource object

Implements KisAbstractResourceModel.

Definition at line 184 of file KisResourceModel.cpp.

185{
186 KoResourceSP resource = 0;
187
188 if (!index.isValid()) return resource;
189 if (index.row() > rowCount()) return resource;
190 if (index.column() > d->columnCount) return resource;
191
192 bool pos = const_cast<KisAllResourcesModel*>(this)->d->resourcesQuery.seek(index.row());
193 if (pos) {
194 int id = d->resourcesQuery.value("id").toInt();
195 resource = resourceForId(id);
196 }
197 return resource;
198}
KoResourceSP resourceForId(int id) const

References KisAllResourcesModel::Private::columnCount, d, resourceForId(), KisAllResourcesModel::Private::resourcesQuery, and rowCount().

◆ resourcesForFilename()

QVector< KoResourceSP > KisAllResourcesModel::resourcesForFilename ( QString filename) const

resourceForFilename returns the first resource with the given filename that is active and is in an active store. Note that the filename does not include a path to the storage, and if there are resources with the same filename in several active storages, only one resource is returned.

Parameters
filenamethe filename we're looking for
checkDependentResourcescheck whether we should try to find a resource embedded in a resource that's not been loaded yet in the metadata table.
Returns
a resource if one is found, or 0 if none are found

Definition at line 285 of file KisResourceModel.cpp.

286{
287 QVector<KoResourceSP> resources;
288
289 if (filename.isEmpty()) return resources;
290
291 QSqlQuery q;
292 bool r = q.prepare("SELECT resources.id AS id\n"
293 "FROM resources\n"
294 ", resource_types\n"
295 "WHERE resources.resource_type_id = resource_types.id\n"
296 "AND resources.filename = :resource_filename\n"
297 "AND resource_types.name = :resource_type\n");
298 if (!r) {
299 qWarning() << "Could not prepare KisAllResourcesModel query for resource name" << q.lastError();
300 }
301 q.bindValue(":resource_filename", filename);
302 q.bindValue(":resource_type", d->resourceType);
303
304 r = q.exec();
305 if (!r) {
306 qWarning() << "Could not select" << d->resourceType << "resources by filename" << q.lastError() << q.boundValues();
307 }
308
309 while (q.next()) {
310 int id = q.value("id").toInt();
312 if (resource) {
313 resources << resource;
314 }
315
316 }
317
318 return resources;
319}

References d, KisResourceLocator::instance(), KisResourceLocator::resourceForId(), and KisAllResourcesModel::Private::resourceType.

◆ resourcesForMD5()

QVector< KoResourceSP > KisAllResourcesModel::resourcesForMD5 ( const QString & md5sum) const

Definition at line 360 of file KisResourceModel.cpp.

361{
362 QVector<KoResourceSP> resources;
363
364 if (md5sum.isEmpty()) return resources;
365
366 KoResourceSP resource = 0;
367
368 QSqlQuery q;
369 bool r = q.prepare("SELECT resource_id AS id\n"
370 "FROM versioned_resources\n"
371 "WHERE md5sum = :md5sum");
372 if (!r) {
373 qWarning() << "Could not prepare KisAllResourcesModel query for resource md5" << q.lastError();
374 }
375 q.bindValue(":md5sum", md5sum);
376
377 r = q.exec();
378 if (!r) {
379 qWarning() << "Could not select" << d->resourceType << "resources by md5" << q.lastError() << q.boundValues();
380 }
381
382 while (q.next()) {
383 int id = q.value("id").toInt();
385 if (resource) {
386 resources << resource;
387 }
388 }
389 return resources;
390}

References d, KisResourceLocator::instance(), KisResourceLocator::resourceForId(), and KisAllResourcesModel::Private::resourceType.

◆ resourcesForName()

QVector< KoResourceSP > KisAllResourcesModel::resourcesForName ( const QString & name) const

resourceForName returns the first resource with the given name that is active and is in an active store. Note that if there are resources with the same name in several active storages, only one resource is returned.

Returns
a resource if one is found, or 0 if none are found

Definition at line 321 of file KisResourceModel.cpp.

322{
323 QVector<KoResourceSP> resources;
324
325 if (name.isEmpty()) return resources;
326
327 KoResourceSP resource = 0;
328
329 QSqlQuery q;
330 bool r = q.prepare("SELECT resources.id AS id\n"
331 "FROM resources\n"
332 ", resource_types\n"
333 "WHERE resources.resource_type_id = resource_types.id\n"
334 "AND resources.name = :resource_name\n"
335 "AND resource_types.name = :resource_type\n");
336 if (!r) {
337 qWarning() << "Could not prepare KisAllResourcesModel query for resource name" << q.lastError();
338 }
339
340 q.bindValue(":resource_type", d->resourceType);
341 q.bindValue(":resource_name", name);
342
343 r = q.exec();
344 if (!r) {
345 qWarning() << "Could not select" << d->resourceType << "resources by name" << q.lastError() << q.boundValues();
346 }
347
348 while (q.next()) {
349 int id = q.value("id").toInt();
351 if (resource) {
352 resources << resource;
353 }
354 }
355
356 return resources;
357}

References d, KisResourceLocator::instance(), KisResourceLocator::resourceForId(), and KisAllResourcesModel::Private::resourceType.

◆ roleNames()

QHash< int, QByteArray > KisAllResourcesModel::roleNames ( ) const
override

Definition at line 159 of file KisResourceModel.cpp.

160{
161 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
162 roles[Qt::UserRole + Id] = "id";
163 roles[Qt::UserRole + StorageId] = "storageId";
164 roles[Qt::UserRole + Name] = "name";
165 roles[Qt::UserRole + Filename] = "filename";
166 //roles[Qt::UserRole + Tooltip] = "tooltip";
167 roles[Qt::UserRole + Thumbnail] = "thumbnail";
168 roles[Qt::UserRole + Status] = "status";
169 roles[Qt::UserRole + Location] = "location";
170 roles[Qt::UserRole + ResourceType] = "resourcetype";
171 roles[Qt::UserRole + MD5] = "md5";
172 roles[Qt::UserRole + Tags] = "tags";
173 roles[Qt::UserRole + LargeThumbnail] = "largethumbnail";
174 roles[Qt::UserRole + Dirty] = "dirty";
175 roles[Qt::UserRole + MetaData] = "metadata";
176 roles[Qt::UserRole + ResourceActive] = "resourceactive";
177 roles[Qt::UserRole + StorageActive] = "storageactive";
178 roles[Qt::UserRole + BrokenStatus] = "brokenstatus";
179 roles[Qt::UserRole + BrokenStatusMessage] = "brokenstatusmessage";
180
181 return roles;
182}

References KisAbstractResourceModel::BrokenStatus, KisAbstractResourceModel::BrokenStatusMessage, KisAbstractResourceModel::Dirty, KisAbstractResourceModel::Filename, KisAbstractResourceModel::Id, KisAbstractResourceModel::LargeThumbnail, KisAbstractResourceModel::Location, KisAbstractResourceModel::MD5, KisAbstractResourceModel::MetaData, KisAbstractResourceModel::Name, KisAbstractResourceModel::ResourceActive, KisAbstractResourceModel::ResourceType, KisAbstractResourceModel::Status, KisAbstractResourceModel::StorageActive, KisAbstractResourceModel::StorageId, KisAbstractResourceModel::Tags, and KisAbstractResourceModel::Thumbnail.

◆ rowCount()

int KisAllResourcesModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

SQLite doesn't support COUNT(DISTINCT ...) over multiple columns, so we need to concatenate them manually on the fly. But SQLite doesn't support CONCAT function either, therefore we should use concatenation operator it provides.

Definition at line 646 of file KisResourceModel.cpp.

647{
648 if (parent.isValid()) {
649 return 0;
650 }
651
652 if (d->cachedRowCount < 0) {
660 QSqlQuery q;
661 bool r = q.prepare("SELECT COUNT(DISTINCT resources.name || resources.filename || resources.md5sum)\n"
662 "FROM resources\n"
663 ", resource_types\n"
664 "WHERE resources.resource_type_id = resource_types.id\n"
665 "AND resource_types.name = :resource_type\n");
666 if (!r) {
667 qWarning() << "Could not prepare all resources rowcount query" << q.lastError();
668 return 0;
669 }
670 q.bindValue(":resource_type", d->resourceType);
671 r = q.exec();
672 if (!r) {
673 qWarning() << "Could not execute all resources rowcount query" << q.lastError() << q.boundValues();
674 return 0;
675 }
676 q.first();
677
678 const_cast<KisAllResourcesModel*>(this)->d->cachedRowCount = q.value(0).toInt();
679 }
680
681 return d->cachedRowCount;
682}

References KisAllResourcesModel::Private::cachedRowCount, d, and KisAllResourcesModel::Private::resourceType.

◆ setData()

bool KisAllResourcesModel::setData ( const QModelIndex & index,
const QVariant & value,
int role )
override

Definition at line 140 of file KisResourceModel.cpp.

141{
142 if (index.isValid() && role == Qt::CheckStateRole &&
143 value.canConvert<bool>()) {
144
145 return setResourceActive(index, value.toBool());
146 }
147
148 return true;
149}
float value(const T *src, size_t ch)
bool setResourceActive(const QModelIndex &index, bool value) override
setResourceActive changes 'active' state of the resource

References setResourceActive(), and value().

◆ setResourceActive()

bool KisAllResourcesModel::setResourceActive ( const QModelIndex & index,
bool value )
overridevirtual

setResourceActive changes 'active' state of the resource

Parameters
indexthe index of the resource
valuenew 'active' state of the resource
Returns
true if the new state has been assigned successfully

Implements KisAbstractResourceModel.

Definition at line 415 of file KisResourceModel.cpp.

416{
417 if (index.row() > rowCount()) return false;
418 if (index.column() > d->columnCount) return false;
419
420 int resourceId = index.data(Qt::UserRole + Id).toInt();
422 qWarning() << "Failed to change active state of the resource" << resourceId;
423 return false;
424 }
425
426 return true;
427}

References KisAllResourcesModel::Private::columnCount, d, KisAbstractResourceModel::Id, KisResourceLocator::instance(), rowCount(), setResourceActive(), and value().

◆ setResourceMetaData()

bool KisAllResourcesModel::setResourceMetaData ( KoResourceSP resource,
QMap< QString, QVariant > metadata )
overridevirtual

setResourceMetaData

Parameters
metadata
Returns

Implements KisAbstractResourceModel.

Definition at line 545 of file KisResourceModel.cpp.

546{
547 Q_ASSERT(resource->resourceId() > -1);
548 return KisResourceLocator::instance()->setMetaDataForResource(resource->resourceId(), metadata);
549}
bool setMetaDataForResource(int id, QMap< QString, QVariant > map) const
setMetaDataForResource

References KisResourceLocator::instance(), and KisResourceLocator::setMetaDataForResource().

◆ slotResourceActiveStateChanged

void KisAllResourcesModel::slotResourceActiveStateChanged ( const QString & resourceType,
int resourceId )
privateslot

A special connection for KisResourceLocator, which is triggered when the resource changes its 'active' state

Definition at line 767 of file KisResourceModel.cpp.

768{
769 if (resourceType != d->resourceType) return;
770 if (resourceId < 0) return;
771
772 resetQuery();
773
774 QModelIndex index = indexForResourceId(resourceId);
775
776 if (index.isValid()) {
777 Q_EMIT dataChanged(index, index, {Qt::CheckStateRole, Qt::UserRole + KisAbstractResourceModel::ResourceActive});
778 }
779}

References d, indexForResourceId(), resetQuery(), KisAbstractResourceModel::ResourceActive, and KisAllResourcesModel::Private::resourceType.

◆ storageActiveStateChanged

void KisAllResourcesModel::storageActiveStateChanged ( const QString & location)
privateslot

Definition at line 684 of file KisResourceModel.cpp.

685{
687 if (resourceIds.isEmpty()) return;
688
689 resetQuery();
690
691 Q_FOREACH (int resourceId, resourceIds) {
692 QModelIndex index = indexForResourceId(resourceId);
693
694 if (index.isValid()) {
695 Q_EMIT dataChanged(index, index, {Qt::UserRole + KisAbstractResourceModel::StorageActive});
696 }
697 }
698}
static QVector< int > resourcesForStorage(const QString &resourceType, const QString &storageLocation)

References d, indexForResourceId(), resetQuery(), KisResourceCacheDb::resourcesForStorage(), KisAllResourcesModel::Private::resourceType, and KisAbstractResourceModel::StorageActive.

◆ storageResynchronized

void KisAllResourcesModel::storageResynchronized ( const QString & storage,
bool isBulkResynchronization )
privateslot

Definition at line 700 of file KisResourceModel.cpp.

701{
702 Q_UNUSED(storage);
703
704 // we handle bulk-synchronization separately in slotStoragesBulkSynchronizationFinished()
705 if (isBulkResynchronization) return;
706
707 // TODO: ideally, we should use more fine-grained updates for
708 // updating the storages, but let's keep it this
709 beginResetModel();
710 resetQuery();
711 endResetModel();
712}

References resetQuery().

◆ storagesBulkSynchronizationFinished

void KisAllResourcesModel::storagesBulkSynchronizationFinished ( )
privateslot

Definition at line 714 of file KisResourceModel.cpp.

715{
716 beginResetModel();
717 resetQuery();
718 endResetModel();
719}

References resetQuery().

◆ tagsForResource()

QVector< KisTagSP > KisAllResourcesModel::tagsForResource ( int resourceId) const

Definition at line 610 of file KisResourceModel.cpp.

611{
612 bool r;
613
614 QSqlQuery q;
615
616 r = q.prepare("SELECT tags.url\n"
617 "FROM tags\n"
618 ", resource_tags\n"
619 ", resource_types\n"
620 "WHERE tags.active > 0\n" // make sure the tag is active
621 "AND tags.id = resource_tags.tag_id\n" // join tags + resource_tags by tag_id
622 "AND resource_tags.resource_id = :resource_id\n"
623 "AND resource_types.id = tags.resource_type_id\n" // make sure we're looking for tags for a specific resource
624 "AND resource_tags.active = 1\n"); // and the tag must be active
625 if (!r) {
626 qWarning() << "Could not prepare TagsForResource query" << q.lastError();
627 }
628
629 q.bindValue(":resource_id", resourceId);
630 r = q.exec();
631 if (!r) {
632 qWarning() << "Could not select tags for" << resourceId << q.lastError() << q.boundValues();
633 }
634
636 while (q.next()) {
637 KisTagSP tag = KisResourceLocator::instance()->tagForUrl(q.value(0).toString(), d->resourceType);
638 if (tag && tag->valid()) {
639 tags << tag;
640 }
641 }
642 return tags;
643}
KisTagSP tagForUrl(const QString &tagUrl, const QString resourceType)
tagForUrl create a tag from the database

References d, KisResourceLocator::instance(), KisAllResourcesModel::Private::resourceType, and KisResourceLocator::tagForUrl().

◆ updateResource()

bool KisAllResourcesModel::updateResource ( KoResourceSP resource)
overridevirtual

updateResource creates a new version of the resource in the storage and in the database. This will also set the resource to active if it was inactive.

Note: if the storage does not support versioning, updating the resource will fail.

Parameters
resource
Returns
true if the resource was successful updated,

Implements KisAbstractResourceModel.

Definition at line 487 of file KisResourceModel.cpp.

488{
489 if (!resource || !resource->valid()) {
490 qWarning() << "Cannot update resource. Resource is null or not valid";
491 return false;
492 }
493
495 qWarning() << "Failed to update resource" << resource;
496 return false;
497 }
498 bool r = resetQuery();
499 QModelIndex index = indexForResource(resource);
500 Q_EMIT dataChanged(index, index);
501 return r;
502}

References d, indexForResource(), KisResourceLocator::instance(), resetQuery(), KisAllResourcesModel::Private::resourceType, and updateResource().

Friends And Related Symbol Documentation

◆ KisResourceModel

friend class KisResourceModel
friend

Definition at line 242 of file KisResourceModel.h.

◆ KisResourceModelProvider

friend class KisResourceModelProvider
friend

Definition at line 241 of file KisResourceModel.h.

◆ KisResourceQueryMapper

friend class KisResourceQueryMapper
friend

Definition at line 243 of file KisResourceModel.h.

Member Data Documentation

◆ d

Private* const KisAllResourcesModel::d
private

Definition at line 359 of file KisResourceModel.h.


The documentation for this class was generated from the following files: