Skip to content
Permalink
Browse files

Make QgsXmlUtils handle storing/retrieving QgsProperty values

  • Loading branch information
nyalldawson committed Aug 31, 2018
1 parent d0026d1 commit 3572ea80bd77168bab4f843c964f4b1e2a6efbaa
@@ -59,6 +59,7 @@ Supported types are
- QVariant.Int
- QVariant.Double
- QVariant.String
- :py:class:`QgsProperty` (since QGIS 3.4)
%End

static QVariant readVariant( const QDomElement &element );
@@ -164,6 +164,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaType<QgsUnitTypes::LayoutUnit>( "QgsUnitTypes::LayoutUnit" );
qRegisterMetaType<QgsFeatureId>( "QgsFeatureId" );
qRegisterMetaType<QgsFeatureIds>( "QgsFeatureIds" );
qRegisterMetaType<QgsProperty>( "QgsProperty" );
qRegisterMetaType<Qgis::MessageLevel>( "Qgis::MessageLevel" );
qRegisterMetaType<QgsReferencedRectangle>( "QgsReferencedRectangle" );
qRegisterMetaType<QgsReferencedPointXY>( "QgsReferencedPointXY" );
@@ -18,7 +18,7 @@

#include "qgslogger.h"
#include "qgsrectangle.h"

#include "qgsproperty.h"

QgsUnitTypes::DistanceUnit QgsXmlUtils::readMapUnits( const QDomElement &element )
{
@@ -153,6 +153,18 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
element.setAttribute( QStringLiteral( "value" ), value.toString() );
break;

case QVariant::UserType:
{
if ( value.canConvert< QgsProperty >() )
{
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsProperty" ) );
const QDomElement propertyElem = QgsXmlUtils::writeVariant( value.value< QgsProperty >().toVariant(), doc );
element.appendChild( propertyElem );
break;
}
FALLTHROUGH
}

default:
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Unknown" ) );
element.setAttribute( QStringLiteral( "value" ), value.toString() );
@@ -217,6 +229,18 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
}
return list;
}
else if ( type == QLatin1String( "QgsProperty" ) )
{
const QDomNodeList values = element.childNodes();
if ( values.isEmpty() )
return QVariant();

QgsProperty p;
if ( p.loadVariant( QgsXmlUtils::readVariant( values.at( 0 ).toElement() ) ) )
return p;

return QVariant();
}
else
{
return QVariant();
@@ -66,7 +66,7 @@ class CORE_EXPORT QgsXmlUtils
* - QVariant::Int
* - QVariant::Double
* - QVariant::String
*
* - QgsProperty (since QGIS 3.4)
*/
static QDomElement writeVariant( const QVariant &value, QDomDocument &doc );

@@ -14,7 +14,8 @@

import qgis # NOQA switch sip api

from qgis.core import QgsXmlUtils
from qgis.core import (QgsXmlUtils,
QgsProperty)

from qgis.PyQt.QtXml import QDomDocument

@@ -102,6 +103,33 @@ def test_complex(self):

self.assertEqual(my_properties, prop2)

def test_property(self):
"""
Test that QgsProperty values are correctly loaded and written
"""
doc = QDomDocument("properties")

prop = QgsProperty.fromValue(1001)
elem = QgsXmlUtils.writeVariant(prop, doc)

prop2 = QgsXmlUtils.readVariant(elem)

self.assertEqual(prop, prop2)

prop = QgsProperty.fromExpression('1+2=5')
elem = QgsXmlUtils.writeVariant(prop, doc)

prop2 = QgsXmlUtils.readVariant(elem)

self.assertEqual(prop, prop2)

prop = QgsProperty.fromField('oid')
elem = QgsXmlUtils.writeVariant(prop, doc)

prop2 = QgsXmlUtils.readVariant(elem)

self.assertEqual(prop, prop2)


if __name__ == '__main__':
unittest.main()

0 comments on commit 3572ea8

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