Skip to content
Permalink
Browse files

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
  • Loading branch information
m-kuhn committed Nov 24, 2017
1 parent fd127ee commit abe1a9b6618680dad9f9546c81d3ed5d92612b79
@@ -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

};
@@ -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;
@@ -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;
}
@@ -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:

0 comments on commit abe1a9b

Please sign in to comment.
You can’t perform that action at this time.