Skip to content

Commit

Permalink
[processing] Add method to specify additional expression context vari…
Browse files Browse the repository at this point in the history
…ables

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.
  • Loading branch information
nyalldawson committed Apr 7, 2019
1 parent ee51551 commit d3016d7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Expand Up @@ -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:
Expand All @@ -471,6 +509,7 @@ the dynamic parameter.




};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
Expand Down
37 changes: 37 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -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 ) );
Expand Down

0 comments on commit d3016d7

Please sign in to comment.