diff --git a/python/core/processing/qgsprocessingparameters.sip b/python/core/processing/qgsprocessingparameters.sip index f880d1950ac..f88ba4f7c42 100644 --- a/python/core/processing/qgsprocessingparameters.sip +++ b/python/core/processing/qgsprocessingparameters.sip @@ -353,6 +353,13 @@ class QgsProcessingParameterDefinition :rtype: list of str %End + QgsProcessingAlgorithm *algorithm() const; +%Docstring + Returns a pointer to the algorithm which owns this parameter. May be None + for non-owned parameters. + :rtype: QgsProcessingAlgorithm +%End + protected: @@ -360,6 +367,8 @@ class QgsProcessingParameterDefinition + + }; QFlags operator|(QgsProcessingParameterDefinition::Flag f1, QFlags f2); diff --git a/src/core/processing/qgsprocessingalgorithm.cpp b/src/core/processing/qgsprocessingalgorithm.cpp index e04a2d3554b..383c6010f4b 100644 --- a/src/core/processing/qgsprocessingalgorithm.cpp +++ b/src/core/processing/qgsprocessingalgorithm.cpp @@ -242,6 +242,7 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def } mParameters << definition; + definition->mAlgorithm = this; if ( createOutput ) return createAutoOutputForParameter( definition ); diff --git a/src/core/processing/qgsprocessingparameters.cpp b/src/core/processing/qgsprocessingparameters.cpp index 3442a649fc1..613f7591ab0 100644 --- a/src/core/processing/qgsprocessingparameters.cpp +++ b/src/core/processing/qgsprocessingparameters.cpp @@ -1237,6 +1237,11 @@ bool QgsProcessingParameterDefinition::fromVariantMap( const QVariantMap &map ) return true; } +QgsProcessingAlgorithm *QgsProcessingParameterDefinition::algorithm() const +{ + return mAlgorithm; +} + QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional ) : QgsProcessingParameterDefinition( name, description, defaultValue, optional ) {} diff --git a/src/core/processing/qgsprocessingparameters.h b/src/core/processing/qgsprocessingparameters.h index 7506fc540be..c28ed09aade 100644 --- a/src/core/processing/qgsprocessingparameters.h +++ b/src/core/processing/qgsprocessingparameters.h @@ -391,6 +391,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition */ virtual QStringList dependsOnOtherParameters() const { return QStringList(); } + /** + * Returns a pointer to the algorithm which owns this parameter. May be nullptr + * for non-owned parameters. + */ + QgsProcessingAlgorithm *algorithm() const; + protected: //! Parameter name @@ -408,6 +414,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition //! Freeform metadata for parameter. Mostly used by widget wrappers to customise their appearance and behavior. QVariantMap mMetadata; + //! Pointer to algorithm which owns this parameter + QgsProcessingAlgorithm *mAlgorithm = nullptr; + + // To allow access to mAlgorithm. We don't want a public setter for this! + friend class QgsProcessingAlgorithm; + }; Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingParameterDefinition::Flags ) diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index c484253ae66..2f8ac9a085d 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -75,6 +75,7 @@ class DummyAlgorithm : public QgsProcessingAlgorithm QVERIFY( addParameter( new QgsProcessingParameterBoolean( "p1" ) ) ); QCOMPARE( parameterDefinitions().count(), 1 ); QCOMPARE( parameterDefinitions().at( 0 )->name(), QString( "p1" ) ); + QCOMPARE( parameterDefinitions().at( 0 )->algorithm(), this ); QVERIFY( !addParameter( nullptr ) ); QCOMPARE( parameterDefinitions().count(), 1 );