QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsfeatureexpressionvaluesgatherer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeatureexpressionvaluesgatherer - QgsFeatureExpressionValuesGatherer
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 QGSFEATUREEXPRESSIONVALUESGATHERER_H
16 #define QGSFEATUREEXPRESSIONVALUESGATHERER_H
17 
18 #include <QThread>
19 #include <QMutex>
20 #include "qgsapplication.h"
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
24 
25 #define SIP_NO_FILE
26 
27 // just internal guff - definitely not for exposing to public API!
29 
36 class QgsFeatureExpressionValuesGatherer: public QThread
37 {
38  Q_OBJECT
39 
40  public:
41 
49  QgsFeatureExpressionValuesGatherer( QgsVectorLayer *layer,
50  const QString &displayExpression = QString(),
51  const QgsFeatureRequest &request = QgsFeatureRequest(),
52  const QStringList &identifierFields = QStringList() )
53  : mSource( new QgsVectorLayerFeatureSource( layer ) )
54  , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
55  , mRequest( request )
56  , mIdentifierFields( identifierFields )
57  {
58  }
59 
60  struct Entry
61  {
62  Entry() = default;
63 
64  Entry( const QVariantList &_identifierFields, const QString &_value, const QgsFeature &_feature )
65  : identifierFields( _identifierFields )
66  , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
67  , value( _value )
68  , feature( _feature )
69  {}
70 
71  Entry( const QgsFeatureId &_featureId, const QString &_value, const QgsVectorLayer *layer )
72  : featureId( _featureId )
73  , value( _value )
74  , feature( QgsFeature( layer ? layer->fields() : QgsFields() ) )
75  {}
76 
77  QVariantList identifierFields;
78  QgsFeatureId featureId;
79  QString value;
80  QgsFeature feature;
81 
82  bool operator()( const Entry &lhs, const Entry &rhs ) const;
83  };
84 
85  static Entry nullEntry( QgsVectorLayer *layer )
86  {
87  return Entry( QVariantList(), QgsApplication::nullRepresentation(), QgsFeature( layer->fields() ) );
88  }
89 
90  void run() override
91  {
92  mWasCanceled = false;
93 
94  QgsFeatureIterator iterator = mSource->getFeatures( mRequest );
95 
96  mDisplayExpression.prepare( &mExpressionContext );
97 
98  QgsFeature feature;
99  QList<int> attributeIndexes;
100  for ( auto it = mIdentifierFields.constBegin(); it != mIdentifierFields.constEnd(); ++it )
101  attributeIndexes << mSource->fields().indexOf( *it );
102 
103  while ( iterator.nextFeature( feature ) )
104  {
105  mExpressionContext.setFeature( feature );
106  QVariantList attributes;
107  for ( const int idx : attributeIndexes )
108  attributes << feature.attribute( idx );
109 
110  const QString expressionValue = mDisplayExpression.evaluate( &mExpressionContext ).toString();
111 
112  mEntries.append( Entry( attributes, expressionValue, feature ) );
113 
114  const QMutexLocker locker( &mCancelMutex );
115  if ( mWasCanceled )
116  return;
117  }
118  }
119 
121  void stop()
122  {
123  const QMutexLocker locker( &mCancelMutex );
124  mWasCanceled = true;
125  }
126 
128  bool wasCanceled() const
129  {
130  const QMutexLocker locker( &mCancelMutex );
131  return mWasCanceled;
132  }
133 
134  QVector<Entry> entries() const
135  {
136  return mEntries;
137  }
138 
139  QgsFeatureRequest request() const
140  {
141  return mRequest;
142  }
143 
147  QVariant data() const
148  {
149  return mData;
150  }
151 
155  void setData( const QVariant &data )
156  {
157  mData = data;
158  }
159 
160  protected:
161  QVector<Entry> mEntries;
162 
163  private:
164  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
165  QgsExpression mDisplayExpression;
166  QgsExpressionContext mExpressionContext;
167  QgsFeatureRequest mRequest;
168  bool mWasCanceled = false;
169  mutable QMutex mCancelMutex;
170  QStringList mIdentifierFields;
171  QVariant mData;
172 };
173 
175 
176 
177 #endif // QGSFEATUREEXPRESSIONVALUESGATHERER_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
FID_NULL
#define FID_NULL
Definition: qgsfeatureid.h:29
qgsapplication.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsFeature::isValid
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:216
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:2018
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
qgsvectorlayerfeatureiterator.h
qgsvectorlayer.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayerFeatureSource
Partial snapshot of vector layer's state (only the members necessary for access to features)
Definition: qgsvectorlayerfeatureiterator.h:52
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgslogger.h
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsVectorLayer::displayExpression
QString displayExpression
Definition: qgsvectorlayer.h:396
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
attributeIndexes
const QgsAttributeList & attributeIndexes
Definition: qgswfsgetfeature.cpp:107
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28