Skip to content

Commit eb02004

Browse files
committedNov 5, 2018
Store original style as QDomDocument instead of an XML string
1 parent bc8574c commit eb02004

File tree

7 files changed

+24
-39
lines changed

7 files changed

+24
-39
lines changed
 

‎python/core/auto_generated/qgsmaplayer.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ Returns true if the refresh on provider nofification is enabled
12371237
.. versionadded:: 3.0
12381238
%End
12391239

1240-
QString originalXmlProperties() const;
1240+
QDomDocument originalXmlProperties() const;
12411241
%Docstring
12421242
Returns the XML properties of the original layer as they were when the layer
12431243
was first read from the project file. In case of new layers this is normally empty.
@@ -1247,7 +1247,7 @@ The storage format for the XML is qlr
12471247
.. versionadded:: 3.6
12481248
%End
12491249

1250-
void setOriginalXmlProperties( const QString &originalXmlProperties );
1250+
void setOriginalXmlProperties( const QDomDocument &originalXmlProperties );
12511251
%Docstring
12521252
Sets the original XML properties for the layer to ``originalXmlProperties``
12531253

‎src/app/qgisapp.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6955,26 +6955,17 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
69556955
bool layerIsValid( layer->isValid() );
69566956
layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() );
69576957
// Re-apply style
6958-
if ( !( layerIsValid || layer->originalXmlProperties().isEmpty() ) )
6958+
if ( !( layerIsValid || layer->originalXmlProperties().isNull() ) )
69596959
{
69606960
QgsReadWriteContext context;
69616961
context.setPathResolver( QgsProject::instance()->pathResolver() );
69626962
context.setProjectTranslator( QgsProject::instance() );
69636963
QString errorMsg;
6964-
QDomDocument doc;
6965-
if ( doc.setContent( layer->originalXmlProperties() ) )
6964+
QDomDocument doc( layer->originalXmlProperties() );
6965+
QDomNode layer_node( doc.firstChild( ) );
6966+
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
69666967
{
6967-
QDomNode layer_node( doc.firstChild( ) );
6968-
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
6969-
{
6970-
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
6971-
.arg( layer->name( ) )
6972-
.arg( errorMsg ) );
6973-
}
6974-
}
6975-
else
6976-
{
6977-
QgsDebugMsg( QStringLiteral( "Failed to create XML QDomDocument for layer %1: %2" )
6968+
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
69786969
.arg( layer->name( ) )
69796970
.arg( errorMsg ) );
69806971
}

‎src/core/layertree/qgslayertreeutils.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,7 @@ void QgsLayerTreeUtils::storeOriginalLayersProperties( QgsLayerTreeGroup *group,
329329
QDomDocument document( documentType );
330330
QDomElement element = mlNode.toElement();
331331
document.appendChild( element );
332-
QString str;
333-
QTextStream stream( &str );
334-
document.save( stream, 4 /*indent*/ );
335-
l->setOriginalXmlProperties( str );
332+
l->setOriginalXmlProperties( document );
336333
}
337334
}
338335
}

‎src/core/qgsmaplayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,12 +1833,12 @@ bool QgsMapLayer::isReadOnly() const
18331833
return true;
18341834
}
18351835

1836-
QString QgsMapLayer::originalXmlProperties() const
1836+
QDomDocument QgsMapLayer::originalXmlProperties() const
18371837
{
18381838
return mOriginalXmlProperties;
18391839
}
18401840

1841-
void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
1841+
void QgsMapLayer::setOriginalXmlProperties( const QDomDocument &originalXmlProperties )
18421842
{
18431843
mOriginalXmlProperties = originalXmlProperties;
18441844
}

‎src/core/qgsmaplayer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
11091109
*
11101110
* \since QGIS 3.6
11111111
*/
1112-
QString originalXmlProperties() const;
1112+
QDomDocument originalXmlProperties() const;
11131113

11141114
/**
11151115
* Sets the original XML properties for the layer to \a originalXmlProperties
@@ -1118,7 +1118,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
11181118
*
11191119
* \since QGIS 3.6
11201120
*/
1121-
void setOriginalXmlProperties( const QString &originalXmlProperties );
1121+
void setOriginalXmlProperties( const QDomDocument &originalXmlProperties );
11221122

11231123
public slots:
11241124

@@ -1529,7 +1529,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
15291529
*
15301530
* This information can be used to pass through the bad layers or to reset changes on a good layer
15311531
*/
1532-
QString mOriginalXmlProperties;
1532+
QDomDocument mOriginalXmlProperties;
15331533

15341534
};
15351535

‎src/core/qgsproject.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,17 +1774,10 @@ bool QgsProject::writeProjectFile( const QString &filename )
17741774
maplayerElem = doc->createElement( QStringLiteral( "maplayer" ) );
17751775
ml->writeLayerXml( maplayerElem, *doc, context );
17761776
}
1777-
else if ( ! ml->originalXmlProperties().isEmpty() )
1777+
else if ( ! ml->originalXmlProperties().isNull() )
17781778
{
1779-
QDomDocument document;
1780-
if ( document.setContent( ml->originalXmlProperties() ) )
1781-
{
1782-
maplayerElem = document.firstChildElement();
1783-
}
1784-
else
1785-
{
1786-
QgsDebugMsg( QStringLiteral( "Could not restore layer properties for layer %1" ).arg( ml->id() ) );
1787-
}
1779+
QDomDocument document( ml->originalXmlProperties() );
1780+
maplayerElem = document.firstChildElement();
17881781
}
17891782

17901783
emit writeMapLayer( ml, maplayerElem, *doc );

‎tests/src/python/test_qgsprojectbadlayers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from qgis.PyQt.QtGui import QFont, QColor
3636
from qgis.PyQt.QtTest import QSignalSpy
3737
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir, QSize
38+
from qgis.PyQt.QtXml import QDomDocument, QDomNode, QDomImplementation
3839

3940
from qgis.testing import start_app, unittest
4041
from utilities import (unitTestDataPath, renderMapToImage)
@@ -99,8 +100,12 @@ def test_project_roundtrip(self):
99100
self.assertTrue(raster.originalXmlProperties() != '')
100101
self.assertTrue(raster_copy.originalXmlProperties() != '')
101102
# Test setter
102-
raster.setOriginalXmlProperties('pippo')
103-
self.assertEqual(raster.originalXmlProperties(), 'pippo')
103+
domimp = QDomImplementation()
104+
documentType = domimp.createDocumentType("qgis", "http://mrcc.com/qgis.dtd", "SYSTEM")
105+
document = QDomDocument(documentType)
106+
document.setContent("<pippo>pluto</pippo>")
107+
raster.setOriginalXmlProperties(document)
108+
self.assertEqual(raster.originalXmlProperties(), document)
104109

105110
# Now create and invalid project:
106111
bad_project_path = os.path.join(temp_dir.path(), 'project_bad.qgs')
@@ -144,7 +149,7 @@ def test_project_roundtrip(self):
144149
self.assertTrue(raster_copy.isValid())
145150

146151
def test_project_relations(self):
147-
"""Tests that a project with bad layers and relations can be saved with relations"""
152+
"""Tests that a project with bad layers and relations can be maintained"""
148153

149154
temp_dir = QTemporaryDir()
150155
p = QgsProject.instance()
@@ -235,7 +240,6 @@ def testStyles(self):
235240
p = QgsProject.instance()
236241
project_path = os.path.join(temp_dir.path(), 'good_layers_test.qgs')
237242
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'good_layers_test.qgs'), project_path)
238-
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'bad_layers_test.gpkg'), os.path.join(temp_dir.path(), 'bad_layers_test.gpkg'))
239243
for f in (
240244
'bad_layer_raster_test.tfw',
241245
'bad_layer_raster_test.tiff',

0 commit comments

Comments
 (0)
Please sign in to comment.