Skip to content

Commit

Permalink
Add method to retrieve dependent parameters for a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 3, 2017
1 parent d4f5ecc commit 6483984
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -338,6 +338,13 @@ class QgsProcessingParameterDefinition
.. seealso:: metadata()
%End

virtual QStringList dependsOnOtherParameters() const;
%Docstring
Returns a list of other parameter names on which this parameter is dependent (e.g.
field parameters which depend on a parent layer parameter).
:rtype: list of str
%End

protected:


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

virtual QStringList dependsOnOtherParameters() const;


QString parentLayerParameter() const;
%Docstring
Expand Down Expand Up @@ -1357,6 +1366,8 @@ class QgsProcessingParameterField : QgsProcessingParameterDefinition

virtual QString asScriptCode() const;

virtual QStringList dependsOnOtherParameters() const;


QString parentLayerParameter() const;
%Docstring
Expand Down
16 changes: 16 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2047,6 +2047,14 @@ QString QgsProcessingParameterExpression::valueAsPythonString( const QVariant &v
return s.prepend( '\'' ).append( '\'' );
}

QStringList QgsProcessingParameterExpression::dependsOnOtherParameters() const
{
QStringList depends;
if ( !mParentLayerParameter.isEmpty() )
depends << mParentLayerParameter;
return depends;
}

QString QgsProcessingParameterExpression::parentLayerParameter() const
{
return mParentLayerParameter;
Expand Down Expand Up @@ -2229,6 +2237,14 @@ QString QgsProcessingParameterField::asScriptCode() const
return code.trimmed();
}

QStringList QgsProcessingParameterField::dependsOnOtherParameters() const
{
QStringList depends;
if ( !mParentLayerParameter.isEmpty() )
depends << mParentLayerParameter;
return depends;
}

QString QgsProcessingParameterField::parentLayerParameter() const
{
return mParentLayerParameter;
Expand Down
8 changes: 8 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -373,6 +373,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
void setMetadata( const QVariantMap &metadata ) { mMetadata = metadata; }

/**
* Returns a list of other parameter names on which this parameter is dependent (e.g.
* field parameters which depend on a parent layer parameter).
*/
virtual QStringList dependsOnOtherParameters() const { return QStringList(); }

protected:

//! Parameter name
Expand Down Expand Up @@ -1219,6 +1225,7 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet

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

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

/**
* Returns the name of the parent layer parameter, or an empty string if this is not set.
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -1314,6 +1314,7 @@ void TestQgsProcessing::parameterGeneral()
QCOMPARE( param.description(), QString( "desc" ) );
QCOMPARE( param.defaultValue(), QVariant( true ) );
QVERIFY( param.flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( param.dependsOnOtherParameters().isEmpty() );

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

QVERIFY( def->dependsOnOtherParameters().isEmpty() );
def->setParentLayerParameter( QStringLiteral( "test_layer" ) );
QCOMPARE( def->dependsOnOtherParameters(), QStringList() << QStringLiteral( "test_layer" ) );

// optional
def.reset( new QgsProcessingParameterExpression( "optional", QString(), QString( "default" ), QString(), true ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
Expand Down Expand Up @@ -3011,7 +3016,10 @@ void TestQgsProcessing::parameterField()
QCOMPARE( fromCode->dataType(), def->dataType() );
QCOMPARE( fromCode->allowMultiple(), def->allowMultiple() );

QVERIFY( def->dependsOnOtherParameters().isEmpty() );
def->setParentLayerParameter( "my_parent" );
QCOMPARE( def->dependsOnOtherParameters(), QStringList() << QStringLiteral( "my_parent" ) );

code = def->asScriptCode();
fromCode.reset( dynamic_cast< QgsProcessingParameterField * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
Expand Down

0 comments on commit 6483984

Please sign in to comment.