Changeset View
Changeset View
Standalone View
Standalone View
src/context/AppletModel.h
- This file was added.
1 | /**************************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2017 Malte Veerman <malte.veerman@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify it under * | ||||
5 | * the terms of the GNU General Public License as published by the Free Software * | ||||
6 | * Foundation; either version 2 of the License, or (at your option) any later * | ||||
7 | * version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||||
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||||
11 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. * | ||||
12 | * * | ||||
13 | * You should have received a copy of the GNU General Public License along with * | ||||
14 | * this program. If not, see <http://www.gnu.org/licenses/>. * | ||||
15 | ****************************************************************************************/ | ||||
16 | | ||||
17 | #ifndef APPLETMODEL_H | ||||
18 | #define APPLETMODEL_H | ||||
19 | | ||||
20 | #include <QAbstractListModel> | ||||
21 | #include <QSortFilterProxyModel> | ||||
22 | | ||||
23 | | ||||
24 | class KPluginMetaData; | ||||
25 | | ||||
26 | namespace Context | ||||
27 | { | ||||
28 | | ||||
29 | class AppletLoader; | ||||
30 | class AppletPackage; | ||||
31 | | ||||
32 | class AppletModel : public QAbstractListModel | ||||
33 | { | ||||
34 | Q_OBJECT | ||||
35 | | ||||
36 | public: | ||||
37 | enum Role | ||||
38 | { | ||||
39 | Name, | ||||
40 | Id, | ||||
41 | Icon, | ||||
42 | Mainscript, | ||||
43 | Collapsed, | ||||
44 | PackagePath, | ||||
45 | ContentHeight | ||||
46 | }; | ||||
47 | Q_ENUM(Role) | ||||
48 | | ||||
49 | AppletModel(AppletLoader *loader, QObject *parent = Q_NULLPTR); | ||||
50 | virtual ~AppletModel(); | ||||
51 | | ||||
52 | virtual int rowCount(const QModelIndex& parent) const Q_DECL_OVERRIDE; | ||||
53 | virtual QVariant data(const QModelIndex& index, int role) const Q_DECL_OVERRIDE; | ||||
54 | virtual bool setData(const QModelIndex& index, const QVariant& value, int role) Q_DECL_OVERRIDE; | ||||
55 | virtual QHash< int, QByteArray > roleNames() const Q_DECL_OVERRIDE; | ||||
56 | | ||||
57 | AppletLoader* loader() const { return m_loader; } | ||||
58 | | ||||
59 | Q_INVOKABLE void setAppletCollapsed(const QString &id, bool collapsed); | ||||
60 | Q_INVOKABLE void setAppletContentHeight(const QString& id, qreal height); | ||||
61 | | ||||
62 | public Q_SLOTS: | ||||
63 | void newApplets(const QList<KPluginMetaData> &applets); | ||||
64 | | ||||
65 | protected: | ||||
66 | AppletPackage findPackage(const QString &id); | ||||
67 | | ||||
68 | private: | ||||
69 | QList<AppletPackage> m_packages; | ||||
70 | AppletLoader *const m_loader; | ||||
71 | }; | ||||
72 | | ||||
73 | class AppletProxyModel : public QSortFilterProxyModel | ||||
74 | { | ||||
75 | Q_OBJECT | ||||
76 | | ||||
77 | Q_PROPERTY(QStringList enabledApplets READ enabledApplets NOTIFY enabledAppletsChanged) | ||||
78 | | ||||
79 | public: | ||||
80 | AppletProxyModel(AppletModel *appletModel, QObject *parent = Q_NULLPTR); | ||||
81 | virtual ~AppletProxyModel(); | ||||
82 | | ||||
83 | /** | ||||
84 | * @returns QStringList with ids of all enabled applets. | ||||
85 | * Sorted in ascending order of place of applets. | ||||
86 | */ | ||||
87 | QStringList enabledApplets() const; | ||||
88 | | ||||
89 | /** | ||||
90 | * Set an applet to be enabled or disabled. Does nothing if id is invalid. | ||||
91 | * Disabling an applet sets its place to -1. | ||||
92 | * | ||||
93 | * @param id Id of the applet. | ||||
94 | * @param enabled Set to true if applet should be enabled and false for disabled. | ||||
95 | * @param place The place of the applet after enabling. -1 appends the applet to the end of the list. | ||||
96 | * Irrelevant if applet is to be disabled. | ||||
97 | */ | ||||
98 | Q_INVOKABLE void setAppletEnabled(const QString &id, bool enabled, int place = -1); | ||||
99 | | ||||
100 | /** | ||||
101 | * Set an applet's place. Does nothing if id is invalid. | ||||
102 | * Enables the applet if it is disabled. | ||||
103 | * | ||||
104 | * @param id Id of the applet. | ||||
105 | * @param place The new place of the applet. Negative values disable the applet. | ||||
106 | */ | ||||
107 | Q_INVOKABLE void setAppletPlace(const QString &id, int place); | ||||
108 | | ||||
109 | /** | ||||
110 | * Find out an applet's place. | ||||
111 | * | ||||
112 | * @returns an integer with the applet's place. -1 if the applet is disabled. | ||||
113 | * | ||||
114 | * @param id Id of applet's place to be returned. | ||||
115 | */ | ||||
116 | Q_INVOKABLE int appletPlace(const QString &id) const; | ||||
117 | | ||||
118 | /** | ||||
119 | * Find out if an applet is enabled or disabled. | ||||
120 | * | ||||
121 | * @returns true if applet is enabled. Returns false if not. | ||||
122 | * | ||||
123 | * @param id Id of the applet. | ||||
124 | */ | ||||
125 | Q_INVOKABLE bool appletEnabled(const QString &id) const; | ||||
126 | | ||||
127 | /** | ||||
128 | * Clear the context area by disabling all applets | ||||
129 | */ | ||||
130 | Q_INVOKABLE void clear(); | ||||
131 | | ||||
132 | Q_SIGNALS: | ||||
133 | void enabledAppletsChanged(); | ||||
134 | | ||||
135 | protected: | ||||
136 | bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const Q_DECL_OVERRIDE; | ||||
137 | bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const Q_DECL_OVERRIDE; | ||||
138 | | ||||
139 | private: | ||||
140 | AppletModel *m_appletModel; | ||||
141 | }; | ||||
142 | | ||||
143 | } | ||||
144 | | ||||
145 | #endif // APPLETMODEL_H |