Skip to content

Commit 6483984

Browse files
committed
Add method to retrieve dependent parameters for a parameter
1 parent d4f5ecc commit 6483984

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

python/core/processing/qgsprocessingparameters.sip

+11
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

src/core/processing/qgsprocessingparameters.cpp

+16
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

+8
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

+8
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() );

0 commit comments

Comments
 (0)