QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 <QThread>
19#include <QMutex>
20#include "qgsapplication.h"
21#include "qgslogger.h"
22#include "qgsvectorlayer.h"
25
26#define SIP_NO_FILE
27
28// just internal guff - definitely not for exposing to public API!
30
37class QgsFeatureExpressionValuesGatherer: public QThread
38{
39 Q_OBJECT
40
41 public:
42
50 QgsFeatureExpressionValuesGatherer( QgsVectorLayer *layer,
51 const QString &displayExpression = QString(),
52 const QgsFeatureRequest &request = QgsFeatureRequest(),
53 const QStringList &identifierFields = QStringList() )
54 : mSource( new QgsVectorLayerFeatureSource( layer ) )
55 , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
56 , mExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
57 , mRequest( request )
58 , mIdentifierFields( identifierFields )
59 {
60 }
61
62 struct Entry
63 {
64 Entry() = default;
65
66 Entry( const QVariantList &_identifierFields, const QString &_value, const QgsFeature &_feature )
67 : identifierFields( _identifierFields )
68 , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
69 , value( _value )
70 , feature( _feature )
71 {}
72
73 Entry( const QgsFeatureId &_featureId, const QString &_value, const QgsVectorLayer *layer )
74 : featureId( _featureId )
75 , value( _value )
76 , feature( QgsFeature( layer ? layer->fields() : QgsFields() ) )
77 {}
78
79 QVariantList identifierFields;
80 QgsFeatureId featureId;
81 QString value;
82 QgsFeature feature;
83
84 bool operator()( const Entry &lhs, const Entry &rhs ) const;
85 };
86
87 static Entry nullEntry( QgsVectorLayer *layer )
88 {
89 return Entry( QVariantList(), QgsApplication::nullRepresentation(), QgsFeature( layer->fields() ) );
90 }
91
92 void run() override
93 {
94 mWasCanceled = false;
95
96 QgsFeatureIterator iterator = mSource->getFeatures( mRequest );
97
98 mDisplayExpression.prepare( &mExpressionContext );
99
100 QgsFeature feature;
101 QList<int> attributeIndexes;
102 for ( auto it = mIdentifierFields.constBegin(); it != mIdentifierFields.constEnd(); ++it )
103 attributeIndexes << mSource->fields().indexOf( *it );
104
105 while ( iterator.nextFeature( feature ) )
106 {
107 mExpressionContext.setFeature( feature );
108 QVariantList attributes;
109 for ( const int idx : attributeIndexes )
110 attributes << feature.attribute( idx );
111
112 const QString expressionValue = mDisplayExpression.evaluate( &mExpressionContext ).toString();
113
114 mEntries.append( Entry( attributes, expressionValue, feature ) );
115
116 const QMutexLocker locker( &mCancelMutex );
117 if ( mWasCanceled )
118 return;
119 }
120 }
121
123 void stop()
124 {
125 const QMutexLocker locker( &mCancelMutex );
126 mWasCanceled = true;
127 }
128
130 bool wasCanceled() const
131 {
132 const QMutexLocker locker( &mCancelMutex );
133 return mWasCanceled;
134 }
135
136 QVector<Entry> entries() const
137 {
138 return mEntries;
139 }
140
141 QgsFeatureRequest request() const
142 {
143 return mRequest;
144 }
145
149 QVariant data() const
150 {
151 return mData;
152 }
153
157 void setData( const QVariant &data )
158 {
159 mData = data;
160 }
161
162 protected:
163 QVector<Entry> mEntries;
164
165 private:
166 std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
167 QgsExpression mDisplayExpression;
168 QgsExpressionContext mExpressionContext;
169 QgsFeatureRequest mRequest;
170 bool mWasCanceled = false;
171 mutable QMutex mCancelMutex;
172 QStringList mIdentifierFields;
173 QVariant mData;
174};
175
177
178
179#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...
Class for 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.
This class 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
QgsFields fields
Definition qgsfeature.h:68
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
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
QString displayExpression
#define FID_NULL
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
const QgsAttributeList & attributeIndexes