Exemple #1
0
    def testBounds(self):
        pr = QgsProject()
        l = QgsLayout(pr)

        shape = QgsLayoutItemMarker(l)
        shape.attemptMove(
            QgsLayoutPoint(10, 20, QgsUnitTypes.LayoutMillimeters))
        props = {}
        props["shape"] = "square"
        props["size"] = "6"
        props["outline_width"] = "2"
        style = QgsMarkerSymbol.createSimple(props)
        shape.setSymbol(style)

        # these must match symbol size
        size = shape.sizeWithUnits().toQSizeF()
        self.assertAlmostEqual(size.width(), 8.0846, 1)
        self.assertAlmostEqual(size.height(), 8.08, 1)
        pos = shape.positionWithUnits().toQPointF()
        self.assertAlmostEqual(pos.x(), 10.0, 1)
        self.assertAlmostEqual(pos.y(), 20.0, 1)

        # these are just rough!
        bounds = shape.sceneBoundingRect()
        self.assertAlmostEqual(bounds.left(), 0.957, 1)
        self.assertAlmostEqual(bounds.right(), 19.04, 1)
        self.assertAlmostEqual(bounds.top(), 10.95, 1)
        self.assertAlmostEqual(bounds.bottom(), 29.04, 1)
Exemple #2
0
    def testDisplayName(self):
        """Test if displayName is valid"""

        layout = QgsLayout(QgsProject.instance())
        marker = QgsLayoutItemMarker(layout)
        self.assertEqual(marker.displayName(), "<Marker>")
        marker.setId('id')
        self.assertEqual(marker.displayName(), "id")
Exemple #3
0
    def testRender(self):
        """Test marker rendering."""
        layout = QgsLayout(QgsProject.instance())
        layout.initializeDefaults()
        marker = QgsLayoutItemMarker(layout)
        marker.attemptMove(
            QgsLayoutPoint(100, 50, QgsUnitTypes.LayoutMillimeters))
        props = {}
        props["color"] = "0,255,255"
        props["outline_width"] = "4"
        props["outline_color"] = "0,0,0"
        props["size"] = "14.4"

        style = QgsMarkerSymbol.createSimple(props)
        marker.setSymbol(style)
        layout.addLayoutItem(marker)
        checker = QgsLayoutChecker('layout_marker_render', layout)
        checker.setControlPathPrefix("layout_marker")
        myTestResult, myMessage = checker.testLayout()
        assert myTestResult, myMessage
Exemple #4
0
    def testReadWriteXml(self):
        pr = QgsProject()
        l = QgsLayout(pr)
        marker = QgsLayoutItemMarker(l)
        l.addLayoutItem(marker)

        map = QgsLayoutItemMap(l)
        l.addLayoutItem(map)

        props = {}
        props["color"] = "green"
        props["outline_style"] = "no"
        props["size"] = "4.4"

        style = QgsMarkerSymbol.createSimple(props)
        marker.setSymbol(style)

        marker.setLinkedMap(map)
        marker.setNorthMode(QgsLayoutNorthArrowHandler.TrueNorth)
        marker.setNorthOffset(15)

        # save original item to xml
        doc = QDomDocument("testdoc")
        elem = doc.createElement("test")
        self.assertTrue(marker.writeXml(elem, doc, QgsReadWriteContext()))

        marker2 = QgsLayoutItemMarker(l)
        self.assertTrue(
            marker2.readXml(elem.firstChildElement(), doc,
                            QgsReadWriteContext()))
        marker2.finalizeRestoreFromXml()

        self.assertEqual(marker2.symbol().symbolLayer(0).color().name(),
                         '#008000')
        self.assertEqual(marker2.symbol().symbolLayer(0).strokeStyle(),
                         Qt.NoPen)
        self.assertEqual(marker2.symbol().symbolLayer(0).size(), 4.4)

        self.assertEqual(marker2.linkedMap(), map)
        self.assertEqual(marker2.northMode(),
                         QgsLayoutNorthArrowHandler.TrueNorth)
        self.assertEqual(marker2.northOffset(), 15.0)
Exemple #5
0
    def testType(self):
        """Test if type is valid"""
        layout = QgsLayout(QgsProject.instance())
        marker = QgsLayoutItemMarker(layout)

        self.assertEqual(marker.type(), QgsLayoutItemRegistry.LayoutMarker)
Exemple #6
0
    def testRenderWithNorthRotation(self):
        layout = QgsLayout(QgsProject.instance())
        layout.initializeDefaults()

        map = QgsLayoutItemMap(layout)
        map.setExtent(QgsRectangle(0, -256, 256, 0))

        marker = QgsLayoutItemMarker(layout)
        marker.attemptMove(
            QgsLayoutPoint(100, 50, QgsUnitTypes.LayoutMillimeters))
        props = {}
        props["color"] = "0,255,255"
        props["outline_style"] = "no"
        props["size"] = "14.4"
        props["name"] = "arrow"
        props["angle"] = "10"

        marker.setLinkedMap(map)
        self.assertEqual(marker.linkedMap(), map)

        marker.setNorthMode(QgsLayoutNorthArrowHandler.GridNorth)
        map.setMapRotation(35)
        self.assertEqual(marker.northArrowRotation(), 35)

        # when rendering, north arrow rotation must be ADDED to symbol rotation! ie.
        # it does not replace it

        style = QgsMarkerSymbol.createSimple(props)
        marker.setSymbol(style)
        layout.addLayoutItem(marker)
        checker = QgsLayoutChecker('layout_marker_render_north', layout)
        checker.setControlPathPrefix("layout_marker")
        myTestResult, myMessage = checker.testLayout()
        assert myTestResult, myMessage
Exemple #7
0
    def testTrueNorth(self):
        """Test syncing picture to true north"""

        layout = QgsLayout(QgsProject.instance())

        map = QgsLayoutItemMap(layout)
        map.attemptSetSceneRect(QRectF(0, 0, 10, 10))
        map.setCrs(QgsCoordinateReferenceSystem.fromEpsgId(3575))
        map.setExtent(
            QgsRectangle(-2126029.962, -2200807.749, -119078.102, -757031.156))
        layout.addLayoutItem(map)

        marker = QgsLayoutItemMarker(layout)
        layout.addLayoutItem(marker)

        marker.setLinkedMap(map)
        self.assertEqual(marker.linkedMap(), map)

        marker.setNorthMode(QgsLayoutNorthArrowHandler.TrueNorth)
        self.assertAlmostEqual(marker.northArrowRotation(), 37.20, 1)

        # shift map
        map.setExtent(
            QgsRectangle(2120672.293, -3056394.691, 2481640.226, -2796718.780))
        self.assertAlmostEqual(marker.northArrowRotation(), -38.18, 1)

        # rotate map
        map.setMapRotation(45)
        self.assertAlmostEqual(marker.northArrowRotation(), -38.18 + 45, 1)

        # add an offset
        marker.setNorthOffset(-10)
        self.assertAlmostEqual(marker.northArrowRotation(), -38.18 + 35, 1)
Exemple #8
0
    def testGridNorth(self):
        """Test syncing picture to grid north"""

        layout = QgsLayout(QgsProject.instance())

        map = QgsLayoutItemMap(layout)
        map.setExtent(QgsRectangle(0, -256, 256, 0))
        layout.addLayoutItem(map)

        marker = QgsLayoutItemMarker(layout)
        layout.addLayoutItem(marker)

        marker.setLinkedMap(map)
        self.assertEqual(marker.linkedMap(), map)

        marker.setNorthMode(QgsLayoutNorthArrowHandler.GridNorth)
        map.setMapRotation(45)
        self.assertEqual(marker.northArrowRotation(), 45)

        # add an offset
        marker.setNorthOffset(-10)
        self.assertEqual(marker.northArrowRotation(), 35)
Exemple #9
0
    def testNorthArrowWithMapItemRotation(self):
        """Test picture rotation when map item is also rotated"""

        layout = QgsLayout(QgsProject.instance())

        map = QgsLayoutItemMap(layout)
        map.setExtent(QgsRectangle(0, -256, 256, 0))
        layout.addLayoutItem(map)

        marker = QgsLayoutItemMarker(layout)
        layout.addLayoutItem(marker)

        marker.setLinkedMap(map)
        self.assertEqual(marker.linkedMap(), map)

        marker.setNorthMode(QgsLayoutNorthArrowHandler.GridNorth)
        map.setItemRotation(45)
        self.assertEqual(marker.northArrowRotation(), 45)
        map.setMapRotation(-34)
        self.assertEqual(marker.northArrowRotation(), 11)

        # add an offset
        marker.setNorthOffset(-10)
        self.assertEqual(marker.northArrowRotation(), 1)

        map.setItemRotation(55)
        self.assertEqual(marker.northArrowRotation(), 11)