QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsfeaturefiltermodel_p.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturefiltermodel_p - QgsFieldExpressionValuesGatherer
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 QGSFEATUREFILTERMODEL_P_H
16 #define QGSFEATUREFILTERMODEL_P_H
17 
18 #include <QThread>
19 #include "qgsfeaturefiltermodel.h"
20 #include "qgslogger.h"
22 
23 #define SIP_NO_FILE
24 
25 // just internal guff - definitely not for exposing to public API!
27 
34 class QgsFieldExpressionValuesGatherer: public QThread
35 {
36  Q_OBJECT
37 
38  public:
39  QgsFieldExpressionValuesGatherer( QgsVectorLayer *layer,
40  const QString &displayExpression,
41  const QStringList &identifierFields,
42  const QgsFeatureRequest &request = QgsFeatureRequest() )
43  : mSource( new QgsVectorLayerFeatureSource( layer ) )
44  , mDisplayExpression( displayExpression )
45  , mRequest( request )
46  , mIdentifierFields( identifierFields )
47  {
48  }
49 
50  void run() override
51  {
52  mWasCanceled = false;
53 
54  mIterator = mSource->getFeatures( mRequest );
55 
56  mDisplayExpression.prepare( &mExpressionContext );
57 
58  QgsFeature feat;
59  QList<int> attributeIndexes;
60  for ( const QString &fieldName : qgis::as_const( mIdentifierFields ) )
61  attributeIndexes << mSource->fields().indexOf( fieldName );
62 
63  while ( mIterator.nextFeature( feat ) )
64  {
65  mExpressionContext.setFeature( feat );
66  QVariantList attributes;
67  for ( const int idx : attributeIndexes )
68  attributes << feat.attribute( idx );
69  mEntries.append( QgsFeatureFilterModel::Entry( attributes, mDisplayExpression.evaluate( &mExpressionContext ).toString(), feat ) );
70 
71  if ( mWasCanceled )
72  return;
73  }
74 
75  emit collectedValues();
76  }
77 
79  void stop()
80  {
81  mWasCanceled = true;
82  }
83 
85  bool wasCanceled() const { return mWasCanceled; }
86 
87  QVector<QgsFeatureFilterModel::Entry> entries() const
88  {
89  return mEntries;
90  }
91 
92  QgsFeatureRequest request() const
93  {
94  return mRequest;
95  }
96 
100  QVariant data() const
101  {
102  return mData;
103  }
104 
108  void setData( const QVariant &data )
109  {
110  mData = data;
111  }
112 
113  signals:
114 
119  void collectedValues();
120 
121  private:
122 
123  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
124  QgsExpression mDisplayExpression;
125  QgsExpressionContext mExpressionContext;
126  QgsFeatureRequest mRequest;
127  QgsFeatureIterator mIterator;
128  bool mWasCanceled = false;
129  QVector<QgsFeatureFilterModel::Entry> mEntries;
130  QStringList mIdentifierFields;
131  QVariant mData;
132 };
133 
135 
136 
137 #endif // QGSFEATUREFILTERMODEL_P_H
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsAttributeList & attributeIndexes
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262