This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
forked from oKcerG/SortFilterProxyModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.cpp
436 lines (353 loc) · 10.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
#include "filter.h"
#include <QtQml>
namespace qqsfpm {
Filter::Filter(QObject *parent) : QObject(parent)
{
}
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 invalidate();
}
bool Filter::inverted() const
{
return m_inverted;
}
void Filter::setInverted(bool inverted)
{
if (m_inverted == inverted)
return;
m_inverted = inverted;
Q_EMIT invertedChanged();
filterChanged();
}
bool Filter::filterAcceptsRow(const QModelIndex &sourceIndex) const
{
return !m_enabled || filterRow(sourceIndex) ^ m_inverted;
}
QQmlSortFilterProxyModel* Filter::proxyModel() const
{
return m_proxyModel;
}
void Filter::proxyModelCompleted()
{
}
void Filter::filterChanged()
{
if (m_enabled)
invalidate();
}
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();
filterChanged();
}
QVariant RoleFilter::sourceData(const QModelIndex &sourceIndex) const
{
return proxyModel()->sourceData(sourceIndex, m_roleName);
}
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();
filterChanged();
}
bool ValueFilter::filterRow(const QModelIndex& sourceIndex) const
{
return !m_value.isValid() || m_value == sourceData(sourceIndex);
}
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();
filterChanged();
}
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();
filterChanged();
}
bool IndexFilter::filterRow(const QModelIndex& sourceIndex) const
{
int sourceRowCount = proxyModel()->sourceModel()->rowCount();
int sourceRow = sourceIndex.row();
bool minimumIsValid;
int minimum = m_minimumIndex.toInt(&minimumIsValid);
int actualMinimum = (sourceRowCount + minimum) % sourceRowCount;
bool lowerThanMinimumIndex = minimumIsValid && sourceRow < actualMinimum;
bool maximumIsValid;
int maximum = m_maximumIndex.toInt(&maximumIsValid);
int actualMaximum = (sourceRowCount + maximum) % sourceRowCount;
bool greaterThanMaximumIndex = maximumIsValid && sourceRow >actualMaximum;
return !lowerThanMinimumIndex && !greaterThanMaximumIndex;
}
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();
filterChanged();
}
QQmlSortFilterProxyModel::PatternSyntax RegExpFilter::syntax() const
{
return m_syntax;
}
void RegExpFilter::setSyntax(QQmlSortFilterProxyModel::PatternSyntax syntax)
{
if (m_syntax == syntax)
return;
m_syntax = syntax;
m_regExp.setPatternSyntax(static_cast<QRegExp::PatternSyntax>(syntax));
Q_EMIT syntaxChanged();
filterChanged();
}
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();
filterChanged();
}
bool RegExpFilter::filterRow(const QModelIndex& sourceIndex) const
{
QString string = sourceData(sourceIndex).toString();
return m_regExp.indexIn(string) != -1;
}
QVariant RangeFilter::minimumValue() const
{
return m_minimumValue;
}
void RangeFilter::setMinimumValue(QVariant minimumValue)
{
if (m_minimumValue == minimumValue)
return;
m_minimumValue = minimumValue;
Q_EMIT minimumValueChanged();
filterChanged();
}
bool RangeFilter::minimumInclusive() const
{
return m_minimumInclusive;
}
void RangeFilter::setMinimumInclusive(bool minimumInclusive)
{
if (m_minimumInclusive == minimumInclusive)
return;
m_minimumInclusive = minimumInclusive;
Q_EMIT minimumInclusiveChanged();
filterChanged();
}
QVariant RangeFilter::maximumValue() const
{
return m_maximumValue;
}
void RangeFilter::setMaximumValue(QVariant maximumValue)
{
if (m_maximumValue == maximumValue)
return;
m_maximumValue = maximumValue;
Q_EMIT maximumValueChanged();
filterChanged();
}
bool RangeFilter::maximumInclusive() const
{
return m_maximumInclusive;
}
void RangeFilter::setMaximumInclusive(bool maximumInclusive)
{
if (m_maximumInclusive == maximumInclusive)
return;
m_maximumInclusive = maximumInclusive;
Q_EMIT maximumInclusiveChanged();
filterChanged();
}
bool RangeFilter::filterRow(const QModelIndex& sourceIndex) const
{
QVariant value = sourceData(sourceIndex);
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);
}
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();
filterChanged();
}
bool ExpressionFilter::filterRow(const QModelIndex& sourceIndex) 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::proxyModelCompleted()
{
updateContext();
}
void ExpressionFilter::updateContext()
{
if (!proxyModel())
return;
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::filterChanged);
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::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::invalidate, that, &FilterContainer::filterChanged);
that->filterChanged();
}
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->filterChanged();
}
void FilterContainer::proxyModelCompleted()
{
for (Filter* filter : m_filters) {
filter->m_proxyModel = proxyModel();
filter->proxyModelCompleted();
}
}
bool AnyOfFilter::filterRow(const QModelIndex& sourceIndex) const
{
//return true if any of the enabled filters return true
return std::any_of(m_filters.begin(), m_filters.end(),
[&sourceIndex] (Filter* filter) {
return filter->enabled() && filter->filterAcceptsRow(sourceIndex);
}
);
}
bool AllOfFilter::filterRow(const QModelIndex& sourceIndex) 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] (Filter* filter) {
return filter->filterAcceptsRow(sourceIndex);
}
);
}
}