Plasma5Support

datamodel.h
1/*
2 SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef DATAMODEL_H
8#define DATAMODEL_H
9
10#include <QAbstractItemModel>
11#include <QJSValue>
12#include <QList>
13#include <QQmlEngine>
14#include <QRegularExpression>
15#include <QSortFilterProxyModel>
16
17#include <Plasma5Support/DataEngine>
18
19class QTimer;
20
21// All classes here will hopefully be removed in KF6 (along with DataEngines in general)
22
23namespace Plasma5Support
24{
25class DataSource;
26class DataModel;
27
28/**
29 * @class SortFilterModel
30 * @short Filter and sort an existing QAbstractItemModel
31 */
33{
35 QML_ELEMENT
36 /**
37 * The source model of this sorting proxy model. It has to inherit QAbstractItemModel (ListModel is not supported)
38 */
40
41 /**
42 * The regular expression for the filter, only items with their filterRole matching filterRegExp will be displayed
43 */
44 Q_PROPERTY(QString filterRegExp READ filterRegExp WRITE setFilterRegExp NOTIFY filterRegExpChanged)
45
46 /**
47 * The string for the filter, only items with their filterRole matching filterString will be displayed
48 */
49 Q_PROPERTY(QString filterString READ filterString WRITE setFilterString NOTIFY filterStringChanged REVISION 1)
50
51 /**
52 * A JavaScript callable that is passed the source model row index as first argument and the value
53 * of filterRole as second argument. The callable's return value is evaluated as boolean to determine
54 * whether the row is accepted (true) or filtered out (false). It overrides the default implementation
55 * that uses filterRegExp or filterString; while filterCallable is set those two properties are
56 * ignored. Attempts to write a non-callable to this property are silently ignored, but you can set
57 * it to null.
58 */
59 Q_PROPERTY(QJSValue filterCallback READ filterCallback WRITE setFilterCallback NOTIFY filterCallbackChanged REVISION 1)
60
61 /**
62 * The role of the sourceModel on which filterRegExp must be applied.
63 */
64 Q_PROPERTY(QString filterRole READ filterRole WRITE setFilterRole)
65
66 /**
67 * The role of the sourceModel that will be used for sorting. if empty the order will be left unaltered
68 */
69 Q_PROPERTY(QString sortRole READ sortRole WRITE setSortRole)
70
71 /**
72 * One of Qt.Ascending or Qt.Descending
73 */
74 Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
75
76 /**
77 * Specify which column should be used for sorting
78 */
79 Q_PROPERTY(int sortColumn READ sortColumn WRITE setSortColumn NOTIFY sortColumnChanged)
80
81 /**
82 * How many items are in this model
83 */
84 Q_PROPERTY(int count READ count NOTIFY countChanged)
85
86 friend class DataModel;
87
88public:
89 explicit SortFilterModel(QObject *parent = nullptr);
90 ~SortFilterModel() override;
91
92 void setModel(QAbstractItemModel *source);
93
94 void setFilterRegExp(const QString &exp);
95 QString filterRegExp() const;
96
97 void setFilterString(const QString &filterString);
98 QString filterString() const;
99
100 void setFilterCallback(const QJSValue &callback);
101 QJSValue filterCallback() const;
102
103 void setFilterRole(const QString &role);
104 QString filterRole() const;
105
106 void setSortRole(const QString &role);
107 QString sortRole() const;
108
109 void setSortOrder(const Qt::SortOrder order);
110
111 void setSortColumn(int column);
112
113 int count() const
114 {
116 }
117
118 /**
119 * Returns the item at index in the list model.
120 * This allows the item data to be accessed (but not modified) from JavaScript.
121 * It returns an Object with a property for each role.
122 *
123 * @param i the row we want
124 */
125 Q_INVOKABLE QVariantMap get(int i) const;
126
127 Q_INVOKABLE int mapRowToSource(int i) const;
128
129 Q_INVOKABLE int mapRowFromSource(int i) const;
130
132 void countChanged();
133 void sortColumnChanged();
135 void filterRegExpChanged(const QString &);
136 Q_REVISION(1) void filterStringChanged(const QString &);
137 Q_REVISION(1) void filterCallbackChanged(const QJSValue &);
138
139protected:
140 int roleNameToId(const QString &name) const;
141 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
142 QHash<int, QByteArray> roleNames() const override;
143
144protected Q_SLOTS:
145 void syncRoleNames();
146
147private:
148 QString m_filterRole;
149 QString m_sortRole;
150 QString m_filterString;
151 QJSValue m_filterCallback;
152 QHash<QString, int> m_roleIds;
153};
154
155/**
156 * @class DataModel
157 * @short DataSource data as a model
158 */
160{
162 QML_ELEMENT
163
164 /**
165 * The instance of DataSource to construct this model on
166 */
167 Q_PROPERTY(QObject *dataSource READ dataSource WRITE setDataSource)
168
169 /**
170 * It's a regular expression. Only data with keys that match this filter
171 * expression will be inserted in the model
172 */
173 Q_PROPERTY(QString keyRoleFilter READ keyRoleFilter WRITE setKeyRoleFilter)
174
175 /**
176 * It's a regular expression. If the DataSource is connected to more than one source,
177 * only inserts data from sources matching this filter expression in the model. If we
178 * want to have a source watch all sources beginning with say "name:", the required
179 * regexp would be sourceFilter: "name:.*"
180 */
181 Q_PROPERTY(QString sourceFilter READ sourceFilter WRITE setSourceFilter)
182
183 /**
184 * How many items are in this model
185 */
186 Q_PROPERTY(int count READ count NOTIFY countChanged)
187
188public:
189 DataModel(QObject *parent = nullptr);
190 ~DataModel() override;
191
192 void setDataSource(QObject *source);
193 QObject *dataSource() const;
194
195 /**
196 * Include only items with a key that matches this regexp in the model
197 */
198 void setKeyRoleFilter(const QString &key);
199 QString keyRoleFilter() const;
200
201 /**
202 * Include only sources that matches this regexp in the model
203 */
204 void setSourceFilter(const QString &key);
205 QString sourceFilter() const;
206
207 // Reimplemented
208 QVariant data(const QModelIndex &index, int role) const override;
209 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
210 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
211 QModelIndex parent(const QModelIndex &child) const override;
212 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
213 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
214
215 int count() const
216 {
217 return countItems();
218 }
219
220 /**
221 * Returns the item at index in the list model.
222 * This allows the item data to be accessed (but not modified) from JavaScript.
223 * It returns an Object with a property for each role.
224 *
225 * @param i the row we want
226 */
227 Q_INVOKABLE QVariantMap get(int i) const;
228
229protected:
230 void setItems(const QString &sourceName, const QVariantList &list);
231 inline int countItems() const;
232 QHash<int, QByteArray> roleNames() const override;
234 void countChanged();
236 void filterRegExpChanged(const QString &);
237
238private Q_SLOTS:
239 void dataUpdated(const QString &sourceName, const QVariantMap &data);
240 void removeSource(const QString &sourceName);
241
242private:
243 DataSource *m_dataSource;
244 QString m_keyRoleFilter;
245 QRegularExpression m_keyRoleFilterRE;
246 QString m_sourceFilter;
247 QRegularExpression m_sourceFilterRE;
249 QHash<int, QByteArray> m_roleNames;
250 QHash<QString, int> m_roleIds;
251 int m_maxRoleId;
252};
253
254int DataModel::countItems() const
255{
256 int count = 0;
257 for (const QList<QVariant> &v : std::as_const(m_items)) {
258 count += v.count();
259 }
260 return count;
261}
262
263}
264#endif
DataSource data as a model.
Definition datamodel.h:160
Provides data from a range of plugins.
Definition datasource.h:32
Filter and sort an existing QAbstractItemModel.
Definition datamodel.h:33
QString sortRole
The role of the sourceModel that will be used for sorting.
Definition datamodel.h:69
int count
How many items are in this model.
Definition datamodel.h:84
QML_ELEMENTQAbstractItemModel * sourceModel
The source model of this sorting proxy model.
Definition datamodel.h:39
int sortColumn
Specify which column should be used for sorting.
Definition datamodel.h:79
Q_INVOKABLE QVariantMap get(int i) const
Returns the item at index in the list model.
QString filterRole
The role of the sourceModel on which filterRegExp must be applied.
Definition datamodel.h:64
QString filterRegExp
The regular expression for the filter, only items with their filterRole matching filterRegExp will be...
Definition datamodel.h:44
QJSValue filterCallback
A JavaScript callable that is passed the source model row index as first argument and the value of fi...
Definition datamodel.h:59
QString filterString
The string for the filter, only items with their filterRole matching filterString will be displayed.
Definition datamodel.h:49
Qt::SortOrder sortOrder
One of Qt.Ascending or Qt.Descending.
Definition datamodel.h:74
Namespace for everything in libplasma.
Definition datamodel.cpp:15
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_REVISIONQ_REVISION
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
virtual int columnCount(const QModelIndex &parent) const const override
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual int rowCount(const QModelIndex &parent) const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.