From d3016d73d4e6a335733e78c8a49de3a8b37baa55 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 7 Apr 2019 12:43:02 +1000 Subject: [PATCH] [processing] Add method to specify additional expression context variables which will be available to a parameter when it is evaluated. Specifying variables via this method is for metadata purposes only. It is the algorithm's responsibility to correctly set the value of these additional variables in all expression context used when evaluating the parameter, in whichever way is appropriate for that particular variable. --- .../processing/qgsprocessingparameters.sip.in | 39 +++++++++++++++++++ src/core/processing/qgsprocessingparameters.h | 37 ++++++++++++++++++ tests/src/analysis/testqgsprocessing.cpp | 6 +++ 3 files changed, 82 insertions(+) diff --git a/python/core/auto_generated/processing/qgsprocessingparameters.sip.in b/python/core/auto_generated/processing/qgsprocessingparameters.sip.in index b2375c260dc4..da68d7b4ce9e 100644 --- a/python/core/auto_generated/processing/qgsprocessingparameters.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingparameters.sip.in @@ -457,6 +457,44 @@ the dynamic parameter. .. seealso:: :py:func:`isDynamic` .. seealso:: :py:func:`setDynamicPropertyDefinition` +%End + + QStringList additionalExpressionContextVariables() const; +%Docstring +Returns a list of additional expression context variables which are available for use when evaluating +this parameter. + +The additional variables will be added to the variables exposed from the usual expression +context available to the parameter. They can be used to expose variables which are ONLY available +to this parameter. + +The returned list should contain the variable names only, without the usual "@" prefix. + +.. seealso:: :py:func:`setAdditionalExpressionContextVariables` + +.. versionadded:: 3.8 +%End + + void setAdditionalExpressionContextVariables( const QStringList &variables ); +%Docstring +Sets a list of additional expression context ``variables`` which are available for use when evaluating +this parameter. + +The additional variables will be added to the variables exposed from the usual expression +context available to the parameter. They can be used to expose variables which are ONLY available +to this parameter. + +The ``variables`` list should contain the variable names only, without the usual "@" prefix. + +.. note:: + + Specifying variables via this method is for metadata purposes only. It is the algorithm's responsibility + to correctly set the value of these additional variables in all expression context used when evaluating the parameter, + in whichever way is appropriate for that particular variable. + +.. seealso:: :py:func:`additionalExpressionContextVariables` + +.. versionadded:: 3.8 %End protected: @@ -471,6 +509,7 @@ the dynamic parameter. + }; QFlags operator|(QgsProcessingParameterDefinition::Flag f1, QFlags f2); diff --git a/src/core/processing/qgsprocessingparameters.h b/src/core/processing/qgsprocessingparameters.h index ef57db8dfd3f..19a4da884c88 100644 --- a/src/core/processing/qgsprocessingparameters.h +++ b/src/core/processing/qgsprocessingparameters.h @@ -515,6 +515,40 @@ class CORE_EXPORT QgsProcessingParameterDefinition */ void setDynamicLayerParameterName( const QString &name ) { mDynamicLayerParameterName = name; } + /** + * Returns a list of additional expression context variables which are available for use when evaluating + * this parameter. + * + * The additional variables will be added to the variables exposed from the usual expression + * context available to the parameter. They can be used to expose variables which are ONLY available + * to this parameter. + * + * The returned list should contain the variable names only, without the usual "@" prefix. + * + * \see setAdditionalExpressionContextVariables() + * \since QGIS 3.8 + */ + QStringList additionalExpressionContextVariables() const { return mAdditionalExpressionVariables; } + + /** + * Sets a list of additional expression context \a variables which are available for use when evaluating + * this parameter. + * + * The additional variables will be added to the variables exposed from the usual expression + * context available to the parameter. They can be used to expose variables which are ONLY available + * to this parameter. + * + * The \a variables list should contain the variable names only, without the usual "@" prefix. + * + * \note Specifying variables via this method is for metadata purposes only. It is the algorithm's responsibility + * to correctly set the value of these additional variables in all expression context used when evaluating the parameter, + * in whichever way is appropriate for that particular variable. + * + * \see additionalExpressionContextVariables() + * \since QGIS 3.8 + */ + void setAdditionalExpressionContextVariables( const QStringList &variables ) { mAdditionalExpressionVariables = variables; } + protected: //! Parameter name @@ -544,6 +578,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition //! Linked vector layer parameter name for dynamic properties QString mDynamicLayerParameterName; + //! Additional expression context variables exposed for use by this parameter + QStringList mAdditionalExpressionVariables; + // To allow access to mAlgorithm. We don't want a public setter for this! friend class QgsProcessingAlgorithm; diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index 34c9fdc95bad..721addac3cf6 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -1972,6 +1972,12 @@ void TestQgsProcessing::parameterGeneral() param.metadata().insert( "p3", 9 ); QCOMPARE( param.metadata().value( "p3" ).toInt(), 9 ); + QVERIFY( param.additionalExpressionContextVariables().isEmpty() ); + param.setAdditionalExpressionContextVariables( QStringList() << "a" << "b" ); + QCOMPARE( param.additionalExpressionContextVariables(), QStringList() << "a" << "b" ); + std::unique_ptr< QgsProcessingParameterDefinition > param2( param.clone() ); + QCOMPARE( param2->additionalExpressionContextVariables(), QStringList() << "a" << "b" ); + QVariantMap map = param.toVariantMap(); QgsProcessingParameterBoolean fromMap( "x" ); QVERIFY( fromMap.fromVariantMap( map ) );