Skip to content

Commit d2ccd82

Browse files
authoredMay 14, 2021
add utility methods to convert map of QgsProperty to/from QVariantMap (#43178)
* add utility methods to convert map of QgsProperty to/from QVariantMap * add test * fix test * fix test
1 parent a57a8dc commit d2ccd82

File tree

7 files changed

+78
-47
lines changed

7 files changed

+78
-47
lines changed
 

‎python/core/auto_generated/qgsproperty.sip.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ can be grouped using a :py:class:`QgsPropertyCollection` for easier bulk storage
195195
ExpressionBasedProperty,
196196
};
197197

198+
static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
199+
%Docstring
200+
Convert a map of QgsProperty to a map of QVariant
201+
This is useful to save a map of properties
202+
203+
.. versionadded:: 3.20
204+
%End
205+
206+
static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
207+
%Docstring
208+
Convert a map of QVariant to a map of QgsProperty
209+
This is useful to restore a map of properties.
210+
The properties are created using QgsProperty.loadVariant
211+
212+
.. versionadded:: 3.20
213+
%End
214+
198215
QgsProperty();
199216
%Docstring
200217
Constructor for a QgsProperty. The property will be set to an InvalidProperty type.

‎src/core/layout/qgslayoutitempicture.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -626,27 +626,13 @@ QString QgsLayoutItemPicture::evaluatedPath() const
626626
QMap<QString, QgsProperty> QgsLayoutItemPicture::svgDynamicParameters() const
627627
{
628628
const QVariantMap parameters = mCustomProperties.value( QStringLiteral( "svg-dynamic-parameters" ), QVariantMap() ).toMap();
629-
630-
QMap<QString, QgsProperty> parametersProperties;
631-
QVariantMap::const_iterator it = parameters.constBegin();
632-
for ( ; it != parameters.constEnd(); ++it )
633-
{
634-
QgsProperty property;
635-
if ( property.loadVariant( it.value() ) )
636-
parametersProperties.insert( it.key(), property );
637-
}
638-
639-
return parametersProperties;
629+
return QgsProperty::variantMapToPropertyMap( parameters );
640630
}
641631

642632
void QgsLayoutItemPicture::setSvgDynamicParameters( const QMap<QString, QgsProperty> &parameters )
643633
{
644-
QVariantMap variantParameters;
645-
QMap<QString, QgsProperty>::const_iterator it = parameters.constBegin();
646-
for ( ; it != parameters.constEnd(); ++it )
647-
variantParameters.insert( it.key(), it.value().toVariant() );
648-
649-
mCustomProperties.setValue( QStringLiteral( "svg-dynamic-parameters" ), variantParameters );
634+
const QVariantMap variantMap = QgsProperty::propertyMapToVariantMap( parameters );
635+
mCustomProperties.setValue( QStringLiteral( "svg-dynamic-parameters" ), variantMap );
650636
refreshPicture();
651637
}
652638

‎src/core/qgsproperty.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ QString QgsPropertyDefinition::trString()
204204
// QgsProperty
205205
//
206206

207+
QVariantMap QgsProperty::propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap )
208+
{
209+
QVariantMap variantMap;
210+
QMap<QString, QgsProperty>::const_iterator it = propertyMap.constBegin();
211+
for ( ; it != propertyMap.constEnd(); ++it )
212+
variantMap.insert( it.key(), it.value().toVariant() );
213+
return variantMap;
214+
}
215+
216+
QMap<QString, QgsProperty> QgsProperty::variantMapToPropertyMap( const QVariantMap &variantMap )
217+
{
218+
QMap<QString, QgsProperty> propertyMap;
219+
QVariantMap::const_iterator it = variantMap.constBegin();
220+
for ( ; it != variantMap.constEnd(); ++it )
221+
{
222+
QgsProperty property;
223+
if ( property.loadVariant( it.value() ) )
224+
propertyMap.insert( it.key(), property );
225+
}
226+
return propertyMap;
227+
}
228+
207229
QgsProperty::QgsProperty()
208230
{
209231
d = new QgsPropertyPrivate();

‎src/core/qgsproperty.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ class CORE_EXPORT QgsProperty
241241
ExpressionBasedProperty, //!< Expression based property (QgsExpressionBasedProperty)
242242
};
243243

244+
/**
245+
* Convert a map of QgsProperty to a map of QVariant
246+
* This is useful to save a map of properties
247+
* \since QGIS 3.20
248+
*/
249+
static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
250+
251+
/**
252+
* Convert a map of QVariant to a map of QgsProperty
253+
* This is useful to restore a map of properties.
254+
* The properties are created using QgsProperty::loadVariant
255+
* \since QGIS 3.20
256+
*/
257+
static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
258+
244259
/**
245260
* Constructor for a QgsProperty. The property will be set to an InvalidProperty type.
246261
*/

‎src/core/symbology/qgsfillsymbollayer.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,16 +2010,7 @@ QgsSymbolLayer *QgsSVGFillSymbolLayer::create( const QVariantMap &properties )
20102010
if ( properties.contains( QStringLiteral( "parameters" ) ) )
20112011
{
20122012
const QVariantMap parameters = properties[QStringLiteral( "parameters" )].toMap();
2013-
QMap<QString, QgsProperty> parametersProperties;
2014-
QVariantMap::const_iterator it = parameters.constBegin();
2015-
for ( ; it != parameters.constEnd(); ++it )
2016-
{
2017-
QgsProperty property;
2018-
if ( property.loadVariant( it.value() ) )
2019-
parametersProperties.insert( it.key(), property );
2020-
}
2021-
2022-
symbolLayer->setParameters( parametersProperties );
2013+
symbolLayer->setParameters( QgsProperty::variantMapToPropertyMap( parameters ) );
20232014
}
20242015

20252016
symbolLayer->restoreOldDataDefinedProperties( properties );
@@ -2145,11 +2136,7 @@ QVariantMap QgsSVGFillSymbolLayer::properties() const
21452136
map.insert( QStringLiteral( "outline_width_unit" ), QgsUnitTypes::encodeUnit( mStrokeWidthUnit ) );
21462137
map.insert( QStringLiteral( "outline_width_map_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mStrokeWidthMapUnitScale ) );
21472138

2148-
QVariantMap parameters;
2149-
QMap<QString, QgsProperty>::const_iterator it = mParameters.constBegin();
2150-
for ( ; it != mParameters.constEnd(); ++it )
2151-
parameters.insert( it.key(), it.value().toVariant() );
2152-
map[QStringLiteral( "parameters" )] = parameters;
2139+
map[QStringLiteral( "parameters" )] = QgsProperty::propertyMapToVariantMap( mParameters );
21532140

21542141
return map;
21552142
}

‎src/core/symbology/qgsmarkersymbollayer.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,16 +2028,7 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::create( const QVariantMap &props )
20282028
if ( props.contains( QStringLiteral( "parameters" ) ) )
20292029
{
20302030
const QVariantMap parameters = props[QStringLiteral( "parameters" )].toMap();
2031-
QMap<QString, QgsProperty> parametersProperties;
2032-
QVariantMap::const_iterator it = parameters.constBegin();
2033-
for ( ; it != parameters.constEnd(); ++it )
2034-
{
2035-
QgsProperty property;
2036-
if ( property.loadVariant( it.value() ) )
2037-
parametersProperties.insert( it.key(), property );
2038-
}
2039-
2040-
m->setParameters( parametersProperties );
2031+
m->setParameters( QgsProperty::variantMapToPropertyMap( parameters ) );
20412032
}
20422033

20432034
return m;
@@ -2418,11 +2409,7 @@ QVariantMap QgsSvgMarkerSymbolLayer::properties() const
24182409
map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
24192410
map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
24202411

2421-
QVariantMap parameters;
2422-
QMap<QString, QgsProperty>::const_iterator it = mParameters.constBegin();
2423-
for ( ; it != mParameters.constEnd(); ++it )
2424-
parameters.insert( it.key(), it.value().toVariant() );
2425-
map[QStringLiteral( "parameters" )] = parameters;
2412+
map[QStringLiteral( "parameters" )] = QgsProperty::propertyMapToVariantMap( mParameters );
24262413

24272414
return map;
24282415
}

‎tests/src/core/testqgsproperty.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class TestQgsProperty : public QObject
9595
void asVariant();
9696
void isProjectColor();
9797
void referencedFieldsIgnoreContext();
98+
void mapToMap();
9899

99100
private:
100101

@@ -1941,5 +1942,21 @@ void TestQgsProperty::checkCurveResult( const QList<QgsPointXY> &controlPoints,
19411942
}
19421943
}
19431944

1945+
void TestQgsProperty::mapToMap()
1946+
{
1947+
QgsProperty p1 = QgsProperty::fromExpression( "project_color('burnt marigold')" );
1948+
QgsProperty p2 = QgsProperty::fromValue( 1 );
1949+
1950+
QMap<QString, QgsProperty> propertyMap;
1951+
propertyMap.insert( "key1", p1 );
1952+
propertyMap.insert( "key2", p2 );
1953+
1954+
const QVariantMap variantMap = QgsProperty::propertyMapToVariantMap( propertyMap );
1955+
QCOMPARE( variantMap.value( "key1" ).toMap().value( "expression" ).toString(), "project_color('burnt marigold')" );
1956+
QCOMPARE( variantMap.value( "key2" ).toMap().value( "val" ).toInt(), 1 );
1957+
1958+
QCOMPARE( QgsProperty::variantMapToPropertyMap( variantMap ), propertyMap );
1959+
}
1960+
19441961
QGSTEST_MAIN( TestQgsProperty )
19451962
#include "testqgsproperty.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.