QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsfeaturefiltermodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturefiltermodel.h - QgsFeatureFilterModel
3  ---------------------
4  begin : 10.3.2017
5  copyright : (C) 2017 by Matthias Kuhn
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSFEATUREFILTERMODEL_H
16 #define QGSFEATUREFILTERMODEL_H
17 
18 #include <QAbstractItemModel>
19 
20 #include "qgsconditionalstyle.h"
21 
22 class QgsFieldExpressionValuesGatherer;
23 
31 class CORE_EXPORT QgsFeatureFilterModel : public QAbstractItemModel
32 {
33  Q_OBJECT
34 
35  Q_PROPERTY( QgsVectorLayer *sourceLayer READ sourceLayer WRITE setSourceLayer NOTIFY sourceLayerChanged )
36  Q_PROPERTY( QString displayExpression READ displayExpression WRITE setDisplayExpression NOTIFY displayExpressionChanged )
37  Q_PROPERTY( QString filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged )
38  Q_PROPERTY( QString filterExpression READ filterExpression WRITE setFilterExpression NOTIFY filterExpressionChanged )
39  Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
40  Q_PROPERTY( bool isLoading READ isLoading NOTIFY isLoadingChanged )
41 
47  Q_PROPERTY( QString identifierField READ identifierField WRITE setIdentifierField NOTIFY identifierFieldChanged )
48 
52  Q_PROPERTY( QVariant extraIdentifierValue READ extraIdentifierValue WRITE setExtraIdentifierValue NOTIFY extraIdentifierValueChanged )
53 
54  Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
55 
56  public:
57 
61  enum Role
62  {
63  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 
143  Q_DECL_DEPRECATED QString identifierField() const;
144 
150  QStringList identifierFields() const;
151 
157  Q_DECL_DEPRECATED void setIdentifierField( const QString &identifierField );
158 
165  void setIdentifierFields( const QStringList &identifierFields );
166 
172  Q_DECL_DEPRECATED QVariant extraIdentifierValue() const;
173 
179  QVariantList extraIdentifierValues() const;
180 
186  Q_DECL_DEPRECATED void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
187 
193  void setExtraIdentifierValues( const QVariantList &extraIdentifierValues );
194 
200  void setExtraIdentifierValuesToNull();
201 
205  int extraIdentifierValueIndex() const;
206 
210  bool extraValueDoesNotExist() const;
211 
215  bool allowNull() const;
216 
220  void setAllowNull( bool allowNull );
221 
222  signals:
223 
227  void sourceLayerChanged();
228 
235  void displayExpressionChanged();
236 
242  void filterValueChanged();
243 
248  void filterExpressionChanged();
249 
253  void isLoadingChanged();
254 
259  void identifierFieldChanged();
260 
264  void filterJobCompleted();
265 
270  void extraIdentifierValueChanged();
271 
275  void extraIdentifierValueIndexChanged( int index );
276 
280  void extraValueDoesNotExistChanged();
281 
285  void beginUpdate();
286 
290  void endUpdate();
291 
295  void allowNullChanged();
296 
297  private slots:
298  void updateCompleter();
299  void gathererThreadFinished();
300  void scheduledReload();
301 
302  private:
303  QSet<QString> requestedAttributes() const;
304  void setExtraIdentifierValuesIndex( int index, bool force = false );
305  void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
306  void reload();
307  void reloadCurrentFeature();
308  void setExtraIdentifierValuesUnguarded( const QVariantList &extraIdentifierValues );
309  struct Entry
310  {
311  Entry() = default;
312 
313  Entry( const QVariantList &_identifierValues, const QString &_value, const QgsFeature &_feature )
314  : identifierValues( _identifierValues )
315  , value( _value )
316  , feature( _feature )
317  {}
318 
319  QVariantList identifierValues;
320  QString value;
321  QgsFeature feature;
322 
323  bool operator()( const Entry &lhs, const Entry &rhs ) const;
324  };
325  Entry nullEntry();
326 
327  QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
328 
329  QgsVectorLayer *mSourceLayer = nullptr;
330  QgsExpression mDisplayExpression;
331  QString mFilterValue;
332  QString mFilterExpression;
333 
334  mutable QgsExpressionContext mExpressionContext;
335  mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
336  QVector<Entry> mEntries;
337  QgsFieldExpressionValuesGatherer *mGatherer = nullptr;
338  QTimer mReloadTimer;
339  bool mShouldReloadCurrentFeature = false;
340  bool mExtraValueDoesNotExist = false;
341  bool mAllowNull = false;
342  bool mIsSettingExtraIdentifierValue = false;
343 
344  QStringList mIdentifierFields;
345  QVariantList mExtraIdentifierValues;
346 
347  int mExtraIdentifierValueIndex = -1;
348 
349  friend class QgsFieldExpressionValuesGatherer;
350 };
351 
352 #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.
Used to retrieve the identifierValues (primary keys) of a feature.