Skip to content
Permalink
Browse files

[processing] Add API to allow custom expression variables to be set

for a model
  • Loading branch information
nyalldawson committed Feb 19, 2019
1 parent fbc22e2 commit e31fb920e76cbc0b8ae31e404cbd169993c9a158
@@ -417,6 +417,30 @@ algorithm ``results`` must be passed.
Creates a new expression context scope for a child algorithm within the model.

.. seealso:: :py:func:`variablesForChildAlgorithm`
%End

QVariantMap variables() const;
%Docstring
Returns the map of custom expression context variables defined for this model.

These variables are added to the model's expression context scope, allowing for preset
"constant" expression variables to be utilised within the model.

.. seealso:: :py:func:`setVariables`

.. versionadded:: 3.8
%End

void setVariables( const QVariantMap &variables );
%Docstring
Sets the map of custom expression context ``variables`` for this model.

These variables are added to the model's expression context scope, allowing for preset
"constant" expression variables to be utilised within the model.

.. seealso:: :py:func:`variables`

.. versionadded:: 3.8
%End

protected:
@@ -777,6 +777,14 @@ QgsExpressionContextScope *QgsExpressionContextUtils::processingModelAlgorithmSc
modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_folder" ), QDir::toNativeSeparators( modelFolder ), true, true ) );
modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_name" ), model->displayName(), true ) );
modelScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "model_group" ), model->group(), true ) );

// custom variables
const QVariantMap customVariables = model->variables();
for ( auto it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
{
modelScope->addVariable( QgsExpressionContextScope::StaticVariable( it.key(), it.value(), true ) );
}

return modelScope.release();
}

@@ -1059,6 +1059,8 @@ QVariant QgsProcessingModelAlgorithm::toVariant() const
}
map.insert( QStringLiteral( "parameterDefinitions" ), paramDefMap );

map.insert( QStringLiteral( "modelVariables" ), mVariables );

return map;
}

@@ -1071,6 +1073,8 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
mModelGroupId = map.value( QStringLiteral( "model_group" ) ).toString();
mHelpContent = map.value( QStringLiteral( "help" ) ).toMap();

mVariables = map.value( QStringLiteral( "modelVariables" ) ).toMap();

mChildAlgorithms.clear();
QVariantMap childMap = map.value( QStringLiteral( "children" ) ).toMap();
QVariantMap::const_iterator childIt = childMap.constBegin();
@@ -1444,5 +1448,15 @@ QgsProcessingAlgorithm *QgsProcessingModelAlgorithm::createInstance() const
return alg;
}

QVariantMap QgsProcessingModelAlgorithm::variables() const
{
return mVariables;
}

void QgsProcessingModelAlgorithm::setVariables( const QVariantMap &variables )
{
mVariables = variables;
}

///@endcond

@@ -376,6 +376,28 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
const QVariantMap &results = QVariantMap() ) const SIP_FACTORY;

/**
* Returns the map of custom expression context variables defined for this model.
*
* These variables are added to the model's expression context scope, allowing for preset
* "constant" expression variables to be utilised within the model.
*
* \see setVariables()
* \since QGIS 3.8
*/
QVariantMap variables() const;

/**
* Sets the map of custom expression context \a variables for this model.
*
* These variables are added to the model's expression context scope, allowing for preset
* "constant" expression variables to be utilised within the model.
*
* \see variables()
* \since QGIS 3.8
*/
void setVariables( const QVariantMap &variables );

protected:

QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;
@@ -400,6 +422,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

QVariantMap mResults;

QVariantMap mVariables;

void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;

@@ -6006,6 +6006,12 @@ void TestQgsProcessing::modelScope()
QgsProcessingContext pc;

QgsProcessingModelAlgorithm alg( "test", "testGroup" );

QVariantMap variables;
variables.insert( QStringLiteral( "v1" ), 5 );
variables.insert( QStringLiteral( "v2" ), QStringLiteral( "aabbccd" ) );
alg.setVariables( variables );

QVariantMap params;
params.insert( QStringLiteral( "a_param" ), 5 );
std::unique_ptr< QgsExpressionContextScope > scope( QgsExpressionContextUtils::processingModelAlgorithmScope( &alg, params, pc ) );
@@ -6016,6 +6022,8 @@ void TestQgsProcessing::modelScope()
QVERIFY( scope->hasVariable( QStringLiteral( "model_folder" ) ) );
QCOMPARE( scope->variable( QStringLiteral( "model_path" ) ).toString(), QString() );
QCOMPARE( scope->variable( QStringLiteral( "model_folder" ) ).toString(), QString() );
QCOMPARE( scope->variable( QStringLiteral( "v1" ) ).toInt(), 5 );
QCOMPARE( scope->variable( QStringLiteral( "v2" ) ).toString(), QStringLiteral( "aabbccd" ) );

QgsProject p;
pc.setProject( &p );
@@ -6590,6 +6598,11 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelAlgorithm alg5( "test", "testGroup" );
alg5.helpContent().insert( "author", "me" );
alg5.helpContent().insert( "usage", "run" );
QVariantMap variables;
variables.insert( QStringLiteral( "v1" ), 5 );
variables.insert( QStringLiteral( "v2" ), QStringLiteral( "aabbccd" ) );
alg5.setVariables( variables );
QCOMPARE( alg5.variables(), variables );
QgsProcessingModelChildAlgorithm alg5c1;
alg5c1.setChildId( "cx1" );
alg5c1.setAlgorithmId( "buffer" );
@@ -6638,6 +6651,7 @@ void TestQgsProcessing::modelerAlgorithm()
QCOMPARE( alg6.name(), QStringLiteral( "test" ) );
QCOMPARE( alg6.group(), QStringLiteral( "testGroup" ) );
QCOMPARE( alg6.helpContent(), alg5.helpContent() );
QCOMPARE( alg6.variables(), variables );
QgsProcessingModelChildAlgorithm alg6c1 = alg6.childAlgorithm( "cx1" );
QCOMPARE( alg6c1.childId(), QStringLiteral( "cx1" ) );
QCOMPARE( alg6c1.algorithmId(), QStringLiteral( "buffer" ) );
BIN +0 Bytes (100%) tests/testdata/points_gpkg.gpkg
Binary file not shown.

0 comments on commit e31fb92

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