File indexing completed on 2024-05-12 15:59:54

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #ifndef KISTAGRESOURCEMODEL_H
0007 #define KISTAGRESOURCEMODEL_H
0008 
0009 #include <QObject>
0010 #include <QAbstractTableModel>
0011 #include <QSortFilterProxyModel>
0012 
0013 #include <KisTag.h>
0014 #include <KoResource.h>
0015 #include <KisResourceModel.h>
0016 
0017 #include "kritaresources_export.h"
0018 
0019 class KRITARESOURCES_EXPORT KisAbstractTagResourceModel
0020 {
0021 public:
0022     virtual ~KisAbstractTagResourceModel() {}
0023 
0024     virtual bool tagResources(const KisTagSP tag, const QVector<int> &resourceIds) = 0;
0025     virtual bool untagResources(const KisTagSP tag, const QVector<int> &resourceIds) = 0;
0026 
0027     /**
0028      * @brief isResourceTagged
0029      * @param tag the tag to check
0030      * @param resourceId the id of the resource to check
0031      * @return  -1 if the resource was never tagged before, 0 if the resource
0032      * was tagged, but then untagged, 1 if the resource is already tagged
0033      */
0034     virtual int isResourceTagged(const KisTagSP tag, const int resourceId) = 0;
0035 };
0036 
0037 class KRITARESOURCES_EXPORT KisAllTagResourceModel
0038         : public QAbstractTableModel
0039         , public KisAbstractTagResourceModel
0040 {
0041     Q_OBJECT
0042 private:
0043 
0044     friend class KisResourceModelProvider;
0045     friend class TestTagResourceModel;
0046     friend class KisTagResourceModel;
0047 
0048     KisAllTagResourceModel(const QString &resourceType, QObject *parent = 0);
0049 
0050 public:
0051     ~KisAllTagResourceModel() override;
0052 
0053 public:
0054 
0055     enum Columns {
0056         TagId = KisAbstractResourceModel::StorageActive + 1,
0057         ResourceId,
0058         Tag,
0059         Resource,
0060         ResourceActive,
0061         TagActive,
0062         ResourceStorageActive,
0063         ResourceName,
0064         TagName
0065     };
0066 
0067     // QAbstractItemModel API
0068 
0069     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0070     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0071 
0072     /// Note: only role is significant, column is not.
0073     QVariant data(const QModelIndex &index, int role) const override;
0074 
0075     // Abstract Tag API
0076     bool tagResources(const KisTagSP tag, const QVector<int> &resourceIds) override;
0077     bool untagResources(const KisTagSP tag, const QVector<int> &resourceId) override;
0078     int isResourceTagged(const KisTagSP tag, const int resourceId) override;
0079 
0080 private Q_SLOTS:
0081     void addStorage(const QString &location);
0082     void removeStorage(const QString &location);
0083 
0084     void slotResourceActiveStateChanged(const QString &resourceType, int resourceId);
0085 
0086 private:
0087 
0088     QString createQuery(bool onlyAcive = true, bool returnADbIndexToo = false);
0089     bool resetQuery();
0090 
0091 
0092     struct Private;
0093     Private* const d;
0094 };
0095 
0096 /**
0097  * @brief The KisTagResourceModel class makes it possible to retrieve the resources for certain
0098  * tags or the tags for certain resources. If the filter for tags or resources is empty, all
0099  * tags or resources that match for the active/inactive/all filters will match.
0100  */
0101 class KRITARESOURCES_EXPORT KisTagResourceModel : public QSortFilterProxyModel
0102     , public KisAbstractTagResourceModel
0103     , public KisAbstractResourceModel
0104     , public KisAbstractResourceFilterInterface
0105 {
0106     Q_OBJECT
0107 
0108 public:
0109 
0110     KisTagResourceModel(const QString &resourceType, QObject *parent = 0);
0111     ~KisTagResourceModel() override;
0112 
0113 public:
0114 
0115     enum TagFilter {
0116         ShowInactiveTags = 0,
0117         ShowActiveTags,
0118         ShowAllTags
0119     };
0120 
0121     void setTagFilter(TagFilter filter);
0122 
0123     void setResourceFilter(ResourceFilter filter) override;
0124     void setStorageFilter(StorageFilter filter) override;
0125 
0126     void setTagsFilter(const QVector<int> tagIds);
0127     void setResourcesFilter(const QVector<int> resourceIds);
0128 
0129     void setTagsFilter(const QVector<KisTagSP> tags);
0130     void setResourcesFilter(const QVector<KoResourceSP> resources);
0131 
0132     // KisAbstractTagResourceModel API
0133 
0134     bool tagResources(const KisTagSP tag, const QVector<int> &resourceIds) override;
0135     bool untagResources(const KisTagSP tag, const QVector<int> &resourceIds) override;
0136     int isResourceTagged(const KisTagSP tag, const int resourceId) override;
0137 
0138     // KisAbstractResourceModel interface
0139 
0140     KoResourceSP resourceForIndex(QModelIndex index) const override;
0141     QModelIndex indexForResource(KoResourceSP resource) const override;
0142     QModelIndex indexForResourceId(int resourceId) const override;
0143     bool setResourceActive(const QModelIndex &index, bool value) override;
0144     KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId = QString()) override;
0145     KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId = QString()) override;
0146     bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation) const override;
0147     bool exportResource(KoResourceSP resource, QIODevice *device) override;
0148     bool addResource(KoResourceSP resource, const QString &storageId) override;
0149     bool updateResource(KoResourceSP resource) override;
0150     bool reloadResource(KoResourceSP resource) override;
0151     bool renameResource(KoResourceSP resource, const QString &name) override;
0152     bool setResourceMetaData(KoResourceSP resource, QMap<QString, QVariant> metadata) override;
0153 
0154 
0155 protected:
0156     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0157     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0158 
0159 protected Q_SLOTS:
0160     void storageChanged(const QString &location);
0161 
0162 private:
0163     struct Private;
0164     Private* const d;
0165 };
0166 
0167 #endif // KISTAGRESOURCEMODEL_H