Expression context creation for processing improvements

Hiding away the implementation directly in QgsProcessingFeatureSource

See discussion ec97102bc6 (r152903378)
This commit is contained in:
Matthias Kuhn 2017-11-24 11:01:41 +01:00
parent fd127ee6a4
commit abe1a9b661
4 changed files with 30 additions and 22 deletions

View File

@ -274,10 +274,11 @@ class QgsProcessingFeatureSource : QgsFeatureSource
virtual QVariant maximumValue( int fieldIndex ) const; virtual QVariant maximumValue( int fieldIndex ) const;
QgsFeatureSource *source() const; QgsExpressionContext createExpressionContext( const QgsProcessingContext &context ) const;
%Docstring %Docstring
Access the underlying original ``source``. Returns an expression context scope suitable for this source or a default global/project
:rtype: QgsFeatureSource context if nothing more specific can be created.
:rtype: QgsExpressionContext
%End %End
}; };

View File

@ -132,17 +132,11 @@ QgsExpressionContext QgsProcessingAlgorithm::createExpressionContext( const QVar
// If there's a source capable of generating a context scope, use it // If there's a source capable of generating a context scope, use it
if ( source ) if ( source )
{ {
QgsExpressionContextGenerator *generator = dynamic_cast<QgsExpressionContextGenerator *>( source->source() ); const auto &scopes = source->createExpressionContext( context ).takeScopes();
if ( generator )
{
const auto &scopes = generator->createExpressionContext().takeScopes();
for ( QgsExpressionContextScope *scope : scopes ) for ( QgsExpressionContextScope *scope : scopes )
c << scope; c << scope;
} }
} else if ( c.scopeCount() == 0 )
else
if ( c.scopeCount() == 0 )
{ {
//empty scope, populate with initial scopes //empty scope, populate with initial scopes
c << QgsExpressionContextUtils::globalScope() c << QgsExpressionContextUtils::globalScope()

View File

@ -721,7 +721,19 @@ QVariant QgsProcessingFeatureSource::maximumValue( int fieldIndex ) const
return mSource->maximumValue( fieldIndex ); 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;
} }

View File

@ -320,9 +320,10 @@ class CORE_EXPORT QgsProcessingFeatureSource : public QgsFeatureSource
QVariant maximumValue( int fieldIndex ) const override; 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: private: