Skip to content
Permalink
Browse files

Ensure that temporary map canvas atlas variables are never written to…

… projects

Causes a crash on debug builds, and is generally undesirable anyway
  • Loading branch information
nyalldawson committed May 13, 2019
1 parent f565d7b commit a33c1b1353b0f9ff59b3cd2bbc1685b29e2827b3
Showing with 34 additions and 1 deletion.
  1. +7 −1 src/gui/qgsmapcanvas.cpp
  2. +27 −0 tests/src/python/test_qgsmapcanvas.py
@@ -2126,7 +2126,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
@@ -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()

0 comments on commit a33c1b1

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