Skip to content

Commit

Permalink
Persist canvas expression context scope variables to project
Browse files Browse the repository at this point in the history
and restore individual scope variables for each canvas on project
load.
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent a0e91f3 commit 7c5dcd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_qgsmapcanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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__':
Expand Down

0 comments on commit 7c5dcd6

Please sign in to comment.