Skip to content
Permalink
Browse files

Allow storing arbitrary metadata in parameter definitions

  • Loading branch information
nyalldawson committed Jun 22, 2017
1 parent 33aa798 commit f304828f59f1134209a98c13943098656870d516
@@ -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);
@@ -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;
}

@@ -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;
}

@@ -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
@@ -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 )
@@ -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()

0 comments on commit f304828

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