QGIS API Documentation  3.0.2-Girona (307d082)
qgsfeaturefiltermodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturefiltermodel.h - QgsFeatureFilterModel
3 
4  ---------------------
5  begin : 10.3.2017
6  copyright : (C) 2017 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSFEATUREFILTERMODEL_H
17 #define QGSFEATUREFILTERMODEL_H
18 
19 #include <QAbstractItemModel>
20 
21 #include "qgsvectorlayer.h"
22 #include "qgsconditionalstyle.h"
23 
24 class QgsFieldExpressionValuesGatherer;
25 
33 class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel
34 {
35  Q_OBJECT
36 
37  Q_PROPERTY( QgsVectorLayer *sourceLayer READ sourceLayer WRITE setSourceLayer NOTIFY sourceLayerChanged )
38  Q_PROPERTY( QString displayExpression READ displayExpression WRITE setDisplayExpression NOTIFY displayExpressionChanged )
39  Q_PROPERTY( QString filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged )
40  Q_PROPERTY( QString filterExpression READ filterExpression WRITE setFilterExpression NOTIFY filterExpressionChanged )
41  Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
42  Q_PROPERTY( bool isLoading READ isLoading NOTIFY isLoadingChanged )
43 
49  Q_PROPERTY( QString identifierField READ identifierField WRITE setIdentifierField NOTIFY identifierFieldChanged )
50 
54  Q_PROPERTY( QVariant extraIdentifierValue READ extraIdentifierValue WRITE setExtraIdentifierValue NOTIFY extraIdentifierValueChanged )
55 
56  Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
57 
58  public:
59 
63  enum Role
64  {
65  IdentifierValueRole = Qt::UserRole,
66  ValueRole
67  };
68 
72  explicit QgsFeatureFilterModel( QObject *parent = nullptr );
73  ~QgsFeatureFilterModel() override;
74 
78  QgsVectorLayer *sourceLayer() const;
79 
83  void setSourceLayer( QgsVectorLayer *sourceLayer );
84 
91  QString displayExpression() const;
92 
99  void setDisplayExpression( const QString &displayExpression );
100 
106  QString filterValue() const;
107 
113  void setFilterValue( const QString &filterValue );
114 
115  QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
116  QModelIndex parent( const QModelIndex &child ) const override;
117  int rowCount( const QModelIndex &parent ) const override;
118  int columnCount( const QModelIndex &parent ) const override;
119  QVariant data( const QModelIndex &index, int role ) const override;
120 
125  QString filterExpression() const;
126 
131  void setFilterExpression( const QString &filterExpression );
132 
136  bool isLoading() const;
137 
142  QString identifierField() const;
143 
148  void setIdentifierField( const QString &identifierField );
149 
154  QVariant extraIdentifierValue() const;
155 
160  void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
161 
165  int extraIdentifierValueIndex() const;
166 
170  bool extraValueDoesNotExist() const;
171 
175  bool allowNull() const;
176 
180  void setAllowNull( bool allowNull );
181 
182  signals:
183 
187  void sourceLayerChanged();
188 
195  void displayExpressionChanged();
196 
202  void filterValueChanged();
203 
208  void filterExpressionChanged();
209 
213  void isLoadingChanged();
214 
219  void identifierFieldChanged();
220 
224  void filterJobCompleted();
225 
230  void extraIdentifierValueChanged();
231 
235  void extraIdentifierValueIndexChanged( int index );
236 
240  void extraValueDoesNotExistChanged();
241 
245  void beginUpdate();
246 
250  void endUpdate();
251 
255  void allowNullChanged();
256 
257  private slots:
258  void updateCompleter();
259  void gathererThreadFinished();
260  void scheduledReload();
261 
262  private:
263  QSet<QString> requestedAttributes() const;
264  void setExtraIdentifierValueIndex( int index );
265  void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
266  void reload();
267  void reloadCurrentFeature();
268  void setExtraIdentifierValueUnguarded( const QVariant &extraIdentifierValue );
269  struct Entry
270  {
271  Entry() = default;
272 
273  Entry( const QVariant &_identifierValue, const QString &_value, const QgsFeature &_feature )
274  : identifierValue( _identifierValue )
275  , value( _value )
276  , feature( _feature )
277  {}
278 
279  QVariant identifierValue;
280  QString value;
281  QgsFeature feature;
282 
283  bool operator()( const Entry &lhs, const Entry &rhs ) const;
284  };
285 
286  QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
287 
288  QgsVectorLayer *mSourceLayer = nullptr;
289  QgsExpression mDisplayExpression;
290  QString mFilterValue;
291  QString mFilterExpression;
292 
293  mutable QgsExpressionContext mExpressionContext;
294  mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
295  QVector<Entry> mEntries;
296  QgsFieldExpressionValuesGatherer *mGatherer = nullptr;
297  QTimer mReloadTimer;
298  bool mShouldReloadCurrentFeature = false;
299  bool mExtraValueDoesNotExist = false;
300  bool mAllowNull = false;
301 
302  QString mIdentifierField;
303 
304  QVariant mExtraIdentifierValue;
305 
306  int mExtraIdentifierValueIndex = -1;
307 
308  friend class QgsFieldExpressionValuesGatherer;
309 };
310 
311 #endif // QGSFEATUREFILTERMODEL_H
Provides a list of features based on filter conditions.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Role
Extra roles that can be used to fetch data from this model.
Conditional styling for a rule.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Represents a vector layer which manages a vector based data sets.