Skip to content

Commit

Permalink
Expression context creation for processing improvements
Browse files Browse the repository at this point in the history
Hiding away the implementation directly in QgsProcessingFeatureSource

See discussion https://github.com/qgis/QGIS/pull/5709/files/ec97102bc687cdd3aace40359b67c9e45913b726#r152903378
  • Loading branch information
m-kuhn committed Nov 24, 2017
1 parent fd127ee commit abe1a9b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
7 changes: 4 additions & 3 deletions python/core/processing/qgsprocessingutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ class QgsProcessingFeatureSource : QgsFeatureSource
virtual QVariant maximumValue( int fieldIndex ) const;


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

};
Expand Down
24 changes: 9 additions & 15 deletions src/core/processing/qgsprocessingalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,16 @@ QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVar
// If there's a source capable of generating a context scope, use it
if ( source )
{
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( source->source() );
if ( generator )
{
const auto &scopes = generator->createExpressionContext().takeScopes();
for ( QgsExpressionContextScope *scope : scopes )
c << scope;
}
const auto &scopes = source->createExpressionContext( context ).takeScopes();
for ( QgsExpressionContextScope *scope : scopes )
c << scope;
}
else if ( c.scopeCount() == 0 )
{
//empty scope, populate with initial scopes
c << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( context.project() );
}
else

if ( c.scopeCount() == 0 )
{
//empty scope, populate with initial scopes
c << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( context.project() );
}

c << QgsExpressionContextUtils::processingAlgorithmScope( this, parameters, context );
return c;
Expand Down
16 changes: 14 additions & 2 deletions src/core/processing/qgsprocessingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,19 @@ QVariant QgsProcessingFeatureSource::maximumValue( int fieldIndex ) const
return mSource->maximumValue( fieldIndex );
}

QgsFeatureSource *QgsProcessingFeatureSource::source() const
QgsExpressionContext QgsProcessingFeatureSource::createExpressionContext( const QgsProcessingContext &context ) const
{
return mSource;
QgsExpressionContext expressionContext;
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( mSource );
if ( generator )
{
expressionContext = generator->createExpressionContext();
}
else
{
expressionContext
<< QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( context.project() );
}
return expressionContext;
}
5 changes: 3 additions & 2 deletions src/core/processing/qgsprocessingutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
QVariant maximumValue( int fieldIndex ) const override;

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

private:

Expand Down

0 comments on commit abe1a9b

Please sign in to comment.