Skip to content

Commit

Permalink
add utility methods to convert map of QgsProperty to/from QVariantMap (
Browse files Browse the repository at this point in the history
…#43178)

* add utility methods to convert map of QgsProperty to/from QVariantMap

* add test

* fix test

* fix test
  • Loading branch information
3nids committed May 14, 2021
1 parent a57a8dc commit d2ccd82
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 47 deletions.
17 changes: 17 additions & 0 deletions python/core/auto_generated/qgsproperty.sip.in
Expand Up @@ -195,6 +195,23 @@ can be grouped using a :py:class:`QgsPropertyCollection` for easier bulk storage
ExpressionBasedProperty,
};

static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );
%Docstring
Convert a map of QgsProperty to a map of QVariant
This is useful to save a map of properties

.. versionadded:: 3.20
%End

static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );
%Docstring
Convert a map of QVariant to a map of QgsProperty
This is useful to restore a map of properties.
The properties are created using QgsProperty.loadVariant

.. versionadded:: 3.20
%End

QgsProperty();
%Docstring
Constructor for a QgsProperty. The property will be set to an InvalidProperty type.
Expand Down
20 changes: 3 additions & 17 deletions src/core/layout/qgslayoutitempicture.cpp
Expand Up @@ -626,27 +626,13 @@ QString QgsLayoutItemPicture::evaluatedPath() const
QMap<QString, QgsProperty> QgsLayoutItemPicture::svgDynamicParameters() const
{
const QVariantMap parameters = mCustomProperties.value( QStringLiteral( "svg-dynamic-parameters" ), QVariantMap() ).toMap();

QMap<QString, QgsProperty> parametersProperties;
QVariantMap::const_iterator it = parameters.constBegin();
for ( ; it != parameters.constEnd(); ++it )
{
QgsProperty property;
if ( property.loadVariant( it.value() ) )
parametersProperties.insert( it.key(), property );
}

return parametersProperties;
return QgsProperty::variantMapToPropertyMap( parameters );
}

void QgsLayoutItemPicture::setSvgDynamicParameters( const QMap<QString, QgsProperty> &parameters )
{
QVariantMap variantParameters;
QMap<QString, QgsProperty>::const_iterator it = parameters.constBegin();
for ( ; it != parameters.constEnd(); ++it )
variantParameters.insert( it.key(), it.value().toVariant() );

mCustomProperties.setValue( QStringLiteral( "svg-dynamic-parameters" ), variantParameters );
const QVariantMap variantMap = QgsProperty::propertyMapToVariantMap( parameters );
mCustomProperties.setValue( QStringLiteral( "svg-dynamic-parameters" ), variantMap );
refreshPicture();
}

Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsproperty.cpp
Expand Up @@ -204,6 +204,28 @@ QString QgsPropertyDefinition::trString()
// QgsProperty
//

QVariantMap QgsProperty::propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap )
{
QVariantMap variantMap;
QMap<QString, QgsProperty>::const_iterator it = propertyMap.constBegin();
for ( ; it != propertyMap.constEnd(); ++it )
variantMap.insert( it.key(), it.value().toVariant() );
return variantMap;
}

QMap<QString, QgsProperty> QgsProperty::variantMapToPropertyMap( const QVariantMap &variantMap )
{
QMap<QString, QgsProperty> propertyMap;
QVariantMap::const_iterator it = variantMap.constBegin();
for ( ; it != variantMap.constEnd(); ++it )
{
QgsProperty property;
if ( property.loadVariant( it.value() ) )
propertyMap.insert( it.key(), property );
}
return propertyMap;
}

QgsProperty::QgsProperty()
{
d = new QgsPropertyPrivate();
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsproperty.h
Expand Up @@ -241,6 +241,21 @@ class CORE_EXPORT QgsProperty
ExpressionBasedProperty, //!< Expression based property (QgsExpressionBasedProperty)
};

/**
* Convert a map of QgsProperty to a map of QVariant
* This is useful to save a map of properties
* \since QGIS 3.20
*/
static QVariantMap propertyMapToVariantMap( const QMap<QString, QgsProperty> &propertyMap );

/**
* Convert a map of QVariant to a map of QgsProperty
* This is useful to restore a map of properties.
* The properties are created using QgsProperty::loadVariant
* \since QGIS 3.20
*/
static QMap<QString, QgsProperty> variantMapToPropertyMap( const QVariantMap &variantMap );

/**
* Constructor for a QgsProperty. The property will be set to an InvalidProperty type.
*/
Expand Down
17 changes: 2 additions & 15 deletions src/core/symbology/qgsfillsymbollayer.cpp
Expand Up @@ -2010,16 +2010,7 @@ QgsSymbolLayer *QgsSVGFillSymbolLayer::create( const QVariantMap &properties )
if ( properties.contains( QStringLiteral( "parameters" ) ) )
{
const QVariantMap parameters = properties[QStringLiteral( "parameters" )].toMap();
QMap<QString, QgsProperty> parametersProperties;
QVariantMap::const_iterator it = parameters.constBegin();
for ( ; it != parameters.constEnd(); ++it )
{
QgsProperty property;
if ( property.loadVariant( it.value() ) )
parametersProperties.insert( it.key(), property );
}

symbolLayer->setParameters( parametersProperties );
symbolLayer->setParameters( QgsProperty::variantMapToPropertyMap( parameters ) );
}

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

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

return map;
}
Expand Down
17 changes: 2 additions & 15 deletions src/core/symbology/qgsmarkersymbollayer.cpp
Expand Up @@ -2028,16 +2028,7 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::create( const QVariantMap &props )
if ( props.contains( QStringLiteral( "parameters" ) ) )
{
const QVariantMap parameters = props[QStringLiteral( "parameters" )].toMap();
QMap<QString, QgsProperty> parametersProperties;
QVariantMap::const_iterator it = parameters.constBegin();
for ( ; it != parameters.constEnd(); ++it )
{
QgsProperty property;
if ( property.loadVariant( it.value() ) )
parametersProperties.insert( it.key(), property );
}

m->setParameters( parametersProperties );
m->setParameters( QgsProperty::variantMapToPropertyMap( parameters ) );
}

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

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

return map;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgsproperty.cpp
Expand Up @@ -95,6 +95,7 @@ class TestQgsProperty : public QObject
void asVariant();
void isProjectColor();
void referencedFieldsIgnoreContext();
void mapToMap();

private:

Expand Down Expand Up @@ -1941,5 +1942,21 @@ void TestQgsProperty::checkCurveResult( const QList<QgsPointXY> &controlPoints,
}
}

void TestQgsProperty::mapToMap()
{
QgsProperty p1 = QgsProperty::fromExpression( "project_color('burnt marigold')" );
QgsProperty p2 = QgsProperty::fromValue( 1 );

QMap<QString, QgsProperty> propertyMap;
propertyMap.insert( "key1", p1 );
propertyMap.insert( "key2", p2 );

const QVariantMap variantMap = QgsProperty::propertyMapToVariantMap( propertyMap );
QCOMPARE( variantMap.value( "key1" ).toMap().value( "expression" ).toString(), "project_color('burnt marigold')" );
QCOMPARE( variantMap.value( "key2" ).toMap().value( "val" ).toInt(), 1 );

QCOMPARE( QgsProperty::variantMapToPropertyMap( variantMap ), propertyMap );
}

QGSTEST_MAIN( TestQgsProperty )
#include "testqgsproperty.moc"

0 comments on commit d2ccd82

Please sign in to comment.