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).
 
bool addResourceDeduplicateFileName (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 the same filename, the resource will be renamed.
 
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 248 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}
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(), 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().

◆ addResourceDeduplicateFileName()

bool KisAllResourcesModel::addResourceDeduplicateFileName ( 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 the same filename, the resource will be renamed.

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 487 of file KisResourceModel.cpp.

488{
489 if (!resource || !resource->valid()) {
490 qWarning() << "Cannot add resource. Resource is null or not valid";
491 return false;
492 }
493
494 bool r = true;
495 beginInsertRows(QModelIndex(), rowCount(), rowCount());
497 qWarning() << "Failed to add resource with name deduplication" << resource->name();
498 r = false;
499 }
500 resetQuery();
501 endInsertRows();
502
503 return r;
504}
bool addResourceDeduplicateFileName(KoResourceSP resource, const QString &storageId=QString("")) override
addResource adds the given resource to the database and storage. If the resource already exists in th...

References addResourceDeduplicateFileName(), 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 740 of file KisResourceModel.cpp.

741{
742 if (resourceType != d->resourceType) return;
743
744 beginInsertRows(QModelIndex(), rowCount(), rowCount() + numResources - 1);
745}

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 755 of file KisResourceModel.cpp.

756{
757 if (resourceType != d->resourceType) return;
758
759 Q_FOREACH (int resourceId, resourceIds) {
760 const QModelIndex index = indexForResourceId(resourceId);
761 if (index.isValid()) {
762 beginRemoveRows(QModelIndex(), index.row(), index.row());
764 } else {
765 // 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)
766 dbgResources << "KisAllResourcesModel::beginExternalResourceRemove got invalid index" << index << "for resourceId" << resourceId
767 << "of type" << resourceType << "(possibly the resource was deduplicated via sql query and that's why it doesn't appear in the model)";
768 }
769 }
770}
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 604 of file KisResourceModel.cpp.

605{
606 d->resourcesQuery.clear();
607}

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 747 of file KisResourceModel.cpp.

748{
749 if (resourceType != d->resourceType) return;
750
751 resetQuery();
752 endInsertRows();
753}

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

◆ endExternalResourceRemove

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

Definition at line 772 of file KisResourceModel.cpp.

773{
774 if (resourceType != d->resourceType) return;
775
777 resetQuery();
778 }
779 for (int i = 0; i < d->externalResourcesRemovedCount; i++) {
780 endRemoveRows();
781 }
782
784}

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 570 of file KisResourceModel.cpp.

571{
572 bool r = d->resourcesQuery.prepare(
573 "SELECT resources.id\n"
574 ", resources.storage_id\n"
575 ", resources.name\n"
576 ", resources.filename\n"
577 ", resources.tooltip\n"
578 ", resources.status\n"
579 ", resources.md5sum\n"
580 ", storages.location\n"
581 ", resource_types.name as resource_type\n"
582 ", resources.status as resource_active\n"
583 ", storages.active as storage_active\n"
584 "FROM resources\n"
585 ", resource_types\n"
586 ", storages\n"
587 "WHERE resources.resource_type_id = resource_types.id\n"
588 "AND resources.storage_id = storages.id\n"
589 "AND resource_types.name = :resource_type\n"
590 "GROUP BY resources.name\n"
591 ", resources.filename\n"
592 ", resources.md5sum\n"
593 "ORDER BY resources.id");
594
595 if (!r) {
596 qWarning() << "Could not prepare KisAllResourcesModel query" << d->resourcesQuery.lastError();
597 return false;
598 }
599
600 d->resourcesQuery.bindValue(":resource_type", d->resourceType);
601 return true;
602}

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 523 of file KisResourceModel.cpp.

524{
525 if (!resource || !resource->valid()) {
526 qWarning() << "Cannot reload resource. Resource is null or not valid";
527 return false;
528 }
529
531 qWarning() << "Failed to reload resource" << resource;
532 return false;
533 }
534
540 QModelIndex index = indexForResource(resource);
541 Q_EMIT dataChanged(index, index);
542 return true;
543}
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 545 of file KisResourceModel.cpp.

546{
547 if (!resource || !resource->valid() || name.isEmpty()) {
548 qWarning() << "Cannot rename resources. Resource is NULL or not valid or name is empty";
549 return false;
550 }
551 resource->setName(name);
553 qWarning() << "Failed to rename resource" << resource << name;
554 return false;
555 }
556 bool r = resetQuery();
557 QModelIndex index = indexForResource(resource);
558 Q_EMIT dataChanged(index, index);
559 return r;
560}
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 609 of file KisResourceModel.cpp.

610{
615 if (!d->resourcesQuery.isValid()) {
616 d->resourcesQuery.clear();
617 prepareQuery();
618 }
619
620 bool r = d->resourcesQuery.exec();
621 if (!r) {
622 qWarning() << "Could not select" << d->resourceType << "resources" << d->resourcesQuery.lastError() << d->resourcesQuery.boundValues();
623 }
624 d->cachedRowCount = -1;
625
626 return r;
627}

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 665 of file KisResourceModel.cpp.

666{
667 if (parent.isValid()) {
668 return 0;
669 }
670
671 if (d->cachedRowCount < 0) {
679 QSqlQuery q;
680 bool r = q.prepare("SELECT COUNT(DISTINCT resources.name || resources.filename || resources.md5sum)\n"
681 "FROM resources\n"
682 ", resource_types\n"
683 "WHERE resources.resource_type_id = resource_types.id\n"
684 "AND resource_types.name = :resource_type\n");
685 if (!r) {
686 qWarning() << "Could not prepare all resources rowcount query" << q.lastError();
687 return 0;
688 }
689 q.bindValue(":resource_type", d->resourceType);
690 r = q.exec();
691 if (!r) {
692 qWarning() << "Could not execute all resources rowcount query" << q.lastError() << q.boundValues();
693 return 0;
694 }
695 q.first();
696
697 const_cast<KisAllResourcesModel*>(this)->d->cachedRowCount = q.value(0).toInt();
698 }
699
700 return d->cachedRowCount;
701}

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 564 of file KisResourceModel.cpp.

565{
566 Q_ASSERT(resource->resourceId() > -1);
567 return KisResourceLocator::instance()->setMetaDataForResource(resource->resourceId(), metadata);
568}
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 786 of file KisResourceModel.cpp.

787{
788 if (resourceType != d->resourceType) return;
789 if (resourceId < 0) return;
790
791 resetQuery();
792
793 QModelIndex index = indexForResourceId(resourceId);
794
795 if (index.isValid()) {
796 Q_EMIT dataChanged(index, index, {Qt::CheckStateRole, Qt::UserRole + KisAbstractResourceModel::ResourceActive});
797 }
798}

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

◆ storageActiveStateChanged

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

Definition at line 703 of file KisResourceModel.cpp.

704{
706 if (resourceIds.isEmpty()) return;
707
708 resetQuery();
709
710 Q_FOREACH (int resourceId, resourceIds) {
711 QModelIndex index = indexForResourceId(resourceId);
712
713 if (index.isValid()) {
714 Q_EMIT dataChanged(index, index, {Qt::UserRole + KisAbstractResourceModel::StorageActive});
715 }
716 }
717}
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 719 of file KisResourceModel.cpp.

720{
721 Q_UNUSED(storage);
722
723 // we handle bulk-synchronization separately in slotStoragesBulkSynchronizationFinished()
724 if (isBulkResynchronization) return;
725
726 // TODO: ideally, we should use more fine-grained updates for
727 // updating the storages, but let's keep it this
728 beginResetModel();
729 resetQuery();
730 endResetModel();
731}

References resetQuery().

◆ storagesBulkSynchronizationFinished

void KisAllResourcesModel::storagesBulkSynchronizationFinished ( )
privateslot

Definition at line 733 of file KisResourceModel.cpp.

734{
735 beginResetModel();
736 resetQuery();
737 endResetModel();
738}

References resetQuery().

◆ tagsForResource()

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

Definition at line 629 of file KisResourceModel.cpp.

630{
631 bool r;
632
633 QSqlQuery q;
634
635 r = q.prepare("SELECT tags.url\n"
636 "FROM tags\n"
637 ", resource_tags\n"
638 ", resource_types\n"
639 "WHERE tags.active > 0\n" // make sure the tag is active
640 "AND tags.id = resource_tags.tag_id\n" // join tags + resource_tags by tag_id
641 "AND resource_tags.resource_id = :resource_id\n"
642 "AND resource_types.id = tags.resource_type_id\n" // make sure we're looking for tags for a specific resource
643 "AND resource_tags.active = 1\n"); // and the tag must be active
644 if (!r) {
645 qWarning() << "Could not prepare TagsForResource query" << q.lastError();
646 }
647
648 q.bindValue(":resource_id", resourceId);
649 r = q.exec();
650 if (!r) {
651 qWarning() << "Could not select tags for" << resourceId << q.lastError() << q.boundValues();
652 }
653
655 while (q.next()) {
656 KisTagSP tag = KisResourceLocator::instance()->tagForUrl(q.value(0).toString(), d->resourceType);
657 if (tag && tag->valid()) {
658 tags << tag;
659 }
660 }
661 return tags;
662}
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 506 of file KisResourceModel.cpp.

507{
508 if (!resource || !resource->valid()) {
509 qWarning() << "Cannot update resource. Resource is null or not valid";
510 return false;
511 }
512
514 qWarning() << "Failed to update resource" << resource;
515 return false;
516 }
517 bool r = resetQuery();
518 QModelIndex index = indexForResource(resource);
519 Q_EMIT dataChanged(index, index);
520 return r;
521}

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

Friends And Related Symbol Documentation

◆ KisResourceModel

friend class KisResourceModel
friend

Definition at line 254 of file KisResourceModel.h.

◆ KisResourceModelProvider

friend class KisResourceModelProvider
friend

Definition at line 253 of file KisResourceModel.h.

◆ KisResourceQueryMapper

friend class KisResourceQueryMapper
friend

Definition at line 255 of file KisResourceModel.h.

Member Data Documentation

◆ d

Private* const KisAllResourcesModel::d
private

Definition at line 372 of file KisResourceModel.h.


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