Skip to content

Commit d2c6999

Browse files
authored
Merge pull request #9742 from m-kuhn/repeatable_xml_order
Make .qgs project file XML element order stable
2 parents 913dee2 + 54d58d9 commit d2c6999

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/core/qgsmapthemecollection.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,14 @@ void QgsMapThemeCollection::readXml( const QDomDocument &doc )
527527
void QgsMapThemeCollection::writeXml( QDomDocument &doc )
528528
{
529529
QDomElement visPresetsElem = doc.createElement( QStringLiteral( "visibility-presets" ) );
530-
MapThemeRecordMap::const_iterator it = mMapThemes.constBegin();
531-
for ( ; it != mMapThemes.constEnd(); ++ it )
530+
531+
auto keys = mMapThemes.keys();
532+
533+
std::sort( keys.begin(), keys.end() );
534+
535+
for ( const QString &grpName : qgis::as_const( keys ) )
532536
{
533-
QString grpName = it.key();
534-
const MapThemeRecord &rec = it.value();
537+
const MapThemeRecord &rec = mMapThemes.value( grpName );
535538
QDomElement visPresetElem = doc.createElement( QStringLiteral( "visibility-preset" ) );
536539
visPresetElem.setAttribute( QStringLiteral( "name" ), grpName );
537540
if ( rec.hasExpandedStateInfo() )

src/core/qgsobjectcustomproperties.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgsobjectcustomproperties.h"
19+
#include "qgis.h"
1920

2021
#include <QDomNode>
2122
#include <QStringList>
@@ -118,17 +119,22 @@ void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &do
118119

119120
QDomElement propsElement = doc.createElement( QStringLiteral( "customproperties" ) );
120121

121-
for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
122+
auto keys = mMap.keys();
123+
124+
std::sort( keys.begin(), keys.end() );
125+
126+
for ( const auto &key : qgis::as_const( keys ) )
122127
{
123128
QDomElement propElement = doc.createElement( QStringLiteral( "property" ) );
124-
propElement.setAttribute( QStringLiteral( "key" ), it.key() );
125-
if ( it.value().canConvert<QString>() )
129+
propElement.setAttribute( QStringLiteral( "key" ), key );
130+
const QVariant value = mMap.value( key );
131+
if ( value.canConvert<QString>() )
126132
{
127-
propElement.setAttribute( QStringLiteral( "value" ), it.value().toString() );
133+
propElement.setAttribute( QStringLiteral( "value" ), value.toString() );
128134
}
129-
else if ( it.value().canConvert<QStringList>() )
135+
else if ( value.canConvert<QStringList>() )
130136
{
131-
const auto constToStringList = it.value().toStringList();
137+
const auto constToStringList = value.toStringList();
132138
for ( const QString &value : constToStringList )
133139
{
134140
QDomElement itemElement = doc.createElement( QStringLiteral( "value" ) );

src/core/qgsprojectproperty.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include "qgsprojectproperty.h"
1919
#include "qgslogger.h"
20+
#include "qgis.h"
21+
#include "qgsmessagelog.h"
2022

2123
#include <QDomDocument>
2224
#include <QStringList>
@@ -407,14 +409,13 @@ bool QgsProjectPropertyKey::writeXml( QString const &nodeName, QDomElement &elem
407409

408410
if ( ! mProperties.isEmpty() )
409411
{
410-
QHashIterator < QString, QgsProjectProperty * > i( mProperties );
411-
while ( i.hasNext() )
412+
auto keys = mProperties.keys();
413+
std::sort( keys.begin(), keys.end() );
414+
415+
for ( const auto &key : qgis::as_const( keys ) )
412416
{
413-
i.next();
414-
if ( !i.value()->writeXml( i.key(), keyElement, document ) )
415-
{
416-
return false;
417-
}
417+
if ( !mProperties.value( key )->writeXml( key, keyElement, document ) )
418+
QgsMessageLog::logMessage( tr( "Failed to save project property %1" ).arg( key ) );
418419
}
419420
}
420421

src/core/qgsprojectproperty.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QHash>
2626
#include <QVariant>
2727
#include <QStringList>
28+
#include <QCoreApplication>
2829

2930
#include "qgis_core.h"
3031

@@ -181,6 +182,8 @@ class CORE_EXPORT QgsProjectPropertyValue : public QgsProjectProperty
181182
*/
182183
class CORE_EXPORT QgsProjectPropertyKey : public QgsProjectProperty
183184
{
185+
Q_DECLARE_TR_FUNCTIONS( QgsProjectPropertyKey )
186+
184187
public:
185188

186189
/**

0 commit comments

Comments
 (0)