17
17
18
18
#include " qgsobjectcustomproperties.h"
19
19
#include " qgis.h"
20
+ #include " qgsxmlutils.h"
20
21
21
22
#include < QDomNode>
22
23
#include < QStringList>
@@ -77,39 +78,53 @@ void QgsObjectCustomProperties::readXml( const QDomNode &parentNode, const QStri
77
78
mMap .clear ();
78
79
}
79
80
80
- QDomNodeList nodes = propsNode.childNodes ();
81
-
82
- for ( int i = 0 ; i < nodes.size (); i++ )
81
+ const QVariant newProps = QgsXmlUtils::readVariant ( propsNode.firstChildElement () );
82
+ if ( newProps.type () == QVariant::Map )
83
+ {
84
+ #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
85
+ const QVariantMap propsMap = newProps.toMap ();
86
+ for ( auto it = propsMap.constBegin (); it != propsMap.constEnd (); ++it )
87
+ mMap .insert ( it.key (), it.value () );
88
+ #else
89
+ mMap .insert ( newProps.toMap () );
90
+ #endif
91
+ }
92
+ else
83
93
{
84
- QDomNode propNode = nodes.at ( i );
85
- if ( propNode.isNull () || propNode.nodeName () != QLatin1String ( " property" ) )
86
- continue ;
87
- QDomElement propElement = propNode.toElement ();
94
+ // backward compatibility code for QGIS < 3.20
95
+ QDomNodeList nodes = propsNode.childNodes ();
88
96
89
- QString key = propElement.attribute ( QStringLiteral ( " key" ) );
90
- if ( key.isEmpty () || key.startsWith ( keyStartsWith ) )
97
+ for ( int i = 0 ; i < nodes.size (); i++ )
91
98
{
92
- if ( propElement.hasAttribute ( QStringLiteral ( " value" ) ) )
93
- {
94
- QString value = propElement.attribute ( QStringLiteral ( " value" ) );
95
- mMap [key] = QVariant ( value );
96
- }
97
- else
98
- {
99
- QStringList list;
99
+ QDomNode propNode = nodes.at ( i );
100
+ if ( propNode.isNull () || propNode.nodeName () != QLatin1String ( " property" ) )
101
+ continue ;
102
+ QDomElement propElement = propNode.toElement ();
100
103
101
- for ( QDomElement itemElement = propElement.firstChildElement ( QStringLiteral ( " value" ) );
102
- !itemElement.isNull ();
103
- itemElement = itemElement.nextSiblingElement ( QStringLiteral ( " value" ) ) )
104
+ QString key = propElement.attribute ( QStringLiteral ( " key" ) );
105
+ if ( key.isEmpty () || key.startsWith ( keyStartsWith ) )
106
+ {
107
+ if ( propElement.hasAttribute ( QStringLiteral ( " value" ) ) )
104
108
{
105
- list << itemElement.text ();
109
+ QString value = propElement.attribute ( QStringLiteral ( " value" ) );
110
+ mMap [key] = QVariant ( value );
106
111
}
112
+ else
113
+ {
114
+ QStringList list;
107
115
108
- mMap [key] = QVariant ( list );
116
+ for ( QDomElement itemElement = propElement.firstChildElement ( QStringLiteral ( " value" ) );
117
+ !itemElement.isNull ();
118
+ itemElement = itemElement.nextSiblingElement ( QStringLiteral ( " value" ) ) )
119
+ {
120
+ list << itemElement.text ();
121
+ }
122
+
123
+ mMap [key] = QVariant ( list );
124
+ }
109
125
}
110
126
}
111
127
}
112
-
113
128
}
114
129
115
130
void QgsObjectCustomProperties::writeXml ( QDomNode &parentNode, QDomDocument &doc ) const
@@ -122,32 +137,7 @@ void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &do
122
137
}
123
138
124
139
QDomElement propsElement = doc.createElement ( QStringLiteral ( " customproperties" ) );
125
-
126
- auto keys = mMap .keys ();
127
-
128
- std::sort ( keys.begin (), keys.end () );
129
-
130
- for ( const auto &key : std::as_const ( keys ) )
131
- {
132
- QDomElement propElement = doc.createElement ( QStringLiteral ( " property" ) );
133
- propElement.setAttribute ( QStringLiteral ( " key" ), key );
134
- const QVariant value = mMap .value ( key );
135
- if ( value.canConvert <QString>() )
136
- {
137
- propElement.setAttribute ( QStringLiteral ( " value" ), value.toString () );
138
- }
139
- else if ( value.canConvert <QStringList>() )
140
- {
141
- const auto constToStringList = value.toStringList ();
142
- for ( const QString &valueStr : constToStringList )
143
- {
144
- QDomElement itemElement = doc.createElement ( QStringLiteral ( " value" ) );
145
- itemElement.appendChild ( doc.createTextNode ( valueStr ) );
146
- propElement.appendChild ( itemElement );
147
- }
148
- }
149
- propsElement.appendChild ( propElement );
150
- }
140
+ propsElement.appendChild ( QgsXmlUtils::writeVariant ( mMap , doc ) );
151
141
152
142
parentNode.appendChild ( propsElement );
153
143
}
0 commit comments