def testSignals(self): shape = QgsLegendPatchShape( QgsSymbol.Fill, QgsGeometry.fromWkt('Polygon((5 5, 1 2, 3 4, 5 5))'), False) widget = QgsLegendPatchShapeButton() spy = QSignalSpy(widget.changed) widget.setSymbolType(QgsSymbol.Fill) self.assertEqual(len(spy), 0) widget.setShape(shape) self.assertEqual(len(spy), 1) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), True) widget.setShape(shape) self.assertEqual(len(spy), 2) self.assertTrue(widget.shape().isNull()) widget.setSymbolType(QgsSymbol.Line) self.assertEqual(len(spy), 3) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 2)'), True) widget.setShape(shape) self.assertEqual(len(spy), 4) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 2)') widget.setToDefault() self.assertEqual(len(spy), 5)
def testFills(self): shape = QgsLegendPatchShape(QgsSymbol.Fill, QgsGeometry.fromWkt('Polygon((5 5, 1 2, 3 4, 5 5))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1))), [[[[1.0, 0.0], [0.0, 1.0], [0.5, 0.333], [1.0, 0.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(10, 2))), [[[[10.0, 0.0], [0.0, 2.0], [5.0, 0.667], [10.0, 0.0]]]]) # requesting different symbol type, should return default self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1))), [[[[0.0, 0.5], [1.0, 0.5]]]]) # rings shape = QgsLegendPatchShape(QgsSymbol.Fill, QgsGeometry.fromWkt('Polygon((5 5, 1 2, 3 4, 5 5), (4.5 4.5, 4.4 4.4, 4.5 4.4, 4.5 4.5))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1))), [[[[1.0, 0.0], [0.0, 1.0], [0.5, 0.333], [1.0, 0.0]], [[0.875, 0.167], [0.85, 0.2], [0.875, 0.2], [0.875, 0.167]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(10, 2))), [[[[10.0, 0.0], [0.0, 2.0], [5.0, 0.667], [10.0, 0.0]], [[8.75, 0.333], [8.5, 0.4], [8.75, 0.4], [8.75, 0.333]]]]) # circular shape = QgsLegendPatchShape(QgsSymbol.Fill, QgsGeometry.fromWkt('CurvePolygon(CircularString(5 5, 3 4, 1 2, 3 0, 5 5))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1)))[0][0][:5], [[0.746, -0.0], [0.722, 0.009], [0.698, 0.018], [0.675, 0.028], [0.651, 0.038]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(10, 2)))[0][0][:5], [[7.459, -0.0], [6.83, 0.04], [6.201, 0.09], [5.574, 0.151], [4.947, 0.223]]) # multipolygon shape = QgsLegendPatchShape(QgsSymbol.Fill, QgsGeometry.fromWkt('MultiPolygon(((5 5, 1 2, 3 4, 5 5), (4.5 4.5, 4.4 4.4, 4.5 4.4, 4.5 4.5)),((10 11, 11 11, 11 10, 10 11)))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1))), [[[[0.4, 0.667], [0.0, 1.0], [0.2, 0.778], [0.4, 0.667]], [[0.35, 0.722], [0.34, 0.733], [0.35, 0.733], [0.35, 0.722]]], [[[0.9, 0.0], [1.0, 0.0], [1.0, 0.111], [0.9, 0.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(10, 2))), [[[[4.0, 1.333], [0.0, 2.0], [2.0, 1.556], [4.0, 1.333]], [[3.5, 1.444], [3.4, 1.467], [3.5, 1.467], [3.5, 1.444]]], [[[9.0, 0.0], [10.0, 0.0], [10.0, 0.222], [9.0, 0.0]]]])
def testPreserveAspect(self): # wider shape = QgsLegendPatchShape( QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (1 2))')) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(1, 1))), [[[[1.0, 0.125], [0.0, 0.875]]]]) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(10, 2))), [[[[6.333, 0.0], [3.667, 2.0]]]]) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(2, 10))), [[[[2.0, 4.25], [0.0, 5.75]]]]) # higher shape = QgsLegendPatchShape( QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (2 1))')) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(1, 1))), [[[[0.875, 0.0], [0.125, 1.0]]]]) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(10, 2))), [[[[5.75, 0.0], [4.25, 2.0]]]]) self.assertEqual( self.polys_to_list( shape.toQPolygonF(QgsSymbol.Marker, QSizeF(2, 10))), [[[[2.0, 3.667], [0.0, 6.333]]]])
def testLines(self): shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('LineString(5 5, 1 2)'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1))), [[[[1.0, 0.0], [0.0, 1.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(10, 2))), [[[[10.0, 0.0], [0.0, 2.0]]]]) shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('LineString(1 5, 6 5)'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1))), [[[[0.0, 0.5], [1.0, 0.5]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(10, 2))), [[[[0.0, 1], [10.0, 1.0]]]]) shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('LineString(1 5, 1 10)'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1))), [[[[0.5, 0.0], [0.5, 1.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(10, 2))), [[[[5, 0.0], [5, 2.0]]]]) # requesting different symbol type, should return default self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1))), [[[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]]]) # circularstring shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('CircularString(5 5, 1 2, 3 4)'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1)))[0][0][:5], [[0.342, 0.026], [0.35, 0.023], [0.359, 0.02], [0.367, 0.018], [0.375, 0.016]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(10, 2)))[0][0][:5], [[3.419, 0.051], [3.647, 0.042], [3.875, 0.036], [4.104, 0.034], [4.332, 0.036]]) # multilinestring shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('MultiLineString((5 5, 1 2),(3 6, 4 2))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(1, 1))), [[[[1.0, 0.25], [0.0, 1.0]]], [[[0.5, 0.0], [0.75, 1.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Line, QSizeF(10, 2))), [[[[10.0, 0.5], [0.0, 2.0]]], [[[5.0, 0.0], [7.5, 2.0]]]])
def testWidget(self): shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), False) widget = QgsLegendPatchShapeWidget(None, shape) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 1)') self.assertFalse(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().symbolType(), QgsSymbol.Line) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), True) widget = QgsLegendPatchShapeWidget(None, shape) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 1)') self.assertTrue(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().symbolType(), QgsSymbol.Line) shape = QgsLegendPatchShape( QgsSymbol.Fill, QgsGeometry.fromWkt('Polygon((5 5, 1 2, 3 4, 5 5))'), False) widget = QgsLegendPatchShapeWidget(None, shape) self.assertEqual(widget.shape().geometry().asWkt(), 'Polygon ((5 5, 1 2, 3 4, 5 5))') self.assertFalse(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().symbolType(), QgsSymbol.Fill) shape = QgsLegendPatchShape( QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (1 2))')) widget = QgsLegendPatchShapeWidget(None, shape) self.assertEqual(widget.shape().geometry().asWkt(), 'MultiPoint ((5 5),(1 2))') self.assertTrue(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().symbolType(), QgsSymbol.Marker)
def testReadWriteXml(self): doc = QDomDocument("testdoc") elem = doc.createElement('test') shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('MultiLineString((5 5, 3 4, 1 2), ( 6 6, 6 0))'), False) shape.writeXml(elem, doc, QgsReadWriteContext()) s2 = QgsLegendPatchShape() s2.readXml(elem, QgsReadWriteContext()) self.assertFalse(s2.isNull()) self.assertEqual(s2.geometry().asWkt(), 'MultiLineString ((5 5, 3 4, 1 2),(6 6, 6 0))') self.assertFalse(s2.preserveAspectRatio()) self.assertEqual(s2.symbolType(), QgsSymbol.Line)
def testNull(self): shape = QgsLegendPatchShape() self.assertTrue(shape.isNull()) shape.setGeometry(QgsGeometry.fromWkt('Multipoint( 1 1, 2 2)')) self.assertFalse(shape.isNull()) shape.setGeometry(QgsGeometry()) self.assertTrue(shape.isNull())
def testRenderPolygon(self): shape = QgsLegendPatchShape( QgsSymbol.Fill, QgsGeometry.fromWkt( 'Polygon((1 1 , 6 1, 6 6, 1 1),(4 2, 5 3, 4 3, 4 2))'), False) rendered_image = self.renderPatch(shape) self.assertTrue(self.imageCheck('Polygon', 'polygon', rendered_image))
def testRenderLinePreserve(self): shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString(5 5, 3 4, 1 2)'), True) rendered_image = self.renderPatch(shape) self.assertTrue( self.imageCheck('Line Preserve', 'line_preserve', rendered_image))
def testRenderMarker(self): shape = QgsLegendPatchShape( QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (3 4), (1 2))'), False) rendered_image = self.renderPatch(shape) self.assertTrue( self.imageCheck('Marker', 'marker_multipoint', rendered_image))
def testRenderMultiLine(self): shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt( 'MultiLineString((5 5, 3 4, 1 2), ( 6 6, 6 0))'), True) rendered_image = self.renderPatch(shape) self.assertTrue( self.imageCheck('Multiline', 'multiline', rendered_image))
def testMarkers(self): # shouldn't matter what a point geometry is, it will always be rendered in center of symbol patch shape = QgsLegendPatchShape(QgsSymbol.Marker, QgsGeometry.fromWkt('Point( 5 5 )'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(1, 1))), [[[[0, 0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(10, 2))), [[[[5.0, 1.0]]]]) # requesting different symbol type, should return default self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Fill, QSizeF(1, 1))), [[[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]]]) # ... but a multipoint WILL change the result! shape = QgsLegendPatchShape(QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (1 2))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(1, 1))), [[[[1.0, 0.0], [0.0, 1.0]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(10, 2))), [[[[10.0, 0.0], [0.0, 2.0]]]]) shape = QgsLegendPatchShape(QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (1 2), (4 3))'), False) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(1, 1))), [[[[1.0, 0.0], [0.0, 1.0], [0.75, 0.667]]]]) self.assertEqual(self.polys_to_list(shape.toQPolygonF(QgsSymbol.Marker, QSizeF(10, 2))), [[[[10.0, 0.0], [0.0, 2.0], [7.5, 1.333]]]])
def testWidget(self): widget = QgsLegendPatchShapeButton(dialogTitle='title') self.assertTrue(widget.shape().isNull()) self.assertEqual(widget.dialogTitle(), 'title') widget.setDialogTitle('title2') self.assertEqual(widget.dialogTitle(), 'title2') widget.setSymbolType(QgsSymbol.Fill) self.assertEqual(widget.symbolType(), QgsSymbol.Fill) self.assertTrue(widget.shape().isNull()) shape = QgsLegendPatchShape( QgsSymbol.Fill, QgsGeometry.fromWkt('Polygon((5 5, 1 2, 3 4, 5 5))'), False) widget.setShape(shape) self.assertEqual(widget.shape().geometry().asWkt(), 'Polygon ((5 5, 1 2, 3 4, 5 5))') self.assertFalse(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().symbolType(), QgsSymbol.Fill) # try to set incompatible shape shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), True) widget.setShape(shape) # should be back to default self.assertTrue(widget.shape().isNull()) # change type widget.setSymbolType(QgsSymbol.Line) self.assertEqual(widget.symbolType(), QgsSymbol.Line) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), True) widget.setShape(shape) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 1)') widget.setToDefault() self.assertTrue(widget.shape().isNull())
def testSignals(self): shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), False) widget = QgsLegendPatchShapeWidget(None, shape) spy = QSignalSpy(widget.changed) widget.setShape(shape) self.assertEqual(len(spy), 0) self.assertFalse(widget.shape().preserveAspectRatio()) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), True) widget.setShape(shape) self.assertEqual(len(spy), 1) self.assertTrue(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 1)') self.assertEqual(widget.shape().symbolType(), QgsSymbol.Line) shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 2)'), True) widget.setShape(shape) self.assertEqual(len(spy), 2) self.assertTrue(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().geometry().asWkt(), 'LineString (0 0, 1 2)') self.assertEqual(widget.shape().symbolType(), QgsSymbol.Line) shape = QgsLegendPatchShape( QgsSymbol.Marker, QgsGeometry.fromWkt('MultiPoint((5 5), (1 2))'), True) widget.setShape(shape) self.assertEqual(len(spy), 3) self.assertTrue(widget.shape().preserveAspectRatio()) self.assertEqual(widget.shape().geometry().asWkt(), 'MultiPoint ((5 5),(1 2))') self.assertEqual(widget.shape().symbolType(), QgsSymbol.Marker)
def testBasic(self): shape = QgsLegendPatchShape(QgsSymbol.Line, QgsGeometry.fromWkt('LineString( 0 0, 1 1)'), False) self.assertFalse(shape.isNull()) self.assertEqual(shape.symbolType(), QgsSymbol.Line) self.assertEqual(shape.geometry().asWkt(), 'LineString (0 0, 1 1)') self.assertFalse(shape.preserveAspectRatio()) shape.setSymbolType(QgsSymbol.Marker) self.assertEqual(shape.symbolType(), QgsSymbol.Marker) shape.setGeometry(QgsGeometry.fromWkt('Multipoint( 1 1, 2 2)')) self.assertEqual(shape.geometry().asWkt(), 'MultiPoint ((1 1),(2 2))') shape.setPreserveAspectRatio(True) self.assertTrue(shape.preserveAspectRatio())
def testScaledGeometry(self): """ Test scaling geometry """ shape = QgsLegendPatchShape( QgsSymbol.Line, QgsGeometry.fromWkt('LineString(5 5, 1 2)')) self.assertEqual( shape.scaledGeometry(QSizeF(20, 30)).asWkt(1), 'LineString (20 7.5, 0 22.5)') self.assertEqual( shape.scaledGeometry(QSizeF(200, 300)).asWkt(1), 'LineString (200 75, 0 225)') shape.setScaleToOutputSize(False) self.assertEqual( shape.scaledGeometry(QSizeF(20, 30)).asWkt(1), 'LineString (5 5, 1 2)') self.assertEqual( shape.scaledGeometry(QSizeF(200, 300)).asWkt(1), 'LineString (5 5, 1 2)')