Skip to content
Permalink
Browse files

Add method to retrieve dependent parameters for a parameter

  • Loading branch information
nyalldawson committed Jul 3, 2017
1 parent d4f5ecc commit 6483984d0b5ed5b29c6142b46890d55a3e2f97f1
@@ -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:


@@ -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
@@ -1357,6 +1366,8 @@ class QgsProcessingParameterField : QgsProcessingParameterDefinition

virtual QString asScriptCode() const;

virtual QStringList dependsOnOtherParameters() const;


QString parentLayerParameter() const;
%Docstring
@@ -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;
@@ -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;
@@ -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
@@ -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.
@@ -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.
@@ -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" );
@@ -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 ) );
@@ -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() );

0 comments on commit 6483984

Please sign in to comment.
You can’t perform that action at this time.