Skip to content

Commit d3016d7

Browse files
committed
[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.
1 parent ee51551 commit d3016d7

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

python/core/auto_generated/processing/qgsprocessingparameters.sip.in

+39
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,44 @@ the dynamic parameter.
457457
.. seealso:: :py:func:`isDynamic`
458458

459459
.. seealso:: :py:func:`setDynamicPropertyDefinition`
460+
%End
461+
462+
QStringList additionalExpressionContextVariables() const;
463+
%Docstring
464+
Returns a list of additional expression context variables which are available for use when evaluating
465+
this parameter.
466+
467+
The additional variables will be added to the variables exposed from the usual expression
468+
context available to the parameter. They can be used to expose variables which are ONLY available
469+
to this parameter.
470+
471+
The returned list should contain the variable names only, without the usual "@" prefix.
472+
473+
.. seealso:: :py:func:`setAdditionalExpressionContextVariables`
474+
475+
.. versionadded:: 3.8
476+
%End
477+
478+
void setAdditionalExpressionContextVariables( const QStringList &variables );
479+
%Docstring
480+
Sets a list of additional expression context ``variables`` which are available for use when evaluating
481+
this parameter.
482+
483+
The additional variables will be added to the variables exposed from the usual expression
484+
context available to the parameter. They can be used to expose variables which are ONLY available
485+
to this parameter.
486+
487+
The ``variables`` list should contain the variable names only, without the usual "@" prefix.
488+
489+
.. note::
490+
491+
Specifying variables via this method is for metadata purposes only. It is the algorithm's responsibility
492+
to correctly set the value of these additional variables in all expression context used when evaluating the parameter,
493+
in whichever way is appropriate for that particular variable.
494+
495+
.. seealso:: :py:func:`additionalExpressionContextVariables`
496+
497+
.. versionadded:: 3.8
460498
%End
461499

462500
protected:
@@ -471,6 +509,7 @@ the dynamic parameter.
471509

472510

473511

512+
474513
};
475514

476515
QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);

src/core/processing/qgsprocessingparameters.h

+37
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,40 @@ class CORE_EXPORT QgsProcessingParameterDefinition
515515
*/
516516
void setDynamicLayerParameterName( const QString &name ) { mDynamicLayerParameterName = name; }
517517

518+
/**
519+
* Returns a list of additional expression context variables which are available for use when evaluating
520+
* this parameter.
521+
*
522+
* The additional variables will be added to the variables exposed from the usual expression
523+
* context available to the parameter. They can be used to expose variables which are ONLY available
524+
* to this parameter.
525+
*
526+
* The returned list should contain the variable names only, without the usual "@" prefix.
527+
*
528+
* \see setAdditionalExpressionContextVariables()
529+
* \since QGIS 3.8
530+
*/
531+
QStringList additionalExpressionContextVariables() const { return mAdditionalExpressionVariables; }
532+
533+
/**
534+
* Sets a list of additional expression context \a variables which are available for use when evaluating
535+
* this parameter.
536+
*
537+
* The additional variables will be added to the variables exposed from the usual expression
538+
* context available to the parameter. They can be used to expose variables which are ONLY available
539+
* to this parameter.
540+
*
541+
* The \a variables list should contain the variable names only, without the usual "@" prefix.
542+
*
543+
* \note Specifying variables via this method is for metadata purposes only. It is the algorithm's responsibility
544+
* to correctly set the value of these additional variables in all expression context used when evaluating the parameter,
545+
* in whichever way is appropriate for that particular variable.
546+
*
547+
* \see additionalExpressionContextVariables()
548+
* \since QGIS 3.8
549+
*/
550+
void setAdditionalExpressionContextVariables( const QStringList &variables ) { mAdditionalExpressionVariables = variables; }
551+
518552
protected:
519553

520554
//! Parameter name
@@ -544,6 +578,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition
544578
//! Linked vector layer parameter name for dynamic properties
545579
QString mDynamicLayerParameterName;
546580

581+
//! Additional expression context variables exposed for use by this parameter
582+
QStringList mAdditionalExpressionVariables;
583+
547584
// To allow access to mAlgorithm. We don't want a public setter for this!
548585
friend class QgsProcessingAlgorithm;
549586

tests/src/analysis/testqgsprocessing.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,12 @@ void TestQgsProcessing::parameterGeneral()
19721972
param.metadata().insert( "p3", 9 );
19731973
QCOMPARE( param.metadata().value( "p3" ).toInt(), 9 );
19741974

1975+
QVERIFY( param.additionalExpressionContextVariables().isEmpty() );
1976+
param.setAdditionalExpressionContextVariables( QStringList() << "a" << "b" );
1977+
QCOMPARE( param.additionalExpressionContextVariables(), QStringList() << "a" << "b" );
1978+
std::unique_ptr< QgsProcessingParameterDefinition > param2( param.clone() );
1979+
QCOMPARE( param2->additionalExpressionContextVariables(), QStringList() << "a" << "b" );
1980+
19751981
QVariantMap map = param.toVariantMap();
19761982
QgsProcessingParameterBoolean fromMap( "x" );
19771983
QVERIFY( fromMap.fromVariantMap( map ) );

0 commit comments

Comments
 (0)