Skip to content

Commit

Permalink
Ensure that temporary map canvas atlas variables are never written to…
Browse files Browse the repository at this point in the history
… projects

Causes a crash on debug builds, and is generally undesirable anyway
  • Loading branch information
nyalldawson committed May 13, 2019
1 parent 279139b commit 83adb92
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,13 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )

// store canvas expression context
QDomElement scopeElement = doc.createElement( QStringLiteral( "expressionContextScope" ) );
mExpressionContextScope.writeXml( scopeElement, doc, QgsReadWriteContext() );
QgsExpressionContextScope tmpScope( mExpressionContextScope );
tmpScope.removeVariable( QStringLiteral( "atlas_featurenumber" ) );
tmpScope.removeVariable( QStringLiteral( "atlas_pagename" ) );
tmpScope.removeVariable( QStringLiteral( "atlas_feature" ) );
tmpScope.removeVariable( QStringLiteral( "atlas_featureid" ) );
tmpScope.removeVariable( QStringLiteral( "atlas_geometry" ) );
tmpScope.writeXml( scopeElement, doc, QgsReadWriteContext() );
mapcanvasNode.appendChild( scopeElement );

// TODO: store only units, extent, projections, dest CRS
Expand Down
27 changes: 27 additions & 0 deletions tests/src/python/test_qgsmapcanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,33 @@ def canvasImageCheck(self, name, reference_image, canvas):
print((self.report))
return result

def testSaveCanvasVariablesToProject(self):
"""
Ensure that temporary canvas atlas variables are not written to project
"""
c1 = QgsMapCanvas()
c1.setObjectName('c1')
c1.expressionContextScope().setVariable('atlas_featurenumber', 1111)
c1.expressionContextScope().setVariable('atlas_pagename', 'bb')
c1.expressionContextScope().setVariable('atlas_feature', QgsFeature(1))
c1.expressionContextScope().setVariable('atlas_featureid', 22)
c1.expressionContextScope().setVariable('atlas_geometry', QgsGeometry.fromWkt('Point( 1 2 )'))
c1.expressionContextScope().setVariable('vara', 1111)
c1.expressionContextScope().setVariable('varb', 'bb')

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

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

self.assertCountEqual(c2.expressionContextScope().variableNames(), ['vara', 'varb'])
self.assertEqual(c2.expressionContextScope().variable('vara'), 1111)
self.assertEqual(c2.expressionContextScope().variable('varb'), 'bb')

def testSaveMultipleCanvasesToProject(self):
# test saving/restoring canvas state to project with multiple canvases
c1 = QgsMapCanvas()
Expand Down

0 comments on commit 83adb92

Please sign in to comment.