Exemplo n.º 1
0
    def testClipPath(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)

        # must be a closed polygon, in scene coordinates!
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((50 30, 100 10, 200 100, 50 30))')
        self.assertTrue(
            int(shape.itemFlags() & QgsLayoutItem.FlagProvidesClipPath))

        spy = QSignalSpy(shape.clipPathChanged)
        self.assertTrue(shape.addNode(QPointF(150, 110), False))
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((50 30, 100 10, 200 100, 150 110, 50 30))')
        self.assertEqual(len(spy), 1)

        shape.removeNode(3)
        self.assertEqual(len(spy), 2)
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((50 30, 100 10, 200 100, 50 30))')

        shape.moveNode(2, QPointF(180, 100))
        self.assertEqual(len(spy), 3)
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((50 30, 100 10, 180 100, 50 30))')

        shape.setNodes(p)
        self.assertEqual(len(spy), 4)
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((100 40, 150 20, 250 110, 100 40))')

        shape.attemptSetSceneRect(QRectF(30, 10, 100, 200))
        self.assertEqual(shape.clipPath().asWkt(),
                         'Polygon ((30 30, 80 10, 180 100, 30 30))')
        # bit gross - this needs fixing in the item. It shouldn't rely on a draw operation to update the
        # path as a result of a move/resize
        im = QImage()
        p = QPainter(im)
        rc = QgsLayoutUtils.createRenderContextForLayout(l, p)
        shape.draw(QgsLayoutItemRenderContext(rc))
        p.end()
        self.assertEqual(len(spy), 5)