QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 : [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 QGSFEATUREFILTERMODELBASE_H
16 #define QGSFEATUREFILTERMODELBASE_H
17 
18 #include <QAbstractItemModel>
19 
20 #include "qgsconditionalstyle.h"
22 
30 class CORE_EXPORT QgsFeaturePickerModelBase : public QAbstractItemModel SIP_ABSTRACT
31 {
32  Q_OBJECT
33 
34  Q_PROPERTY( QgsVectorLayer *sourceLayer READ sourceLayer WRITE setSourceLayer NOTIFY sourceLayerChanged )
35  Q_PROPERTY( QString displayExpression READ displayExpression WRITE setDisplayExpression NOTIFY displayExpressionChanged )
36  Q_PROPERTY( QString filterValue READ filterValue WRITE setFilterValue NOTIFY filterValueChanged )
37  Q_PROPERTY( QString filterExpression READ filterExpression WRITE setFilterExpression NOTIFY filterExpressionChanged )
38  Q_PROPERTY( bool allowNull READ allowNull WRITE setAllowNull NOTIFY allowNullChanged )
39  Q_PROPERTY( bool fetchGeometry READ fetchGeometry WRITE setFetchGeometry NOTIFY fetchGeometryChanged )
40  Q_PROPERTY( int fetchLimit READ fetchLimit WRITE setFetchLimit NOTIFY fetchLimitChanged )
41  Q_PROPERTY( int extraIdentifierValueIndex READ extraIdentifierValueIndex NOTIFY extraIdentifierValueIndexChanged )
42 
43  public:
44 
48  enum Role
49  {
50  IdentifierValueRole = Qt::UserRole,
54  FeatureIdRole
55  };
56 
60  explicit QgsFeaturePickerModelBase( QObject *parent = nullptr );
61  ~QgsFeaturePickerModelBase() override;
62 
66  QgsVectorLayer *sourceLayer() const;
67 
71  void setSourceLayer( QgsVectorLayer *sourceLayer );
72 
79  QString displayExpression() const;
80 
87  void setDisplayExpression( const QString &displayExpression );
88 
94  QString filterValue() const;
95 
101  void setFilterValue( const QString &filterValue );
102 
103  QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
104  QModelIndex parent( const QModelIndex &child ) const override;
105  int rowCount( const QModelIndex &parent ) const override;
106  int columnCount( const QModelIndex &parent ) const override
107  {
108  Q_UNUSED( parent )
109  return 1;
110  }
111  QVariant data( const QModelIndex &index, int role ) const override;
112 
117  QString filterExpression() const;
118 
123  void setFilterExpression( const QString &filterExpression );
124 
128  bool isLoading() const;
129 
134  virtual void setExtraIdentifierValueToNull() = 0;
135 
139  int extraIdentifierValueIndex() const;
140 
144  bool extraValueDoesNotExist() const;
145 
149  bool allowNull() const;
150 
154  void setAllowNull( bool allowNull );
155 
159  bool fetchGeometry() const;
160 
164  void setFetchGeometry( bool fetchGeometry );
165 
169  int fetchLimit() const;
170 
175  void setFetchLimit( int fetchLimit );
176 
177  signals:
178 
183 
191 
198 
204 
209 
214 
220 
225 
230 
234  void beginUpdate();
235 
239  void endUpdate();
240 
245 
250 
255 
256 
257  private slots:
258  void updateCompleter();
259  void scheduledReload();
260 
261  protected:
262 
267  QVariant extraIdentifierValue() const;
268 
273  void setExtraIdentifierValue( const QVariant &extraIdentifierValue );
274 
276  virtual void requestToReloadCurrentFeature( QgsFeatureRequest &request ) = 0;
277 
279  void setExtraIdentifierValueUnguarded( const QVariant &identifierValue );
280 
281 #ifndef SIP_RUN
282 
287  virtual QSet<QString> requestedAttributes() const {return {};}
288 
290  virtual QgsFeatureExpressionValuesGatherer *createValuesGatherer( const QgsFeatureRequest &request ) const = 0;
291 
293  virtual QgsFeatureExpressionValuesGatherer::Entry createEntry( const QVariant &identifier ) const = 0;
294 
296  virtual QVariant entryIdentifier( const QgsFeatureExpressionValuesGatherer::Entry &entry ) const = 0;
297 
299  virtual bool compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const = 0;
300 
302  virtual QVariant nullIdentifier() const = 0;
303 
308  virtual bool identifierIsNull( const QVariant &identifier ) const = 0;
309 
310  QVector<QgsFeatureExpressionValuesGatherer::Entry> mEntries;
311 #endif
312 
315 
317  int mExtraValueIndex = -1;
318 
319  private:
320  void setExtraIdentifierValueIndex( int index, bool force = false );
321  void setExtraValueDoesNotExist( bool extraValueDoesNotExist );
322  void reload();
323  void reloadCurrentFeature();
324  QSet<QString> requestedAttributesForStyle() const;
325 
326  QgsConditionalStyle featureStyle( const QgsFeature &feature ) const;
327 
328  QgsVectorLayer *mSourceLayer = nullptr;
329  QgsExpression mDisplayExpression;
330  QString mFilterValue;
331  QString mFilterExpression;
332 
333  mutable QgsExpressionContext mExpressionContext;
334  mutable QMap< QgsFeatureId, QgsConditionalStyle > mEntryStylesMap;
335 
336  QgsFeatureExpressionValuesGatherer *mGatherer = nullptr;
337  bool mFetchGeometry = true;
338  int mFetchLimit = 100;
339 
340  QTimer mReloadTimer;
341  bool mShouldReloadCurrentFeature = false;
342  bool mKeepCurrentEntry = false; // need to keep the current value after a reload or if the value does not exist
343  bool mExtraValueDoesNotExist = false;
344  bool mAllowNull = false;
345  bool mIsSettingExtraIdentifierValue = false;
346 
347  friend class TestQgsFeatureListComboBox;
348 };
349 
350 #endif // QGSFEATUREFILTERMODELBASE_H
QgsFeaturePickerModelBase::nullIdentifier
virtual QVariant nullIdentifier() const =0
Returns a null identifier.
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsconditionalstyle.h
QgsFeaturePickerModelBase::displayExpressionChanged
void displayExpressionChanged()
The display expression will be used for.
QgsFeaturePickerModelBase::compareEntries
virtual bool compareEntries(const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b) const =0
Returns true if the 2 entries refers to the same feature.
QgsFeaturePickerModelBase::IdentifierValuesRole
@ IdentifierValuesRole
Used to retrieve the identifierValues (primary keys) of a feature.
Definition: qgsfeaturepickermodelbase.h:51
QgsFeaturePickerModelBase
Provides a list of features based on filter conditions.
Definition: qgsfeaturepickermodelbase.h:31
QgsFeaturePickerModelBase::identifierIsNull
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...
QgsFeaturePickerModelBase::ValueRole
@ ValueRole
Used to retrieve the displayExpression of a feature.
Definition: qgsfeaturepickermodelbase.h:52
QgsConditionalStyle
Conditional styling for a rule.
Definition: qgsconditionalstyle.h:113
QgsFeaturePickerModelBase::filterJobCompleted
void filterJobCompleted()
Indicates that a filter job has been completed and new data may be available.
QgsFeaturePickerModelBase::mExtraIdentifierValue
QVariant mExtraIdentifierValue
The current identifier value.
Definition: qgsfeaturepickermodelbase.h:314
QgsFeaturePickerModelBase::endUpdate
void endUpdate()
Notification that the model change is finished.
QgsFeaturePickerModelBase::isLoadingChanged
void isLoadingChanged()
Indicator if the model is currently performing any feature iteration in the background.
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsFeaturePickerModelBase::createEntry
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.
QgsFeaturePickerModelBase::mEntries
QVector< QgsFeatureExpressionValuesGatherer::Entry > mEntries
Definition: qgsfeaturepickermodelbase.h:310
qgsfeatureexpressionvaluesgatherer.h
QgsFeaturePickerModelBase::extraIdentifierValueIndexChanged
void extraIdentifierValueIndexChanged(int index)
The index at which the extra identifier value is available within the model.
QgsFeaturePickerModelBase::extraValueDoesNotExistChanged
void extraValueDoesNotExistChanged()
Flag indicating that the extraIdentifierValue does not exist in the data.
QgsFeaturePickerModelBase::fetchLimitChanged
void fetchLimitChanged()
Emitted when the fetching limit for the feature request changes.
QgsFeaturePickerModelBase::requestedAttributes
virtual QSet< QString > requestedAttributes() const
Returns the attributes to be fetched in the request.
Definition: qgsfeaturepickermodelbase.h:287
QgsFeaturePickerModelBase::requestToReloadCurrentFeature
virtual void requestToReloadCurrentFeature(QgsFeatureRequest &request)=0
Update the request to match the current feature to be reloaded.
QgsFeaturePickerModelBase::FeatureRole
@ FeatureRole
Used to retrieve the feature, it might be incomplete if the request doesn't fetch all attributes or g...
Definition: qgsfeaturepickermodelbase.h:53
QgsFeaturePickerModelBase::entryIdentifier
virtual QVariant entryIdentifier(const QgsFeatureExpressionValuesGatherer::Entry &entry) const =0
Returns the identifier of the given entry.
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsFeaturePickerModelBase::allowNullChanged
void allowNullChanged()
Add a NULL entry to the list.
QgsFeaturePickerModelBase::createValuesGatherer
virtual QgsFeatureExpressionValuesGatherer * createValuesGatherer(const QgsFeatureRequest &request) const =0
Creates the value gatherer.
QgsFeaturePickerModelBase::setExtraIdentifierValueToNull
virtual void setExtraIdentifierValueToNull()=0
Allows specifying one value that does not need to match the filter criteria but will still be availab...
QgsFeaturePickerModelBase::extraIdentifierValueChanged
void extraIdentifierValueChanged()
Allows specifying one value that does not need to match the filter criteria but will still be availab...
QgsFeaturePickerModelBase::fetchGeometryChanged
void fetchGeometryChanged()
Emitted when the fetching of the geometry changes.
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFeaturePickerModelBase::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: qgsfeaturepickermodelbase.h:106
QgsFeaturePickerModelBase::Role
Role
Extra roles that can be used to fetch data from this model.
Definition: qgsfeaturepickermodelbase.h:49
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsFeaturePickerModelBase::sourceLayerChanged
void sourceLayerChanged()
The source layer from which features will be fetched.
QgsFeaturePickerModelBase::filterValueChanged
void filterValueChanged()
This value will be used to filter the features available from this model.
QgsFeaturePickerModelBase::beginUpdate
void beginUpdate()
Notification that the model is about to be changed because a job was completed.
SIP_ABSTRACT
#define SIP_ABSTRACT
Definition: qgis_sip.h:199
QgsFeaturePickerModelBase::filterExpressionChanged
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.