mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
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
This commit is contained in:
parent
a57a8dcbb6
commit
d2ccd82cd9
@ -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.
|
||||
|
@ -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> ¶meters )
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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 );
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
|
@ -95,6 +95,7 @@ class TestQgsProperty : public QObject
|
||||
void asVariant();
|
||||
void isProjectColor();
|
||||
void referencedFieldsIgnoreContext();
|
||||
void mapToMap();
|
||||
|
||||
private:
|
||||
|
||||
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user