Skip to content

Commit f304828

Browse files
committedJun 22, 2017
Allow storing arbitrary metadata in parameter definitions
1 parent 33aa798 commit f304828

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
 

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,29 @@ class QgsProcessingParameterDefinition
315315
:rtype: bool
316316
%End
317317

318+
319+
QVariantMap &metadata();
320+
%Docstring
321+
Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
322+
in order to customise their appearance and behavior.
323+
.. seealso:: setMetadata()
324+
:rtype: QVariantMap
325+
%End
326+
327+
void setMetadata( const QVariantMap &metadata );
328+
%Docstring
329+
Sets the parameter's freeform ``metadata``. This is mostly used by parameter widget wrappers
330+
in order to customise their appearance and behavior.
331+
.. seealso:: metadata()
332+
%End
333+
318334
protected:
319335

320336

321337

322338

323339

340+
324341
};
325342

326343
QFlags<QgsProcessingParameterDefinition::Flag> operator|(QgsProcessingParameterDefinition::Flag f1, QFlags<QgsProcessingParameterDefinition::Flag> f2);

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ QVariantMap QgsProcessingParameterDefinition::toVariantMap() const
808808
map.insert( QStringLiteral( "description" ), mDescription );
809809
map.insert( QStringLiteral( "default" ), mDefault );
810810
map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
811+
map.insert( QStringLiteral( "metadata" ), mMetadata );
811812
return map;
812813
}
813814

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

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,28 @@ class CORE_EXPORT QgsProcessingParameterDefinition
344344
*/
345345
virtual bool fromVariantMap( const QVariantMap &map );
346346

347+
/**
348+
* Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
349+
* in order to customise their appearance and behavior.
350+
* \see setMetadata()
351+
* \note not available in Python bindings.
352+
*/
353+
SIP_SKIP QVariantMap metadata() const { return mMetadata; }
354+
355+
/**
356+
* Returns the parameter's freeform metadata. This is mostly used by parameter widget wrappers
357+
* in order to customise their appearance and behavior.
358+
* \see setMetadata()
359+
*/
360+
QVariantMap &metadata() { return mMetadata; }
361+
362+
/**
363+
* Sets the parameter's freeform \a metadata. This is mostly used by parameter widget wrappers
364+
* in order to customise their appearance and behavior.
365+
* \see metadata()
366+
*/
367+
void setMetadata( const QVariantMap &metadata ) { mMetadata = metadata; }
368+
347369
protected:
348370

349371
//! Parameter name
@@ -358,6 +380,9 @@ class CORE_EXPORT QgsProcessingParameterDefinition
358380
//! Parameter flags
359381
Flags mFlags;
360382

383+
//! Freeform metadata for parameter. Mostly used by widget wrappers to customise their appearance and behavior.
384+
QVariantMap mMetadata;
385+
361386
};
362387

363388
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingParameterDefinition::Flags )

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,23 @@ void TestQgsProcessing::parameterGeneral()
13211321
QCOMPARE( param.defaultValue(), QVariant( true ) );
13221322
param.setDefaultValue( QVariant() );
13231323
QCOMPARE( param.defaultValue(), QVariant() );
1324+
1325+
QVariantMap metadata;
1326+
metadata.insert( "p1", 5 );
1327+
metadata.insert( "p2", 7 );
1328+
param.setMetadata( metadata );
1329+
QCOMPARE( param.metadata(), metadata );
1330+
param.metadata().insert( "p3", 9 );
1331+
QCOMPARE( param.metadata().value( "p3" ).toInt(), 9 );
1332+
1333+
QVariantMap map = param.toVariantMap();
1334+
QgsProcessingParameterBoolean fromMap( "x" );
1335+
QVERIFY( fromMap.fromVariantMap( map ) );
1336+
QCOMPARE( fromMap.name(), param.name() );
1337+
QCOMPARE( fromMap.description(), param.description() );
1338+
QCOMPARE( fromMap.flags(), param.flags() );
1339+
QCOMPARE( fromMap.defaultValue(), param.defaultValue() );
1340+
QCOMPARE( fromMap.metadata(), param.metadata() );
13241341
}
13251342

13261343
void TestQgsProcessing::parameterBoolean()

0 commit comments

Comments
 (0)
Please sign in to comment.