Skip to content

Commit

Permalink
Allow specifying expression context for feature count
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 11, 2017
1 parent 1fb55db commit 2474498
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayerfeaturecounter.sip
Expand Up @@ -24,7 +24,7 @@ class QgsVectorLayerFeatureCounter : QgsTask
%End
public:

QgsVectorLayerFeatureCounter( QgsVectorLayer *layer );
QgsVectorLayerFeatureCounter( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext() );
%Docstring
Create a new feature counter for ``layer``.
%End
Expand Down
10 changes: 7 additions & 3 deletions src/core/qgsvectorlayerfeaturecounter.cpp
@@ -1,11 +1,15 @@
#include "qgsvectorlayerfeaturecounter.h"

QgsVectorLayerFeatureCounter::QgsVectorLayerFeatureCounter( QgsVectorLayer *layer )
QgsVectorLayerFeatureCounter::QgsVectorLayerFeatureCounter( QgsVectorLayer *layer, const QgsExpressionContext &context )
: mSource( new QgsVectorLayerFeatureSource( layer ) )
, mRenderer( layer->renderer()->clone() )
, mExpressionContextScopes( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) )
, mExpressionContext( context )
, mFeatureCount( layer->featureCount() )
{
if ( !mExpressionContext.scopeCount() )
{
mExpressionContext = layer->createExpressionContext();
}
}

bool QgsVectorLayerFeatureCounter::run()
Expand All @@ -26,7 +30,7 @@ bool QgsVectorLayerFeatureCounter::run()
// Renderer (rule based) may depend on context scale, with scale is ignored if 0
QgsRenderContext renderContext;
renderContext.setRendererScale( 0 );
renderContext.expressionContext().appendScopes( mExpressionContextScopes );
renderContext.setExpressionContext( mExpressionContext );

QgsFeatureRequest request;
if ( !mRenderer->filterNeedsGeometry() )
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayerfeaturecounter.h
Expand Up @@ -24,7 +24,7 @@ class CORE_EXPORT QgsVectorLayerFeatureCounter : public QgsTask
/**
* Create a new feature counter for \a layer.
*/
QgsVectorLayerFeatureCounter( QgsVectorLayer *layer );
QgsVectorLayerFeatureCounter( QgsVectorLayer *layer, const QgsExpressionContext &context = QgsExpressionContext() );

virtual bool run() override;

Expand Down Expand Up @@ -52,7 +52,7 @@ class CORE_EXPORT QgsVectorLayerFeatureCounter : public QgsTask
private:
std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
std::unique_ptr<QgsFeatureRenderer> mRenderer;
QList<QgsExpressionContextScope *> mExpressionContextScopes;
QgsExpressionContext mExpressionContext;
QHash<QString, long> mSymbolFeatureCountMap;
int mFeatureCount;

Expand Down

0 comments on commit 2474498

Please sign in to comment.