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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 boud <boud@valdyas.org>
0003  * SPDX-FileCopyrightText: 2020 Agata Cacko <cacko.azh@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 #ifndef KisAllTagsModel_H
0008 #define KisAllTagsModel_H
0009 
0010 #include <QObject>
0011 #include <QAbstractTableModel>
0012 #include <QSortFilterProxyModel>
0013 
0014 #include <KisTag.h>
0015 #include <KoResource.h>
0016 
0017 #include "kritaresources_export.h"
0018 
0019 class KRITARESOURCES_EXPORT KisAbstractTagModel
0020 {
0021 public:
0022 
0023     virtual ~KisAbstractTagModel() {}
0024 
0025     virtual QModelIndex indexForTag(KisTagSP tag) const = 0;
0026     virtual KisTagSP tagForIndex(QModelIndex index = QModelIndex()) const = 0;
0027 
0028     /// Retrieve a tag by url
0029     virtual KisTagSP tagForUrl(const QString& url) const = 0;
0030 
0031     /// Add a new tag with a possibly empty list of resources to tag
0032     virtual KisTagSP addTag(const QString &tagName, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces)  = 0;
0033 
0034     /// Add a tag, if it doesn't exist yet, with a possibly empty list of resources to tag
0035     virtual bool addTag(const KisTagSP tag, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces = QVector<KoResourceSP>()) = 0;
0036 
0037     virtual bool setTagActive(const KisTagSP tag) = 0;
0038     virtual bool setTagInactive(const KisTagSP tag) = 0;
0039     virtual bool renameTag(const KisTagSP tag, const QString &newName, const bool allowOverwrite) = 0;
0040     virtual bool changeTagActive(const KisTagSP tag, bool active) = 0;
0041 };
0042 
0043 
0044 class KRITARESOURCES_EXPORT KisAllTagsModel
0045         : public QAbstractTableModel
0046         , public KisAbstractTagModel
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051 
0052     enum Columns {
0053         Id = 0,
0054         Url,
0055         Name,
0056         Comment,
0057         ResourceType,
0058         Active,
0059         KisTagRole,
0060     };
0061 
0062     enum Ids { // to get actual id, you need to add s_fakeRowsCount
0063         All = -2, // so it gets on top in the combobox
0064         AllUntagged = -1,
0065     };
0066 
0067     ~KisAllTagsModel() override;
0068 
0069     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0070     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0071     QVariant data(const QModelIndex &index, int role) const override;
0072     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0073     Qt::ItemFlags flags(const QModelIndex &index) const override;
0074 
0075 // KisAllTagsModel API
0076 
0077     QModelIndex indexForTag(KisTagSP tag) const override;
0078     KisTagSP tagForIndex(QModelIndex index = QModelIndex()) const override;
0079 
0080     KisTagSP tagForUrl(const QString& tagUrl) const override;
0081 
0082     // TODO: replace ALL occurrences of KoResourceSP here with the resource id's.
0083     KisTagSP addTag(const QString &tagName, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces) override;
0084     bool addTag(const KisTagSP tag, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces = QVector<KoResourceSP>()) override;
0085     bool setTagActive(const KisTagSP tag) override;
0086     bool setTagInactive(const KisTagSP tag) override;
0087 
0088     bool renameTag(const KisTagSP tag, const QString &newName, const bool allowOverwrite) override;
0089     bool changeTagActive(const KisTagSP tag, bool active) override;
0090 
0091     // TODO: they can be static const variables, too, if it's better than functions
0092     static QString urlAll() { return "All"; }
0093     static QString urlAllUntagged() { return "All Untagged"; }
0094 
0095 private Q_SLOTS:
0096 
0097     void addStorage(const QString &location);
0098     void removeStorage(const QString &location);
0099 
0100 private:
0101 
0102     friend class KisResourceModelProvider;
0103     friend class TestTagResourceModel;
0104     friend class KisTagModel;
0105 
0106     KisAllTagsModel(const QString &resourceType, QObject *parent = 0);
0107 
0108     bool tagResourceByUrl(const QString& tagUrl, const int resourceId);
0109     bool tagResourceById(const int tagId, const int resource);
0110 
0111     void untagAllResources(KisTagSP tag);
0112 
0113     bool resetQuery();
0114 
0115     struct Private;
0116     Private* const d;
0117 
0118 };
0119 
0120 class KRITARESOURCES_EXPORT KisTagModel
0121         : public QSortFilterProxyModel
0122         , public KisAbstractTagModel
0123 {
0124 
0125     Q_OBJECT
0126 
0127 public:
0128     KisTagModel(const QString &type, QObject *parent = 0);
0129     ~KisTagModel() override;
0130 
0131     enum TagFilter {
0132         ShowInactiveTags = 0,
0133         ShowActiveTags,
0134         ShowAllTags
0135     };
0136 
0137     void setTagFilter(TagFilter filter);
0138 
0139     enum StorageFilter {
0140         ShowInactiveStorages = 0,
0141         ShowActiveStorages,
0142         ShowAllStorages
0143     };
0144 
0145     void setStorageFilter(StorageFilter filter);
0146 
0147 
0148     // KisAllTagsModel API
0149 
0150     QModelIndex indexForTag(KisTagSP tag) const override;
0151     KisTagSP tagForIndex(QModelIndex index = QModelIndex()) const override;
0152     KisTagSP addTag(const QString &tagName, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces) override;
0153     KisTagSP tagForUrl(const QString& url) const override;
0154     bool addTag(const KisTagSP tag, const bool allowOverwrite, QVector<KoResourceSP> taggedResouces = QVector<KoResourceSP>()) override;
0155     bool setTagInactive(const KisTagSP tag) override;
0156     bool setTagActive(const KisTagSP tag) override;
0157     bool renameTag(const KisTagSP tag, const QString &newName, const bool allowOverwrite) override;
0158     bool changeTagActive(const KisTagSP tag, bool active) override;
0159 
0160 protected:
0161 
0162     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0163     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0164 
0165 private:
0166 
0167     friend class DlgDbExplorer;
0168     friend class TestTagModel;
0169 
0170 
0171     struct Private;
0172     Private *const d;
0173 
0174     Q_DISABLE_COPY(KisTagModel)
0175 
0176 };
0177 
0178 typedef QSharedPointer<KisAllTagsModel> KisAllTagsModelSP;
0179 
0180 #endif // KisAllTagsModel_H