Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #9742 from m-kuhn/repeatable_xml_order
Browse files Browse the repository at this point in the history
Make .qgs project file XML element order stable
  • Loading branch information
m-kuhn committed Apr 10, 2019
2 parents 913dee2 + 54d58d9 commit d2c6999
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
11 changes: 7 additions & 4 deletions src/core/qgsmapthemecollection.cpp
Expand Up @@ -527,11 +527,14 @@ void QgsMapThemeCollection::readXml( const QDomDocument &doc )
void QgsMapThemeCollection::writeXml( QDomDocument &doc )
{
QDomElement visPresetsElem = doc.createElement( QStringLiteral( "visibility-presets" ) );
MapThemeRecordMap::const_iterator it = mMapThemes.constBegin();
for ( ; it != mMapThemes.constEnd(); ++ it )

auto keys = mMapThemes.keys();

std::sort( keys.begin(), keys.end() );

for ( const QString &grpName : qgis::as_const( keys ) )
{
QString grpName = it.key();
const MapThemeRecord &rec = it.value();
const MapThemeRecord &rec = mMapThemes.value( grpName );
QDomElement visPresetElem = doc.createElement( QStringLiteral( "visibility-preset" ) );
visPresetElem.setAttribute( QStringLiteral( "name" ), grpName );
if ( rec.hasExpandedStateInfo() )
Expand Down
18 changes: 12 additions & 6 deletions src/core/qgsobjectcustomproperties.cpp
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsobjectcustomproperties.h"
#include "qgis.h"

#include <QDomNode>
#include <QStringList>
Expand Down Expand Up @@ -118,17 +119,22 @@ void QgsObjectCustomProperties::writeXml( QDomNode &parentNode, QDomDocument &do

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

for ( QMap<QString, QVariant>::const_iterator it = mMap.constBegin(); it != mMap.constEnd(); ++it )
auto keys = mMap.keys();

std::sort( keys.begin(), keys.end() );

for ( const auto &key : qgis::as_const( keys ) )
{
QDomElement propElement = doc.createElement( QStringLiteral( "property" ) );
propElement.setAttribute( QStringLiteral( "key" ), it.key() );
if ( it.value().canConvert<QString>() )
propElement.setAttribute( QStringLiteral( "key" ), key );
const QVariant value = mMap.value( key );
if ( value.canConvert<QString>() )
{
propElement.setAttribute( QStringLiteral( "value" ), it.value().toString() );
propElement.setAttribute( QStringLiteral( "value" ), value.toString() );
}
else if ( it.value().canConvert<QStringList>() )
else if ( value.canConvert<QStringList>() )
{
const auto constToStringList = it.value().toStringList();
const auto constToStringList = value.toStringList();
for ( const QString &value : constToStringList )
{
QDomElement itemElement = doc.createElement( QStringLiteral( "value" ) );
Expand Down
15 changes: 8 additions & 7 deletions src/core/qgsprojectproperty.cpp
Expand Up @@ -17,6 +17,8 @@

#include "qgsprojectproperty.h"
#include "qgslogger.h"
#include "qgis.h"
#include "qgsmessagelog.h"

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

if ( ! mProperties.isEmpty() )
{
QHashIterator < QString, QgsProjectProperty * > i( mProperties );
while ( i.hasNext() )
auto keys = mProperties.keys();
std::sort( keys.begin(), keys.end() );

for ( const auto &key : qgis::as_const( keys ) )
{
i.next();
if ( !i.value()->writeXml( i.key(), keyElement, document ) )
{
return false;
}
if ( !mProperties.value( key )->writeXml( key, keyElement, document ) )
QgsMessageLog::logMessage( tr( "Failed to save project property %1" ).arg( key ) );
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsprojectproperty.h
Expand Up @@ -25,6 +25,7 @@
#include <QHash>
#include <QVariant>
#include <QStringList>
#include <QCoreApplication>

#include "qgis_core.h"

Expand Down Expand Up @@ -181,6 +182,8 @@ class CORE_EXPORT QgsProjectPropertyValue : public QgsProjectProperty
*/
class CORE_EXPORT QgsProjectPropertyKey : public QgsProjectProperty
{
Q_DECLARE_TR_FUNCTIONS( QgsProjectPropertyKey )

public:

/**
Expand Down

0 comments on commit d2c6999

Please sign in to comment.