QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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->fields() ) )
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  mIterator = mSource->getFeatures( mRequest );
95 
96  mDisplayExpression.prepare( &mExpressionContext );
97 
98  QgsFeature feature;
99  QList<int> attributeIndexes;
100  for ( const QString &fieldName : qgis::as_const( mIdentifierFields ) )
101  attributeIndexes << mSource->fields().indexOf( fieldName );
102 
103  while ( mIterator.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  QMutexLocker locker( &mCancelMutex );
115  if ( mWasCanceled )
116  return;
117  }
118  }
119 
121  void stop()
122  {
123  QMutexLocker locker( &mCancelMutex );
124  mWasCanceled = true;
125  }
126 
128  bool wasCanceled() const
129  {
130  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  QgsFeatureIterator mIterator;
169  bool mWasCanceled = false;
170  mutable QMutex mCancelMutex;
171  QStringList mIdentifierFields;
172  QVariant mData;
173 };
174 
176 
177 
178 #endif // QGSFEATUREEXPRESSIONVALUESGATHERER_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
FID_NULL
#define FID_NULL
Definition: qgsfeatureid.h:26
qgsapplication.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsFeature::isValid
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:183
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1802
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
qgsvectorlayerfeatureiterator.h
qgsvectorlayer.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsVectorLayerFeatureSource
Definition: qgsvectorlayerfeatureiterator.h:51
QgsFeature
Definition: qgsfeature.h:55
qgslogger.h
QgsExpression
Definition: qgsexpression.h:113
QgsVectorLayer::displayExpression
QString displayExpression
Definition: qgsvectorlayer.h:390
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
attributeIndexes
const QgsAttributeList & attributeIndexes
Definition: qgswfsgetfeature.cpp:107
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25