Skip to content
Permalink
Browse files

Update tests

  • Loading branch information
nyalldawson committed Jul 8, 2020
1 parent ff14c6f commit 48c322e983de810bdc6a59459ac41069f1ac194e
Showing with 99 additions and 53 deletions.
  1. +2 −0 src/core/layout/qgslayoutitemmapgrid.cpp
  2. +44 −40 tests/src/core/testqgslayoutmapgrid.cpp
  3. +53 −13 tests/src/python/test_qgslayoutmapgrid.py
  4. BIN ...omposermap_datadefined_annotationdistance/expected_composermap_datadefined_annotationdistance.png
  5. BIN ...ges/composer_mapgrid/expected_composermap_dynamic_5_10/expected_composermap_dynamic_5_10_mask.png
  6. BIN ...composer_mapgrid/expected_composermap_dynamic_5_10_2/expected_composermap_dynamic_5_10_2_mask.png
  7. BIN ...composer_mapgrid/expected_composermap_dynamic_5_10_3/expected_composermap_dynamic_5_10_3_mask.png
  8. BIN ...composer_mapgrid/expected_composermap_dynamic_5_10_4/expected_composermap_dynamic_5_10_4_mask.png
  9. BIN ...rid/expected_composermap_exteriorticks_annotated/expected_composermap_exteriorticks_annotated.png
  10. BIN ...xpected_composermap_exteriorticks_annotated/expected_composermap_exteriorticks_annotated_mask.png
  11. BIN ...d/expected_composermap_exteriorticks_annotated2/expected_composermap_exteriorticks_annotated2.png
  12. BIN ...ected_composermap_exteriorticks_annotated2/expected_composermap_exteriorticks_annotated2_mask.png
  13. BIN ...ser_mapgrid/expected_composermap_filteredannotations/expected_composermap_filteredannotations.png
  14. BIN ...apgrid/expected_composermap_filteredannotations/expected_composermap_filteredannotations_mask.png
  15. BIN ...a/control_images/composer_mapgrid/expected_composermap_grid/default/expected_composermap_grid.png
  16. BIN ...trol_images/composer_mapgrid/expected_composermap_grid/default/expected_composermap_grid_mask.png
  17. BIN .../testdata/control_images/composer_mapgrid/expected_composermap_grid/expected_composermap_grid.png
  18. BIN ...data/control_images/composer_mapgrid/expected_composermap_grid/expected_composermap_grid_mask.png
  19. BIN ...ta/control_images/composer_mapgrid/expected_composermap_grid/fedora/expected_composermap_grid.png
  20. BIN ...ntrol_images/composer_mapgrid/expected_composermap_grid/fedora/expected_composermap_grid_mask.png
  21. BIN ...posermap_interiorexteriorticks_annotated/expected_composermap_interiorexteriorticks_annotated.png
  22. BIN ...map_interiorexteriorticks_annotated/expected_composermap_interiorexteriorticks_annotated_mask.png
  23. BIN ...sermap_interiorexteriorticks_annotated2/expected_composermap_interiorexteriorticks_annotated2.png
  24. BIN ...p_interiorexteriorticks_annotated2/expected_composermap_interiorexteriorticks_annotated2_mask.png
  25. BIN ...rid/expected_composermap_interiorticks_annotated/expected_composermap_interiorticks_annotated.png
  26. BIN ...xpected_composermap_interiorticks_annotated/expected_composermap_interiorticks_annotated_mask.png
  27. BIN ...d/expected_composermap_interiorticks_annotated2/expected_composermap_interiorticks_annotated2.png
  28. BIN ...ected_composermap_interiorticks_annotated2/expected_composermap_interiorticks_annotated2_mask.png
  29. BIN ...r_mapgrid/expected_composermap_lineborder_annotated/expected_composermap_lineborder_annotated.png
  30. BIN ...grid/expected_composermap_lineborder_annotated/expected_composermap_lineborder_annotated_mask.png
  31. BIN ...mapgrid/expected_composermap_lineborder_annotated2/expected_composermap_lineborder_annotated2.png
  32. BIN ...id/expected_composermap_lineborder_annotated2/expected_composermap_lineborder_annotated2_mask.png
  33. BIN ...poser_mapgrid/expected_composermap_rotatedannotations/expected_composermap_rotatedannotations.png
  34. BIN ..._mapgrid/expected_composermap_rotatedannotations/expected_composermap_rotatedannotations_mask.png
  35. BIN ...expected_composermap_verticaldescending_inside/expected_composermap_verticaldescending_inside.png
  36. BIN ...ted_composermap_verticaldescending_inside/expected_composermap_verticaldescending_inside_mask.png
  37. BIN ...pected_composermap_verticaldescending_outside/expected_composermap_verticaldescending_outside.png
  38. BIN ...d_composermap_verticaldescending_outside/expected_composermap_verticaldescending_outside_mask.png
@@ -1091,6 +1091,8 @@ void QgsLayoutItemMapGrid::drawCoordinateAnnotation( QgsRenderContext &context,

QgsLayoutItemMapGrid::BorderSide frameBorder = borderForLineCoord( pos, coordinateType );
double textWidth = QgsTextRenderer::textWidth( context, mAnnotationFormat, QStringList() << annotationString ) / context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
if ( extension )
textWidth *= 1.1; // little bit of extra padding when we are calculating the bounding rect, to account for antialiasing
//relevant for annotations is the height of digits
const QFontMetricsF metrics = QgsTextRenderer::fontMetrics( context, mAnnotationFormat, QgsTextRenderer::FONT_WORKAROUND_SCALE );
double textHeight = ( extension ? ( QgsTextRenderer::textHeight( context, mAnnotationFormat, QChar(), true ) )

Large diffs are not rendered by default.

@@ -12,7 +12,7 @@

import qgis # NOQA

from qgis.PyQt.QtCore import QRectF
from qgis.PyQt.QtCore import QRectF, QDir
from qgis.PyQt.QtGui import QPainter, QColor

from qgis.core import (QgsLayoutItemMap,
@@ -22,7 +22,9 @@
QgsProperty,
QgsLayoutObject,
QgsFontUtils,
QgsProject)
QgsTextFormat,
QgsProject,
QgsCoordinateReferenceSystem)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath, getTestFont
from qgslayoutchecker import QgsLayoutChecker
@@ -33,10 +35,19 @@

class TestQgsLayoutMapGrid(unittest.TestCase):

def setUp(self):
self.report = "<h1>Python QgsLayoutItemMap Tests</h1>\n"

def tearDown(self):
report_file_path = "%s/qgistest.html" % QDir.tempPath()
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testGrid(self):
layout = QgsLayout(QgsProject.instance())
layout.initializeDefaults()
map = QgsLayoutItemMap(layout)
map.setCrs(QgsCoordinateReferenceSystem('EPSG:32633'))
map.attemptSetSceneRect(QRectF(20, 20, 200, 100))
map.setFrameEnabled(True)
map.setBackgroundColor(QColor(150, 100, 100))
@@ -55,21 +66,24 @@ def testGrid(self):
map.grid().setAnnotationEnabled(True)
map.grid().setGridLineColor(QColor(0, 255, 0))
map.grid().setGridLineWidth(0.5)
map.grid().setAnnotationFont(getTestFont())
format = QgsTextFormat.fromQFont(getTestFont('Bold'))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
map.grid().setAnnotationTextFormat(format)
map.grid().setAnnotationPrecision(0)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Left)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Top)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationFontColor(QColor(255, 0, 0, 150))
map.grid().setBlendMode(QPainter.CompositionMode_Overlay)
map.updateBoundingRect()

checker = QgsLayoutChecker('composermap_grid', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
map.grid().setEnabled(False)
map.grid().setAnnotationEnabled(False)

@@ -100,6 +114,7 @@ def testCrossGrid(self):
checker = QgsLayoutChecker('composermap_crossgrid', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()

map.grid().setStyle(QgsLayoutItemMapGrid.Solid)
map.grid().setEnabled(False)
@@ -131,6 +146,7 @@ def testMarkerGrid(self):
checker = QgsLayoutChecker('composermap_markergrid', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()

map.grid().setStyle(QgsLayoutItemMapGrid.Solid)
map.grid().setEnabled(False)
@@ -162,6 +178,7 @@ def testFrameOnly(self):
checker = QgsLayoutChecker('composermap_gridframeonly', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()

map.grid().setStyle(QgsLayoutItemMapGrid.Solid)
map.grid().setEnabled(False)
@@ -186,7 +203,6 @@ def testZebraStyle(self):
map.grid().setIntervalX(2000)
map.grid().setIntervalY(2000)
map.grid().setGridLineColor(QColor(0, 0, 0))
map.grid().setAnnotationFontColor(QColor(0, 0, 0))
map.grid().setBlendMode(QPainter.CompositionMode_SourceOver)
map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra)
map.grid().setFrameWidth(10)
@@ -201,6 +217,7 @@ def testZebraStyle(self):
checker = QgsLayoutChecker('composermap_zebrastyle', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout(0, 100)
self.report += checker.report()
assert myTestResult, myMessage

def testZebraStyleSides(self):
@@ -218,7 +235,6 @@ def testZebraStyleSides(self):
map.grid().setIntervalX(2000)
map.grid().setIntervalY(2000)
map.grid().setGridLineColor(QColor(0, 0, 0))
map.grid().setAnnotationFontColor(QColor(0, 0, 0))
map.grid().setBlendMode(QPainter.CompositionMode_SourceOver)
map.grid().setFrameStyle(QgsLayoutItemMapGrid.Zebra)
map.grid().setFrameWidth(10)
@@ -238,20 +254,23 @@ def testZebraStyleSides(self):
checker = QgsLayoutChecker('composermap_zebrastyle_left', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout(0, 100)
self.report += checker.report()
assert myTestResult, myMessage

map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameTop, True)
map.updateBoundingRect()
checker = QgsLayoutChecker('composermap_zebrastyle_lefttop', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout(0, 100)
self.report += checker.report()
assert myTestResult, myMessage

map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameRight, True)
map.updateBoundingRect()
checker = QgsLayoutChecker('composermap_zebrastyle_lefttopright', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout(0, 100)
self.report += checker.report()
assert myTestResult, myMessage

map.grid().setFrameSideFlag(QgsLayoutItemMapGrid.FrameBottom, True)
@@ -271,7 +290,6 @@ def testInteriorTicks(self):
map.setExtent(myRectangle)
map.grid().setIntervalX(2000)
map.grid().setIntervalY(2000)
map.grid().setAnnotationFontColor(QColor(0, 0, 0))
map.grid().setBlendMode(QPainter.CompositionMode_SourceOver)
map.grid().setFrameStyle(QgsLayoutItemMapGrid.InteriorTicks)
map.grid().setFrameWidth(10)
@@ -284,6 +302,7 @@ def testInteriorTicks(self):
checker = QgsLayoutChecker('composermap_interiorticks', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout(0, 100)
self.report += checker.report()
assert myTestResult, myMessage

def testExpressionContext(self):
@@ -321,15 +340,17 @@ def testDataDefinedEnabled(self):
map.grid().setAnnotationEnabled(True)
map.grid().setGridLineColor(QColor(0, 255, 0))
map.grid().setGridLineWidth(0.5)
map.grid().setAnnotationFont(getTestFont())
format = QgsTextFormat.fromQFont(getTestFont('Bold'))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
map.grid().setAnnotationTextFormat(format)
map.grid().setAnnotationPrecision(0)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Left)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Top)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationFontColor(QColor(255, 0, 0, 150))
map.grid().setBlendMode(QPainter.CompositionMode_Overlay)
map.updateBoundingRect()

@@ -339,6 +360,7 @@ def testDataDefinedEnabled(self):
checker = QgsLayoutChecker('composermap_grid', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

map.grid().dataDefinedProperties().setProperty(QgsLayoutObject.MapGridEnabled, QgsProperty.fromValue(False))
@@ -347,6 +369,7 @@ def testDataDefinedEnabled(self):
checker = QgsLayoutChecker('composermap_datadefined_disabled', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

def testDataDefinedIntervalOffset(self):
@@ -377,6 +400,7 @@ def testDataDefinedIntervalOffset(self):

checker = QgsLayoutChecker('composermap_datadefined_intervaloffset', layout)
checker.setControlPathPrefix("composer_mapgrid")
self.report += checker.report()
myTestResult, myMessage = checker.testLayout()
self.assertTrue(myTestResult, myMessage)

@@ -415,6 +439,7 @@ def testDataDefinedFrameSize(self):
'composermap_datadefined_framesizemargin', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

def testDataDefinedCrossSize(self):
@@ -446,6 +471,7 @@ def testDataDefinedCrossSize(self):

checker = QgsLayoutChecker('composermap_datadefined_crosssize', layout)
checker.setControlPathPrefix("composer_mapgrid")
self.report += checker.report()
myTestResult, myMessage = checker.testLayout()
self.assertTrue(myTestResult, myMessage)

@@ -482,6 +508,7 @@ def testDataDefinedFrameThickness(self):
checker = QgsLayoutChecker('composermap_datadefined_framethickness', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

def testDataDefinedAnnotationDistance(self):
@@ -501,15 +528,19 @@ def testDataDefinedAnnotationDistance(self):
map.grid().setAnnotationEnabled(True)
map.grid().setGridLineColor(QColor(0, 255, 0))
map.grid().setGridLineWidth(0.5)
map.grid().setAnnotationFont(getTestFont('Bold', 20))

format = QgsTextFormat.fromQFont(getTestFont('Bold', 20))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
map.grid().setAnnotationTextFormat(format)

map.grid().setAnnotationPrecision(0)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Left)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Top)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationFontColor(QColor(255, 0, 0, 150))
map.grid().setBlendMode(QPainter.CompositionMode_Overlay)
map.updateBoundingRect()

@@ -519,6 +550,7 @@ def testDataDefinedAnnotationDistance(self):
checker = QgsLayoutChecker('composermap_datadefined_annotationdistance', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

def testDynamicInterval(self):
@@ -539,15 +571,19 @@ def testDynamicInterval(self):
map.grid().setAnnotationEnabled(True)
map.grid().setGridLineColor(QColor(0, 255, 0))
map.grid().setGridLineWidth(0.5)
map.grid().setAnnotationFont(getTestFont('Bold', 20))

format = QgsTextFormat.fromQFont(getTestFont('Bold', 20))
format.setColor(QColor(255, 0, 0))
format.setOpacity(150 / 255)
map.grid().setAnnotationTextFormat(format)

map.grid().setAnnotationPrecision(0)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Left)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDisplay(QgsLayoutItemMapGrid.HideAll, QgsLayoutItemMapGrid.Top)
map.grid().setAnnotationPosition(QgsLayoutItemMapGrid.OutsideMapFrame, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Right)
map.grid().setAnnotationDirection(QgsLayoutItemMapGrid.Horizontal, QgsLayoutItemMapGrid.Bottom)
map.grid().setAnnotationFontColor(QColor(255, 0, 0, 150))
map.grid().setBlendMode(QPainter.CompositionMode_Overlay)
map.updateBoundingRect()

@@ -556,20 +592,23 @@ def testDynamicInterval(self):
checker = QgsLayoutChecker('composermap_dynamic_5_10', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

map.setScale(map.scale() * 1.1)

checker = QgsLayoutChecker('composermap_dynamic_5_10_2', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

map.setScale(map.scale() * 1.8)

checker = QgsLayoutChecker('composermap_dynamic_5_10_3', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)

map.grid().setMinimumIntervalWidth(10)
@@ -579,6 +618,7 @@ def testDynamicInterval(self):
checker = QgsLayoutChecker('composermap_dynamic_5_10_4', layout)
checker.setControlPathPrefix("composer_mapgrid")
myTestResult, myMessage = checker.testLayout()
self.report += checker.report()
self.assertTrue(myTestResult, myMessage)


Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.

0 comments on commit 48c322e

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