QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsfeaturepickermodelbase.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 : matthias@opengis.ch
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 QGSFEATUREFILTERMODELBASE_H
16 #define QGSFEATUREFILTERMODELBASE_H
17 
18 #include "qgsconditionalstyle.h"
20 
21 #include <QAbstractItemModel>
22 #include <QTimer>
23 
32 class CORE_EXPORT QgsFeaturePickerModelBase : public QAbstractItemModel SIP_ABSTRACT
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 fetchGeometry READ fetchGeometry WRITE setFetchGeometry NOTIFY fetchGeometryChanged )
42  Q_PROPERTY( int fetchLimit READ fetchLimit WRITE setFetchLimit NOTIFY fetchLimitChanged )
43  Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
44 
45  public:
46 
50  enum Role
51  {
52  IdentifierValueRole = Qt::UserRole,
56  FeatureIdRole
57  };
58 
62  explicit QgsFeaturePickerModelBase( QObject *parent = nullptr );
63  ~QgsFeaturePickerModelBase() override;
64 
68  QgsVectorLayer *sourceLayer() const;
69 
73  void setSourceLayer( QgsVectorLayer *sourceLayer );
74 
81  QString displayExpression() const;
82 
89  void setDisplayExpression( const QString &displayExpression );
90 
96  QString filterValue() const;
97 
103  void setFilterValue( const QString &filterValue );
104 
105  QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
106  QModelIndex parent( const QModelIndex &child ) const override;
107  int rowCount( const QModelIndex &parent ) const override;
108  int columnCount( const QModelIndex &parent ) const override
109  {
110  Q_UNUSED( parent )
111  return 1;
112  }
113  QVariant data( const QModelIndex &index, int role ) const override;
114 
119  QString filterExpression() const;
120 
125  void setFilterExpression( const QString &filterExpression );
126 
130  bool isLoading() const;
131 
136  virtual void setExtraIdentifierValueToNull() = 0;
137 
141  int extraIdentifierValueIndex() const;
142 
146  bool extraValueDoesNotExist() const;
147 
151  bool allowNull() const;
152 
156  void setAllowNull( bool allowNull );
157 
161  bool fetchGeometry() const;
162 
166  void setFetchGeometry( bool fetchGeometry );
167 
171  int fetchLimit() const;
172 
177  void setFetchLimit( int fetchLimit );
178 
179  signals:
180 
185 
193 
200 
206 
211 
216 
222 
227 
232 
236  void beginUpdate();
237 
241  void endUpdate();
242 
247 
252 
257 
258 
259  private slots:
260  void updateCompleter();
261  void scheduledReload();
262 
263  protected:
264 
269  QVariant extraIdentifierValue() const;
270 
275  void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
276 
278  virtual void requestToReloadCurrentFeature( QgsFeatureRequest &request ) = 0;
279 
281  void setExtraIdentifierValueUnguarded( const QVariant &identifierValue );
282 
283 #ifndef SIP_RUN
284 
289  virtual QSet<QString> requestedAttributes() const {return {};}
290 
292  virtual QgsFeatureExpressionValuesGatherer *createValuesGatherer( const QgsFeatureRequest &request ) const = 0;
293 
295  virtual QgsFeatureExpressionValuesGatherer::Entry createEntry( const QVariant &identifier ) const = 0;
296 
298  virtual QVariant entryIdentifier( const QgsFeatureExpressionValuesGatherer::Entry &entry ) const = 0;
299 
301  virtual bool compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const = 0;
302 
304  virtual QVariant nullIdentifier() const = 0;
305 
310  virtual bool identifierIsNull( const QVariant &identifier ) const = 0;
311 
312  QVector<QgsFeatureExpressionValuesGatherer::Entry> mEntries;
313 #endif
314 
317 
319  int mExtraValueIndex = -1;
320 
321  private:
322  void setExtraIdentifierValueIndex( int index, bool force = false );
323  void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
324  void reload();
325  void reloadCurrentFeature();
326  QSet<QString> requestedAttributesForStyle() const;
327 
328  QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
329 
330  QgsVectorLayer *mSourceLayer = nullptr;
331  QgsExpression mDisplayExpression;
332  QString mFilterValue;
333  QString mFilterExpression;
334 
335  mutable QgsExpressionContext mExpressionContext;
336  mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
337 
338  QgsFeatureExpressionValuesGatherer *mGatherer = nullptr;
339  bool mFetchGeometry = true;
340  int mFetchLimit = 100;
341 
342  QTimer mReloadTimer;
343  bool mShouldReloadCurrentFeature = false;
344  bool mKeepCurrentEntry = false; // need to keep the current value after a reload or if the value does not exist
345  bool mExtraValueDoesNotExist = false;
346  bool mAllowNull = false;
347  bool mIsSettingExtraIdentifierValue = false;
348 
349  friend class TestQgsFeatureListComboBox;
350 };
351 
352 #endif // QGSFEATUREFILTERMODELBASE_H
Conditional styling for a rule.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Provides a list of features based on filter conditions.
void extraIdentifierValueIndexChanged(int index)
The index at which the extra identifier value is available within the model.
void beginUpdate()
Notification that the model is about to be changed because a job was completed.
virtual QVariant entryIdentifier(const QgsFeatureExpressionValuesGatherer::Entry &entry) const =0
Returns the identifier of the given entry.
void filterValueChanged()
This value will be used to filter the features available from this model.
virtual void setExtraIdentifierValueToNull()=0
Allows specifying one value that does not need to match the filter criteria but will still be availab...
virtual void requestToReloadCurrentFeature(QgsFeatureRequest &request)=0
Update the request to match the current feature to be reloaded.
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.
int columnCount(const QModelIndex &parent) const override
void extraIdentifierValueChanged()
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void filterJobCompleted()
Indicates that a filter job has been completed and new data may be available.
QVariant mExtraIdentifierValue
The current identifier value.
virtual QgsFeatureExpressionValuesGatherer * createValuesGatherer(const QgsFeatureRequest &request) const =0
Creates the value gatherer.
virtual QgsFeatureExpressionValuesGatherer::Entry createEntry(const QVariant &identifier) const =0
Creates an entry with just the identifier so the feature can be retrieved in a next iteration.
virtual bool identifierIsNull(const QVariant &identifier) const =0
Returns true if the entry is null The identifier can be either the feature ID or the list of identifi...
void fetchLimitChanged()
Emitted when the fetching limit for the feature request changes.
void sourceLayerChanged()
The source layer from which features will be fetched.
virtual QSet< QString > requestedAttributes() const
Returns the attributes to be fetched in the request.
Role
Extra roles that can be used to fetch data from this model.
@ IdentifierValuesRole
Used to retrieve the identifierValues (primary keys) of a feature.
@ FeatureRole
Used to retrieve the feature, it might be incomplete if the request doesn't fetch all attributes or g...
@ ValueRole
Used to retrieve the displayExpression of a feature.
void allowNullChanged()
Add a NULL entry to the list.
virtual bool compareEntries(const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b) const =0
Returns true if the 2 entries refers to the same feature.
void fetchGeometryChanged()
Emitted when the fetching of the geometry changes.
void isLoadingChanged()
Indicator if the model is currently performing any feature iteration in the background.
QVector< QgsFeatureExpressionValuesGatherer::Entry > mEntries
virtual QVariant nullIdentifier() const =0
Returns a null identifier.
void endUpdate()
Notification that the model change is finished.
void displayExpressionChanged()
The display expression will be used for.
void extraValueDoesNotExistChanged()
Flag indicating that the extraIdentifierValue does not exist in the data.
This class wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Represents a vector layer which manages a vector based data sets.
#define SIP_ABSTRACT
Definition: qgis_sip.h:199