QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgsconditionalstyle.h"
22 
23 class QgsFieldExpressionValuesGatherer;
24 
32 class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel
33 {
34  Q_OBJECT
35 
36  Q_PROPERTY( QgsVectorLayer *sourceLayer READ sourceLayer WRITE setSourceLayer NOTIFY sourceLayerChanged )
37  Q_PROPERTY( QString displayExpression READ displayExpression WRITE setDisplayExpression NOTIFY displayExpressionChanged )
38  Q_PROPERTY( QString filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged )
39  Q_PROPERTY( QString filterExpression READ filterExpression WRITE setFilterExpression NOTIFY filterExpressionChanged )
40  Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
41  Q_PROPERTY( bool isLoading READ isLoading NOTIFY isLoadingChanged )
42 
48  Q_PROPERTY( QString identifierField READ identifierField WRITE setIdentifierField NOTIFY identifierFieldChanged )
49 
53  Q_PROPERTY( QVariant extraIdentifierValue READ extraIdentifierValue WRITE setExtraIdentifierValue NOTIFY extraIdentifierValueChanged )
54 
55  Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
56 
57  public:
58 
62  enum Role
63  {
64  IdentifierValueRole = Qt::UserRole,
65  ValueRole
66  };
67 
71  explicit QgsFeatureFilterModel( QObject *parent = nullptr );
72  ~QgsFeatureFilterModel() override;
73 
77  QgsVectorLayer *sourceLayer() const;
78 
82  void setSourceLayer( QgsVectorLayer *sourceLayer );
83 
90  QString displayExpression() const;
91 
98  void setDisplayExpression( const QString &displayExpression );
99 
105  QString filterValue() const;
106 
112  void setFilterValue( const QString &filterValue );
113 
114  QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
115  QModelIndex parent( const QModelIndex &child ) const override;
116  int rowCount( const QModelIndex &parent ) const override;
117  int columnCount( const QModelIndex &parent ) const override;
118  QVariant data( const QModelIndex &index, int role ) const override;
119 
124  QString filterExpression() const;
125 
130  void setFilterExpression( const QString &filterExpression );
131 
135  bool isLoading() const;
136 
141  QString identifierField() const;
142 
147  void setIdentifierField( const QString &identifierField );
148 
153  QVariant extraIdentifierValue() const;
154 
159  void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
160 
164  int extraIdentifierValueIndex() const;
165 
169  bool extraValueDoesNotExist() const;
170 
174  bool allowNull() const;
175 
179  void setAllowNull( bool allowNull );
180 
181  signals:
182 
186  void sourceLayerChanged();
187 
194  void displayExpressionChanged();
195 
201  void filterValueChanged();
202 
207  void filterExpressionChanged();
208 
212  void isLoadingChanged();
213 
218  void identifierFieldChanged();
219 
223  void filterJobCompleted();
224 
229  void extraIdentifierValueChanged();
230 
234  void extraIdentifierValueIndexChanged( int index );
235 
239  void extraValueDoesNotExistChanged();
240 
244  void beginUpdate();
245 
249  void endUpdate();
250 
254  void allowNullChanged();
255 
256  private slots:
257  void updateCompleter();
258  void gathererThreadFinished();
259  void scheduledReload();
260 
261  private:
262  QSet<QString> requestedAttributes() const;
263  void setExtraIdentifierValueIndex( int index, bool force = false );
264  void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
265  void reload();
266  void reloadCurrentFeature();
267  void setExtraIdentifierValueUnguarded( const QVariant &extraIdentifierValue );
268  struct Entry
269  {
270  Entry() = default;
271 
272  Entry( const QVariant &_identifierValue, const QString &_value, const QgsFeature &_feature )
273  : identifierValue( _identifierValue )
274  , value( _value )
275  , feature( _feature )
276  {}
277 
278  QVariant identifierValue;
279  QString value;
280  QgsFeature feature;
281 
282  bool operator()( const Entry &lhs, const Entry &rhs ) const;
283  };
284 
285  QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
286 
287  QgsVectorLayer *mSourceLayer = nullptr;
288  QgsExpression mDisplayExpression;
289  QString mFilterValue;
290  QString mFilterExpression;
291 
292  mutable QgsExpressionContext mExpressionContext;
293  mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
294  QVector<Entry> mEntries;
295  QgsFieldExpressionValuesGatherer *mGatherer = nullptr;
296  QTimer mReloadTimer;
297  bool mShouldReloadCurrentFeature = false;
298  bool mExtraValueDoesNotExist = false;
299  bool mAllowNull = false;
300  bool mIsSettingExtraIdentifierValue = 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
Class for parsing and evaluation of expressions (formerly called "search strings").
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:55
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.