Skip to content
Permalink
Browse files

Always render main annotation layer above map layers in layout maps

  • Loading branch information
nyalldawson committed Sep 1, 2020
1 parent 4d51e48 commit 636653df2327f608e8e83e3befa10467c639cbe9
@@ -33,6 +33,7 @@
#include "qgsapplication.h"
#include "qgsexpressioncontextutils.h"
#include "qgsstyleentityvisitor.h"
#include "qgsannotationlayer.h"

#include <QPainter>
#include <QStyleOptionGraphicsItem>
@@ -1467,6 +1468,10 @@ QgsMapSettings QgsLayoutItemMap::mapSettings( const QgsRectangle &extent, QSizeF
{
//set layers to render
QList<QgsMapLayer *> layers = layersToRender( &expressionContext );

// render main annotation layer above all other layers
layers.insert( 0, mLayout->project()->mainAnnotationLayer() );

jobMapSettings.setLayers( layers );
jobMapSettings.setLayerStyleOverrides( layerStyleOverridesToRender( expressionContext ) );
}
@@ -46,7 +46,8 @@
QgsGeometry,
QgsLayoutItemShape,
QgsMapClippingRegion,
QgsLayoutItemMapOverview)
QgsLayoutItemMapOverview,
QgsAnnotationPolygonItem)

from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
@@ -769,6 +770,58 @@ def testClippingBackgroundFrame(self):
TestQgsLayoutMap.report += checker.report()
self.assertTrue(result, message)

def testMainAnnotationLayer(self):
"""
Make sure main annotation layer is rendered in maps above all other layers
"""
p = QgsProject()

vl = QgsVectorLayer("Polygon?crs=epsg:4326&field=fldtxt:string",
"layer", "memory")
sym3 = QgsFillSymbol.createSimple({'color': '#b200b2'})
vl.renderer().setSymbol(sym3)

p.addMapLayer(vl)
layout = QgsLayout(p)
layout.initializeDefaults()
p.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
map = QgsLayoutItemMap(layout)
map.attemptSetSceneRect(QRectF(10, 10, 180, 180))
map.setFrameEnabled(True)
map.setFrameStrokeWidth(QgsLayoutMeasurement(2, QgsUnitTypes.LayoutMillimeters))
map.setBackgroundEnabled(True)
map.setBackgroundColor(QColor(200, 255, 200))
map.zoomToExtent(QgsRectangle(10, 30, 20, 35))
map.setLayers([vl])
layout.addLayoutItem(map)

# add polygon to layer
f = QgsFeature()
f.setGeometry(QgsGeometry.fromRect(QgsRectangle(5, 25, 25, 45)))
self.assertTrue(vl.dataProvider().addFeatures([f]))

# no annotation yet...
checker = QgsLayoutChecker('composermap_annotation_empty', layout)
checker.setControlPathPrefix("composer_map")
result, message = checker.testLayout()
TestQgsLayoutMap.report += checker.report()
self.assertTrue(result, message)

annotation_layer = p.mainAnnotationLayer()
annotation_layer.setCrs(QgsCoordinateReferenceSystem(4326))
annotation_geom = QgsGeometry.fromRect(QgsRectangle(12, 30, 18, 33))
annotation = QgsAnnotationPolygonItem(annotation_geom.constGet().clone())
sym3 = QgsFillSymbol.createSimple({'color': '#ff0000', 'outline_style': 'no'})
annotation.setSymbol(sym3)
annotation_layer.addItem(annotation)

# annotation must be drawn above map layers
checker = QgsLayoutChecker('composermap_annotation_item', layout)
checker.setControlPathPrefix("composer_map")
result, message = checker.testLayout()
TestQgsLayoutMap.report += checker.report()
self.assertTrue(result, message)


if __name__ == '__main__':
unittest.main()
Binary file not shown.
Binary file not shown.

0 comments on commit 636653d

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