def testRenderLineLayerDataDefined(self): """ test that rendering a line symbol with data defined enabled layer works""" lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr') QgsProject.instance().addMapLayer(lines_layer) layer = QgsSimpleLineSymbolLayer() layer.setDataDefinedProperty(QgsSymbolLayer.PropertyLayerEnabled, QgsProperty.fromExpression("Name='Highway'")) layer.setColor(QColor(100, 150, 150)) layer.setWidth(5) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, layer) lines_layer.setRenderer(QgsSingleSymbolRenderer(symbol)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([lines_layer]) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_layer') renderchecker.setControlName('expected_linelayer_ddenabled') self.assertTrue(renderchecker.runTest('linelayer_ddenabled')) QgsProject.instance().removeMapLayer(lines_layer)
def testRenderLineLayerDataDefined(self): """ test that rendering a line symbol with data defined enabled layer works""" lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr') QgsProject.instance().addMapLayer(lines_layer) layer = QgsSimpleLineSymbolLayer() layer.setDataDefinedProperty( QgsSymbolLayer.PropertyLayerEnabled, QgsProperty.fromExpression("Name='Highway'")) layer.setColor(QColor(100, 150, 150)) layer.setWidth(5) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, layer) lines_layer.setRenderer(QgsSingleSymbolRenderer(symbol)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([lines_layer]) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_layer') renderchecker.setControlName('expected_linelayer_ddenabled') self.assertTrue(renderchecker.runTest('linelayer_ddenabled')) QgsProject.instance().removeMapLayer(lines_layer)
def testOpacityWithDataDefinedColor(self): line_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') line_layer = QgsVectorLayer(line_shp, 'Lines', 'ogr') self.assertTrue(line_layer.isValid()) s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setDataDefinedProperty(QgsSymbolLayer.PropertyStrokeColor, QgsProperty.fromExpression( "if(Name='Arterial', 'red', 'green')")) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) line_symbol.setOpacity(0.5) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) # set opacity on both the symbol and subsymbol, to test that they get combined s.setOpacity(0.5) line_layer.setRenderer(QgsSingleSymbolRenderer(s)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-118.5, 19.0, -81.4, 50.4)) ms.setLayers([line_layer]) # Test rendering renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_hashline') renderchecker.setControlName('expected_hashline_opacityddcolor') res = renderchecker.runTest('expected_hashline_opacityddcolor') self.report += renderchecker.report() self.assertTrue(res)
def testCondenseFillAndOutline(self): """ Test QgsSymbolLayerUtils.condenseFillAndOutline """ self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( None, None)) # not simple fill or line self.assertFalse( QgsSymbolLayerUtils.condenseFillAndOutline( QgsShapeburstFillSymbolLayer(), QgsSimpleLineSymbolLayer())) self.assertFalse( QgsSymbolLayerUtils.condenseFillAndOutline( QgsSimpleFillSymbolLayer(), QgsMarkerLineSymbolLayer())) # simple fill/line fill = QgsSimpleFillSymbolLayer() line = QgsSimpleLineSymbolLayer() # set incompatible settings on outline line.setUseCustomDashPattern(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setDashPatternOffset(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setAlignDashPattern(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setTweakDashPatternOnCorners(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setTrimDistanceStart(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setTrimDistanceEnd(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setDrawInsidePolygon(True) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setRingFilter(QgsSimpleLineSymbolLayer.ExteriorRingOnly) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setOffset(1) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) line = QgsSimpleLineSymbolLayer() line.setDataDefinedProperty(QgsSymbolLayer.PropertyTrimEnd, QgsProperty.fromValue(4)) self.assertFalse(QgsSymbolLayerUtils.condenseFillAndOutline( fill, line)) # compatible! line = QgsSimpleLineSymbolLayer() line.setColor(QColor(255, 0, 0)) line.setWidth(1.2) line.setWidthUnit(QgsUnitTypes.RenderPoints) line.setWidthMapUnitScale(QgsMapUnitScale(1, 2)) line.setPenJoinStyle(Qt.MiterJoin) line.setPenStyle(Qt.DashDotDotLine) self.assertTrue(QgsSymbolLayerUtils.condenseFillAndOutline(fill, line)) self.assertEqual(fill.strokeColor(), QColor(255, 0, 0)) self.assertEqual(fill.strokeWidth(), 1.2) self.assertEqual(fill.strokeWidthUnit(), QgsUnitTypes.RenderPoints) self.assertEqual(fill.strokeWidthMapUnitScale(), QgsMapUnitScale(1, 2)) self.assertEqual(fill.penJoinStyle(), Qt.MiterJoin) self.assertEqual(fill.strokeStyle(), Qt.DashDotDotLine)