QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
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 "qgsapplication.h"
20#include "qgslogger.h"
21#include "qgsvectorlayer.h"
23
24#include <QMutex>
25#include <QThread>
26
27#define SIP_NO_FILE
28
29// just internal guff - definitely not for exposing to public API!
31
38class QgsFeatureExpressionValuesGatherer: public QThread
39{
40 Q_OBJECT
41
42 public:
43
51 QgsFeatureExpressionValuesGatherer( QgsVectorLayer *layer,
52 const QString &displayExpression = QString(),
53 const QgsFeatureRequest &request = QgsFeatureRequest(),
54 const QStringList &identifierFields = QStringList() )
55 : mSource( new QgsVectorLayerFeatureSource( layer ) )
56 , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
57 , mExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
58 , mRequest( request )
59 , mIdentifierFields( identifierFields )
60 {
61 }
62
63 QgsFeatureExpressionValuesGatherer( QgsVectorLayer *layer,
64 const QString &displayExpression = QString(),
65 const QString &orderExpression = QString(),
66 const QgsFeatureRequest &request = QgsFeatureRequest(),
67 const QStringList &identifierFields = QStringList() )
68 : mSource( new QgsVectorLayerFeatureSource( layer ) )
69 , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
70 , mOrderExpression( orderExpression.isEmpty() ? displayExpression.isEmpty() ? layer->displayExpression() : displayExpression : orderExpression )
71 , mExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
72 , mRequest( request )
73 , mIdentifierFields( identifierFields )
74 {
75 }
76
77 struct Entry
78 {
79 Entry() = default;
80
81 Entry( const QVariantList &_identifierFields, const QString &_value, const QString &_orderValue, const QgsFeature &_feature )
82 : identifierFields( _identifierFields )
83 , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
84 , value( _value )
85 , orderValue( _orderValue )
86 , feature( _feature )
87 {}
88
89 Entry( const QVariantList &_identifierFields, const QString &_value, const QgsFeature &_feature )
90 : identifierFields( _identifierFields )
91 , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
92 , value( _value )
93 , feature( _feature )
94 {}
95
96 Entry( const QgsFeatureId &_featureId, const QString &_value, const QgsVectorLayer *layer )
97 : featureId( _featureId )
98 , value( _value )
99 , feature( QgsFeature( layer ? layer->fields() : QgsFields() ) )
100 {}
101
102 QVariantList identifierFields;
103 QgsFeatureId featureId;
104 QString value;
105 QString orderValue;
106 QgsFeature feature;
107
108 bool operator()( const Entry &lhs, const Entry &rhs ) const;
109 };
110
111 static Entry nullEntry( QgsVectorLayer *layer )
112 {
113 return Entry( QVariantList(), QgsApplication::nullRepresentation(), QgsFeature( layer->fields() ) );
114 }
115
116 void run() override
117 {
118 mWasCanceled = false;
119
120 QgsFeatureIterator iterator = mSource->getFeatures( mRequest );
121
122 mDisplayExpression.prepare( &mExpressionContext );
123 mOrderExpression.prepare( &mExpressionContext );
124
125 QgsFeature feature;
126 QList<int> attributeIndexes;
127 for ( auto it = mIdentifierFields.constBegin(); it != mIdentifierFields.constEnd(); ++it )
128 attributeIndexes << mSource->fields().indexOf( *it );
129
130 while ( iterator.nextFeature( feature ) )
131 {
132 mExpressionContext.setFeature( feature );
133 QVariantList attributes;
134 for ( const int idx : attributeIndexes )
135 attributes << feature.attribute( idx );
136
137 const QString expressionValue = mDisplayExpression.evaluate( &mExpressionContext ).toString();
138 const QString orderValue = mOrderExpression.evaluate( &mExpressionContext ).toString();
139
140 mEntries.append( Entry( attributes, expressionValue, orderValue, feature ) );
141
142 const QMutexLocker locker( &mCancelMutex );
143 if ( mWasCanceled )
144 return;
145 }
146 }
147
149 void stop()
150 {
151 const QMutexLocker locker( &mCancelMutex );
152 mWasCanceled = true;
153 }
154
156 bool wasCanceled() const
157 {
158 const QMutexLocker locker( &mCancelMutex );
159 return mWasCanceled;
160 }
161
162 QVector<Entry> entries() const
163 {
164 return mEntries;
165 }
166
167 QgsFeatureRequest request() const
168 {
169 return mRequest;
170 }
171
175 QVariant data() const
176 {
177 return mData;
178 }
179
183 void setData( const QVariant &data )
184 {
185 mData = data;
186 }
187
188 protected:
189 QVector<Entry> mEntries;
190
191 private:
192 std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
193 QgsExpression mDisplayExpression;
194 QgsExpression mOrderExpression;
195 QgsExpressionContext mExpressionContext;
196 QgsFeatureRequest mRequest;
197 bool mWasCanceled = false;
198 mutable QMutex mCancelMutex;
199 QStringList mIdentifierFields;
200 QVariant mData;
201};
202
204
205
206#endif // QGSFEATUREEXPRESSIONVALUESGATHERER_H
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
bool isValid() const
Returns the validity of this feature.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Container of fields for a vector layer.
Definition qgsfields.h:46
Partial snapshot of vector layer's state (only the members necessary for access to features).
Represents a vector layer which manages a vector based dataset.
QString displayExpression
#define FID_NULL
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features