Skip to content

Commit b7c7d2f

Browse files
committedMay 13, 2019
Avoid crashes on debug builds when trying to write xml for more value types
1 parent 9e052fe commit b7c7d2f

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed
 

‎src/core/qgsapplication.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ void QgsApplication::init( QString profileFolder )
209209
qRegisterMetaType<QgsAuthManager::MessageLevel>( "QgsAuthManager::MessageLevel" );
210210
qRegisterMetaType<QgsNetworkRequestParameters>( "QgsNetworkRequestParameters" );
211211
qRegisterMetaType<QgsNetworkReplyContent>( "QgsNetworkReplyContent" );
212+
qRegisterMetaType<QgsGeometry>( "QgsGeometry" );
212213

213214
( void ) resolvePkgPath();
214215

‎src/core/qgsxmlutils.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
152152
}
153153

154154
case QVariant::Int:
155+
case QVariant::UInt:
155156
case QVariant::Bool:
156157
case QVariant::Double:
158+
case QVariant::LongLong:
159+
case QVariant::ULongLong:
157160
case QVariant::String:
158161
element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) );
159162
element.setAttribute( QStringLiteral( "value" ), value.toString() );
@@ -175,6 +178,13 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
175178
crs.writeXml( element, doc );
176179
break;
177180
}
181+
else if ( value.canConvert< QgsGeometry >() )
182+
{
183+
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsGeometry" ) );
184+
const QgsGeometry geom = value.value< QgsGeometry >();
185+
element.setAttribute( QStringLiteral( "value" ), geom.asWkt() );
186+
break;
187+
}
178188
FALLTHROUGH
179189
}
180190

@@ -198,6 +208,18 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
198208
{
199209
return element.attribute( QStringLiteral( "value" ) ).toInt();
200210
}
211+
else if ( type == QLatin1String( "uint" ) )
212+
{
213+
return element.attribute( QStringLiteral( "value" ) ).toUInt();
214+
}
215+
else if ( type == QLatin1String( "qlonglong" ) )
216+
{
217+
return element.attribute( QStringLiteral( "value" ) ).toLongLong();
218+
}
219+
else if ( type == QLatin1String( "qulonglong" ) )
220+
{
221+
return element.attribute( QStringLiteral( "value" ) ).toULongLong();
222+
}
201223
else if ( type == QLatin1String( "double" ) )
202224
{
203225
return element.attribute( QStringLiteral( "value" ) ).toDouble();
@@ -263,6 +285,10 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
263285
crs.readXml( element );
264286
return crs;
265287
}
288+
else if ( type == QLatin1String( "QgsGeometry" ) )
289+
{
290+
return QgsGeometry::fromWkt( element.attribute( "value" ) );
291+
}
266292
else
267293
{
268294
return QVariant();

‎tests/src/python/test_qgsxmlutils.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
__revision__ = '$Format:%H$'
1414

1515
import qgis # NOQA switch sip api
16-
1716
from qgis.core import (QgsXmlUtils,
1817
QgsProperty,
18+
QgsGeometry,
1919
QgsCoordinateReferenceSystem)
2020

2121
from qgis.PyQt.QtXml import QDomDocument
@@ -50,6 +50,19 @@ def test_integer(self):
5050
prop2 = QgsXmlUtils.readVariant(elem)
5151
self.assertEqual(my_properties, prop2)
5252

53+
def test_long(self):
54+
"""
55+
Test that maps are correctly loaded and written
56+
"""
57+
doc = QDomDocument("properties")
58+
59+
# not sure if this actually does map to a long?
60+
my_properties = {'a': 9223372036854775808}
61+
elem = QgsXmlUtils.writeVariant(my_properties, doc)
62+
63+
prop2 = QgsXmlUtils.readVariant(elem)
64+
self.assertEqual(my_properties, prop2)
65+
5366
def test_string(self):
5467
"""
5568
Test that maps are correctly loaded and written
@@ -160,6 +173,18 @@ def test_crs(self):
160173
crs2 = QgsXmlUtils.readVariant(elem)
161174
self.assertFalse(crs2.isValid())
162175

176+
def test_geom(self):
177+
"""
178+
Test that QgsGeometry values are correctly loaded and written
179+
"""
180+
doc = QDomDocument("properties")
181+
182+
g = QgsGeometry.fromWkt('Point(3 4)')
183+
elem = QgsXmlUtils.writeVariant(g, doc)
184+
185+
g2 = QgsXmlUtils.readVariant(elem)
186+
self.assertEqual(g2.asWkt(), 'Point (3 4)')
187+
163188

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

0 commit comments

Comments
 (0)
Please sign in to comment.