File indexing completed on 2024-11-24 05:01:58

0001 /*
0002     SPDX-FileCopyrightText: 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QIcon>
0010 #include <QPair>
0011 #include <QSortFilterProxyModel>
0012 #include <QStandardItem>
0013 
0014 namespace KCategorizedItemsViewModels
0015 {
0016 typedef QPair<QString, QVariant> Filter;
0017 
0018 /**
0019  * Abstract class that needs to be implemented and used with the ItemModel
0020  */
0021 class AbstractItem : public QStandardItem
0022 {
0023 public:
0024     /**
0025      * Returns a localized string - name of the item
0026      */
0027     virtual QString name() const;
0028 
0029     /**
0030      * Returns a unique id related to this item
0031      */
0032     virtual QString id() const;
0033 
0034     /**
0035      * Returns a localized string - description of the item
0036      */
0037     virtual QString description() const;
0038 
0039     /**
0040      * Returns if the item is flagged as favorite
0041      * Default implementation checks if the item passes the Filter("favorite", "1") filter
0042      */
0043     virtual bool isFavorite() const;
0044 
0045     /**
0046      * Returns the item's number of running applets
0047      * Default implementation just returns 0
0048      */
0049     virtual int running() const;
0050 
0051     /**
0052      * Returns if the item contains string specified by pattern.
0053      * Default implementation checks whether name or description contain the
0054      * string (not needed to be exactly that string)
0055      */
0056     virtual bool matches(const QString &pattern) const;
0057 
0058     /**
0059      * sets the number of running applets for the item
0060      */
0061     virtual void setRunning(int count) = 0;
0062 
0063     /**
0064      * Returns if the item passes the filter specified
0065      */
0066     virtual bool passesFiltering(const Filter &filter) const = 0;
0067 
0068     virtual QStringList keywords() const
0069     {
0070         return {};
0071     };
0072 
0073 private:
0074 };
0075 
0076 /**
0077  * The default implementation of the model containing filters
0078  */
0079 class DefaultFilterModel : public QStandardItemModel
0080 {
0081     Q_OBJECT
0082     Q_PROPERTY(int count READ count NOTIFY countChanged)
0083 public:
0084     enum Roles {
0085         FilterTypeRole = Qt::UserRole + 1,
0086         FilterDataRole = Qt::UserRole + 2,
0087         SeparatorRole = Qt::UserRole + 3,
0088     };
0089     explicit DefaultFilterModel(QObject *parent = nullptr);
0090 
0091     QHash<int, QByteArray> roleNames() const override;
0092 
0093     /**
0094      * Adds a filter to the model
0095      * @param caption The localized string to be displayed as a name of the filter
0096      * @param filter The filter structure
0097      * @param icon The filter icon
0098      */
0099     void addFilter(const QString &caption, const Filter &filter, const QIcon &icon = QIcon());
0100 
0101     /**
0102      * Adds a separator to the model
0103      * @param caption The localized string to be displayed as a name of the separator
0104      */
0105     void addSeparator(const QString &caption);
0106 
0107     int count()
0108     {
0109         return rowCount(QModelIndex());
0110     }
0111 
0112     Q_INVOKABLE QVariantHash get(int i) const;
0113 
0114 Q_SIGNALS:
0115     void countChanged();
0116 };
0117 
0118 /**
0119  * Default filter proxy model.
0120  */
0121 class DefaultItemFilterProxyModel : public QSortFilterProxyModel
0122 {
0123     Q_OBJECT
0124     Q_PROPERTY(QString searchTerm READ searchTerm WRITE setSearchTerm NOTIFY searchTermChanged)
0125     Q_PROPERTY(QString filterType READ filterType WRITE setFilterType NOTIFY filterChanged)
0126     Q_PROPERTY(QVariant filterQuery READ filterQuery WRITE setFilterQuery NOTIFY filterChanged)
0127     Q_PROPERTY(int count READ count NOTIFY countChanged)
0128 
0129 public:
0130     explicit DefaultItemFilterProxyModel(QObject *parent = nullptr);
0131 
0132     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0133     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0134 
0135     void setSearchTerm(const QString &pattern);
0136     QString searchTerm() const;
0137 
0138     void setFilterType(const QString &type);
0139     QString filterType() const;
0140 
0141     void setFilterQuery(const QVariant &query);
0142     QVariant filterQuery() const;
0143 
0144     void setFilter(const Filter &filter);
0145 
0146     void setSourceModel(QAbstractItemModel *sourceModel) override;
0147 
0148     QAbstractItemModel *sourceModel() const;
0149 
0150     int columnCount(const QModelIndex &index) const override;
0151 
0152     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0153 
0154     int count()
0155     {
0156         return rowCount(QModelIndex());
0157     }
0158 
0159     Q_INVOKABLE QVariantHash get(int i) const;
0160 
0161 Q_SIGNALS:
0162     void searchTermChanged(const QString &term);
0163     void filterChanged();
0164     void countChanged();
0165 
0166 private:
0167     Filter m_filter;
0168     QString m_searchPattern;
0169 };
0170 
0171 } // end of namespace