forked from oKcerG/SortFilterProxyModel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.h
262 lines (194 loc) · 7.23 KB
/
filter.h
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
#ifndef FILTER_H
#define FILTER_H
#include <QObject>
#include <QQmlExpression>
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {
class Filter : public QObject
{
Q_OBJECT
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool inverted READ inverted WRITE setInverted NOTIFY invertedChanged)
public:
explicit Filter(QObject *parent = nullptr);
virtual ~Filter() = default;
bool enabled() const;
void setEnabled(bool enabled);
bool inverted() const;
void setInverted(bool inverted);
bool filterAcceptsRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
virtual void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel);
Q_SIGNALS:
void enabledChanged();
void invertedChanged();
void invalidated();
protected:
virtual bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const = 0;
void invalidate();
private:
bool m_enabled = true;
bool m_inverted = false;
};
class RoleFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QString roleName READ roleName WRITE setRoleName NOTIFY roleNameChanged)
public:
using Filter::Filter;
const QString& roleName() const;
void setRoleName(const QString& roleName);
Q_SIGNALS:
void roleNameChanged();
protected:
QVariant sourceData(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const;
private:
QString m_roleName;
};
class ValueFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
public:
using RoleFilter::RoleFilter;
const QVariant& value() const;
void setValue(const QVariant& value);
protected:
bool filterRow(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void valueChanged();
private:
QVariant m_value;
};
class IndexFilter: public Filter {
Q_OBJECT
Q_PROPERTY(QVariant minimumIndex READ minimumIndex WRITE setMinimumIndex NOTIFY minimumIndexChanged)
Q_PROPERTY(QVariant maximumIndex READ maximumIndex WRITE setMaximumIndex NOTIFY maximumIndexChanged)
public:
using Filter::Filter;
const QVariant& minimumIndex() const;
void setMinimumIndex(const QVariant& minimumIndex);
const QVariant& maximumIndex() const;
void setMaximumIndex(const QVariant& maximumIndex);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumIndexChanged();
void maximumIndexChanged();
private:
QVariant m_minimumIndex;
QVariant m_maximumIndex;
};
class RegExpFilter : public RoleFilter {
Q_OBJECT
Q_PROPERTY(QString pattern READ pattern WRITE setPattern NOTIFY patternChanged)
Q_PROPERTY(PatternSyntax syntax READ syntax WRITE setSyntax NOTIFY syntaxChanged)
Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity NOTIFY caseSensitivityChanged)
public:
enum PatternSyntax {
RegExp = QRegExp::RegExp,
Wildcard = QRegExp::Wildcard,
FixedString = QRegExp::FixedString,
RegExp2 = QRegExp::RegExp2,
WildcardUnix = QRegExp::WildcardUnix,
W3CXmlSchema11 = QRegExp::W3CXmlSchema11 };
Q_ENUMS(PatternSyntax)
using RoleFilter::RoleFilter;
QString pattern() const;
void setPattern(const QString& pattern);
PatternSyntax syntax() const;
void setSyntax(PatternSyntax syntax);
Qt::CaseSensitivity caseSensitivity() const;
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void patternChanged();
void syntaxChanged();
void caseSensitivityChanged();
private:
QRegExp m_regExp;
Qt::CaseSensitivity m_caseSensitivity = m_regExp.caseSensitivity();
PatternSyntax m_syntax = static_cast<PatternSyntax>(m_regExp.patternSyntax());
QString m_pattern = m_regExp.pattern();
};
class RangeFilter : public RoleFilter
{
Q_OBJECT
Q_PROPERTY(QVariant minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged)
Q_PROPERTY(bool minimumInclusive READ minimumInclusive WRITE setMinimumInclusive NOTIFY minimumInclusiveChanged)
Q_PROPERTY(QVariant maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged)
Q_PROPERTY(bool maximumInclusive READ maximumInclusive WRITE setMaximumInclusive NOTIFY maximumInclusiveChanged)
public:
using RoleFilter::RoleFilter;
QVariant minimumValue() const;
void setMinimumValue(QVariant minimumValue);
bool minimumInclusive() const;
void setMinimumInclusive(bool minimumInclusive);
QVariant maximumValue() const;
void setMaximumValue(QVariant maximumValue);
bool maximumInclusive() const;
void setMaximumInclusive(bool maximumInclusive);
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void minimumValueChanged();
void minimumInclusiveChanged();
void maximumValueChanged();
void maximumInclusiveChanged();
private:
QVariant m_minimumValue;
bool m_minimumInclusive = true;
QVariant m_maximumValue;
bool m_maximumInclusive = true;
};
class ExpressionFilter : public Filter
{
Q_OBJECT
Q_PROPERTY(QQmlScriptString expression READ expression WRITE setExpression NOTIFY expressionChanged)
public:
using Filter::Filter;
const QQmlScriptString& expression() const;
void setExpression(const QQmlScriptString& scriptString);
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
Q_SIGNALS:
void expressionChanged();
private:
void updateContext(const QQmlSortFilterProxyModel& proxyModel);
void updateExpression();
QQmlScriptString m_scriptString;
QQmlExpression* m_expression = nullptr;
QQmlContext* m_context = nullptr;
};
class FilterContainer : public Filter {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filters)
Q_CLASSINFO("DefaultProperty", "filters")
public:
using Filter::Filter;
QQmlListProperty<Filter> filters();
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
protected:
QList<Filter*> m_filters;
private:
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
};
class AnyOfFilter : public FilterContainer {
Q_OBJECT
public:
using FilterContainer::FilterContainer;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
class AllOfFilter : public FilterContainer {
Q_OBJECT
public:
using FilterContainer::FilterContainer;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
}
#endif // FILTER_H