File indexing completed on 2024-05-05 04:47:44

0001 /****************************************************************************************
0002  * Copyright (c) 2017 Malte Veerman <malte.veerman@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef APPLETMODEL_H
0018 #define APPLETMODEL_H
0019 
0020 #include <QAbstractListModel>
0021 #include <QSortFilterProxyModel>
0022 
0023 
0024 class KPluginMetaData;
0025 
0026 namespace Context
0027 {
0028 
0029 class AppletLoader;
0030 class AppletPackage;
0031 
0032 class AppletModel : public QAbstractListModel
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     enum Role
0038     {
0039         Name,
0040         Id,
0041         Icon,
0042         Mainscript,
0043         Collapsed,
0044         ContentHeight
0045     };
0046     Q_ENUM(Role)
0047 
0048     explicit AppletModel(AppletLoader *loader, QObject *parent = nullptr);
0049     ~AppletModel() override;
0050 
0051     int rowCount(const QModelIndex& parent) const override;
0052     QVariant data(const QModelIndex& index, int role) const override;
0053     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0054     QHash< int, QByteArray > roleNames() const override;
0055 
0056     AppletLoader* loader() const { return m_loader; }
0057 
0058     Q_INVOKABLE void setAppletCollapsed(const QString &id, bool collapsed);
0059     Q_INVOKABLE void setAppletContentHeight(const QString& id, qreal height);
0060     Q_INVOKABLE QUrl imageUrl(const QString &id, const QString &imageName);
0061 
0062 public Q_SLOTS:
0063     void newApplets(const QList<KPluginMetaData> &applets);
0064 
0065 protected:
0066     AppletPackage findPackage(const QString &id);
0067 
0068 private:
0069     QList<AppletPackage> m_packages;
0070     AppletLoader *const m_loader;
0071 };
0072 
0073 class AppletProxyModel : public QSortFilterProxyModel
0074 {
0075     Q_OBJECT
0076 
0077     Q_PROPERTY(QStringList enabledApplets READ enabledApplets NOTIFY enabledAppletsChanged)
0078 
0079 public:
0080     explicit AppletProxyModel(AppletModel *appletModel, QObject *parent = nullptr);
0081     ~AppletProxyModel() override;
0082 
0083     /**
0084      * @returns QStringList with ids of all enabled applets.
0085      * Sorted in ascending order of place of applets.
0086      */
0087     QStringList enabledApplets() const;
0088 
0089     /**
0090      * Set an applet to be enabled or disabled. Does nothing if id is invalid.
0091      * Disabling an applet sets its place to -1.
0092      *
0093      * @param id Id of the applet.
0094      * @param enabled Set to true if applet should be enabled and false for disabled.
0095      * @param place The place of the applet after enabling. -1 appends the applet to the end of the list.
0096      * Irrelevant if applet is to be disabled.
0097      */
0098     Q_INVOKABLE void setAppletEnabled(const QString &id, bool enabled, int place = -1);
0099 
0100     /**
0101      * Set an applet's place. Does nothing if id is invalid.
0102      * Enables the applet if it is disabled.
0103      *
0104      * @param id Id of the applet.
0105      * @param place The new place of the applet. Negative values disable the applet.
0106      */
0107     Q_INVOKABLE void setAppletPlace(const QString &id, int place);
0108 
0109     /**
0110      * Find out an applet's place.
0111      *
0112      * @returns an integer with the applet's place. -1 if the applet is disabled.
0113      *
0114      * @param id Id of applet's place to be returned.
0115      */
0116     Q_INVOKABLE int appletPlace(const QString &id) const;
0117 
0118     /**
0119      * Find out if an applet is enabled or disabled.
0120      *
0121      * @returns true if applet is enabled. Returns false if not.
0122      *
0123      * @param id Id of the applet.
0124      */
0125     Q_INVOKABLE bool appletEnabled(const QString &id) const;
0126 
0127     /**
0128      * Clear the context area by disabling all applets
0129      */
0130     Q_INVOKABLE void clear();
0131 
0132 Q_SIGNALS:
0133     void enabledAppletsChanged();
0134 
0135 protected:
0136     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0137     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0138 
0139 private:
0140     AppletModel *m_appletModel;
0141 };
0142 
0143 }
0144 
0145 #endif // APPLETMODEL_H