Skip to content
Permalink
Browse files

Persist canvas expression context scope variables to project

and restore individual scope variables for each canvas on project
load.
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent a0e91f3 commit 7c5dcd67ba3e3852948dd1a4664615d08b77dd34
Showing with 24 additions and 0 deletions.
  1. +14 −0 src/gui/qgsmapcanvas.cpp
  2. +10 −0 tests/src/python/test_qgsmapcanvas.py
@@ -2087,6 +2087,14 @@ void QgsMapCanvas::readProject( const QDomDocument &doc )
}
}
setAnnotationsVisible( elem.attribute( QStringLiteral( "annotationsVisible" ), QStringLiteral( "1" ) ).toInt() );

// restore canvas expression context
const QDomNodeList scopeElements = elem.elementsByTagName( QStringLiteral( "expressionContextScope" ) );
if ( scopeElements.size() > 0 )
{
const QDomElement scopeElement = scopeElements.at( 0 ).toElement();
mExpressionContextScope.readXml( scopeElement, QgsReadWriteContext() );
}
}
else
{
@@ -2114,6 +2122,12 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )
qgisNode.appendChild( mapcanvasNode );

mSettings.writeXml( mapcanvasNode, doc );

// store canvas expression context
QDomElement scopeElement = doc.createElement( QStringLiteral( "expressionContextScope" ) );
mExpressionContextScope.writeXml( scopeElement, doc, QgsReadWriteContext() );
mapcanvasNode.appendChild( scopeElement );

// TODO: store only units, extent, projections, dest CRS
}

@@ -355,10 +355,14 @@ def testSaveMultipleCanvasesToProject(self):
c1.setObjectName('c1')
c1.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
c1.setRotation(45)
c1.expressionContextScope().setVariable('vara', 1111)
c1.expressionContextScope().setVariable('varb', 'bb')
c2 = QgsMapCanvas()
c2.setObjectName('c2')
c2.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
c2.setRotation(65)
c2.expressionContextScope().setVariable('vara', 2222)
c2.expressionContextScope().setVariable('varc', 'cc')

doc = QDomDocument("testdoc")
elem = doc.createElement("qgis")
@@ -375,8 +379,14 @@ def testSaveMultipleCanvasesToProject(self):

self.assertEqual(c3.mapSettings().destinationCrs().authid(), 'EPSG:3111')
self.assertEqual(c3.rotation(), 45)
self.assertEqual(set(c3.expressionContextScope().variableNames()), {'vara', 'varb'})
self.assertEqual(c3.expressionContextScope().variable('vara'), 1111)
self.assertEqual(c3.expressionContextScope().variable('varb'), 'bb')
self.assertEqual(c4.mapSettings().destinationCrs().authid(), 'EPSG:4326')
self.assertEqual(c4.rotation(), 65)
self.assertEqual(set(c4.expressionContextScope().variableNames()), {'vara', 'varc'})
self.assertEqual(c4.expressionContextScope().variable('vara'), 2222)
self.assertEqual(c4.expressionContextScope().variable('varc'), 'cc')


if __name__ == '__main__':

0 comments on commit 7c5dcd6

Please sign in to comment.
You can’t perform that action at this time.