QGIS API Documentation  3.2.0-Bonn (bc43194)
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 : [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_P_H
17 #define QGSFEATUREFILTERMODEL_P_H
18 
19 #include <QThread>
20 #include "qgsvectorlayer.h"
21 #include "qgsfeaturefiltermodel.h"
22 #include "qgslogger.h"
24 
25 #define SIP_NO_FILE
26 
27 // just internal guff - definitely not for exposing to public API!
29 
36 class QgsFieldExpressionValuesGatherer: public QThread
37 {
38  Q_OBJECT
39 
40  public:
41  QgsFieldExpressionValuesGatherer( QgsVectorLayer *layer, const QString &displayExpression, const QString &identifierField, const QgsFeatureRequest &request = QgsFeatureRequest() )
42  : mSource( new QgsVectorLayerFeatureSource( layer ) )
43  , mDisplayExpression( displayExpression )
44  , mRequest( request )
45  , mIdentifierField( identifierField )
46  {
47  }
48 
49  void run() override
50  {
51  mWasCanceled = false;
52 
53  mIterator = mSource->getFeatures( mRequest );
54 
55  mDisplayExpression.prepare( &mExpressionContext );
56 
57  QgsFeature feat;
58  const int attribute = mSource->fields().indexOf( mIdentifierField );
59 
60  while ( mIterator.nextFeature( feat ) )
61  {
62  mExpressionContext.setFeature( feat );
63  mEntries.append( QgsFeatureFilterModel::Entry( feat.attribute( attribute ), mDisplayExpression.evaluate( &mExpressionContext ).toString(), feat ) );
64 
65  if ( mWasCanceled )
66  return;
67  }
68 
69  emit collectedValues();
70  }
71 
73  void stop()
74  {
75  mWasCanceled = true;
76  }
77 
79  bool wasCanceled() const { return mWasCanceled; }
80 
81  QVector<QgsFeatureFilterModel::Entry> entries() const
82  {
83  return mEntries;
84  }
85 
86  QgsFeatureRequest request() const
87  {
88  return mRequest;
89  }
90 
94  QVariant data() const
95  {
96  return mData;
97  }
98 
102  void setData( const QVariant &data )
103  {
104  mData = data;
105  }
106 
107  signals:
108 
113  void collectedValues();
114 
115  private:
116 
117  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
118  QgsExpression mDisplayExpression;
119  QgsExpressionContext mExpressionContext;
120  QgsFeatureRequest mRequest;
121  QgsFeatureIterator mIterator;
122  bool mWasCanceled = false;
123  QVector<QgsFeatureFilterModel::Entry> mEntries;
124  QString mIdentifierField;
125  QVariant mData;
126 };
127 
129 
130 
131 #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:62
QgsFields fields
Definition: qgsfeature.h:73
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:189
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:255