[layouts] Fix data defined atlas margin isn't evaluated

Fixes #19896
This commit is contained in:
Nyall Dawson 2018-10-22 12:48:15 +10:00
parent 4030390acc
commit c94eefb6df
2 changed files with 49 additions and 3 deletions

View File

@ -540,7 +540,7 @@ bool QgsLayoutItemMap::writePropertiesToElement( QDomElement &mapElem, QDomDocum
}
// follow map theme
mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? "true" : "false" );
mapElem.setAttribute( QStringLiteral( "followPreset" ), mFollowVisibilityPreset ? QStringLiteral( "true" ) : QStringLiteral( "false" ) );
mapElem.setAttribute( QStringLiteral( "followPresetName" ), mFollowVisibilityPresetName );
//map rotation
@ -1960,9 +1960,10 @@ void QgsLayoutItemMap::updateAtlasFeature()
}
newExtent = QgsRectangle( xa1, ya1, xa2, ya2 );
if ( mAtlasMargin > 0.0 )
const double evaluatedAtlasMargin = atlasMargin();
if ( evaluatedAtlasMargin > 0.0 )
{
newExtent.scale( 1 + mAtlasMargin );
newExtent.scale( 1 + evaluatedAtlasMargin );
}
}

View File

@ -626,6 +626,51 @@ class TestQgsLayoutAtlas(unittest.TestCase):
QgsProject.instance().removeMapLayer(polygonLayer)
def test_datadefined_margin(self):
polygonLayer = QgsVectorLayer('Polygon?field=margin:int', 'test_polygon', 'memory')
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([0])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((30 30, 40 30, 40 40, 30 40, 30 30))'))
polygonLayer.dataProvider().addFeatures([poly])
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([10])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((10 10, 20 10, 20 20, 10 20, 10 10))'))
polygonLayer.dataProvider().addFeatures([poly])
poly = QgsFeature(polygonLayer.fields())
poly.setAttributes([20])
poly.setGeometry(QgsGeometry.fromWkt('Polygon((50 50, 60 50, 60 60, 50 60, 50 50))'))
polygonLayer.dataProvider().addFeatures([poly])
QgsProject.instance().addMapLayer(polygonLayer)
layout = QgsPrintLayout(QgsProject.instance())
map = QgsLayoutItemMap(layout)
map.setCrs(polygonLayer.crs())
map.attemptSetSceneRect(QRectF(20, 20, 130, 130))
map.setFrameEnabled(True)
map.setLayers([polygonLayer])
map.setExtent(QgsRectangle(0, 0, 100, 50))
layout.addLayoutItem(map)
atlas = layout.atlas()
atlas.setCoverageLayer(polygonLayer)
atlas.setEnabled(True)
map.setAtlasDriven(True)
map.setAtlasScalingMode(QgsLayoutItemMap.Auto)
map.setAtlasMargin(77.0)
map.dataDefinedProperties().setProperty(QgsLayoutObject.MapAtlasMargin, QgsProperty.fromExpression('margin/2'))
atlas.beginRender()
atlas.first()
self.assertEqual(map.extent(), QgsRectangle(25, 30, 45, 40))
self.assertTrue(atlas.next())
self.assertEqual(map.extent(), QgsRectangle(4.5, 9.75, 25.5, 20.25))
self.assertTrue(atlas.next())
self.assertEqual(map.extent(), QgsRectangle(44, 49.5, 66, 60.5))
QgsProject.instance().removeMapLayer(polygonLayer)
if __name__ == '__main__':
unittest.main()