Skip to content
Permalink
Browse files

[processing] Expose additional expression variables for parameters in…

… GUI
  • Loading branch information
nyalldawson committed Apr 7, 2019
1 parent d3016d7 commit 5a85e206f60261b8f5941ba9b34223224c01f400
Showing with 23 additions and 1 deletion.
  1. +17 −1 src/gui/processing/qgsprocessingwidgetwrapper.cpp
  2. +6 −0 tests/src/gui/testprocessinggui.cpp
@@ -235,7 +235,23 @@ void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<Qg

QgsExpressionContext QgsAbstractProcessingParameterWidgetWrapper::createExpressionContext() const
{
return QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
QgsExpressionContext context = QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
if ( mParameterDefinition && !mParameterDefinition->additionalExpressionContextVariables().isEmpty() )
{
std::unique_ptr< QgsExpressionContextScope > paramScope = qgis::make_unique< QgsExpressionContextScope >();
const QStringList additional = mParameterDefinition->additionalExpressionContextVariables();
for ( const QString &var : additional )
{
paramScope->setVariable( var, QVariant() );
}
context.appendScope( paramScope.release() );

// we always highlight additional variables for visibility
QStringList highlighted = context.highlightedVariables();
highlighted.append( additional );
context.setHighlightedVariables( highlighted );
}
return context;
}

void QgsAbstractProcessingParameterWidgetWrapper::setDialog( QDialog * )
@@ -360,9 +360,15 @@ void TestProcessingGui::testWrapperFactoryRegistry()
void TestProcessingGui::testWrapperGeneral()
{
TestParamType param( QStringLiteral( "boolean" ), QStringLiteral( "bool" ) );
param.setAdditionalExpressionContextVariables( QStringList() << QStringLiteral( "a" ) << QStringLiteral( "b" ) );
QgsProcessingBooleanWidgetWrapper wrapper( &param );
QCOMPARE( wrapper.type(), QgsProcessingGui::Standard );

QgsExpressionContext expContext = wrapper.createExpressionContext();
QVERIFY( expContext.hasVariable( QStringLiteral( "a" ) ) );
QVERIFY( expContext.hasVariable( QStringLiteral( "b" ) ) );
QCOMPARE( expContext.highlightedVariables(), QStringList() << QStringLiteral( "a" ) << QStringLiteral( "b" ) );

QgsProcessingBooleanWidgetWrapper wrapper2( &param, QgsProcessingGui::Batch );
QCOMPARE( wrapper2.type(), QgsProcessingGui::Batch );
QCOMPARE( wrapper2.parameterDefinition()->name(), QStringLiteral( "bool" ) );

0 comments on commit 5a85e20

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