From 921939e4ec00bd69b335ec0d245b055c3d27505c Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 3 Jul 2017 10:50:35 +1000 Subject: [PATCH] Add method to determine whether dependencies exist between model parameters --- .../core/processing/qgsprocessingmodelalgorithm.sip | 10 ++++++++++ src/core/processing/qgsprocessingmodelalgorithm.cpp | 13 +++++++++++++ src/core/processing/qgsprocessingmodelalgorithm.h | 9 +++++++++ tests/src/core/testqgsprocessing.cpp | 10 ++++++++++ 4 files changed, 42 insertions(+) diff --git a/python/core/processing/qgsprocessingmodelalgorithm.sip b/python/core/processing/qgsprocessingmodelalgorithm.sip index 1d84a169eb5..8d4b741229c 100644 --- a/python/core/processing/qgsprocessingmodelalgorithm.sip +++ b/python/core/processing/qgsprocessingmodelalgorithm.sip @@ -724,6 +724,16 @@ Copies are protected to avoid slicing %Docstring Returns true if any child algorithms depend on the model parameter with the specified ``name``. +.. seealso:: otherParametersDependOnParameter() + :rtype: bool +%End + + bool otherParametersDependOnParameter( const QString &name ) const; +%Docstring + Returns true if any other model parameters depend on the parameter + with the specified ``name`` (e.g. field parameters where ``name`` + is the parent layer parameter). +.. seealso:: childAlgorithmsDependOnParameter() :rtype: bool %End diff --git a/src/core/processing/qgsprocessingmodelalgorithm.cpp b/src/core/processing/qgsprocessingmodelalgorithm.cpp index 1e7c0ecde48..87f45ffe223 100644 --- a/src/core/processing/qgsprocessingmodelalgorithm.cpp +++ b/src/core/processing/qgsprocessingmodelalgorithm.cpp @@ -949,6 +949,19 @@ bool QgsProcessingModelAlgorithm::childAlgorithmsDependOnParameter( const QStrin return false; } +bool QgsProcessingModelAlgorithm::otherParametersDependOnParameter( const QString &name ) const +{ + Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters ) + { + if ( def->name() == name ) + continue; + + if ( def->dependsOnOtherParameters().contains( name ) ) + return true; + } + return false; +} + QMap QgsProcessingModelAlgorithm::parameterComponents() const { return mParameterComponents; diff --git a/src/core/processing/qgsprocessingmodelalgorithm.h b/src/core/processing/qgsprocessingmodelalgorithm.h index ac2873f4102..0cb3c0a4bda 100644 --- a/src/core/processing/qgsprocessingmodelalgorithm.h +++ b/src/core/processing/qgsprocessingmodelalgorithm.h @@ -718,9 +718,18 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm /** * Returns true if any child algorithms depend on the model parameter * with the specified \a name. + * \see otherParametersDependOnParameter() */ bool childAlgorithmsDependOnParameter( const QString &name ) const; + /** + * Returns true if any other model parameters depend on the parameter + * with the specified \a name (e.g. field parameters where \a name + * is the parent layer parameter). + * \see childAlgorithmsDependOnParameter() + */ + bool otherParametersDependOnParameter( const QString &name ) const; + /** * Returns the map of parameter components used by the model. The keys * should match the algorithm's parameter names (see parameterDefinitions() ). diff --git a/tests/src/core/testqgsprocessing.cpp b/tests/src/core/testqgsprocessing.cpp index 845341e229a..4e7855176cf 100644 --- a/tests/src/core/testqgsprocessing.cpp +++ b/tests/src/core/testqgsprocessing.cpp @@ -4458,6 +4458,16 @@ void TestQgsProcessing::modelerAlgorithm() alg4.setChildAlgorithm( c10 ); QVERIFY( alg4.childAlgorithmsDependOnParameter( "p1" ) ); + QgsProcessingModelAlgorithm::ModelParameter vlP; + alg4.addModelParameter( new QgsProcessingParameterVectorLayer( "layer" ), vlP ); + QgsProcessingModelAlgorithm::ModelParameter field; + alg4.addModelParameter( new QgsProcessingParameterField( "field", QString(), QVariant(), QStringLiteral( "layer" ) ), field ); + QVERIFY( !alg4.otherParametersDependOnParameter( "p1" ) ); + QVERIFY( !alg4.otherParametersDependOnParameter( "field" ) ); + QVERIFY( alg4.otherParametersDependOnParameter( "layer" ) ); + + + // to/from XML