forked from oKcerG/SortFilterProxyModel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter.cpp
744 lines (603 loc) · 21.4 KB
/
filter.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#include "filter.h"
#include <QtQml>
namespace qqsfpm {
/*!
\qmltype Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for the \l SortFilterProxyModel filters
The Filter 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 filter types that inherit from it.
Attempting to use the Filter type directly will result in an error.
*/
Filter::Filter(QObject *parent) : QObject(parent)
{
}
/*!
\qmlproperty bool Filter::enabled
This property holds whether the filter is enabled.
A disabled filter will accept every rows unconditionally (even if it's inverted).
By default, filters are enabled.
*/
bool Filter::enabled() const
{
return m_enabled;
}
void Filter::setEnabled(bool enabled)
{
if (m_enabled == enabled)
return;
m_enabled = enabled;
Q_EMIT enabledChanged();
Q_EMIT invalidated();
}
/*!
\qmlproperty bool Filter::inverted
This property holds whether the filter is inverted.
When a filter is inverted, a row normally accepted would be rejected, and vice-versa.
By default, filters are not inverted.
*/
bool Filter::inverted() const
{
return m_inverted;
}
void Filter::setInverted(bool inverted)
{
if (m_inverted == inverted)
return;
m_inverted = inverted;
Q_EMIT invertedChanged();
invalidate();
}
bool Filter::filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_enabled || filterRow(sourceIndex, proxyModel) ^ m_inverted;
}
void Filter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
Q_UNUSED(proxyModel)
}
void Filter::invalidate()
{
if (m_enabled)
Q_EMIT invalidated();
}
/*!
\qmltype RoleFilter
\qmlabstract
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Base type for filters based on a source model role
The RoleFilter 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 filter types that inherit from it.
Attempting to use the RoleFilter type directly will result in an error.
*/
/*!
\qmlproperty string RoleFilter::roleName
This property holds the role name that the filter is using to query the source model's data when filtering items.
*/
const QString& RoleFilter::roleName() const
{
return m_roleName;
}
void RoleFilter::setRoleName(const QString& roleName)
{
if (m_roleName == roleName)
return;
m_roleName = roleName;
Q_EMIT roleNameChanged();
invalidate();
}
QVariant RoleFilter::sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return proxyModel.sourceData(sourceIndex, m_roleName);
}
/*!
\qmltype ValueFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching exactly a value
A ValueFilter is a simple \l RoleFilter that accepts rows matching exactly the filter's value
In the following example, only rows with their \c favorite role set to \c true will be accepted when the checkbox is checked :
\code
CheckBox {
id: showOnlyFavoriteCheckBox
}
SortFilterProxyModel {
sourceModel: contactModel
filters: ValueFilter {
roleName: "favorite"
value: true
enabled: showOnlyFavoriteCheckBox.checked
}
}
\endcode
*/
/*!
\qmlproperty variant ValueFilter::value
This property holds the value used to filter the contents of the source model.
*/
const QVariant &ValueFilter::value() const
{
return m_value;
}
void ValueFilter::setValue(const QVariant& value)
{
if (m_value == value)
return;
m_value = value;
Q_EMIT valueChanged();
invalidate();
}
bool ValueFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
return !m_value.isValid() || m_value == sourceData(sourceIndex, proxyModel);
}
/*!
\qmltype IndexFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters rows based on their source index
An IndexFilter is a filter allowing contents to be filtered based on their source model index.
In the following example, only the first row of the source model will be accepted:
\code
SortFilterProxyModel {
sourceModel: contactModel
filters: IndexFilter {
maximumIndex: 0
}
}
\endcode
*/
/*!
\qmlproperty int IndexFilter::minimumIndex
This property holds the minimumIndex of the filter.
Rows with a source index lower than \c minimumIndex will be rejected.
If \c minimumIndex is negative, it is counted from the end of the source model, meaning that :
\code minimumIndex: -1\endcode
is equivalent to :
\code minimumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::minimumIndex() const
{
return m_minimumIndex;
}
void IndexFilter::setMinimumIndex(const QVariant& minimumIndex)
{
if (m_minimumIndex == minimumIndex)
return;
m_minimumIndex = minimumIndex;
Q_EMIT minimumIndexChanged();
invalidate();
}
/*!
\qmlproperty int IndexFilter::maximumIndex
This property holds the maximumIndex of the filter.
Rows with a source index higher than \c maximumIndex will be rejected.
If \c maximumIndex is negative, it is counted from the end of the source model, meaning that:
\code maximumIndex: -1\endcode
is equivalent to :
\code maximumIndex: sourceModel.count - 1\endcode
By default, no value is set.
*/
const QVariant& IndexFilter::maximumIndex() const
{
return m_maximumIndex;
}
void IndexFilter::setMaximumIndex(const QVariant& maximumIndex)
{
if (m_maximumIndex == maximumIndex)
return;
m_maximumIndex = maximumIndex;
Q_EMIT maximumIndexChanged();
invalidate();
}
bool IndexFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
int sourceRowCount = proxyModel.sourceModel()->rowCount();
int sourceRow = sourceIndex.row();
bool minimumIsValid;
int minimum = m_minimumIndex.toInt(&minimumIsValid);
if (minimumIsValid) {
int actualMinimum = minimum < 0 ? sourceRowCount + minimum : minimum;
if (sourceRow < actualMinimum)
return false;
}
bool maximumIsValid;
int maximum = m_maximumIndex.toInt(&maximumIsValid);
if (maximumIsValid) {
int actualMaximum = maximum < 0 ? sourceRowCount + maximum : maximum;
if (sourceRow > actualMaximum)
return false;
}
return true;
}
/*!
\qmltype RegExpFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows matching a regular expression
A RegExpFilter is a \l RoleFilter that accepts rows matching a regular rexpression.
In the following example, only rows with their \c lastName role beggining with the content of textfield the will be accepted:
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: RegExpFilter {
roleName: "lastName"
pattern: "^" + nameTextField.displayText
}
}
\endcode
*/
/*!
\qmlproperty bool RegExpFilter::pattern
The pattern used to filter the contents of the source model.
\sa syntax
*/
QString RegExpFilter::pattern() const
{
return m_pattern;
}
void RegExpFilter::setPattern(const QString& pattern)
{
if (m_pattern == pattern)
return;
m_pattern = pattern;
m_regExp.setPattern(pattern);
Q_EMIT patternChanged();
invalidate();
}
/*!
\qmlproperty enum RegExpFilter::syntax
The pattern used to filter the contents of the source model.
Only the source model's value having their \l roleName data matching this \l pattern with the specified \l syntax will be kept.
\value RegExpFilter.RegExp A rich Perl-like pattern matching syntax. This is the default.
\value RegExpFilter.Wildcard This provides a simple pattern matching syntax similar to that used by shells (command interpreters) for "file globbing".
\value RegExpFilter.FixedString The pattern is a fixed string. This is equivalent to using the RegExp pattern on a string in which all metacharacters are escaped.
\value RegExpFilter.RegExp2 Like RegExp, but with greedy quantifiers.
\value RegExpFilter.WildcardUnix This is similar to Wildcard but with the behavior of a Unix shell. The wildcard characters can be escaped with the character "\".
\value RegExpFilter.W3CXmlSchema11 The pattern is a regular expression as defined by the W3C XML Schema 1.1 specification.
\sa pattern
*/
RegExpFilter::PatternSyntax RegExpFilter::syntax() const
{
return m_syntax;
}
void RegExpFilter::setSyntax(RegExpFilter::PatternSyntax syntax)
{
if (m_syntax == syntax)
return;
m_syntax = syntax;
m_regExp.setPatternSyntax(static_cast<QRegExp::PatternSyntax>(syntax));
Q_EMIT syntaxChanged();
invalidate();
}
/*!
\qmlproperty Qt::CaseSensitivity RegExpFilter::caseSensitivity
This property holds the caseSensitivity of the filter.
*/
Qt::CaseSensitivity RegExpFilter::caseSensitivity() const
{
return m_caseSensitivity;
}
void RegExpFilter::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
{
if (m_caseSensitivity == caseSensitivity)
return;
m_caseSensitivity = caseSensitivity;
m_regExp.setCaseSensitivity(caseSensitivity);
Q_EMIT caseSensitivityChanged();
invalidate();
}
bool RegExpFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QString string = sourceData(sourceIndex, proxyModel).toString();
return m_regExp.indexIn(string) != -1;
}
/*!
\qmltype RangeFilter
\inherits RoleFilter
\inqmlmodule SortFilterProxyModel
\brief Filters rows between boundary values
A RangeFilter is a \l RoleFilter that accepts rows if their data is between the filter's minimum and maximum value.
In the following example, only rows with their \c price role set to a value between the tow boundary of the slider will be accepted :
\code
RangeSlider {
id: priceRangeSlider
}
SortFilterProxyModel {
sourceModel: priceModel
filters: RangeFilter {
roleName: "price"
minimumValue: priceRangeSlider.first.value
maximumValue: priceRangeSlider.second.value
}
}
\endcode
*/
/*!
\qmlproperty int RangeFilter::minimumValue
This property holds the minimumValue of the filter.
Rows with a value lower than \c minimumValue will be rejected.
By default, no value is set.
\sa minimumInclusive
*/
QVariant RangeFilter::minimumValue() const
{
return m_minimumValue;
}
void RangeFilter::setMinimumValue(QVariant minimumValue)
{
if (m_minimumValue == minimumValue)
return;
m_minimumValue = minimumValue;
Q_EMIT minimumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::minimumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::minimumInclusive() const
{
return m_minimumInclusive;
}
void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
if (m_minimumInclusive == minimumInclusive)
return;
m_minimumInclusive = minimumInclusive;
Q_EMIT minimumInclusiveChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumValue
This property holds the maximumValue of the filter.
Rows with a value higher than \c maximumValue will be rejected.
By default, no value is set.
\sa maximumInclusive
*/
QVariant RangeFilter::maximumValue() const
{
return m_maximumValue;
}
void RangeFilter::setMaximumValue(QVariant maximumValue)
{
if (m_maximumValue == maximumValue)
return;
m_maximumValue = maximumValue;
Q_EMIT maximumValueChanged();
invalidate();
}
/*!
\qmlproperty int RangeFilter::maximumInclusive
This property holds whether the \l minimumValue is inclusive.
By default, the \l minimumValue is inclusive.
\sa minimumValue
*/
bool RangeFilter::maximumInclusive() const
{
return m_maximumInclusive;
}
void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
if (m_maximumInclusive == maximumInclusive)
return;
m_maximumInclusive = maximumInclusive;
Q_EMIT maximumInclusiveChanged();
invalidate();
}
bool RangeFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
QVariant value = sourceData(sourceIndex, proxyModel);
bool lessThanMin = m_minimumValue.isValid() &&
m_minimumInclusive ? value < m_minimumValue : value <= m_minimumValue;
bool moreThanMax = m_maximumValue.isValid() &&
m_maximumInclusive ? value > m_maximumValue : value >= m_maximumValue;
return !(lessThanMin || moreThanMax);
}
/*!
\qmltype ExpressionFilter
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filters row with a custom filtering
An ExpressionFilter is a \l Filter allowing to implement custom filtering based on a javascript expression.
*/
/*!
\qmlproperty expression ExpressionFilter::expression
An expression to implement custom filtering, it must evaluate to a boolean.
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.
Rows that have their expression evaluating to \c true will be accepted by the model.
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& ExpressionFilter::expression() const
{
return m_scriptString;
}
void ExpressionFilter::setExpression(const QQmlScriptString& scriptString)
{
if (m_scriptString == scriptString)
return;
m_scriptString = scriptString;
updateExpression();
Q_EMIT expressionChanged();
invalidate();
}
void ExpressionFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
updateContext(proxyModel);
}
bool ExpressionFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
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 variantResult = expression.evaluate();
if (expression.hasError()) {
qWarning() << expression.error();
return true;
}
if (variantResult.canConvert<bool>()) {
return variantResult.toBool();
} else {
qWarning("%s:%i:%i : Can't convert result to bool",
expression.sourceFile().toUtf8().data(),
expression.lineNumber(),
expression.columnNumber());
return true;
}
}
return true;
}
void ExpressionFilter::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 ExpressionFilter::updateExpression()
{
if (!m_context)
return;
delete m_expression;
m_expression = new QQmlExpression(m_scriptString, m_context, 0, this);
connect(m_expression, &QQmlExpression::valueChanged, this, &ExpressionFilter::invalidate);
m_expression->setNotifyOnValueChanged(true);
m_expression->evaluate();
}
QQmlListProperty<Filter> FilterContainer::filters()
{
return QQmlListProperty<Filter>(this, &m_filters,
&FilterContainer::append_filter,
&FilterContainer::count_filter,
&FilterContainer::at_filter,
&FilterContainer::clear_filters);
}
void FilterContainer::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
for (Filter* filter : m_filters)
filter->proxyModelCompleted(proxyModel);
}
void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
{
if (!filter)
return;
FilterContainer* that = static_cast<FilterContainer*>(list->object);
that->m_filters.append(filter);
connect(filter, &Filter::invalidated, that, &FilterContainer::invalidate);
that->invalidate();
}
int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
}
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, int index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void FilterContainer::clear_filters(QQmlListProperty<Filter> *list)
{
FilterContainer* that = static_cast<FilterContainer*>(list->object);
that->m_filters.clear();
that->invalidate();
}
/*!
\qmltype AnyOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by at least one of its child filters
The AnyOf type is a \l Filter container that accepts rows if any of its contained (and enabled) filters accept them.
In the following example, only the rows where the \c firstName role or the \c lastName role match the text entered in the \c nameTextField will be accepted :
\code
TextField {
id: nameTextField
}
SortFilterProxyModel {
sourceModel: contactModel
filters: AnyOf {
RegExpFilter {
roleName: "lastName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
RegExpFilter {
roleName: "firstName"
pattern: nameTextField.text
caseSensitivity: Qt.CaseInsensitive
}
}
}
\endcode
*/
bool AnyOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if any of the enabled filters return true
return std::any_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->enabled() && filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
/*!
\qmltype AllOf
\inherits Filter
\inqmlmodule SortFilterProxyModel
\brief Filter container accepting rows accepted by all its child filters
The AllOf type is a \l Filter container that accepts rows if all of its contained (and enabled) filters accept them, or if it has no filter.
Using it as a top level filter has the same effect as putting all its child filters as top level filters. It can however be usefull to use an AllOf filter when nested in an AnyOf filter.
*/
bool AllOfFilter::filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const
{
//return true if all filters return false, or if there is no filter.
return std::all_of(m_filters.begin(), m_filters.end(),
[&sourceIndex, &proxyModel] (Filter* filter) {
return filter->filterAcceptsRow(sourceIndex, proxyModel);
}
);
}
void registerFilterTypes() {
qmlRegisterUncreatableType<Filter>("SortFilterProxyModel", 0, 2, "Filter", "Filter is an abstract class");
qmlRegisterType<ValueFilter>("SortFilterProxyModel", 0, 2, "ValueFilter");
qmlRegisterType<IndexFilter>("SortFilterProxyModel", 0, 2, "IndexFilter");
qmlRegisterType<RegExpFilter>("SortFilterProxyModel", 0, 2, "RegExpFilter");
qmlRegisterType<RangeFilter>("SortFilterProxyModel", 0, 2, "RangeFilter");
qmlRegisterType<ExpressionFilter>("SortFilterProxyModel", 0, 2, "ExpressionFilter");
qmlRegisterType<AnyOfFilter>("SortFilterProxyModel", 0, 2, "AnyOf");
qmlRegisterType<AllOfFilter>("SortFilterProxyModel", 0, 2, "AllOf");
}
Q_COREAPP_STARTUP_FUNCTION(registerFilterTypes)
}