Skip to content

Commit

Permalink
Correctly handle reading/writing multiple canvas to project
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent cb43ec3 commit ee969df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1857,6 +1857,21 @@ void QgsMapCanvas::readProject( const QDomDocument &doc )
{
QDomNode node = nodes.item( 0 );

// Search the specific MapCanvas node using the name
if ( nodes.count() > 1 )
{
for ( int i = 0; i < nodes.size(); ++i )
{
QDomElement elementNode = nodes.at( i ).toElement();

if ( elementNode.hasAttribute( "name" ) && elementNode.attribute( "name" ) == objectName() )
{
node = nodes.at( i );
break;
}
}
}

QgsMapSettings tmpSettings;
tmpSettings.readXml( node );
setDestinationCrs( tmpSettings.destinationCrs() );
Expand Down Expand Up @@ -1886,6 +1901,7 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )
QDomNode qgisNode = nl.item( 0 ); // there should only be one, so zeroth element ok

QDomElement mapcanvasNode = doc.createElement( QStringLiteral( "mapcanvas" ) );
mapcanvasNode.setAttribute( "name", objectName() );
qgisNode.appendChild( mapcanvasNode );

mSettings.writeXml( mapcanvasNode, doc );
Expand Down
39 changes: 36 additions & 3 deletions tests/src/python/test_qgsmapcanvas.py
Expand Up @@ -14,7 +14,8 @@

import qgis # NOQA

from qgis.core import (QgsCoordinateReferenceSystem,
from qgis.core import (QgsMapSettings,
QgsCoordinateReferenceSystem,
QgsRectangle,
QgsVectorLayer,
QgsFeature,
Expand All @@ -23,10 +24,13 @@
QgsFillSymbol,
QgsSingleSymbolRenderer,
QgsMapThemeCollection,
QgsProject)
QgsProject,
QgsApplication)
from qgis.gui import (QgsMapCanvas)

from qgis.PyQt.QtCore import QDir
from qgis.PyQt.QtCore import (Qt,
QDir)
from qgis.PyQt.QtXml import (QDomDocument, QDomElement)
import time
from qgis.testing import start_app, unittest

Expand Down Expand Up @@ -337,6 +341,35 @@ def canvasImageCheck(self, name, reference_image, canvas):
print((self.report))
return result

def testSaveMultipleCanvasesToProject(self):
# test saving/restoring canvas state to project with multiple canvases
c1 = QgsMapCanvas()
c1.setObjectName('c1')
c1.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
c1.setRotation(45)
c2 = QgsMapCanvas()
c2.setObjectName('c2')
c2.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
c2.setRotation(65)

doc = QDomDocument("testdoc")
elem = doc.createElement("qgis")
doc.appendChild(elem)
c1.writeProject(doc)
c2.writeProject(doc)

c3 = QgsMapCanvas()
c3.setObjectName('c1')
c4 = QgsMapCanvas()
c4.setObjectName('c2')
c3.readProject(doc)
c4.readProject(doc)

self.assertEqual(c3.mapSettings().destinationCrs().authid(), 'EPSG:3111')
self.assertEqual(c3.rotation(), 45)
self.assertEqual(c4.mapSettings().destinationCrs().authid(), 'EPSG:4326')
self.assertEqual(c4.rotation(), 65)


if __name__ == '__main__':
unittest.main()

0 comments on commit ee969df

Please sign in to comment.