diff --git a/libs/resources/KisResourceModel.h b/libs/resources/KisResourceModel.h index 4a358d3e07..24924acb22 100644 --- a/libs/resources/KisResourceModel.h +++ b/libs/resources/KisResourceModel.h @@ -1,312 +1,311 @@ /* * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KISRESOURCEMODEL_H #define KISRESOURCEMODEL_H #include #include #include #include #include /** * KisAbstractResourceModel defines the interface for accessing resources * that is used in KisResourceModel and the various filter/proxy models */ class KRITARESOURCES_EXPORT KisAbstractResourceModel { public: /** * @brief The Columns enum indexes the columns in the model. To get * the thumbnail for a particular resource, create the index with * QModelIndex(row, Thumbnail). */ enum Columns { Id = 0, StorageId, Name, Filename, Tooltip, Thumbnail, Status, Location, ResourceType, Tags, /// A larger thumbnail for displaying in a tooltip. 200x200 or so. LargeThumbnail, /// A dirty resource is one that has been modified locally but not saved Dirty, /// MetaData is a map of key, value pairs that is associated with this resource MetaData, /// XXX: what is this used for, again? KoResourceRole, /// Whether the current resource is active ResourceActive, /// Whether the current resource's storage isa ctive StorageActive }; virtual ~KisAbstractResourceModel(){} /** * @brief resourceForIndex * @param index * @return */ virtual KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const = 0; /** * @brief indexFromResource * @param resource * @return */ virtual QModelIndex indexFromResource(KoResourceSP resource) const = 0; /** * @brief setResourceInactive deactivates the specified resource * @param index * @return */ virtual bool setResourceInactive(const QModelIndex &index) = 0; /** * @brief importResourceFile * @param filename * @return */ virtual bool importResourceFile(const QString &filename) = 0; /** * @brief addResource adds the given resource to the database and storage * @param resource the resource itself * @param storageId the 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 * @return true if adding the resoruce succeeded. */ virtual bool addResource(KoResourceSP resource, const QString &storageId = QString()) = 0; /** * @brief updateResource * @param resource * @return */ virtual bool updateResource(KoResourceSP resource) = 0; /** * @brief 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. * @param resource The resource to rename * @param name The new name * @return true if the operation succeeded. */ virtual bool renameResource(KoResourceSP resource, const QString &name) = 0; /** * @brief setResourceInactive sets the specified resource's status to inactive * @param resource * @return */ virtual bool setResourceInactive(KoResourceSP resource) = 0; /** * @brief setResourceMetaData * @param metadata * @return */ virtual bool setResourceMetaData(KoResourceSP resource, QMap metadata) = 0; }; /** * @brief 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. */ class KRITARESOURCES_EXPORT KisAllResourcesModel : public QAbstractTableModel, public KisAbstractResourceModel { Q_OBJECT private: friend class KisResourceModel; KisAllResourcesModel(const QString &resourceType, QObject *parent = 0); public: ~KisAllResourcesModel() override; // QAbstractItemModel API int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; // Resources API /** * @brief resourceForIndex returns a properly versioned and id's resource object */ KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const override; QModelIndex indexFromResource(KoResourceSP resource) const override; bool setResourceInactive(const QModelIndex &index) override; bool setResourceInactive(KoResourceSP resource) override; bool importResourceFile(const QString &filename) override; bool addResource(KoResourceSP resource, const QString &storageId = QString()) override; bool updateResource(KoResourceSP resource) override; bool renameResource(KoResourceSP resource, const QString &name) override; bool setResourceMetaData(KoResourceSP resource, QMap metadata) override; private Q_SLOTS: void addStorage(const QString &location); void removeStorage(const QString &location); private: KoResourceSP resourceForId(int id) 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. * * @return a resource if one is found, or 0 if none are found */ KoResourceSP resourceForFilename(QString fileName) 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. * * @return a resource if one is found, or 0 if none are found */ KoResourceSP resourceForName(QString name) const; KoResourceSP resourceForMD5(const QByteArray md5sum) const; QVector tagsForResource(int resourceId) const; bool resetQuery(); struct Private; Private *const d; }; /** * @brief The KisResourceModel class provides the main access to resources. It is possible * to filter the resources returned by the active status flag of the resources and the * storages */ class KRITARESOURCES_EXPORT KisResourceModel : public QSortFilterProxyModel, public KisAbstractResourceModel { Q_OBJECT private: friend class TestResourceModel; friend class KisResourceModelProvider; KisResourceModel(const QString &type, QObject *parent = 0); public: ~KisResourceModel() override; enum ResourceFilter { ShowInactiveResources = 0, ShowActiveResources, ShowAllResources }; void setResourceFilter(ResourceFilter filter); enum StorageFilter { ShowInactiveStorages = 0, ShowActiveStorages, ShowAllStorages }; void setStorageFilter(StorageFilter filter); public: KoResourceSP resourceForIndex(QModelIndex index = QModelIndex()) const override; QModelIndex indexFromResource(KoResourceSP resource) const override; bool setResourceInactive(const QModelIndex &index) override; bool importResourceFile(const QString &filename) override; bool addResource(KoResourceSP resource, const QString &storageId = QString()) override; bool updateResource(KoResourceSP resource) override; bool renameResource(KoResourceSP resource, const QString &name) override; bool setResourceInactive(KoResourceSP resource) override; bool setResourceMetaData(KoResourceSP resource, QMap metadata) override; - public: KoResourceSP resourceForId(int id) 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. * * @return a resource if one is found, or 0 if none are found */ KoResourceSP resourceForFilename(QString fileName) 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. * * @return a resource if one is found, or 0 if none are found */ KoResourceSP resourceForName(QString name) const; KoResourceSP resourceForMD5(const QByteArray md5sum) const; QVector tagsForResource(int resourceId) const; protected: bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override; bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override; private: bool resetQuery(); struct Private; Private *const d; Q_DISABLE_COPY(KisResourceModel) }; #endif // KISRESOURCEMODEL_H diff --git a/libs/resources/KoResource.h b/libs/resources/KoResource.h index b40ee137e9..78834f79aa 100644 --- a/libs/resources/KoResource.h +++ b/libs/resources/KoResource.h @@ -1,240 +1,241 @@ /* This file is part of the KDE project Copyright (c) 2003 Patrick Julien Copyright (c) 2005 Boudewijn Rempt This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef KORESOURCE_H #define KORESOURCE_H #include #include #include #include #include #include "KisResourceTypes.h" #include #include class QDomDocument; class QDomElement; class KoResource; typedef QSharedPointer KoResourceSP; class KisResourcesInterface; typedef QSharedPointer KisResourcesInterfaceSP; /** * The KoResource class provides a representation of resources. This * includes, but not limited to, brushes and patterns. * * A resource knows its filename, but not the location where it's stored. * A new version of a resource is stored with an updated filename, the old * version isn't renamed. * */ class KRITARESOURCES_EXPORT KoResource : public boost::equality_comparable { public: /** * Creates a new KoResource object using @p filename. No file is opened * in the constructor, you have to call load. * * @param filename the file name to save and load from. */ explicit KoResource(const QString &filename); virtual ~KoResource(); KoResource(const KoResource &rhs); KoResource &operator=(const KoResource &rhs) = delete; virtual KoResourceSP clone() const = 0; bool operator==(const KoResource &other) const; public: /** * Load this resource. * @return true if loading the resource succeeded. */ virtual bool load(KisResourcesInterfaceSP resourcesInterface); virtual bool loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP resourcesInterface) = 0; /** * Save this resource. *@return true if saving the resource succeeded. */ virtual bool save(); virtual bool saveToDevice(QIODevice* dev) const; /** * @returns a QImage image representing this resource: in the case * of some resources, it is the actual resource. * * This image could be null. The image can be in any valid format. */ QImage image() const; void setImage(const QImage &image); /** * @brief updateThumbnail updates the thumbnail for this resource. * Reimplement if your thumbnail is something else than the image * set with setImage. */ virtual void updateThumbnail(); /** * @brief thumbnail the thumbnail image to use in resource selectors * @return a valid qimage. All thumbnails for a given resource have the * same size (which is not true for image(), but that size need not * be square. By default it's the same as image(), but that is not guaranteed. */ virtual QImage thumbnail() const; /// @return the md5sum calculated over the contents of the resource. QByteArray md5() const; /// @return the unique identifier of this resource within the container (folder, bundle, ...) QString filename() const; void setFilename(const QString& filename); /// @return the user-visible name of the resource QString name() const; void setName(const QString& name); /// @return true if the resource is ready for use bool valid() const; void setValid(bool valid); /// @return the default file extension which should be used when saving the resource virtual QString defaultFileExtension() const; /// @return true if the resource is permanent and can't be removed by the user bool permanent() const; void setPermanent(bool permanent); /// @return the name of the storage location of the resource QString storageLocation() const; /// Mark the preset as modified but not saved void setDirty(bool value); /// @return true if the preset has been modified, but not saved bool isDirty() const; /// store the given key, value pair in the resource void addMetaData(QString key, QVariant value); /// get a map with all the metadata QMap metadata() const; /// Get the version of the resource int version() const; /// @return the unique id of the resource in the resource database int resourceId() const; /// @return the resource type virtual QPair resourceType() const = 0; /** * Loads all the required resources either from \p globalResourcesInterface or * from embedded data. The preset first tries to fetch the required resource * from the global source, and only if it fails, tries to load it from the * embedded data. One can check if the loaded resource is embedded by checking * its resourceId(). * * The set of resources returned is basically: linkedResources() + embeddedResources() */ QList requiredResources(KisResourcesInterfaceSP globalResourcesInterface) const; /** * @return all the resources that are needed but (*this) resource and * are not embedded into it. The resources are fetched from * \p globalResourcesInterface. If fetching of some resources is failed, * then (*this) resource is invalid. */ virtual QList linkedResources(KisResourcesInterfaceSP globalResourcesInterface) const; /** * @return all the resources that were embedded into (*this) resource. * If the resources were already added to the global database, then they * are fetched from \p globalResourcesInterface to save time/memory. */ virtual QList embeddedResources(KisResourcesInterfaceSP globalResourcesInterface) const; private: + friend class KisResourceCacheDb; friend class KisResourceModel; friend class KisResourceLocator; friend class TestResourceModel; friend class TestResourceLocator; friend class TestFolderStorage; friend class KisFolderStorage; friend class KisBundleStorage; friend class KisStorageVersioningHelper; friend class KisMemoryStorage; void setVersion(int version); void setResourceId(int id); void setStorageLocation(const QString &location); protected: /// override generateMD5 and in your resource subclass virtual QByteArray generateMD5() const; /// call this when the contents of the resource change so the md5 needs to be recalculated void setMD5(const QByteArray &md5); private: struct Private; Private* const d; }; static inline bool operator==(const KoResource &resource1, const KoResource &resource2) { return (resource1.md5() == resource2.md5()); } static inline uint qHash(const KoResource &resource) { return qHash(resource.md5()); } Q_DECLARE_METATYPE(QSharedPointer) inline QDebug operator<<(QDebug dbg, const KoResourceSP res) { if (!res) { dbg.noquote() << "NULL Resource"; } else { dbg.nospace() << "[RESOURCE] Name: " << res->name() << " Version: " << res->version() << " Filename: " << res->filename() << " Valid: " << res->valid() << " Storage: " << res->storageLocation(); } return dbg.space(); } #endif // KORESOURCE_H_