-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsfeaturefiltermodel.h
351 lines (291 loc) · 11.7 KB
/
qgsfeaturefiltermodel.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
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
/***************************************************************************
qgsfeaturefiltermodel.h - QgsFeatureFilterModel
---------------------
begin : 10.3.2017
copyright : (C) 2017 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSFEATUREFILTERMODEL_H
#define QGSFEATUREFILTERMODEL_H
#include <QAbstractItemModel>
#include "qgsconditionalstyle.h"
class QgsFieldExpressionValuesGatherer;
/**
* \ingroup core
* Provides a list of features based on filter conditions.
* Features are fetched asynchronously.
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel
{
Q_OBJECT
Q_PROPERTY( QgsVectorLayer *sourceLayer READ sourceLayer WRITE setSourceLayer NOTIFY sourceLayerChanged )
Q_PROPERTY( QString displayExpression READ displayExpression WRITE setDisplayExpression NOTIFY displayExpressionChanged )
Q_PROPERTY( QString filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged )
Q_PROPERTY( QString filterExpression READ filterExpression WRITE setFilterExpression NOTIFY filterExpressionChanged )
Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
Q_PROPERTY( bool isLoading READ isLoading NOTIFY isLoadingChanged )
/**
* A field of sourceLayer that is unique and should be used to identify features.
* Normally the primary key field.
* Needs to match the identifierValue.
*/
Q_PROPERTY( QString identifierField READ identifierField WRITE setIdentifierField NOTIFY identifierFieldChanged )
/**
* The value that identifies the current feature.
*/
Q_PROPERTY( QVariant extraIdentifierValue READ extraIdentifierValue WRITE setExtraIdentifierValue NOTIFY extraIdentifierValueChanged )
Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
public:
/**
* Extra roles that can be used to fetch data from this model.
*/
enum Role
{
IdentifierValueRole = Qt::UserRole, //!< \deprecated Use IdentifierValuesRole instead
IdentifierValuesRole, //!< Used to retrieve the identifierValues (primary keys) of a feature.
ValueRole //!< Used to retrieve the displayExpression of a feature.
};
/**
* Create a new QgsFeatureFilterModel, optionally specifying a \a parent.
*/
explicit QgsFeatureFilterModel( QObject *parent = nullptr );
~QgsFeatureFilterModel() override;
/**
* The source layer from which features will be fetched.
*/
QgsVectorLayer *sourceLayer() const;
/**
* The source layer from which features will be fetched.
*/
void setSourceLayer( QgsVectorLayer *sourceLayer );
/**
* The display expression will be used for
*
* - displaying values in the combobox
* - filtering based on filterValue
*/
QString displayExpression() const;
/**
* The display expression will be used for
*
* - displaying values in the combobox
* - filtering based on filterValue
*/
void setDisplayExpression( const QString &displayExpression );
/**
* This value will be used to filter the features available from
* this model. Whenever a substring of the displayExpression of a feature
* matches the filter value, it will be accessible by this model.
*/
QString filterValue() const;
/**
* This value will be used to filter the features available from
* this model. Whenever a substring of the displayExpression of a feature
* matches the filter value, it will be accessible by this model.
*/
void setFilterValue( const QString &filterValue );
QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
QModelIndex parent( const QModelIndex &child ) const override;
int rowCount( const QModelIndex &parent ) const override;
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;
/**
* An additional filter expression to apply, next to the filterValue.
* Can be used for spatial filtering etc.
*/
QString filterExpression() const;
/**
* An additional filter expression to apply, next to the filterValue.
* Can be used for spatial filtering etc.
*/
void setFilterExpression( const QString &filterExpression );
/**
* Indicator if the model is currently performing any feature iteration in the background.
*/
bool isLoading() const;
/**
* The identifier field should be a unique field that can be used to identify individual features.
* It is normally set to the primary key of the layer.
* If there are several identifier fields defined, the behavior is not guaranteed
* \deprecated since QGIS 3.10 use identifierFields instead
*/
Q_DECL_DEPRECATED QString identifierField() const;
/**
* The identifier field should be a unique field that can be used to identify individual features.
* It is normally set to the primary key of the layer.
* \since QGIS 3.10
*/
QStringList identifierFields() const;
/**
* The identifier field should be a unique field that can be used to identify individual features.
* It is normally set to the primary key of the layer.
* \deprecated since QGIS 3.10
*/
Q_DECL_DEPRECATED void setIdentifierField( const QString &identifierField );
/**
* The identifier field should be a unique field that can be used to identify individual features.
* It is normally set to the primary key of the layer.
* \note This will also reset identifier fields to NULL
* \since QGIS 3.10
*/
void setIdentifierFields( const QStringList &identifierFields );
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model.
* \deprecated since QGIS 3.10
*/
Q_DECL_DEPRECATED QVariant extraIdentifierValue() const;
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model.
* \since QGIS 3.10
*/
QVariantList extraIdentifierValues() const;
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model.
* \deprecated since QGIS 3.10
*/
Q_DECL_DEPRECATED void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model.
* \since QGIS 3.10
*/
void setExtraIdentifierValues( const QVariantList &extraIdentifierValues );
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model as NULL value(s).
* \since QGIS 3.10
*/
void setExtraIdentifierValuesToNull();
/**
* The index at which the extra identifier value is available within the model.
*/
int extraIdentifierValueIndex() const;
/**
* Flag indicating that the extraIdentifierValue does not exist in the data.
*/
bool extraValueDoesNotExist() const;
/**
* Add a NULL entry to the list.
*/
bool allowNull() const;
/**
* Add a NULL entry to the list.
*/
void setAllowNull( bool allowNull );
signals:
/**
* The source layer from which features will be fetched.
*/
void sourceLayerChanged();
/**
* The display expression will be used for
*
* - displaying values in the combobox
* - filtering based on filterValue
*/
void displayExpressionChanged();
/**
* This value will be used to filter the features available from
* this model. Whenever a substring of the displayExpression of a feature
* matches the filter value, it will be accessible by this model.
*/
void filterValueChanged();
/**
* An additional filter expression to apply, next to the filterValue.
* Can be used for spatial filtering etc.
*/
void filterExpressionChanged();
/**
* Indicator if the model is currently performing any feature iteration in the background.
*/
void isLoadingChanged();
/**
* The identifier field should be a unique field that can be used to identify individual features.
* It is normally set to the primary key of the layer.
*/
void identifierFieldChanged();
/**
* Indicates that a filter job has been completed and new data may be available.
*/
void filterJobCompleted();
/**
* Allows specifying one value that does not need to match the filter criteria but will
* still be available in the model.
*/
void extraIdentifierValueChanged();
/**
* The index at which the extra identifier value is available within the model.
*/
void extraIdentifierValueIndexChanged( int index );
/**
* Flag indicating that the extraIdentifierValue does not exist in the data.
*/
void extraValueDoesNotExistChanged();
/**
* Notification that the model is about to be changed because a job was completed.
*/
void beginUpdate();
/**
* Notification that the model change is finished. Will always be emitted in sync with beginUpdate.
*/
void endUpdate();
/**
* Add a NULL entry to the list.
*/
void allowNullChanged();
private slots:
void updateCompleter();
void scheduledReload();
private:
QSet<QString> requestedAttributes() const;
void setExtraIdentifierValuesIndex( int index, bool force = false );
void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
void reload();
void reloadCurrentFeature();
void setExtraIdentifierValuesUnguarded( const QVariantList &extraIdentifierValues );
struct Entry
{
Entry() = default;
Entry( const QVariantList &_identifierValues, const QString &_value, const QgsFeature &_feature )
: identifierValues( _identifierValues )
, value( _value )
, feature( _feature )
{}
QVariantList identifierValues;
QString value;
QgsFeature feature;
bool operator()( const Entry &lhs, const Entry &rhs ) const;
};
Entry nullEntry();
QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
QgsVectorLayer *mSourceLayer = nullptr;
QgsExpression mDisplayExpression;
QString mFilterValue;
QString mFilterExpression;
mutable QgsExpressionContext mExpressionContext;
mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
QVector<Entry> mEntries;
QgsFieldExpressionValuesGatherer *mGatherer = nullptr;
QTimer mReloadTimer;
bool mShouldReloadCurrentFeature = false;
bool mExtraValueDoesNotExist = false;
bool mAllowNull = false;
bool mIsSettingExtraIdentifierValue = false;
QStringList mIdentifierFields;
QVariantList mExtraIdentifierValues;
int mExtraIdentifierValueIndex = -1;
friend class QgsFieldExpressionValuesGatherer;
};
#endif // QGSFEATUREFILTERMODEL_H