Skip to content
Permalink
Browse files

Add simple line custom dash pattern tests

  • Loading branch information
nirvn committed Apr 28, 2019
1 parent db5bfdc commit 8401db1e0b62e04fe5e9a657f5ce15b2834c608c
@@ -90,6 +90,82 @@ def testSimpleLineWithOffset(self):

self.assertTrue(self.imageCheck('symbol_layer', 'simpleline_offset', image))

def testSimpleLineWithCustomDashPattern(self):
""" test that rendering a simple line symbol with custom dash pattern"""
layer = QgsSimpleLineSymbolLayer(QColor(0, 0, 0))
layer.setWidth(0.5)
layer.setCustomDashVector([2, 5])
layer.setUseCustomDashPattern(True)

symbol = QgsLineSymbol()
symbol.changeSymbolLayer(0, layer)

image = QImage(200, 200, QImage.Format_RGB32)
painter = QPainter()
ms = QgsMapSettings()

geom = QgsGeometry.fromWkt('LineString (0 0, 10 0, 10 10, 0 10, 0 0)')
f = QgsFeature()
f.setGeometry(geom)

extent = geom.constGet().boundingBox()
# buffer extent by 10%
extent = extent.buffered((extent.height() + extent.width()) / 20.0)

ms.setExtent(extent)
ms.setOutputSize(image.size())
context = QgsRenderContext.fromMapSettings(ms)
context.setPainter(painter)
context.setScaleFactor(96 / 25.4) # 96 DPI

painter.begin(image)
image.fill(QColor(255, 255, 255))

symbol.startRender(context)
symbol.renderFeature(f, context)
symbol.stopRender(context)
painter.end()

self.assertTrue(self.imageCheck('symbol_layer_simpleline_customdashpattern', 'simpleline_customdashpattern', image))

def testSimpleLineWithCustomDashPatternHairline(self):
""" test that rendering a simple line symbol with custom dash pattern"""
layer = QgsSimpleLineSymbolLayer(QColor(0, 0, 0))
layer.setWidth(0)
layer.setCustomDashVector([3, 3, 2, 2])
layer.setUseCustomDashPattern(True)

symbol = QgsLineSymbol()
symbol.changeSymbolLayer(0, layer)

image = QImage(200, 200, QImage.Format_RGB32)
painter = QPainter()
ms = QgsMapSettings()

geom = QgsGeometry.fromWkt('LineString (0 0, 10 0, 10 10, 0 10, 0 0)')
f = QgsFeature()
f.setGeometry(geom)

extent = geom.constGet().boundingBox()
# buffer extent by 10%
extent = extent.buffered((extent.height() + extent.width()) / 20.0)

ms.setExtent(extent)
ms.setOutputSize(image.size())
context = QgsRenderContext.fromMapSettings(ms)
context.setPainter(painter)
context.setScaleFactor(96 / 25.4) # 96 DPI

painter.begin(image)
image.fill(QColor(255, 255, 255))

symbol.startRender(context)
symbol.renderFeature(f, context)
symbol.stopRender(context)
painter.end()

self.assertTrue(self.imageCheck('symbol_layer_simpleline_customdashpattern_hairline', 'simpleline_customdashpattern_hairline', image))


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

0 comments on commit 8401db1

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