Skip to content

Commit 851adb0

Browse files
committedNov 23, 2017
Allow adding sources to processing expression context
1 parent 3a576d8 commit 851adb0

9 files changed

+59
-7
lines changed
 

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,12 @@ class QgsProcessingAlgorithm
326326
%End
327327

328328
QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
329-
QgsProcessingContext &context ) const;
329+
QgsProcessingContext &context, QgsProcessingFeatureSource *source = 0 ) const;
330330
%Docstring
331331
Creates an expression context relating to the algorithm. This can be called by algorithms
332332
to create a new expression context ready for evaluating expressions within the algorithm.
333+
Optionally, a ``source`` can be specified which will be used to populate the context if it
334+
implements the QgsExpressionContextGenerator interface.
333335
:rtype: QgsExpressionContext
334336
%End
335337

‎python/core/processing/qgsprocessingutils.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ class QgsProcessingFeatureSource : QgsFeatureSource
274274
virtual QVariant maximumValue( int fieldIndex ) const;
275275

276276

277+
QgsFeatureSource *source() const;
278+
%Docstring
279+
Access the underlying original ``source``.
280+
:rtype: QgsFeatureSource
281+
%End
282+
277283
};
278284

279285

‎python/core/qgsexpressioncontext.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ Constructor for QgsExpressionContext
562562
%End
563563

564564

565+
565566
void setFeature( const QgsFeature &feature );
566567
%Docstring
567568
Convenience function for setting a feature for the context. The feature

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,30 @@ QWidget *QgsProcessingAlgorithm::createCustomParametersWidget( QWidget * ) const
124124
}
125125

126126
QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVariantMap &parameters,
127-
QgsProcessingContext &context ) const
127+
QgsProcessingContext &context, QgsProcessingFeatureSource *source ) const
128128
{
129129
// start with context's expression context
130130
QgsExpressionContext c = context.expressionContext();
131-
if ( c.scopeCount() == 0 )
131+
132+
// If there's a source capable of generating a context scope, use it
133+
if ( source )
132134
{
133-
//empty scope, populate with initial scopes
134-
c << QgsExpressionContextUtils::globalScope()
135-
<< QgsExpressionContextUtils::projectScope( context.project() );
135+
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( source->source() );
136+
if ( generator )
137+
{
138+
const auto &scopes = generator->createExpressionContext().takeScopes();
139+
for ( QgsExpressionContextScope *scope : scopes )
140+
c << scope;
141+
}
136142
}
143+
else
144+
145+
if ( c.scopeCount() == 0 )
146+
{
147+
//empty scope, populate with initial scopes
148+
c << QgsExpressionContextUtils::globalScope()
149+
<< QgsExpressionContextUtils::projectScope( context.project() );
150+
}
137151

138152
c << QgsExpressionContextUtils::processingAlgorithmScope( this, parameters, context );
139153
return c;

‎src/core/processing/qgsprocessingalgorithm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,11 @@ class CORE_EXPORT QgsProcessingAlgorithm
339339
/**
340340
* Creates an expression context relating to the algorithm. This can be called by algorithms
341341
* to create a new expression context ready for evaluating expressions within the algorithm.
342+
* Optionally, a \a source can be specified which will be used to populate the context if it
343+
* implements the QgsExpressionContextGenerator interface.
342344
*/
343345
QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
344-
QgsProcessingContext &context ) const;
346+
QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
345347

346348
/**
347349
* Checks whether the coordinate reference systems for the specified set of \a parameters

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,8 @@ QVariant QgsProcessingFeatureSource::maximumValue( int fieldIndex ) const
695695
{
696696
return mSource->maximumValue( fieldIndex );
697697
}
698+
699+
QgsFeatureSource *QgsProcessingFeatureSource::source() const
700+
{
701+
return mSource;
702+
}

‎src/core/processing/qgsprocessingutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
317317
QVariant minimumValue( int fieldIndex ) const override;
318318
QVariant maximumValue( int fieldIndex ) const override;
319319

320+
/**
321+
* Access the underlying original \a source.
322+
*/
323+
QgsFeatureSource *source() const;
324+
320325
private:
321326

322327
QgsFeatureSource *mSource = nullptr;

‎src/core/qgsexpressioncontext.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,13 @@ QgsExpressionContextScope *QgsExpressionContext::popScope()
474474
return nullptr;
475475
}
476476

477+
QList<QgsExpressionContextScope *> QgsExpressionContext::takeScopes()
478+
{
479+
QList<QgsExpressionContextScope *> stack = mStack;
480+
mStack.clear();
481+
return stack;
482+
}
483+
477484
QgsExpressionContext &QgsExpressionContext::operator<<( QgsExpressionContextScope *scope )
478485
{
479486
mStack.append( scope );

‎src/core/qgsexpressioncontext.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,16 @@ class CORE_EXPORT QgsExpressionContext
581581
*/
582582
QgsExpressionContextScope *popScope();
583583

584+
/**
585+
* Return all scopes from this context and remove them, leaving this context without
586+
* any context.
587+
* Ownership is transferred to the caller.
588+
*
589+
* \since QGIS 3.0
590+
* \note Not available in Python
591+
*/
592+
QList<QgsExpressionContextScope *> takeScopes() SIP_SKIP;
593+
584594
/**
585595
* Appends a scope to the end of the context. This scope will override
586596
* any matching variables or functions provided by existing scopes within the

0 commit comments

Comments
 (0)
Please sign in to comment.