Skip to content

Commit abe1a9b

Browse files
committedNov 24, 2017
Expression context creation for processing improvements
Hiding away the implementation directly in QgsProcessingFeatureSource See discussion https://github.com/qgis/QGIS/pull/5709/files/ec97102bc687cdd3aace40359b67c9e45913b726#r152903378
1 parent fd127ee commit abe1a9b

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed
 

‎python/core/processing/qgsprocessingutils.sip

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

276276

277-
QgsFeatureSource *source() const;
277+
QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
278278
%Docstring
279-
Access the underlying original ``source``.
280-
:rtype: QgsFeatureSource
279+
Returns an expression context scope suitable for this source or a default global/project
280+
context if nothing more specific can be created.
281+
:rtype: QgsExpressionContext
281282
%End
282283

283284
};

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,16 @@ QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVar
132132
// If there's a source capable of generating a context scope, use it
133133
if ( source )
134134
{
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-
}
135+
const auto &scopes = source->createExpressionContext( context ).takeScopes();
136+
for ( QgsExpressionContextScope *scope : scopes )
137+
c << scope;
138+
}
139+
else if ( c.scopeCount() == 0 )
140+
{
141+
//empty scope, populate with initial scopes
142+
c << QgsExpressionContextUtils::globalScope()
143+
<< QgsExpressionContextUtils::projectScope( context.project() );
142144
}
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-
}
151145

152146
c << QgsExpressionContextUtils::processingAlgorithmScope( this, parameters, context );
153147
return c;

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,19 @@ QVariant QgsProcessingFeatureSource::maximumValue( int fieldIndex ) const
721721
return mSource->maximumValue( fieldIndex );
722722
}
723723

724-
QgsFeatureSource *QgsProcessingFeatureSource::source() const
724+
QgsExpressionContext QgsProcessingFeatureSource::createExpressionContext( const QgsProcessingContext &context ) const
725725
{
726-
return mSource;
726+
QgsExpressionContext expressionContext;
727+
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( mSource );
728+
if ( generator )
729+
{
730+
expressionContext = generator->createExpressionContext();
731+
}
732+
else
733+
{
734+
expressionContext
735+
<< QgsExpressionContextUtils::globalScope()
736+
<< QgsExpressionContextUtils::projectScope( context.project() );
737+
}
738+
return expressionContext;
727739
}

‎src/core/processing/qgsprocessingutils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,10 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
320320
QVariant maximumValue( int fieldIndex ) const override;
321321

322322
/**
323-
* Access the underlying original \a source.
323+
* Returns an expression context scope suitable for this source or a default global/project
324+
* context if nothing more specific can be created.
324325
*/
325-
QgsFeatureSource *source() const;
326+
QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
326327

327328
private:
328329

0 commit comments

Comments
 (0)
Please sign in to comment.