Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourceModel.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef KISRESOURCEMODEL_H
8#define KISRESOURCEMODEL_H
9
10#include <QAbstractTableModel>
11#include <QSortFilterProxyModel>
12
13#include <kritaresources_export.h>
14
15#include <KoResource.h>
16#include <KisTag.h>
17
18
23class KRITARESOURCES_EXPORT KisAbstractResourceModel {
24
25public:
26
61
63
67 virtual KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const = 0;
68
74 virtual QModelIndex indexForResource(KoResourceSP resource) const = 0;
75
81 virtual QModelIndex indexForResourceId(int resourceId) const = 0;
82
89 virtual bool setResourceActive(const QModelIndex &index, bool value) = 0;
90
94 inline bool setResourceInactive(const QModelIndex &index) {
95 return setResourceActive(index, false);
96 }
97
103 virtual KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId = QString("")) = 0;
104
120 virtual KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId = QString("")) = 0;
121
134 virtual bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation = QString()) const = 0;
135
149 virtual bool exportResource(KoResourceSP resource, QIODevice *device) = 0;
150
163 virtual bool addResource(KoResourceSP resource, const QString &storageId = QString("")) = 0;
164
174 virtual bool updateResource(KoResourceSP resource) = 0;
175
181 virtual bool reloadResource(KoResourceSP resource) = 0;
182
191 virtual bool renameResource(KoResourceSP resource, const QString &name) = 0;
192
198 virtual bool setResourceMetaData(KoResourceSP resource, QMap<QString, QVariant> metadata) = 0;
199};
200
201class KRITARESOURCES_EXPORT KisAbstractResourceFilterInterface
202{
203public:
205
207 ShowInactiveResources = 0,
209 ShowAllResources
210 };
211
213 ShowInactiveStorages = 0,
215 ShowAllStorages
216 };
217public:
218
222 virtual void setResourceFilter(ResourceFilter filter) = 0;
223
227 virtual void setStorageFilter(StorageFilter filter) = 0;
228};
229
236class KRITARESOURCES_EXPORT KisAllResourcesModel : public QAbstractTableModel, public KisAbstractResourceModel
237{
238 Q_OBJECT
239
240private:
242 friend class KisResourceModel;
244 KisAllResourcesModel(const QString &resourceType, QObject *parent = 0);
245
246public:
247
248 ~KisAllResourcesModel() override;
249
250// QAbstractItemModel API
251
252 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
253 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
254 QVariant data(const QModelIndex &index, int role) const override;
255 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
256 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
257 Qt::ItemFlags flags(const QModelIndex &index) const override;
258 QHash<int, QByteArray> roleNames() const override;
259
260// Resources API
261
262 KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const override;
263 QModelIndex indexForResource(KoResourceSP resource) const override;
264 QModelIndex indexForResourceId(int resourceId) const override;
265 bool setResourceActive(const QModelIndex &index, bool value) override;
266 KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId = QString("")) override;
267 KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId = QString("")) override;
268 bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation = QString()) const override;
269 bool exportResource(KoResourceSP resource, QIODevice *device) override;
270 bool addResource(KoResourceSP resource, const QString &storageId = QString("")) override;
271 bool updateResource(KoResourceSP resource) override;
272 bool reloadResource(KoResourceSP resource) override;
273 bool renameResource(KoResourceSP resource, const QString &name) override;
274 bool setResourceMetaData(KoResourceSP resource, QMap<QString, QVariant> metadata) override;
275
276private Q_SLOTS:
277
278 void storageActiveStateChanged(const QString &location);
279 void storageResynchronized(const QString &storage, bool isBulkResynchronization);
280 void storagesBulkSynchronizationFinished();
281
292 void beginExternalResourceImport(const QString &resourceType, int numResources);
293
297 void endExternalResourceImport(const QString &resourceType);
298
304 void beginExternalResourceRemove(const QString &resourceType, const QVector<int> &resourceId);
305
309 void endExternalResourceRemove(const QString &resourceType);
310
315 void slotResourceActiveStateChanged(const QString &resourceType, int resourceId);
316
317public:
318
319 KoResourceSP resourceForId(int id) const;
320
325 bool resourceExists(const QString &md5, const QString &filename, const QString &name);
326
338 QVector<KoResourceSP> resourcesForFilename(QString filename) const;
339
348 QVector<KoResourceSP> resourcesForName(const QString &name) const;
349 QVector<KoResourceSP> resourcesForMD5(const QString &md5sum) const;
350 QVector<KisTagSP> tagsForResource(int resourceId) const;
351
352private:
353
354 bool prepareQuery();
355 bool resetQuery();
356 void closeQuery();
357
358 struct Private;
359 Private *const d;
360
361};
362
368class KRITARESOURCES_EXPORT KisResourceModel : public QSortFilterProxyModel, public KisAbstractResourceModel, public KisAbstractResourceFilterInterface
369{
370 Q_OBJECT
371
372public:
373
374 KisResourceModel(const QString &type, QObject *parent = 0);
375 ~KisResourceModel() override;
376
377
378 void setResourceFilter(ResourceFilter filter) override;
379 void setStorageFilter(StorageFilter filter) override;
380
381 void showOnlyUntaggedResources(bool showOnlyUntagged);
382
383public:
384
385 KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const override;
386 QModelIndex indexForResource(KoResourceSP resource) const override;
387 QModelIndex indexForResourceId(int resourceId) const override;
388 bool setResourceActive(const QModelIndex &index, bool value) override;
389 KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId = QString("")) override;
390 KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId = QString("")) override;
391 bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation = QString()) const override;
392 bool exportResource(KoResourceSP resource, QIODevice *device) override;
393 bool addResource(KoResourceSP resource, const QString &storageId = QString("")) override;
394 bool updateResource(KoResourceSP resource) override;
395 bool reloadResource(KoResourceSP resource) override;
396 bool renameResource(KoResourceSP resource, const QString &name) override;
397 bool setResourceMetaData(KoResourceSP resource, QMap<QString, QVariant> metadata) override;
398
399public:
400
401 KoResourceSP resourceForId(int id) const;
402
410 QVector<KoResourceSP> resourcesForFilename(QString fileName) const;
411
418 QVector<KoResourceSP> resourcesForName(QString name) const;
419 QVector<KoResourceSP> resourcesForMD5(const QString md5sum) const;
420 QVector<KisTagSP> tagsForResource(int resourceId) const;
421
422protected:
423
424 bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override;
425 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
426 bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
427
428private:
429 QVector<KoResourceSP> filterByColumn(const QString filter, KisAllResourcesModel::Columns column) const;
430 bool filterResource(const QModelIndex &idx) const;
431
432 struct Private;
433 Private *const d;
434
435 Q_DISABLE_COPY(KisResourceModel)
436
437};
438
439
440
441#endif // KISRESOURCEMODEL_H
float value(const T *src, size_t ch)
virtual void setStorageFilter(StorageFilter filter)=0
virtual void setResourceFilter(ResourceFilter filter)=0
virtual KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId=QString(""))=0
importResource imports a resource from a QIODevice
virtual QModelIndex indexForResource(KoResourceSP resource) const =0
indexFromResource
virtual bool exportResource(KoResourceSP resource, QIODevice *device)=0
exportResource exports a resource into a QIODevice
virtual bool updateResource(KoResourceSP resource)=0
updateResource creates a new version of the resource in the storage and in the database....
virtual KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const =0
resourceForIndex returns a properly versioned and id'ed resource object
virtual bool setResourceActive(const QModelIndex &index, bool value)=0
setResourceActive changes 'active' state of the resource
bool setResourceInactive(const QModelIndex &index)
virtual QModelIndex indexForResourceId(int resourceId) const =0
indexFromResource
Columns
The Columns enum indexes the columns in the model. To get the thumbnail for a particular resource,...
@ 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.
virtual KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId=QString(""))=0
importResourceFile
virtual bool addResource(KoResourceSP resource, const QString &storageId=QString(""))=0
addResource adds the given resource to the database and storage. If the resource already exists in th...
virtual bool renameResource(KoResourceSP resource, const QString &name)=0
renameResource name the given resource. The resource will have its name field reset,...
virtual bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation=QString()) const =0
importWillOverwriteResource checks is importing a resource with this filename will overwrite anything
virtual bool reloadResource(KoResourceSP resource)=0
reloadResource
virtual bool setResourceMetaData(KoResourceSP resource, QMap< QString, QVariant > metadata)=0
setResourceMetaData
The KisAllresourcesModel class provides access to the cache database for a particular resource type....
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...