def testBounds(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(50.0, 30.0)) p.append(QPointF(100.0, 10.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "6.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) # scene bounding rect should include symbol outline bounds = shape.sceneBoundingRect() self.assertEqual(bounds.left(), 47.0) self.assertEqual(bounds.right(), 203.0) self.assertEqual(bounds.top(), 7.0) self.assertEqual(bounds.bottom(), 103.0) # rectWithFrame should include symbol outline too bounds = shape.rectWithFrame() self.assertEqual(bounds.left(), -3.0) self.assertEqual(bounds.right(), 153.0) self.assertEqual(bounds.top(), -3.0) self.assertEqual(bounds.bottom(), 93.0)
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.polygon = QgsLayoutItemPolygon(polygon, self.layout) self.layout.addLayoutItem(self.polygon) # style props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "black" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) self.polygon.setSymbol(style)
def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) props = {} props["color"] = "255,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(shape.writeXml(elem, doc, QgsReadWriteContext())) shape2 = QgsLayoutItemPolyline(l) self.assertTrue(shape2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(shape2.nodes(), shape.nodes()) self.assertEqual(shape2.symbol().symbolLayer(0).color().name(), '#ff0000')
def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolygon(p, l) props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "red" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) shape.setSymbol(style) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(shape.writeXml(elem, doc, QgsReadWriteContext())) shape2 = QgsLayoutItemPolygon(l) self.assertTrue(shape2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(shape2.nodes(), shape.nodes()) self.assertEqual(shape2.symbol().symbolLayer(0).color().name(), '#008000') self.assertEqual(shape2.symbol().symbolLayer(0).strokeColor().name(), '#ff0000')
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.polyline = QgsLayoutItemPolyline( polygon, self.layout) self.layout.addLayoutItem(self.polyline) # style props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) self.polyline.setSymbol(style)
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) self.mapSettings = QgsMapSettings() # create composition self.mComposition = QgsComposition(self.mapSettings) self.mComposition.setPaperSize(297, 210) # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.mComposerPolygon = QgsComposerPolygon(polygon, self.mComposition) self.mComposition.addComposerPolygon(self.mComposerPolygon) # style props = {} props["color"] = "green" props["style"] = "solid" props["style_border"] = "solid" props["color_border"] = "black" props["width_border"] = "10.0" props["joinstyle"] = "miter" style = QgsFillSymbol.createSimple(props) self.mComposerPolygon.setPolygonStyleSymbol(style)
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) self.mapSettings = QgsMapSettings() # create composition self.mComposition = QgsComposition(self.mapSettings, QgsProject.instance()) self.mComposition.setPaperSize(297, 210) # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.mComposerPolyline = QgsComposerPolyline( polygon, self.mComposition) self.mComposition.addComposerPolyline(self.mComposerPolyline) # style props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) self.mComposerPolyline.setPolylineStyleSymbol(style)
def testVerticalLine(self): pr = QgsProject() l = QgsLayout(pr) l.initializeDefaults() p = QPolygonF() p.append(QPointF(100.0, 50.0)) p.append(QPointF(100.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) l.addLayoutItem(shape) props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) checker = QgsLayoutChecker( 'composerpolyline_vertline', l) checker.setControlPathPrefix("composer_polyline") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage
def testNodeAtPosition(self): """Test nodeAtPosition method""" p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) p.append(QPointF(100.0, 200.0)) polygon = QgsLayoutItemPolygon(p, self.layout) # default searching radius is 10 rc = polygon.nodeAtPosition(QPointF(100.0, 210.0)) self.assertEqual(rc, -1) # default searching radius is 10 rc = polygon.nodeAtPosition( QPointF(100.0, 210.0), False) self.assertEqual(rc, 3) # default searching radius is 10 rc = polygon.nodeAtPosition( QPointF(100.0, 210.0), True, 10.1) self.assertEqual(rc, 3)
def testNodes(self): polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) p = QgsLayoutItemPolygon(polygon, self.layout) self.assertEqual(p.nodes(), polygon) polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(1000.0, 0.0)) polygon.append(QPointF(2000.0, 100.0)) polygon.append(QPointF(1000.0, 200.0)) p.setNodes(polygon) self.assertEqual(p.nodes(), polygon)
def polygon6(self, p1, p2, p3, p4, p5, p6, layout): py = QPolygonF() py.append(QPointF(p6, p1)) py.append(QPointF(p6, p2)) py.append(QPointF(p5, p2)) py.append(QPointF(p5, p1)) py.append(QPointF(p4, p1)) py.append(QPointF(p4, p2)) py.append(QPointF(p3, p2)) py.append(QPointF(p3, p1)) py.append(QPointF(p4, p1)) py.append(QPointF(p4, p2)) py.append(QPointF(p5, p2)) py.append(QPointF(p5, p1)) py.append(QPointF(p6, p1)) polygonItem = QgsLayoutItemPolygon(py, layout) layout.addItem(polygonItem)