Skip to content

Commit

Permalink
[processing] Some more framework for dynamic (data defined) parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 2, 2017
1 parent 618baf9 commit ecbc471
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
66 changes: 66 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,69 @@ class QgsProcessingParameterDefinition
:rtype: str
%End

bool isDynamic() const;
%Docstring
Returns true if the parameter supports is dynamic, and can support data-defined values
(i.e. QgsProperty based values).
.. seealso:: setIsDynamic()
.. seealso:: dynamicPropertyDefinition()
.. seealso:: dynamicLayerParameterName()
:rtype: bool
%End

void setIsDynamic( bool dynamic );
%Docstring
Sets whether the parameter is ``dynamic``, and can support data-defined values
(i.e. QgsProperty based values).
.. seealso:: isDynamic()
.. seealso:: setDynamicPropertyDefinition()
.. seealso:: setDynamicLayerParameterName()
%End

QgsPropertyDefinition dynamicPropertyDefinition() const;
%Docstring
Returns the property definition for dynamic properties.
.. seealso:: isDynamic()
.. seealso:: setDynamicPropertyDefinition()
.. seealso:: dynamicLayerParameterName()
:rtype: QgsPropertyDefinition
%End

void setDynamicPropertyDefinition( const QgsPropertyDefinition &definition );
%Docstring
Sets the property ``definition`` for dynamic properties.
.. seealso:: isDynamic()
.. seealso:: dynamicPropertyDefinition()
.. seealso:: setDynamicLayerParameterName()
%End

QString dynamicLayerParameterName() const;
%Docstring
Returns the name of the parameter for a layer linked to a dynamic parameter, or an empty string if this is not set.

Dynamic parameters (see isDynamic()) can have an optional vector layer parameter linked to them,
which indicates which layer the fields and values will be available from when evaluating
the dynamic parameter.

.. seealso:: setDynamicLayerParameterName()
.. seealso:: isDynamic()
.. seealso:: dynamicPropertyDefinition()
:rtype: str
%End

void setDynamicLayerParameterName( const QString &name );
%Docstring
Sets the ``name`` for the parameter for a layer linked to a dynamic parameter, or an empty string if this is not set.

Dynamic parameters (see isDynamic()) can have an optional vector layer parameter linked to them,
which indicates which layer the fields and values will be available from when evaluating
the dynamic parameter.

.. seealso:: dynamicLayerParameterName()
.. seealso:: isDynamic()
.. seealso:: setDynamicPropertyDefinition()
%End

protected:


Expand All @@ -385,6 +448,9 @@ class QgsProcessingParameterDefinition






};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
Expand Down
69 changes: 69 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,66 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual QString toolTip() const;

/**
* Returns true if the parameter supports is dynamic, and can support data-defined values
* (i.e. QgsProperty based values).
* \see setIsDynamic()
* \see dynamicPropertyDefinition()
* \see dynamicLayerParameterName()
*/
bool isDynamic() const { return mIsDynamic; }

/**
* Sets whether the parameter is \a dynamic, and can support data-defined values
* (i.e. QgsProperty based values).
* \see isDynamic()
* \see setDynamicPropertyDefinition()
* \see setDynamicLayerParameterName()
*/
void setIsDynamic( bool dynamic ) { mIsDynamic = dynamic; }

/**
* Returns the property definition for dynamic properties.
* \see isDynamic()
* \see setDynamicPropertyDefinition()
* \see dynamicLayerParameterName()
*/
QgsPropertyDefinition dynamicPropertyDefinition() const { return mPropertyDefinition; }

/**
* Sets the property \a definition for dynamic properties.
* \see isDynamic()
* \see dynamicPropertyDefinition()
* \see setDynamicLayerParameterName()
*/
void setDynamicPropertyDefinition( const QgsPropertyDefinition &definition ) { mPropertyDefinition = definition; }

/**
* Returns the name of the parameter for a layer linked to a dynamic parameter, or an empty string if this is not set.
*
* Dynamic parameters (see isDynamic()) can have an optional vector layer parameter linked to them,
* which indicates which layer the fields and values will be available from when evaluating
* the dynamic parameter.
*
* \see setDynamicLayerParameterName()
* \see isDynamic()
* \see dynamicPropertyDefinition()
*/
QString dynamicLayerParameterName() const { return mDynamicLayerParameterName; }

/**
* Sets the \a name for the parameter for a layer linked to a dynamic parameter, or an empty string if this is not set.
*
* Dynamic parameters (see isDynamic()) can have an optional vector layer parameter linked to them,
* which indicates which layer the fields and values will be available from when evaluating
* the dynamic parameter.
*
* \see dynamicLayerParameterName()
* \see isDynamic()
* \see setDynamicPropertyDefinition()
*/
void setDynamicLayerParameterName( const QString &name ) { mDynamicLayerParameterName = name; }

protected:

//! Parameter name
Expand All @@ -432,6 +492,15 @@ class CORE_EXPORT QgsProcessingParameterDefinition
//! Pointer to algorithm which owns this parameter
QgsProcessingAlgorithm *mAlgorithm = nullptr;

//! True for dynamic parameters, which can have data-defined (QgsProperty) based values
bool mIsDynamic = false;

//! Data defined property definition
QgsPropertyDefinition mPropertyDefinition;

//! Linked vector layer parameter name for dynamic properties
QString mDynamicLayerParameterName;

// To allow access to mAlgorithm. We don't want a public setter for this!
friend class QgsProcessingAlgorithm;

Expand Down
8 changes: 8 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ void TestQgsProcessing::parameters()
// test parameter utilities

std::unique_ptr< QgsProcessingParameterDefinition > def;

QVariantMap params;
params.insert( QStringLiteral( "prop" ), QgsProperty::fromField( "a_field" ) );
params.insert( QStringLiteral( "string" ), QStringLiteral( "a string" ) );
Expand All @@ -1471,6 +1472,13 @@ void TestQgsProcessing::parameters()
def->setName( QStringLiteral( "bad" ) );
QCOMPARE( QgsProcessingParameters::parameterAsString( def.get(), params, context ), QString() );

def->setIsDynamic( true );
QVERIFY( def->isDynamic() );
def->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Buffer distance" ), QgsPropertyDefinition::Double ) );
QCOMPARE( def->dynamicPropertyDefinition().name(), QStringLiteral( "Distance" ) );
def->setDynamicLayerParameterName( "parent" );
QCOMPARE( def->dynamicLayerParameterName(), QStringLiteral( "parent" ) );

// string with dynamic property (feature not set)
def->setName( QStringLiteral( "prop" ) );
QCOMPARE( QgsProcessingParameters::parameterAsString( def.get(), params, context ), QString() );
Expand Down

0 comments on commit ecbc471

Please sign in to comment.