Skip to content

Commit

Permalink
Avoid crashes on debug builds when trying to write xml for more value…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
nyalldawson committed May 13, 2019
1 parent 9e052fe commit b7c7d2f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/qgsapplication.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" ); qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" );
qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" ); qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" );
qRegisterMetaType<QgsNetworkReplyContent>( "QgsNetworkReplyContent" ); qRegisterMetaType<QgsNetworkReplyContent>( "QgsNetworkReplyContent" );
qRegisterMetaType<QgsGeometry>( "QgsGeometry" );


( void ) resolvePkgPath(); ( void ) resolvePkgPath();


Expand Down
26 changes: 26 additions & 0 deletions src/core/qgsxmlutils.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
} }


case QVariant::Int: case QVariant::Int:
case QVariant::UInt:
case QVariant::Bool: case QVariant::Bool:
case QVariant::Double: case QVariant::Double:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::String: case QVariant::String:
element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) ); element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) );
element.setAttribute( QStringLiteral( "value" ), value.toString() ); element.setAttribute( QStringLiteral( "value" ), value.toString() );
Expand All @@ -175,6 +178,13 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
crs.writeXml( element, doc ); crs.writeXml( element, doc );
break; break;
} }
else if ( value.canConvert< QgsGeometry >() )
{
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsGeometry" ) );
const QgsGeometry geom = value.value< QgsGeometry >();
element.setAttribute( QStringLiteral( "value" ), geom.asWkt() );
break;
}
FALLTHROUGH FALLTHROUGH
} }


Expand All @@ -198,6 +208,18 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
{ {
return element.attribute( QStringLiteral( "value" ) ).toInt(); return element.attribute( QStringLiteral( "value" ) ).toInt();
} }
else if ( type == QLatin1String( "uint" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toUInt();
}
else if ( type == QLatin1String( "qlonglong" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toLongLong();
}
else if ( type == QLatin1String( "qulonglong" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toULongLong();
}
else if ( type == QLatin1String( "double" ) ) else if ( type == QLatin1String( "double" ) )
{ {
return element.attribute( QStringLiteral( "value" ) ).toDouble(); return element.attribute( QStringLiteral( "value" ) ).toDouble();
Expand Down Expand Up @@ -263,6 +285,10 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
crs.readXml( element ); crs.readXml( element );
return crs; return crs;
} }
else if ( type == QLatin1String( "QgsGeometry" ) )
{
return QgsGeometry::fromWkt( element.attribute( "value" ) );
}
else else
{ {
return QVariant(); return QVariant();
Expand Down
27 changes: 26 additions & 1 deletion tests/src/python/test_qgsxmlutils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'


import qgis # NOQA switch sip api import qgis # NOQA switch sip api

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


from qgis.PyQt.QtXml import QDomDocument from qgis.PyQt.QtXml import QDomDocument
Expand Down Expand Up @@ -50,6 +50,19 @@ def test_integer(self):
prop2 = QgsXmlUtils.readVariant(elem) prop2 = QgsXmlUtils.readVariant(elem)
self.assertEqual(my_properties, prop2) self.assertEqual(my_properties, prop2)


def test_long(self):
"""
Test that maps are correctly loaded and written
"""
doc = QDomDocument("properties")

# not sure if this actually does map to a long?
my_properties = {'a': 9223372036854775808}
elem = QgsXmlUtils.writeVariant(my_properties, doc)

prop2 = QgsXmlUtils.readVariant(elem)
self.assertEqual(my_properties, prop2)

def test_string(self): def test_string(self):
""" """
Test that maps are correctly loaded and written Test that maps are correctly loaded and written
Expand Down Expand Up @@ -160,6 +173,18 @@ def test_crs(self):
crs2 = QgsXmlUtils.readVariant(elem) crs2 = QgsXmlUtils.readVariant(elem)
self.assertFalse(crs2.isValid()) self.assertFalse(crs2.isValid())


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

g = QgsGeometry.fromWkt('Point(3 4)')
elem = QgsXmlUtils.writeVariant(g, doc)

g2 = QgsXmlUtils.readVariant(elem)
self.assertEqual(g2.asWkt(), 'Point (3 4)')



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

0 comments on commit b7c7d2f

Please sign in to comment.