Skip to content

Commit d924b85

Browse files
authored
Merge pull request #4806 from nyalldawson/dependent_params
[modeler] Don't allow removal of model parameters on which other parameters depend
2 parents d4f5ecc + 37cc8fc commit d924b85

File tree

8 files changed

+91
-2
lines changed

8 files changed

+91
-2
lines changed

python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,16 @@ Copies are protected to avoid slicing
724724
%Docstring
725725
Returns true if any child algorithms depend on the model parameter
726726
with the specified ``name``.
727+
.. seealso:: otherParametersDependOnParameter()
728+
:rtype: bool
729+
%End
730+
731+
bool otherParametersDependOnParameter( const QString &name ) const;
732+
%Docstring
733+
Returns true if any other model parameters depend on the parameter
734+
with the specified ``name`` (e.g. field parameters where ``name``
735+
is the parent layer parameter).
736+
.. seealso:: childAlgorithmsDependOnParameter()
727737
:rtype: bool
728738
%End
729739

python/core/processing/qgsprocessingparameters.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ class QgsProcessingParameterDefinition
338338
.. seealso:: metadata()
339339
%End
340340

341+
virtual QStringList dependsOnOtherParameters() const;
342+
%Docstring
343+
Returns a list of other parameter names on which this parameter is dependent (e.g.
344+
field parameters which depend on a parent layer parameter).
345+
:rtype: list of str
346+
%End
347+
341348
protected:
342349

343350

@@ -1261,6 +1268,8 @@ class QgsProcessingParameterExpression : QgsProcessingParameterDefinition
12611268
virtual QString type() const;
12621269
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
12631270

1271+
virtual QStringList dependsOnOtherParameters() const;
1272+
12641273

12651274
QString parentLayerParameter() const;
12661275
%Docstring
@@ -1357,6 +1366,8 @@ class QgsProcessingParameterField : QgsProcessingParameterDefinition
13571366

13581367
virtual QString asScriptCode() const;
13591368

1369+
virtual QStringList dependsOnOtherParameters() const;
1370+
13601371

13611372
QString parentLayerParameter() const;
13621373
%Docstring

python/plugins/processing/modeler/ModelerGraphicItem.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,12 @@ def updateAlgorithm(self, alg):
229229
def removeElement(self):
230230
if isinstance(self.element, QgsProcessingModelAlgorithm.ModelParameter):
231231
if self.model.childAlgorithmsDependOnParameter(self.element.parameterName()):
232-
QMessageBox.warning(None, 'Could not remove element',
233-
'Other elements depend on the selected one.\n'
232+
QMessageBox.warning(None, 'Could not remove input',
233+
'Algorithms depend on the selected input.\n'
234+
'Remove them before trying to remove it.')
235+
elif self.model.otherParametersDependOnParameter(self.element.parameterName()):
236+
QMessageBox.warning(None, 'Could not remove input',
237+
'Other inputs depend on the selected input.\n'
234238
'Remove them before trying to remove it.')
235239
else:
236240
self.model.removeModelParameter(self.element.parameterName())

src/core/processing/qgsprocessingmodelalgorithm.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,19 @@ bool QgsProcessingModelAlgorithm::childAlgorithmsDependOnParameter( const QStrin
949949
return false;
950950
}
951951

952+
bool QgsProcessingModelAlgorithm::otherParametersDependOnParameter( const QString &name ) const
953+
{
954+
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
955+
{
956+
if ( def->name() == name )
957+
continue;
958+
959+
if ( def->dependsOnOtherParameters().contains( name ) )
960+
return true;
961+
}
962+
return false;
963+
}
964+
952965
QMap<QString, QgsProcessingModelAlgorithm::ModelParameter> QgsProcessingModelAlgorithm::parameterComponents() const
953966
{
954967
return mParameterComponents;

src/core/processing/qgsprocessingmodelalgorithm.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,18 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
718718
/**
719719
* Returns true if any child algorithms depend on the model parameter
720720
* with the specified \a name.
721+
* \see otherParametersDependOnParameter()
721722
*/
722723
bool childAlgorithmsDependOnParameter( const QString &name ) const;
723724

725+
/**
726+
* Returns true if any other model parameters depend on the parameter
727+
* with the specified \a name (e.g. field parameters where \a name
728+
* is the parent layer parameter).
729+
* \see childAlgorithmsDependOnParameter()
730+
*/
731+
bool otherParametersDependOnParameter( const QString &name ) const;
732+
724733
/**
725734
* Returns the map of parameter components used by the model. The keys
726735
* should match the algorithm's parameter names (see parameterDefinitions() ).

src/core/processing/qgsprocessingparameters.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,14 @@ QString QgsProcessingParameterExpression::valueAsPythonString( const QVariant &v
20472047
return s.prepend( '\'' ).append( '\'' );
20482048
}
20492049

2050+
QStringList QgsProcessingParameterExpression::dependsOnOtherParameters() const
2051+
{
2052+
QStringList depends;
2053+
if ( !mParentLayerParameter.isEmpty() )
2054+
depends << mParentLayerParameter;
2055+
return depends;
2056+
}
2057+
20502058
QString QgsProcessingParameterExpression::parentLayerParameter() const
20512059
{
20522060
return mParentLayerParameter;
@@ -2229,6 +2237,14 @@ QString QgsProcessingParameterField::asScriptCode() const
22292237
return code.trimmed();
22302238
}
22312239

2240+
QStringList QgsProcessingParameterField::dependsOnOtherParameters() const
2241+
{
2242+
QStringList depends;
2243+
if ( !mParentLayerParameter.isEmpty() )
2244+
depends << mParentLayerParameter;
2245+
return depends;
2246+
}
2247+
22322248
QString QgsProcessingParameterField::parentLayerParameter() const
22332249
{
22342250
return mParentLayerParameter;

src/core/processing/qgsprocessingparameters.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition
373373
*/
374374
void setMetadata( const QVariantMap &metadata ) { mMetadata = metadata; }
375375

376+
/**
377+
* Returns a list of other parameter names on which this parameter is dependent (e.g.
378+
* field parameters which depend on a parent layer parameter).
379+
*/
380+
virtual QStringList dependsOnOtherParameters() const { return QStringList(); }
381+
376382
protected:
377383

378384
//! Parameter name
@@ -1219,6 +1225,7 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
12191225

12201226
QString type() const override { return QStringLiteral( "expression" ); }
12211227
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
1228+
QStringList dependsOnOtherParameters() const override;
12221229

12231230
/**
12241231
* Returns the name of the parent layer parameter, or an empty string if this is not set.
@@ -1306,6 +1313,7 @@ class CORE_EXPORT QgsProcessingParameterField : public QgsProcessingParameterDef
13061313
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
13071314
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
13081315
QString asScriptCode() const override;
1316+
QStringList dependsOnOtherParameters() const override;
13091317

13101318
/**
13111319
* Returns the name of the parent layer parameter, or an empty string if this is not set.

tests/src/core/testqgsprocessing.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ void TestQgsProcessing::parameterGeneral()
13141314
QCOMPARE( param.description(), QString( "desc" ) );
13151315
QCOMPARE( param.defaultValue(), QVariant( true ) );
13161316
QVERIFY( param.flags() & QgsProcessingParameterDefinition::FlagOptional );
1317+
QVERIFY( param.dependsOnOtherParameters().isEmpty() );
13171318

13181319
// test getters and setters
13191320
param.setDescription( "p2" );
@@ -2951,6 +2952,10 @@ void TestQgsProcessing::parameterExpression()
29512952
def.reset( dynamic_cast< QgsProcessingParameterExpression *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
29522953
QVERIFY( dynamic_cast< QgsProcessingParameterExpression *>( def.get() ) );
29532954

2955+
QVERIFY( def->dependsOnOtherParameters().isEmpty() );
2956+
def->setParentLayerParameter( QStringLiteral( "test_layer" ) );
2957+
QCOMPARE( def->dependsOnOtherParameters(), QStringList() << QStringLiteral( "test_layer" ) );
2958+
29542959
// optional
29552960
def.reset( new QgsProcessingParameterExpression( "optional", QString(), QString( "default" ), QString(), true ) );
29562961
QVERIFY( def->checkValueIsAcceptable( 1 ) );
@@ -3011,7 +3016,10 @@ void TestQgsProcessing::parameterField()
30113016
QCOMPARE( fromCode->dataType(), def->dataType() );
30123017
QCOMPARE( fromCode->allowMultiple(), def->allowMultiple() );
30133018

3019+
QVERIFY( def->dependsOnOtherParameters().isEmpty() );
30143020
def->setParentLayerParameter( "my_parent" );
3021+
QCOMPARE( def->dependsOnOtherParameters(), QStringList() << QStringLiteral( "my_parent" ) );
3022+
30153023
code = def->asScriptCode();
30163024
fromCode.reset( dynamic_cast< QgsProcessingParameterField * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
30173025
QVERIFY( fromCode.get() );
@@ -4450,6 +4458,16 @@ void TestQgsProcessing::modelerAlgorithm()
44504458
alg4.setChildAlgorithm( c10 );
44514459
QVERIFY( alg4.childAlgorithmsDependOnParameter( "p1" ) );
44524460

4461+
QgsProcessingModelAlgorithm::ModelParameter vlP;
4462+
alg4.addModelParameter( new QgsProcessingParameterVectorLayer( "layer" ), vlP );
4463+
QgsProcessingModelAlgorithm::ModelParameter field;
4464+
alg4.addModelParameter( new QgsProcessingParameterField( "field", QString(), QVariant(), QStringLiteral( "layer" ) ), field );
4465+
QVERIFY( !alg4.otherParametersDependOnParameter( "p1" ) );
4466+
QVERIFY( !alg4.otherParametersDependOnParameter( "field" ) );
4467+
QVERIFY( alg4.otherParametersDependOnParameter( "layer" ) );
4468+
4469+
4470+
44534471

44544472

44554473
// to/from XML

0 commit comments

Comments
 (0)