Skip to content

Commit

Permalink
Allow storing arbitrary metadata in parameter definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 22, 2017
1 parent 33aa798 commit f304828
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -315,12 +315,29 @@ class QgsProcessingParameterDefinition
:rtype: bool
%End


QVariantMap &metadata();
%Docstring
Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
in order to customise their appearance and behavior.
.. seealso:: setMetadata()
:rtype: QVariantMap
%End

void setMetadata( const QVariantMap &metadata );
%Docstring
Sets the parameter's freeform ``metadata``. This is mostly used by parameter widget wrappers
in order to customise their appearance and behavior.
.. seealso:: metadata()
%End

protected:






};

QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -808,6 +808,7 @@ QVariantMap QgsProcessingParameterDefinition::toVariantMap() const
map.insert( QStringLiteral( "description" ), mDescription );
map.insert( QStringLiteral( "default" ), mDefault );
map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
map.insert( QStringLiteral( "metadata" ), mMetadata );
return map;
}

Expand All @@ -817,6 +818,7 @@ bool QgsProcessingParameterDefinition::fromVariantMap( const QVariantMap &map )
mDescription = map.value( QStringLiteral( "description" ) ).toString();
mDefault = map.value( QStringLiteral( "default" ) );
mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
return true;
}

Expand Down
25 changes: 25 additions & 0 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -344,6 +344,28 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual bool fromVariantMap( const QVariantMap &map );

/**
* Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
* in order to customise their appearance and behavior.
* \see setMetadata()
* \note not available in Python bindings.
*/
SIP_SKIP QVariantMap metadata() const { return mMetadata; }

/**
* Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
* in order to customise their appearance and behavior.
* \see setMetadata()
*/
QVariantMap &metadata() { return mMetadata; }

/**
* Sets the parameter's freeform \a metadata. This is mostly used by parameter widget wrappers
* in order to customise their appearance and behavior.
* \see metadata()
*/
void setMetadata( const QVariantMap &metadata ) { mMetadata = metadata; }

protected:

//! Parameter name
Expand All @@ -358,6 +380,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition
//! Parameter flags
Flags mFlags;

//! Freeform metadata for parameter. Mostly used by widget wrappers to customise their appearance and behavior.
QVariantMap mMetadata;

};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingParameterDefinition::Flags )
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -1321,6 +1321,23 @@ void TestQgsProcessing::parameterGeneral()
QCOMPARE( param.defaultValue(), QVariant( true ) );
param.setDefaultValue( QVariant() );
QCOMPARE( param.defaultValue(), QVariant() );

QVariantMap metadata;
metadata.insert( "p1", 5 );
metadata.insert( "p2", 7 );
param.setMetadata( metadata );
QCOMPARE( param.metadata(), metadata );
param.metadata().insert( "p3", 9 );
QCOMPARE( param.metadata().value( "p3" ).toInt(), 9 );

QVariantMap map = param.toVariantMap();
QgsProcessingParameterBoolean fromMap( "x" );
QVERIFY( fromMap.fromVariantMap( map ) );
QCOMPARE( fromMap.name(), param.name() );
QCOMPARE( fromMap.description(), param.description() );
QCOMPARE( fromMap.flags(), param.flags() );
QCOMPARE( fromMap.defaultValue(), param.defaultValue() );
QCOMPARE( fromMap.metadata(), param.metadata() );
}

void TestQgsProcessing::parameterBoolean()
Expand Down

0 comments on commit f304828

Please sign in to comment.