QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsfeaturefiltermodel_p.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturefiltermodel_p - QgsFieldExpressionValuesGatherer
3 
4  ---------------------
5  begin : 10.3.2017
6  copyright : (C) 2017 by Matthias Kuhn
7  email : matthias@opengis.ch
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_P_H
17 #define QGSFEATUREFILTERMODEL_P_H
18 
19 #include <QThread>
20 #include "qgsfeaturefiltermodel.h"
21 #include "qgslogger.h"
23 
24 #define SIP_NO_FILE
25 
26 // just internal guff - definitely not for exposing to public API!
28 
35 class QgsFieldExpressionValuesGatherer: public QThread
36 {
37  Q_OBJECT
38 
39  public:
40  QgsFieldExpressionValuesGatherer( QgsVectorLayer *layer, const QString &displayExpression, const QString &identifierField, const QgsFeatureRequest &request = QgsFeatureRequest() )
41  : mSource( new QgsVectorLayerFeatureSource( layer ) )
42  , mDisplayExpression( displayExpression )
43  , mRequest( request )
44  , mIdentifierField( identifierField )
45  {
46  }
47 
48  void run() override
49  {
50  mWasCanceled = false;
51 
52  mIterator = mSource->getFeatures( mRequest );
53 
54  mDisplayExpression.prepare( &mExpressionContext );
55 
56  QgsFeature feat;
57  const int attribute = mSource->fields().indexOf( mIdentifierField );
58 
59  while ( mIterator.nextFeature( feat ) )
60  {
61  mExpressionContext.setFeature( feat );
62  mEntries.append( QgsFeatureFilterModel::Entry( feat.attribute( attribute ), mDisplayExpression.evaluate( &mExpressionContext ).toString(), feat ) );
63 
64  if ( mWasCanceled )
65  return;
66  }
67 
68  emit collectedValues();
69  }
70 
72  void stop()
73  {
74  mWasCanceled = true;
75  }
76 
78  bool wasCanceled() const { return mWasCanceled; }
79 
80  QVector<QgsFeatureFilterModel::Entry> entries() const
81  {
82  return mEntries;
83  }
84 
85  QgsFeatureRequest request() const
86  {
87  return mRequest;
88  }
89 
93  QVariant data() const
94  {
95  return mData;
96  }
97 
101  void setData( const QVariant &data )
102  {
103  mData = data;
104  }
105 
106  signals:
107 
112  void collectedValues();
113 
114  private:
115 
116  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
117  QgsExpression mDisplayExpression;
118  QgsExpressionContext mExpressionContext;
119  QgsFeatureRequest mRequest;
120  QgsFeatureIterator mIterator;
121  bool mWasCanceled = false;
122  QVector<QgsFeatureFilterModel::Entry> mEntries;
123  QString mIdentifierField;
124  QVariant mData;
125 };
126 
128 
129 
130 #endif // QGSFEATUREFILTERMODEL_P_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
QgsFields fields
Definition: qgsfeature.h:66
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262