Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e70a15f

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

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
@@ -154,8 +154,11 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
154154
}
155155

156156
case QVariant::Int:
157+
case QVariant::UInt:
157158
case QVariant::Bool:
158159
case QVariant::Double:
160+
case QVariant::LongLong:
161+
case QVariant::ULongLong:
159162
case QVariant::String:
160163
element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) );
161164
element.setAttribute( QStringLiteral( "value" ), value.toString() );
@@ -177,6 +180,13 @@ QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc
177180
crs.writeXml( element, doc );
178181
break;
179182
}
183+
else if ( value.canConvert< QgsGeometry >() )
184+
{
185+
element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsGeometry" ) );
186+
const QgsGeometry geom = value.value< QgsGeometry >();
187+
element.setAttribute( QStringLiteral( "value" ), geom.asWkt() );
188+
break;
189+
}
180190
FALLTHROUGH
181191
}
182192

@@ -200,6 +210,18 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
200210
{
201211
return element.attribute( QStringLiteral( "value" ) ).toInt();
202212
}
213+
else if ( type == QLatin1String( "uint" ) )
214+
{
215+
return element.attribute( QStringLiteral( "value" ) ).toUInt();
216+
}
217+
else if ( type == QLatin1String( "qlonglong" ) )
218+
{
219+
return element.attribute( QStringLiteral( "value" ) ).toLongLong();
220+
}
221+
else if ( type == QLatin1String( "qulonglong" ) )
222+
{
223+
return element.attribute( QStringLiteral( "value" ) ).toULongLong();
224+
}
203225
else if ( type == QLatin1String( "double" ) )
204226
{
205227
return element.attribute( QStringLiteral( "value" ) ).toDouble();
@@ -265,6 +287,10 @@ QVariant QgsXmlUtils::readVariant( const QDomElement &element )
265287
crs.readXml( element );
266288
return crs;
267289
}
290+
else if ( type == QLatin1String( "QgsGeometry" ) )
291+
{
292+
return QgsGeometry::fromWkt( element.attribute( "value" ) );
293+
}
268294
else
269295
{
270296
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.