forked from oKcerG/SortFilterProxyModel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxyrole.cpp
450 lines (369 loc) · 13.2 KB
/
proxyrole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "proxyrole.h"
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlExpression>
#include <QCoreApplication>
#include <QDebug>
#include <QQmlInfo>
#include "filter.h"
namespace qqsfpm {
/*!
\qmltype ProxyRole
\inqmlmodule SortFilterProxyModel
\brief Base type for the \l SortFilterProxyModel proxy roles
The ProxyRole type cannot be used directly in a QML file.
It exists to provide a set of common properties and methods,
available across all the other proxy role types that inherit from it.
Attempting to use the ProxyRole type directly will result in an error.
*/
ProxyRole::ProxyRole(QObject *parent) : QObject(parent)
{
}
/*!
\qmlproperty string ProxyRole::name
This property holds the role name of the proxy role.
*/
const QString& ProxyRole::name() const
{
return m_name;
}
void ProxyRole::setName(const QString& name)
{
if (m_name == name)
return;
Q_EMIT nameAboutToBeChanged();
m_name = name;
Q_EMIT nameChanged();
}
QVariant ProxyRole::roleData(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel)
{
if (m_mutex.tryLock()) {
QVariant result = data(sourceIndex, proxyModel);
m_mutex.unlock();
return result;
} else {
return QVariant{};
}
}
void ProxyRole::proxyModelCompleted(const QQmlSortFilterProxyModel &proxyModel)
{
Q_UNUSED(proxyModel)
}
void ProxyRole::invalidate()
{
Q_EMIT invalidated();
}
/*!
\qmltype JoinRole
\inherits ProxyRole
\inqmlmodule SortFilterProxyModel
\brief a role made from concatenating other roles
A JoinRole is a simple \l ProxyRole that concatenates other roles.
In the following example, the \c fullName role is computed by the concatenation of the \c firstName role and the \c lastName role separated by a space :
\code
SortFilterProxyModel {
sourceModel: contactModel
proxyRoles: JoinRole {
name: "fullName"
roleNames: ["firstName", "lastName"]
}
}
\endcode
*/
/*!
\qmlproperty list<string> JoinRole::roleNames
This property holds the role names that are joined by this role.
*/
QStringList JoinRole::roleNames() const
{
return m_roleNames;
}
void JoinRole::setRoleNames(const QStringList& roleNames)
{
if (m_roleNames == roleNames)
return;
m_roleNames = roleNames;
Q_EMIT roleNamesChanged();
invalidate();
}
/*!
\qmlproperty string JoinRole::separator
This property holds the separator that is used to join the roles specified in \l roleNames.
By default, it's a space.
*/
QString JoinRole::separator() const
{
return m_separator;
}
void JoinRole::setSeparator(const QString& separator)
{
if (m_separator == separator)
return;
m_separator = separator;
Q_EMIT separatorChanged();
invalidate();
}
QVariant JoinRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel)
{
QString result;
for (const QString& roleName : m_roleNames)
result += proxyModel.sourceData(sourceIndex, roleName).toString() + m_separator;
if (!m_roleNames.isEmpty())
result.chop(m_separator.length());
return result;
}
/*!
\qmltype SwitchRole
\inherits ProxyRole
\inqmlmodule SortFilterProxyModel
\brief a role using \l Filter to conditionnaly compute its data
A SwitchRole is a \l ProxyRole that computes its data with the help of \l Filter.
Each top level filters specified in the \l SwitchRole is evaluated on the rows of the model, if a \l Filter evaluates to true, the data of the \l SwitchRole for this row will be the one of the attached \l {value} {SwitchRole.value} property.
If no top level filters evaluate to true, the data will default to the one of the \l defaultRoleName (or the \l defaultValue if no \l defaultRoleName is specified).
In the following example, the \c favoriteOrFirstNameSection role is equal to \c * if the \c favorite role of a row is true, otherwise it's the same as the \c firstName role :
\code
SortFilterProxyModel {
sourceModel: contactModel
proxyRoles: SwitchRole {
name: "favoriteOrFirstNameSection"
filters: ValueFilter {
roleName: "favorite"
value: true
SwitchRole.value: "*"
}
defaultRoleName: "firstName"
}
}
\endcode
*/
SwitchRoleAttached::SwitchRoleAttached(QObject* parent) : QObject (parent)
{
if (!qobject_cast<Filter*>(parent))
qmlInfo(parent) << "SwitchRole must be attached to a Filter";
}
/*!
\qmlattachedproperty var SwitchRole::value
This property attaches a value to a \l Filter.
*/
QVariant SwitchRoleAttached::value() const
{
return m_value;
}
void SwitchRoleAttached::setValue(QVariant value)
{
if (m_value == value)
return;
m_value = value;
Q_EMIT valueChanged();
}
/*!
\qmlproperty string SwitchRole::defaultRoleName
This property holds the default role name of the role.
If no filter match a row, the data of this role will be the data of the role whose name is \c defaultRoleName.
*/
QString SwitchRole::defaultRoleName() const
{
return m_defaultRoleName;
}
void SwitchRole::setDefaultRoleName(const QString& defaultRoleName)
{
if (m_defaultRoleName == defaultRoleName)
return;
m_defaultRoleName = defaultRoleName;
Q_EMIT defaultRoleNameChanged();
invalidate();
}
/*!
\qmlproperty var SwitchRole::defaultValue
This property holds the default value of the role.
If no filter match a row, and no \l defaultRoleName is set, the data of this role will be \c defaultValue.
*/
QVariant SwitchRole::defaultValue() const
{
return m_defaultValue;
}
void SwitchRole::setDefaultValue(const QVariant& defaultValue)
{
if (m_defaultValue == defaultValue)
return;
m_defaultValue = defaultValue;
Q_EMIT defaultValueChanged();
invalidate();
}
/*!
\qmlproperty list<Filter> SwitchRole::filters
This property holds the list of filters for this proxy role.
The data of this role will be equal to the attached \l {value} {SwitchRole.value} property of the first filter that matches the model row.
\sa Filter
*/
QQmlListProperty<Filter> SwitchRole::filters()
{
return QQmlListProperty<Filter>(this, &m_filters,
&SwitchRole::append_filter,
&SwitchRole::count_filter,
&SwitchRole::at_filter,
&SwitchRole::clear_filters);
}
void SwitchRole::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
for (Filter* filter : m_filters)
filter->proxyModelCompleted(proxyModel);
}
SwitchRoleAttached* SwitchRole::qmlAttachedProperties(QObject* object)
{
return new SwitchRoleAttached(object);
}
QVariant SwitchRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel &proxyModel)
{
for (auto filter: m_filters) {
if (!filter->enabled())
continue;
if (filter->filterAcceptsRow(sourceIndex, proxyModel)) {
auto attached = static_cast<SwitchRoleAttached*>(qmlAttachedPropertiesObject<SwitchRole>(filter, false));
if (!attached) {
qWarning() << "No SwitchRole.value provided for this filter" << filter;
continue;
}
QVariant value = attached->value();
if (!value.isValid()) {
qWarning() << "No SwitchRole.value provided for this filter" << filter;
continue;
}
return value;
}
}
if (!m_defaultRoleName.isEmpty())
return proxyModel.sourceData(sourceIndex, m_defaultRoleName);
return m_defaultValue;
}
void SwitchRole::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
{
if (!filter)
return;
SwitchRole* that = static_cast<SwitchRole*>(list->object);
that->m_filters.append(filter);
connect(filter, &Filter::invalidated, that, &SwitchRole::invalidate);
auto attached = static_cast<SwitchRoleAttached*>(qmlAttachedPropertiesObject<SwitchRole>(filter, true));
connect(attached, &SwitchRoleAttached::valueChanged, that, &SwitchRole::invalidate);
that->invalidate();
}
int SwitchRole::count_filter(QQmlListProperty<Filter>* list)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
}
Filter* SwitchRole::at_filter(QQmlListProperty<Filter>* list, int index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void SwitchRole::clear_filters(QQmlListProperty<Filter> *list)
{
SwitchRole* that = static_cast<SwitchRole*>(list->object);
that->m_filters.clear();
that->invalidate();
}
/*!
\qmltype ExpressionRole
\inherits ProxyRole
\inqmlmodule SortFilterProxyModel
\brief A custom role computed from a javascrip expression
An ExpressionRole is a \l ProxyRole allowing to implement a custom role based on a javascript expression.
In the following example, the \c c role is computed by adding the \c a role and \c b role of the model :
\code
SortFilterProxyModel {
sourceModel: numberModel
proxyRoles: ExpressionRole {
name: "c"
expression: model.a + model.b
}
}
\endcode
*/
/*!
\qmlproperty expression ExpressionRole::expression
An expression to implement a custom role.
It has the same syntax has a \l {http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html} {Property Binding} except it will be evaluated for each of the source model's rows.
The data for this role will be the retuned valued of the expression.
Data for each row is exposed like for a delegate of a QML View.
This expression is reevaluated for a row every time its model data changes.
When an external property (not \c index or in \c model) the expression depends on changes, the expression is reevaluated for every row of the source model.
To capture the properties the expression depends on, the expression is first executed with invalid data and each property access is detected by the QML engine.
This means that if a property is not accessed because of a conditional, it won't be captured and the expression won't be reevaluted when this property changes.
A workaround to this problem is to access all the properties the expressions depends unconditionally at the beggining of the expression.
*/
const QQmlScriptString& ExpressionRole::expression() const
{
return m_scriptString;
}
void ExpressionRole::setExpression(const QQmlScriptString& scriptString)
{
if (m_scriptString == scriptString)
return;
m_scriptString = scriptString;
updateExpression();
Q_EMIT expressionChanged();
invalidate();
}
void ExpressionRole::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
updateContext(proxyModel);
}
QVariant ExpressionRole::data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel)
{
if (!m_scriptString.isEmpty()) {
QVariantMap modelMap;
QHash<int, QByteArray> roles = proxyModel.roleNames();
QQmlContext context(qmlContext(this));
auto addToContext = [&] (const QString &name, const QVariant& value) {
context.setContextProperty(name, value);
modelMap.insert(name, value);
};
for (auto it = roles.cbegin(); it != roles.cend(); ++it)
addToContext(it.value(), proxyModel.sourceData(sourceIndex, it.key()));
addToContext("index", sourceIndex.row());
context.setContextProperty("model", modelMap);
QQmlExpression expression(m_scriptString, &context);
QVariant result = expression.evaluate();
if (expression.hasError()) {
qWarning() << expression.error();
return true;
}
return result;
}
return QVariant();
}
void ExpressionRole::updateContext(const QQmlSortFilterProxyModel& proxyModel)
{
delete m_context;
m_context = new QQmlContext(qmlContext(this), this);
// what about roles changes ?
QVariantMap modelMap;
auto addToContext = [&] (const QString &name, const QVariant& value) {
m_context->setContextProperty(name, value);
modelMap.insert(name, value);
};
for (const QByteArray& roleName : proxyModel.roleNames().values())
addToContext(roleName, QVariant());
addToContext("index", -1);
m_context->setContextProperty("model", modelMap);
updateExpression();
}
void ExpressionRole::updateExpression()
{
if (!m_context)
return;
delete m_expression;
m_expression = new QQmlExpression(m_scriptString, m_context, 0, this);
connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionRole::invalidate);
m_expression->setNotifyOnValueChanged(true);
m_expression->evaluate();
}
void registerProxyRoleTypes() {
qmlRegisterUncreatableType<ProxyRole>("SortFilterProxyModel", 0, 2, "ProxyRole", "ProxyRole is an abstract class");
qmlRegisterType<JoinRole>("SortFilterProxyModel", 0, 2, "JoinRole");
qmlRegisterType<SwitchRole>("SortFilterProxyModel", 0, 2, "SwitchRole");
qmlRegisterType<ExpressionRole>("SortFilterProxyModel", 0, 2, "ExpressionRole");
}
Q_COREAPP_STARTUP_FUNCTION(registerProxyRoleTypes)
}