Skip to content
Permalink
Browse files

Correctly handle reading/writing multiple canvas to project

  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent cb43ec3 commit ee969df8a920cabe55122adb38b1884e3ed5911a
Showing with 52 additions and 3 deletions.
  1. +16 −0 src/gui/qgsmapcanvas.cpp
  2. +36 −3 tests/src/python/test_qgsmapcanvas.py
@@ -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() );
@@ -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 );
@@ -14,7 +14,8 @@

import qgis # NOQA

from qgis.core import (QgsCoordinateReferenceSystem,
from qgis.core import (QgsMapSettings,
QgsCoordinateReferenceSystem,
QgsRectangle,
QgsVectorLayer,
QgsFeature,
@@ -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

@@ -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.
You can’t perform that action at this time.