QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
50 QgsFeatureExpressionValuesGatherer(
51 QgsVectorLayer *layer, const QString &displayExpression = QString(), const QgsFeatureRequest &request = QgsFeatureRequest(), const QStringList &identifierFields = QStringList()
52 )
53 : mSource( new QgsVectorLayerFeatureSource( layer ) )
54 , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
55 , mExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
56 , mRequest( request )
57 , mIdentifierFields( identifierFields )
58 {}
59
60 QgsFeatureExpressionValuesGatherer(
61 QgsVectorLayer *layer,
62 const QString &displayExpression = QString(),
63 const QString &orderExpression = QString(),
64 const QgsFeatureRequest &request = QgsFeatureRequest(),
65 const QStringList &identifierFields = QStringList()
66 )
67 : mSource( new QgsVectorLayerFeatureSource( layer ) )
68 , mDisplayExpression( displayExpression.isEmpty() ? layer->displayExpression() : displayExpression )
69 , mOrderExpression( orderExpression.isEmpty() ? displayExpression.isEmpty() ? layer->displayExpression() : displayExpression : orderExpression )
70 , mExpressionContext( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
71 , mRequest( request )
72 , mIdentifierFields( identifierFields )
73 {}
74
75 struct Entry
76 {
77 Entry() = default;
78
79 Entry( const QVariantList &_identifierFields, const QString &_value, const QString &_orderValue, const QgsFeature &_feature )
80 : identifierFields( _identifierFields )
81 , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
82 , value( _value )
83 , orderValue( _orderValue )
84 , feature( _feature )
85 {}
86
87 Entry( const QVariantList &_identifierFields, const QString &_value, const QgsFeature &_feature )
88 : identifierFields( _identifierFields )
89 , featureId( _feature.isValid() ? _feature.id() : FID_NULL )
90 , value( _value )
91 , feature( _feature )
92 {}
93
94 Entry( const QgsFeatureId &_featureId, const QString &_value, const QgsVectorLayer *layer )
95 : featureId( _featureId )
96 , value( _value )
97 , feature( QgsFeature( layer ? layer->fields() : QgsFields() ) )
98 {}
99
100 QVariantList identifierFields;
101 QgsFeatureId featureId;
102 QString value;
103 QString orderValue;
104 QgsFeature feature;
105
106 bool operator()( const Entry &lhs, const Entry &rhs ) const;
107 };
108
109 static Entry nullEntry( QgsVectorLayer *layer ) { return Entry( QVariantList(), QgsApplication::nullRepresentation(), QgsFeature( layer->fields() ) ); }
110
111 void run() override
112 {
113 mWasCanceled = false;
114
115 QgsFeatureIterator iterator = mSource->getFeatures( mRequest );
116
117 mDisplayExpression.prepare( &mExpressionContext );
118 mOrderExpression.prepare( &mExpressionContext );
119
120 QgsFeature feature;
121 QList<int> attributeIndexes;
122 for ( auto it = mIdentifierFields.constBegin(); it != mIdentifierFields.constEnd(); ++it )
123 attributeIndexes << mSource->fields().indexOf( *it );
124
125 while ( iterator.nextFeature( feature ) )
126 {
127 mExpressionContext.setFeature( feature );
128 QVariantList attributes;
129 for ( const int idx : attributeIndexes )
130 attributes << feature.attribute( idx );
131
132 const QString expressionValue = mDisplayExpression.evaluate( &mExpressionContext ).toString();
133 const QString orderValue = mOrderExpression.evaluate( &mExpressionContext ).toString();
134
135 mEntries.append( Entry( attributes, expressionValue, orderValue, feature ) );
136
137 const QMutexLocker locker( &mCancelMutex );
138 if ( mWasCanceled )
139 return;
140 }
141 }
142
144 void stop()
145 {
146 const QMutexLocker locker( &mCancelMutex );
147 mWasCanceled = true;
148 }
149
151 bool wasCanceled() const
152 {
153 const QMutexLocker locker( &mCancelMutex );
154 return mWasCanceled;
155 }
156
157 QVector<Entry> entries() const { return mEntries; }
158
159 QgsFeatureRequest request() const { return mRequest; }
160
164 QVariant data() const { return mData; }
165
169 void setData( const QVariant &data ) { mData = data; }
170
171 protected:
172 QVector<Entry> mEntries;
173
174 private:
175 std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
176 QgsExpression mDisplayExpression;
177 QgsExpression mOrderExpression;
178 QgsExpressionContext mExpressionContext;
179 QgsFeatureRequest mRequest;
180 bool mWasCanceled = false;
181 mutable QMutex mCancelMutex;
182 QStringList mIdentifierFields;
183 QVariant mData;
184};
185
187
188
189#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:60
QgsFeatureId id
Definition qgsfeature.h:68
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