File indexing completed on 2024-05-19 04:27:42

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     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0081 
0082 private Q_SLOTS:
0083     void addStorage(const QString &location);
0084     void removeStorage(const QString &location);
0085 
0086     void slotResourceActiveStateChanged(const QString &resourceType, int resourceId);
0087 
0088 private:
0089 
0090     QString createQuery(bool onlyAchieve = true, bool returnADbIndexToo = false);
0091     bool resetQuery();
0092 
0093 
0094     struct Private;
0095     Private* const d;
0096 };
0097 
0098 /**
0099  * @brief The KisTagResourceModel class makes it possible to retrieve the resources for certain
0100  * tags or the tags for certain resources. If the filter for tags or resources is empty, all
0101  * tags or resources that match for the active/inactive/all filters will match.
0102  */
0103 class KRITARESOURCES_EXPORT KisTagResourceModel : public QSortFilterProxyModel
0104     , public KisAbstractTagResourceModel
0105     , public KisAbstractResourceModel
0106     , public KisAbstractResourceFilterInterface
0107 {
0108     Q_OBJECT
0109 
0110 public:
0111 
0112     KisTagResourceModel(const QString &resourceType, QObject *parent = 0);
0113     ~KisTagResourceModel() override;
0114 
0115 public:
0116 
0117     enum TagFilter {
0118         ShowInactiveTags = 0,
0119         ShowActiveTags,
0120         ShowAllTags
0121     };
0122 
0123     void setTagFilter(TagFilter filter);
0124 
0125     void setResourceFilter(ResourceFilter filter) override;
0126     void setStorageFilter(StorageFilter filter) override;
0127 
0128     void setTagsFilter(const QVector<int> tagIds);
0129     void setResourcesFilter(const QVector<int> resourceIds);
0130 
0131     void setTagsFilter(const QVector<KisTagSP> tags);
0132     void setResourcesFilter(const QVector<KoResourceSP> resources);
0133 
0134     // KisAbstractTagResourceModel API
0135 
0136     bool tagResources(const KisTagSP tag, const QVector<int> &resourceIds) override;
0137     bool untagResources(const KisTagSP tag, const QVector<int> &resourceIds) override;
0138     int isResourceTagged(const KisTagSP tag, const int resourceId) override;
0139 
0140     // KisAbstractResourceModel interface
0141 
0142     KoResourceSP resourceForIndex(QModelIndex index) const override;
0143     QModelIndex indexForResource(KoResourceSP resource) const override;
0144     QModelIndex indexForResourceId(int resourceId) const override;
0145     bool setResourceActive(const QModelIndex &index, bool value) override;
0146     KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId = QString()) override;
0147     KoResourceSP importResource(const QString &filename, QIODevice *device, const bool allowOverwrite, const QString &storageId = QString()) override;
0148     bool importWillOverwriteResource(const QString &fileName, const QString &storageLocation) const override;
0149     bool exportResource(KoResourceSP resource, QIODevice *device) override;
0150     bool addResource(KoResourceSP resource, const QString &storageId) override;
0151     bool updateResource(KoResourceSP resource) override;
0152     bool reloadResource(KoResourceSP resource) override;
0153     bool renameResource(KoResourceSP resource, const QString &name) override;
0154     bool setResourceMetaData(KoResourceSP resource, QMap<QString, QVariant> metadata) override;
0155 
0156     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0157 
0158 protected:
0159     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0160     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0161 
0162 protected Q_SLOTS:
0163     void storageChanged(const QString &location);
0164 
0165 private:
0166     struct Private;
0167     Private* const d;
0168 };
0169 
0170 #endif // KISTAGRESOURCEMODEL_H