Skip to content

Commit

Permalink
Remove unnecessary project from test data
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 5, 2018
1 parent eb02004 commit f02345b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -1237,7 +1237,7 @@ Returns true if the refresh on provider nofification is enabled
.. versionadded:: 3.0
%End

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

void setOriginalXmlProperties( const QDomDocument &originalXmlProperties );
void setOriginalXmlProperties( const QString &originalXmlProperties );
%Docstring
Sets the original XML properties for the layer to ``originalXmlProperties``

Expand Down
19 changes: 14 additions & 5 deletions src/app/qgisapp.cpp
Expand Up @@ -6955,17 +6955,26 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
bool layerIsValid( layer->isValid() );
layer->setDataSource( uri.uri, layer->name(), uri.providerKey, QgsDataProvider::ProviderOptions() );
// Re-apply style
if ( !( layerIsValid || layer->originalXmlProperties().isNull() ) )
if ( !( layerIsValid || layer->originalXmlProperties().isEmpty() ) )
{
QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() );
context.setProjectTranslator( QgsProject::instance() );
QString errorMsg;
QDomDocument doc( layer->originalXmlProperties() );
QDomNode layer_node( doc.firstChild( ) );
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
QDomDocument doc;
if ( doc.setContent( layer->originalXmlProperties() ) )
{
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
QDomNode layer_node( doc.firstChild( ) );
if ( ! layer->readSymbology( layer_node, errorMsg, context ) )
{
QgsDebugMsg( QStringLiteral( "Failed to restore original layer style from stored XML for layer %1: %2" )
.arg( layer->name( ) )
.arg( errorMsg ) );
}
}
else
{
QgsDebugMsg( QStringLiteral( "Failed to create XML QDomDocument for layer %1: %2" )
.arg( layer->name( ) )
.arg( errorMsg ) );
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/layertree/qgslayertreeutils.cpp
Expand Up @@ -329,7 +329,10 @@ void QgsLayerTreeUtils::storeOriginalLayersProperties( QgsLayerTreeGroup *group,
QDomDocument document( documentType );
QDomElement element = mlNode.toElement();
document.appendChild( element );
l->setOriginalXmlProperties( document );
QString str;
QTextStream stream( &str );
document.save( stream, 4 /*indent*/ );
l->setOriginalXmlProperties( str );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -1833,12 +1833,12 @@ bool QgsMapLayer::isReadOnly() const
return true;
}

QDomDocument QgsMapLayer::originalXmlProperties() const
QString QgsMapLayer::originalXmlProperties() const
{
return mOriginalXmlProperties;
}

void QgsMapLayer::setOriginalXmlProperties( const QDomDocument &originalXmlProperties )
void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
{
mOriginalXmlProperties = originalXmlProperties;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsmaplayer.h
Expand Up @@ -1109,7 +1109,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
*
* \since QGIS 3.6
*/
QDomDocument originalXmlProperties() const;
QString originalXmlProperties() const;

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

public slots:

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

};

Expand Down
13 changes: 10 additions & 3 deletions src/core/qgsproject.cpp
Expand Up @@ -1774,10 +1774,17 @@ bool QgsProject::writeProjectFile( const QString &filename )
maplayerElem = doc->createElement( QStringLiteral( "maplayer" ) );
ml->writeLayerXml( maplayerElem, *doc, context );
}
else if ( ! ml->originalXmlProperties().isNull() )
else if ( ! ml->originalXmlProperties().isEmpty() )
{
QDomDocument document( ml->originalXmlProperties() );
maplayerElem = document.firstChildElement();
QDomDocument document;
if ( document.setContent( ml->originalXmlProperties() ) )
{
maplayerElem = document.firstChildElement();
}
else
{
QgsDebugMsg( QStringLiteral( "Could not restore layer properties for layer %1" ).arg( ml->id() ) );
}
}

emit writeMapLayer( ml, maplayerElem, *doc );
Expand Down
12 changes: 4 additions & 8 deletions tests/src/python/test_qgsprojectbadlayers.py
Expand Up @@ -35,7 +35,6 @@
from qgis.PyQt.QtGui import QFont, QColor
from qgis.PyQt.QtTest import QSignalSpy
from qgis.PyQt.QtCore import QT_VERSION_STR, QTemporaryDir, QSize
from qgis.PyQt.QtXml import QDomDocument, QDomNode, QDomImplementation

from qgis.testing import start_app, unittest
from utilities import (unitTestDataPath, renderMapToImage)
Expand Down Expand Up @@ -100,12 +99,8 @@ def test_project_roundtrip(self):
self.assertTrue(raster.originalXmlProperties() != '')
self.assertTrue(raster_copy.originalXmlProperties() != '')
# Test setter
domimp = QDomImplementation()
documentType = domimp.createDocumentType("qgis", "http://mrcc.com/qgis.dtd", "SYSTEM")
document = QDomDocument(documentType)
document.setContent("<pippo>pluto</pippo>")
raster.setOriginalXmlProperties(document)
self.assertEqual(raster.originalXmlProperties(), document)
raster.setOriginalXmlProperties('pippo')
self.assertEqual(raster.originalXmlProperties(), 'pippo')

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

def test_project_relations(self):
"""Tests that a project with bad layers and relations can be maintained"""
"""Tests that a project with bad layers and relations can be saved with relations"""

temp_dir = QTemporaryDir()
p = QgsProject.instance()
Expand Down Expand Up @@ -240,6 +235,7 @@ def testStyles(self):
p = QgsProject.instance()
project_path = os.path.join(temp_dir.path(), 'good_layers_test.qgs')
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'good_layers_test.qgs'), project_path)
copyfile(os.path.join(TEST_DATA_DIR, 'projects', 'bad_layers_test.gpkg'), os.path.join(temp_dir.path(), 'bad_layers_test.gpkg'))
for f in (
'bad_layer_raster_test.tfw',
'bad_layer_raster_test.tiff',
Expand Down

0 comments on commit f02345b

Please sign in to comment.