Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Allow QgsVectorLayerSelectedFeatureSource to
create expression context scopes

Allows algorithms which require the underlying layer scope
to operate correctly when running in selected features only
mode.

Fixes #17170

(cherry-picked from d5617b8)
  • Loading branch information
nyalldawson committed Apr 6, 2018
1 parent c1af6ec commit 1e29996
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python/core/qgsvectorlayerfeatureiterator.sip.in
Expand Up @@ -153,7 +153,7 @@ Setup the simplification of geometries to fetch using the specified simplify met



class QgsVectorLayerSelectedFeatureSource : QgsFeatureSource
class QgsVectorLayerSelectedFeatureSource : QgsFeatureSource, QgsExpressionContextScopeGenerator
{
%Docstring
QgsFeatureSource subclass for the selected features from a :py:class:`QgsVectorLayer`.
Expand Down Expand Up @@ -185,6 +185,8 @@ the QgsVectorLayerSelectedFeatureSource will not be reflected.

virtual QString sourceName() const;

virtual QgsExpressionContextScope *createExpressionContextScope() const;



};
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -1083,6 +1083,7 @@ QgsVectorLayerSelectedFeatureSource::QgsVectorLayerSelectedFeatureSource( QgsVec
, mSelectedFeatureIds( layer->selectedFeatureIds() )
, mWkbType( layer->wkbType() )
, mName( layer->name() )
, mLayer( layer )
{}

QgsFeatureIterator QgsVectorLayerSelectedFeatureSource::getFeatures( const QgsFeatureRequest &request ) const
Expand Down Expand Up @@ -1127,3 +1128,11 @@ QString QgsVectorLayerSelectedFeatureSource::sourceName() const
{
return mName;
}

QgsExpressionContextScope *QgsVectorLayerSelectedFeatureSource::createExpressionContextScope() const
{
if ( mLayer )
return mLayer->createExpressionContextScope();
else
return nullptr;
}
6 changes: 5 additions & 1 deletion src/core/qgsvectorlayerfeatureiterator.h
Expand Up @@ -21,7 +21,9 @@
#include "qgsfields.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsfeaturesource.h"
#include "qgsexpressioncontextscopegenerator.h"

#include <QPointer>
#include <QSet>
#include <memory>

Expand Down Expand Up @@ -285,7 +287,7 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera
* QgsFeatureSource subclass for the selected features from a QgsVectorLayer.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsVectorLayerSelectedFeatureSource : public QgsFeatureSource
class CORE_EXPORT QgsVectorLayerSelectedFeatureSource : public QgsFeatureSource, public QgsExpressionContextScopeGenerator
{
public:

Expand All @@ -302,6 +304,7 @@ class CORE_EXPORT QgsVectorLayerSelectedFeatureSource : public QgsFeatureSource
QgsWkbTypes::Type wkbType() const override;
long featureCount() const override;
QString sourceName() const override;
QgsExpressionContextScope *createExpressionContextScope() const override;


private:
Expand All @@ -311,6 +314,7 @@ class CORE_EXPORT QgsVectorLayerSelectedFeatureSource : public QgsFeatureSource
QgsFeatureIds mSelectedFeatureIds;
QgsWkbTypes::Type mWkbType = QgsWkbTypes::Unknown;
QString mName;
QPointer< QgsVectorLayer > mLayer;

};

Expand Down

0 comments on commit 1e29996

Please sign in to comment.