Exemplo n.º 1
0
    def testReadWriteXml(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()
        guides = l.guides()

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(6, QgsUnitTypes.LayoutInches), l.pageCollection().page(0))
        guides.addGuide(g2)

        guides.setVisible(False)

        doc = QDomDocument("testdoc")
        elem = doc.createElement("test")
        self.assertTrue(guides.writeXml(elem, doc, QgsReadWriteContext()))

        l2 = QgsLayout(p)
        l2.initializeDefaults()
        guides2 = l2.guides()

        self.assertTrue(guides2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext()))
        guide_list = guides2.guidesOnPage(0)
        self.assertEqual(len(guide_list), 2)

        self.assertEqual(guide_list[0].orientation(), Qt.Horizontal)
        self.assertEqual(guide_list[0].position().length(), 5.0)
        self.assertEqual(guide_list[0].position().units(), QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(guide_list[1].orientation(), Qt.Vertical)
        self.assertEqual(guide_list[1].position().length(), 6.0)
        self.assertEqual(guide_list[1].position().units(), QgsUnitTypes.LayoutInches)
Exemplo n.º 2
0
    def testRenderRegion(self):
        l = QgsLayout(QgsProject.instance())
        l.initializeDefaults()

        # add a guide, to ensure it is not included in export
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15, QgsUnitTypes.LayoutMillimeters), l.pageCollection().page(0))
        l.guides().addGuide(g1)

        # add some items
        item1 = QgsLayoutItemShape(l)
        item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
        fill = QgsSimpleFillSymbolLayer()
        fill_symbol = QgsFillSymbol()
        fill_symbol.changeSymbolLayer(0, fill)
        fill.setColor(Qt.green)
        fill.setStrokeStyle(Qt.NoPen)
        item1.setSymbol(fill_symbol)
        l.addItem(item1)

        # get width/height, create image and render the composition to it
        size = QSize(560, 509)
        output_image = QImage(size, QImage.Format_RGB32)

        output_image.setDotsPerMeterX(self.dots_per_meter)
        output_image.setDotsPerMeterY(self.dots_per_meter)
        QgsMultiRenderChecker.drawBackground(output_image)
        painter = QPainter(output_image)
        exporter = QgsLayoutExporter(l)

        exporter.renderRegion(painter, QRectF(5, 10, 110, 100))
        painter.end()

        rendered_file_path = os.path.join(self.basetestpath, 'test_renderregion.png')
        output_image.save(rendered_file_path, "PNG")
        self.assertTrue(self.checkImage('renderregion', 'renderregion', rendered_file_path))
Exemplo n.º 3
0
    def testReadWriteXml(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.setName('my layout')
        l.setUnits(QgsUnitTypes.LayoutInches)
        collection = l.pageCollection()

        # add a page
        page = QgsLayoutItemPage(l)
        page.setPageSize('A6')
        collection.addPage(page)

        grid = l.gridSettings()
        grid.setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutPoints))

        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters),
                            l.pageCollection().page(0))
        l.guides().addGuide(g1)

        snapper = l.snapper()
        snapper.setSnapTolerance(7)

        # add some items
        item1 = QgsLayoutItemMap(l)
        item1.setId('xxyyxx')
        l.addItem(item1)
        item2 = QgsLayoutItemMap(l)
        item2.setId('zzyyzz')
        l.addItem(item2)

        doc = QDomDocument("testdoc")
        elem = l.writeXml(doc, QgsReadWriteContext())

        l2 = QgsLayout(p)
        self.assertTrue(l2.readXml(elem, doc, QgsReadWriteContext()))
        self.assertEqual(l2.name(), 'my layout')
        self.assertEqual(l2.units(), QgsUnitTypes.LayoutInches)

        collection2 = l2.pageCollection()
        self.assertEqual(collection2.pageCount(), 1)
        self.assertAlmostEqual(collection2.page(0).pageSize().width(), 105, 4)
        self.assertEqual(collection2.page(0).pageSize().height(), 148)
        self.assertEqual(l2.gridSettings().resolution().length(), 5.0)
        self.assertEqual(l2.gridSettings().resolution().units(), QgsUnitTypes.LayoutPoints)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].orientation(), Qt.Horizontal)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].position().length(), 5.0)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].position().units(), QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l2.snapper().snapTolerance(), 7)

        # check restored items
        new_item1 = l2.itemByUuid(item1.uuid())
        self.assertTrue(new_item1)
        self.assertEqual(new_item1.id(), 'xxyyxx')
        new_item2 = l2.itemByUuid(item2.uuid())
        self.assertTrue(new_item2)
        self.assertEqual(new_item2.id(), 'zzyyzz')
Exemplo n.º 4
0
    def testSnapPoint(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        # first test snapping to grid
        l.gridSettings().setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
        s.setSnapToGrid(True)
        s.setSnapTolerance(1)

        point, snapped = s.snapPoint(QPointF(1, 1), 1)
        self.assertTrue(snapped)
        self.assertEqual(point, QPointF(0, 0))

        s.setSnapToGrid(False)
        point, snapped = s.snapPoint(QPointF(1, 1), 1)
        self.assertFalse(snapped)
        self.assertEqual(point, QPointF(1, 1))

        # test that guide takes precedence
        s.setSnapToGrid(True)
        s.setSnapToGuides(True)
        guides.addGuide(QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(0.5), page))
        point, snapped = s.snapPoint(QPointF(1, 1), 1)
        self.assertTrue(snapped)
        self.assertEqual(point, QPointF(0, 0.5))
Exemplo n.º 5
0
    def testSnapRect(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        # first test snapping to grid
        l.gridSettings().setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
        s.setSnapToItems(False)
        s.setSnapToGrid(True)
        s.setSnapTolerance(1)

        rect, snapped = s.snapRect(QRectF(1, 1, 2, 1), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0, 0, 2, 1))
        rect, snapped = s.snapRect(QRectF(1, 1, 3.5, 3.5), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(1.5, 1.5, 3.5, 3.5))

        s.setSnapToItems(False)
        s.setSnapToGrid(False)
        rect, snapped = s.snapRect(QRectF(1, 1, 3.5, 3.5), 1)
        self.assertFalse(snapped)
        self.assertEqual(rect, QRectF(1, 1, 3.5, 3.5))

        # test that guide takes precedence
        s.setSnapToGrid(True)
        s.setSnapToGuides(True)
        guides.addGuide(QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(0.5), page))
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.5, 2.0, 3.0))

        # add an item
        item1 = QgsLayoutItemMap(l)
        item1.attemptMove(QgsLayoutPoint(121, 1.1, QgsUnitTypes.LayoutMillimeters))
        l.addItem(item1)

        # test that guide takes precedence over item
        s.setSnapToGrid(True)
        s.setSnapToGuides(True)
        s.setSnapToItems(True)
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.5, 2.0, 3.0))
        # but items take precedence over grid
        s.setSnapToGuides(False)
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 1.1, 2.0, 3.0))

        # ... unless item is ignored!
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1, None, None, [item1])
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.0, 2.0, 3.0))
Exemplo n.º 6
0
    def testCollection(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults() # add a page
        guides = l.guides()

        # no guides initially
        self.assertEqual(guides.rowCount(QModelIndex()), 0)
        self.assertFalse(guides.data(QModelIndex(), QgsLayoutGuideCollection.OrientationRole))
        self.assertFalse(guides.guides(Qt.Horizontal))
        self.assertFalse(guides.guides(Qt.Vertical))

        # add a guide
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        self.assertEqual(guides.rowCount(QModelIndex()), 1)
        self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.OrientationRole), Qt.Horizontal)
        self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.PositionRole), 5)
        self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.UnitsRole), QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(guides.data(guides.index(0, 0), QgsLayoutGuideCollection.PageRole), 0)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1])
        self.assertFalse(guides.guides(Qt.Vertical))
        self.assertEqual(guides.guidesOnPage(0), [g1])
        self.assertEqual(guides.guidesOnPage(1), [])

        g2 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(0))
        guides.addGuide(g2)
        self.assertEqual(guides.rowCount(QModelIndex()), 2)
        self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.OrientationRole), Qt.Horizontal)
        self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.PositionRole), 15)
        self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.UnitsRole), QgsUnitTypes.LayoutMillimeters)
        self.assertEqual(guides.data(guides.index(1, 0), QgsLayoutGuideCollection.PageRole), 0)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1, g2])
        self.assertFalse(guides.guides(Qt.Vertical))
        self.assertEqual(guides.guidesOnPage(0), [g1, g2])

        page2 = QgsLayoutItemPage(l)
        page2.setPageSize('A3')
        l.pageCollection().addPage(page2)
        g3 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(1))
        guides.addGuide(g3)
        self.assertEqual(guides.rowCount(QModelIndex()), 3)
        self.assertEqual(guides.data(guides.index(2, 0), QgsLayoutGuideCollection.OrientationRole), Qt.Vertical)
        self.assertEqual(guides.data(guides.index(2, 0), QgsLayoutGuideCollection.PositionRole), 35)
        self.assertEqual(guides.data(guides.index(2, 0), QgsLayoutGuideCollection.UnitsRole), QgsUnitTypes.LayoutMillimeters)
        self.assertEqual(guides.data(guides.index(2, 0), QgsLayoutGuideCollection.PageRole), 1)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1, g2])
        self.assertEqual(guides.guides(Qt.Horizontal, 0), [g1, g2])
        self.assertEqual(guides.guides(Qt.Horizontal, 1), [])
        self.assertEqual(guides.guides(Qt.Vertical), [g3])
        self.assertEqual(guides.guides(Qt.Vertical, 0), [])
        self.assertEqual(guides.guides(Qt.Vertical, 1), [g3])
        self.assertEqual(guides.guides(Qt.Vertical, 2), [])
        self.assertEqual(guides.guidesOnPage(0), [g1, g2])
        self.assertEqual(guides.guidesOnPage(1), [g3])
Exemplo n.º 7
0
    def testSnapPointsToGuides(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        s.setSnapToGuides(True)
        s.setSnapTolerance(1)

        # no guides
        delta, snapped = s.snapPointsToGuides([0.5], Qt.Vertical, 1)
        self.assertFalse(snapped)

        guides.addGuide(QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(1), page))
        point, snapped = s.snapPointsToGuides([0.7], Qt.Vertical, 1)
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, 0.3, 5)

        point, snapped = s.snapPointsToGuides([0.7, 1.2], Qt.Vertical, 1)
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, -0.2, 5)

        # outside tolerance
        point, snapped = s.snapPointsToGuides([5.5], Qt.Vertical, 1)
        self.assertFalse(snapped)

        # snapping off
        s.setSnapToGuides(False)
        point, snapped = s.snapPointsToGuides([0.5], Qt.Vertical, 1)
        self.assertFalse(snapped)

        s.setSnapToGuides(True)

        # snap to hoz
        point, snapped = s.snapPointsToGuides([0.5], Qt.Horizontal, 1)
        self.assertFalse(snapped)
        guides.addGuide(QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(1), page))
        point, snapped = s.snapPointsToGuides([0.7], Qt.Horizontal, 1)
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, 0.3, 5)
        point, snapped = s.snapPointsToGuides([0.7, 1.2], Qt.Horizontal, 1)
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, -0.2, 5)
        point, snapped = s.snapPointsToGuides([0.7, 0.9, 1.2], Qt.Horizontal, 1)
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, 0.1, 5)

        # with different pixel scale
        point, snapped = s.snapPointsToGuides([0.5, 1.5], Qt.Horizontal, 3)
        self.assertFalse(snapped)
Exemplo n.º 8
0
    def testRemoveGuide(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults() # add a page
        guides = l.guides()

        # add a guide
        g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
        guides.removeGuide(None)
        self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
        guides.removeGuide(g1)
        self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])
Exemplo n.º 9
0
    def testClear(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults() # add a page
        guides = l.guides()

        # add a guide
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g2)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1, g2])
        guides.clear()
        self.assertEqual(guides.guides(Qt.Horizontal), [])
Exemplo n.º 10
0
    def testGuideLayoutPosition(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()
        guides = l.guides()

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(1, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)

        # set position in layout units (mm)
        guides.setGuideLayoutPosition(g1, 50)

        self.assertEqual(g1.position().length(), 5.0)
        self.assertEqual(g1.position().units(), QgsUnitTypes.LayoutCentimeters)
Exemplo n.º 11
0
    def testSetVisible(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()
        guides = l.guides()

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(6), l.pageCollection().page(0))
        guides.addGuide(g2)

        guides.setVisible(False)
        self.assertFalse(g1.item().isVisible())
        self.assertFalse(g2.item().isVisible())
        guides.setVisible(True)
        self.assertTrue(g1.item().isVisible())
        self.assertTrue(g2.item().isVisible())
Exemplo n.º 12
0
    def testSnapPointToGuides(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        s.setSnapToGuides(True)
        s.setSnapTolerance(1)

        # no guides
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Vertical, 1)
        self.assertFalse(snapped)

        guides.addGuide(QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(1), page))
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Vertical, 1)
        self.assertTrue(snapped)
        self.assertEqual(point, 1)

        # outside tolerance
        point, snapped = s.snapPointToGuides(5.5, QgsLayoutGuide.Vertical, 1)
        self.assertFalse(snapped)

        # snapping off
        s.setSnapToGuides(False)
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Vertical, 1)
        self.assertFalse(snapped)

        s.setSnapToGuides(True)
        # snap to hoz
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Horizontal, 1)
        self.assertFalse(snapped)
        guides.addGuide(QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(1), page))
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Horizontal, 1)
        self.assertTrue(snapped)
        self.assertEqual(point, 1)

        # with different pixel scale
        point, snapped = s.snapPointToGuides(0.5, QgsLayoutGuide.Horizontal, 3)
        self.assertFalse(snapped)
Exemplo n.º 13
0
    def testDeleteRows(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()
        guides = l.guides()

        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(0))
        guides.addGuide(g2)
        g3 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(0))
        guides.addGuide(g3)

        self.assertTrue(guides.removeRows(1, 1))
        self.assertEqual(guides.guides(Qt.Horizontal), [g1])
        self.assertEqual(guides.guides(Qt.Vertical), [g3])

        self.assertTrue(guides.removeRows(0, 2))
        self.assertEqual(guides.guides(Qt.Horizontal), [])
        self.assertEqual(guides.guides(Qt.Vertical), [])
Exemplo n.º 14
0
    def testApplyToOtherPages(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()
        page2 = QgsLayoutItemPage(l)
        page2.setPageSize('A6')
        l.pageCollection().addPage(page2)
        guides = l.guides()

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(6), l.pageCollection().page(0))
        guides.addGuide(g2)
        g3 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(190), l.pageCollection().page(0))
        guides.addGuide(g3)
        g4 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(1), l.pageCollection().page(1))
        guides.addGuide(g4)

        # apply guides from page 0 - should delete g4
        guides.applyGuidesToAllOtherPages(0)
        self.assertEqual(guides.guides(Qt.Horizontal, 0), [g1, g3])
        self.assertEqual(guides.guides(Qt.Vertical, 0), [g2])
        self.assertTrue(sip.isdeleted(g4))

        # g3 is outside of page 2 bounds - should not be copied
        self.assertEqual(len(guides.guides(Qt.Horizontal, 1)), 1)
        self.assertEqual(guides.guides(Qt.Horizontal, 1)[0].position().length(), 5)
        self.assertEqual(len(guides.guides(Qt.Vertical, 1)), 1)
        self.assertEqual(guides.guides(Qt.Vertical, 1)[0].position().length(), 6)

        # apply guides from page 1 to 0
        guides.applyGuidesToAllOtherPages(1)
        self.assertTrue(sip.isdeleted(g1))
        self.assertTrue(sip.isdeleted(g2))
        self.assertTrue(sip.isdeleted(g3))
        self.assertEqual(len(guides.guides(Qt.Horizontal, 0)), 1)
        self.assertEqual(guides.guides(Qt.Horizontal, 0)[0].position().length(), 5)
        self.assertEqual(len(guides.guides(Qt.Vertical, 0)), 1)
        self.assertEqual(guides.guides(Qt.Vertical, 0)[0].position().length(), 6)
Exemplo n.º 15
0
    def testQgsLayoutGuideProxyModel(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults() # add a page
        page2 = QgsLayoutItemPage(l)
        page2.setPageSize('A3')
        l.pageCollection().addPage(page2)
        guides = l.guides()

        hoz_filter = QgsLayoutGuideProxyModel(None, Qt.Horizontal, 0)
        hoz_filter.setSourceModel(guides)
        hoz_page_1_filter = QgsLayoutGuideProxyModel(None, Qt.Horizontal, 1)
        hoz_page_1_filter.setSourceModel(guides)
        vert_filter = QgsLayoutGuideProxyModel(None, Qt.Vertical, 0)
        vert_filter.setSourceModel(guides)

        # no guides initially
        self.assertEqual(hoz_filter.rowCount(QModelIndex()), 0)
        self.assertEqual(hoz_page_1_filter.rowCount(QModelIndex()), 0)
        self.assertEqual(vert_filter.rowCount(QModelIndex()), 0)

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        guides.addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15), l.pageCollection().page(1))
        guides.addGuide(g2)
        g3 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(35), l.pageCollection().page(0))
        guides.addGuide(g3)

        self.assertEqual(hoz_filter.rowCount(QModelIndex()), 1)
        self.assertEqual(hoz_filter.data(hoz_filter.index(0, 0), QgsLayoutGuideCollection.PositionRole), 5)
        self.assertEqual(hoz_page_1_filter.rowCount(QModelIndex()), 1)
        self.assertEqual(hoz_page_1_filter.data(hoz_page_1_filter.index(0, 0), QgsLayoutGuideCollection.PositionRole), 15)
        self.assertEqual(vert_filter.rowCount(QModelIndex()), 1)
        self.assertEqual(vert_filter.data(vert_filter.index(0, 0), QgsLayoutGuideCollection.PositionRole), 35)

        # change page
        hoz_page_1_filter.setPage(0)
        self.assertEqual(hoz_page_1_filter.rowCount(QModelIndex()), 1)
        self.assertEqual(hoz_page_1_filter.data(hoz_page_1_filter.index(0, 0), QgsLayoutGuideCollection.PositionRole), 5)
Exemplo n.º 16
0
    def testReadWriteXml(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.setName('my layout')
        l.setUnits(QgsUnitTypes.LayoutInches)
        collection = l.pageCollection()

        # add a page
        page = QgsLayoutItemPage(l)
        page.setPageSize('A6')
        collection.addPage(page)

        grid = l.gridSettings()
        grid.setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutPoints))

        g1 = QgsLayoutGuide(
            Qt.Horizontal,
            QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters),
            l.pageCollection().page(0))
        l.guides().addGuide(g1)

        snapper = l.snapper()
        snapper.setSnapTolerance(7)

        # add some items
        item1 = QgsLayoutItemMap(l)
        item1.setId('xxyyxx')
        l.addItem(item1)
        item2 = QgsLayoutItemMap(l)
        item2.setId('zzyyzz')
        l.addItem(item2)

        doc = QDomDocument("testdoc")
        elem = l.writeXml(doc, QgsReadWriteContext())

        l2 = QgsLayout(p)
        self.assertTrue(l2.readXml(elem, doc, QgsReadWriteContext()))
        self.assertEqual(l2.name(), 'my layout')
        self.assertEqual(l2.units(), QgsUnitTypes.LayoutInches)

        collection2 = l2.pageCollection()
        self.assertEqual(collection2.pageCount(), 1)
        self.assertAlmostEqual(collection2.page(0).pageSize().width(), 105, 4)
        self.assertEqual(collection2.page(0).pageSize().height(), 148)
        self.assertEqual(l2.gridSettings().resolution().length(), 5.0)
        self.assertEqual(l2.gridSettings().resolution().units(),
                         QgsUnitTypes.LayoutPoints)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].orientation(),
                         Qt.Horizontal)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].position().length(),
                         5.0)
        self.assertEqual(l2.guides().guidesOnPage(0)[0].position().units(),
                         QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l2.snapper().snapTolerance(), 7)

        # check restored items
        new_item1 = l2.itemByUuid(item1.uuid())
        self.assertTrue(new_item1)
        self.assertEqual(new_item1.id(), 'xxyyxx')
        new_item2 = l2.itemByUuid(item2.uuid())
        self.assertTrue(new_item2)
        self.assertEqual(new_item2.id(), 'zzyyzz')
Exemplo n.º 17
0
    def testResizeToContents(self):
        p = QgsProject()
        l = QgsLayout(p)

        shape1 = QgsLayoutItemShape(l)
        shape1.attemptResize(QgsLayoutSize(90, 50))
        shape1.attemptMove(QgsLayoutPoint(90, 50))
        shape1.setItemRotation(45, False)
        l.addLayoutItem(shape1)
        shape2 = QgsLayoutItemShape(l)
        shape2.attemptResize(QgsLayoutSize(110, 50))
        shape2.attemptMove(QgsLayoutPoint(100, 150), True, False, 0)
        l.addLayoutItem(shape2)
        shape3 = QgsLayoutItemShape(l)
        l.addLayoutItem(shape3)
        shape3.attemptResize(QgsLayoutSize(50, 100))
        shape3.attemptMove(QgsLayoutPoint(210, 250), True, False, 0)
        shape4 = QgsLayoutItemShape(l)
        l.addLayoutItem(shape4)
        shape4.attemptResize(QgsLayoutSize(50, 30))
        shape4.attemptMove(QgsLayoutPoint(10, 340), True, False, 0)
        shape4.setVisibility(False)

        # resize with no existing pages
        l.pageCollection().resizeToContents(QgsMargins(1, 2, 3, 4),
                                            QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l.pageCollection().pageCount(), 1)

        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().width(), 290.3, 2)
        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().height(), 380.36, 2)
        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().units(),
            QgsUnitTypes.LayoutMillimeters)

        self.assertAlmostEqual(shape1.positionWithUnits().x(), 90.15, 2)
        self.assertAlmostEqual(shape1.positionWithUnits().y(), 20.21, 2)
        self.assertAlmostEqual(shape2.positionWithUnits().x(), 100.15, 2)
        self.assertAlmostEqual(shape2.positionWithUnits().y(), 120.21, 2)
        self.assertAlmostEqual(shape3.positionWithUnits().x(), 210.15, 2)
        self.assertAlmostEqual(shape3.positionWithUnits().y(), 220.21, 2)
        self.assertAlmostEqual(shape4.positionWithUnits().x(), 10.15, 2)
        self.assertAlmostEqual(shape4.positionWithUnits().y(), 310.21, 2)

        # add a second page
        page2 = QgsLayoutItemPage(l)
        page2.setPageSize("A4", QgsLayoutItemPage.Landscape)
        l.pageCollection().addPage(page2)

        # add some guides
        g1 = QgsLayoutGuide(
            Qt.Horizontal,
            QgsLayoutMeasurement(2.5, QgsUnitTypes.LayoutCentimeters),
            l.pageCollection().page(0))
        l.guides().addGuide(g1)
        g2 = QgsLayoutGuide(
            Qt.Vertical,
            QgsLayoutMeasurement(4.5, QgsUnitTypes.LayoutCentimeters),
            l.pageCollection().page(0))
        l.guides().addGuide(g2)

        # second page should be removed
        l.pageCollection().resizeToContents(QgsMargins(0, 0, 0, 0),
                                            QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l.pageCollection().pageCount(), 1)

        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().width(), 250.3, 2)
        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().height(), 320.36, 2)
        self.assertAlmostEqual(
            l.pageCollection().page(0).sizeWithUnits().units(),
            QgsUnitTypes.LayoutMillimeters)

        self.assertAlmostEqual(g1.position().length(), 0.5, 2)
        self.assertAlmostEqual(g2.position().length(), 3.5, 2)
Exemplo n.º 18
0
    def testCollection(self):
        p = QgsProject()
        l = QgsLayout(p)
        l.initializeDefaults()  # add a page
        guides = l.guides()

        # no guides initially
        self.assertEqual(guides.rowCount(QModelIndex()), 0)
        self.assertFalse(
            guides.data(QModelIndex(),
                        QgsLayoutGuideCollection.OrientationRole))
        self.assertFalse(guides.guides(Qt.Horizontal))
        self.assertFalse(guides.guides(Qt.Vertical))

        # add a guide
        g1 = QgsLayoutGuide(
            Qt.Horizontal,
            QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters),
            l.pageCollection().page(0))
        guides.addGuide(g1)
        self.assertEqual(guides.rowCount(QModelIndex()), 1)
        self.assertEqual(
            guides.data(guides.index(0, 0),
                        QgsLayoutGuideCollection.OrientationRole),
            Qt.Horizontal)
        self.assertEqual(
            guides.data(guides.index(0, 0),
                        QgsLayoutGuideCollection.PositionRole), 5)
        self.assertEqual(
            guides.data(guides.index(0, 0),
                        QgsLayoutGuideCollection.UnitsRole),
            QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(
            guides.data(guides.index(0, 0), QgsLayoutGuideCollection.PageRole),
            0)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1])
        self.assertFalse(guides.guides(Qt.Vertical))
        self.assertEqual(guides.guidesOnPage(0), [g1])
        self.assertEqual(guides.guidesOnPage(1), [])

        g2 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(15),
                            l.pageCollection().page(0))
        guides.addGuide(g2)
        self.assertEqual(guides.rowCount(QModelIndex()), 2)
        self.assertEqual(
            guides.data(guides.index(1, 0),
                        QgsLayoutGuideCollection.OrientationRole),
            Qt.Horizontal)
        self.assertEqual(
            guides.data(guides.index(1, 0),
                        QgsLayoutGuideCollection.PositionRole), 15)
        self.assertEqual(
            guides.data(guides.index(1, 0),
                        QgsLayoutGuideCollection.UnitsRole),
            QgsUnitTypes.LayoutMillimeters)
        self.assertEqual(
            guides.data(guides.index(1, 0), QgsLayoutGuideCollection.PageRole),
            0)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1, g2])
        self.assertFalse(guides.guides(Qt.Vertical))
        self.assertEqual(guides.guidesOnPage(0), [g1, g2])

        page2 = QgsLayoutItemPage(l)
        page2.setPageSize('A3')
        l.pageCollection().addPage(page2)
        g3 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(35),
                            l.pageCollection().page(1))
        guides.addGuide(g3)
        self.assertEqual(guides.rowCount(QModelIndex()), 3)
        self.assertEqual(
            guides.data(guides.index(2, 0),
                        QgsLayoutGuideCollection.OrientationRole), Qt.Vertical)
        self.assertEqual(
            guides.data(guides.index(2, 0),
                        QgsLayoutGuideCollection.PositionRole), 35)
        self.assertEqual(
            guides.data(guides.index(2, 0),
                        QgsLayoutGuideCollection.UnitsRole),
            QgsUnitTypes.LayoutMillimeters)
        self.assertEqual(
            guides.data(guides.index(2, 0), QgsLayoutGuideCollection.PageRole),
            1)
        self.assertEqual(guides.guides(Qt.Horizontal), [g1, g2])
        self.assertEqual(guides.guides(Qt.Horizontal, 0), [g1, g2])
        self.assertEqual(guides.guides(Qt.Horizontal, 1), [])
        self.assertEqual(guides.guides(Qt.Vertical), [g3])
        self.assertEqual(guides.guides(Qt.Vertical, 0), [])
        self.assertEqual(guides.guides(Qt.Vertical, 1), [g3])
        self.assertEqual(guides.guides(Qt.Vertical, 2), [])
        self.assertEqual(guides.guidesOnPage(0), [g1, g2])
        self.assertEqual(guides.guidesOnPage(1), [g3])
Exemplo n.º 19
0
    def testSnapPointsToItems(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        #l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        s.setSnapToItems(True)
        s.setSnapTolerance(1)

        # no items
        point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, [])
        self.assertFalse(snapped)

        line = QGraphicsLineItem()
        line.setVisible(True)
        point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, [], line)
        self.assertFalse(line.isVisible())

        guides.addGuide(
            QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(1), page))

        # add an item
        item1 = QgsLayoutItemMap(l)
        item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters))
        item1.attemptResize(
            QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
        l.addItem(item1)

        point, snapped = s.snapPointsToItems([3.5], Qt.Horizontal, 1, [], line)
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)
        self.assertTrue(line.isVisible())
        point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)
        point, snapped = s.snapPointsToItems([4.6, 4.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)
        point, snapped = s.snapPointsToItems([4.6, 4.5, 3.7], Qt.Horizontal, 1,
                                             [])
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, 0.3, 5)

        # ignoring item
        point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, [item1])
        self.assertFalse(snapped)

        # outside tolerance
        point, snapped = s.snapPointsToItems([5.5], Qt.Horizontal, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # snap to center
        point, snapped = s.snapPointsToItems([12.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)

        # snap to right
        point, snapped = s.snapPointsToItems([22.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        #snap to top
        point, snapped = s.snapPointsToItems([7.5], Qt.Vertical, 1, [], line)
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)
        self.assertTrue(line.isVisible())
        point, snapped = s.snapPointsToItems([8.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        # outside tolerance
        point, snapped = s.snapPointsToItems([5.5], Qt.Vertical, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # snap to center
        point, snapped = s.snapPointsToItems([13.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)

        # snap to bottom
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        # snapping off
        s.setSnapToItems(False)
        line.setVisible(True)
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # with different pixel scale
        s.setSnapToItems(True)
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 3, [])
        self.assertFalse(snapped)
Exemplo n.º 20
0
    def testSnapPointsToItems(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        #l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        s.setSnapToItems(True)
        s.setSnapTolerance(1)

        # no items
        point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, [])
        self.assertFalse(snapped)

        line = QGraphicsLineItem()
        line.setVisible(True)
        point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, [], line)
        self.assertFalse(line.isVisible())

        guides.addGuide(QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(1), page))

        # add an item
        item1 = QgsLayoutItemMap(l)
        item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters))
        item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters))
        l.addItem(item1)

        point, snapped = s.snapPointsToItems([3.5], Qt.Horizontal, 1, [], line)
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)
        self.assertTrue(line.isVisible())
        point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)
        point, snapped = s.snapPointsToItems([4.6, 4.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)
        point, snapped = s.snapPointsToItems([4.6, 4.5, 3.7], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertAlmostEqual(point, 0.3, 5)

        # ignoring item
        point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, [item1])
        self.assertFalse(snapped)

        # outside tolerance
        point, snapped = s.snapPointsToItems([5.5], Qt.Horizontal, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # snap to center
        point, snapped = s.snapPointsToItems([12.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)

        # snap to right
        point, snapped = s.snapPointsToItems([22.5], Qt.Horizontal, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        #snap to top
        point, snapped = s.snapPointsToItems([7.5], Qt.Vertical, 1, [], line)
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)
        self.assertTrue(line.isVisible())
        point, snapped = s.snapPointsToItems([8.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        # outside tolerance
        point, snapped = s.snapPointsToItems([5.5], Qt.Vertical, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # snap to center
        point, snapped = s.snapPointsToItems([13.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, 0.5)

        # snap to bottom
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, [])
        self.assertTrue(snapped)
        self.assertEqual(point, -0.5)

        # snapping off
        s.setSnapToItems(False)
        line.setVisible(True)
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, [], line)
        self.assertFalse(snapped)
        self.assertFalse(line.isVisible())

        # with different pixel scale
        s.setSnapToItems(True)
        point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 3, [])
        self.assertFalse(snapped)
Exemplo n.º 21
0
    def testSnapRect(self):
        p = QgsProject()
        l = QgsLayout(p)
        page = QgsLayoutItemPage(l)
        page.setPageSize('A4')
        l.pageCollection().addPage(page)
        s = QgsLayoutSnapper(l)
        guides = l.guides()

        # first test snapping to grid
        l.gridSettings().setResolution(
            QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
        s.setSnapToItems(False)
        s.setSnapToGrid(True)
        s.setSnapTolerance(1)

        rect, snapped = s.snapRect(QRectF(1, 1, 2, 1), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0, 0, 2, 1))
        rect, snapped = s.snapRect(QRectF(1, 1, 3.5, 3.5), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(1.5, 1.5, 3.5, 3.5))

        s.setSnapToItems(False)
        s.setSnapToGrid(False)
        rect, snapped = s.snapRect(QRectF(1, 1, 3.5, 3.5), 1)
        self.assertFalse(snapped)
        self.assertEqual(rect, QRectF(1, 1, 3.5, 3.5))

        # test that guide takes precedence
        s.setSnapToGrid(True)
        s.setSnapToGuides(True)
        guides.addGuide(
            QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(0.5), page))
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.5, 2.0, 3.0))

        # add an item
        item1 = QgsLayoutItemMap(l)
        item1.attemptMove(
            QgsLayoutPoint(121, 1.1, QgsUnitTypes.LayoutMillimeters))
        l.addItem(item1)

        # test that guide takes precedence over item
        s.setSnapToGrid(True)
        s.setSnapToGuides(True)
        s.setSnapToItems(True)
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.5, 2.0, 3.0))
        # but items take precedence over grid
        s.setSnapToGuides(False)
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1)
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 1.1, 2.0, 3.0))

        # ... unless item is ignored!
        rect, snapped = s.snapRect(QRectF(1, 1, 2, 3), 1, None, None, [item1])
        self.assertTrue(snapped)
        self.assertEqual(rect, QRectF(0.0, 0.0, 2.0, 3.0))
    def testResizeToContents(self):
        p = QgsProject()
        l = QgsLayout(p)

        shape1 = QgsLayoutItemShape(l)
        shape1.attemptResize(QgsLayoutSize(90, 50))
        shape1.attemptMove(QgsLayoutPoint(90, 50))
        shape1.setItemRotation(45, False)
        l.addLayoutItem(shape1)
        shape2 = QgsLayoutItemShape(l)
        shape2.attemptResize(QgsLayoutSize(110, 50))
        shape2.attemptMove(QgsLayoutPoint(100, 150), True, False, 0)
        l.addLayoutItem(shape2)
        shape3 = QgsLayoutItemShape(l)
        l.addLayoutItem(shape3)
        shape3.attemptResize(QgsLayoutSize(50, 100))
        shape3.attemptMove(QgsLayoutPoint(210, 250), True, False, 0)
        shape4 = QgsLayoutItemShape(l)
        l.addLayoutItem(shape4)
        shape4.attemptResize(QgsLayoutSize(50, 30))
        shape4.attemptMove(QgsLayoutPoint(10, 340), True, False, 0)
        shape4.setVisibility(False)

        # resize with no existing pages
        l.pageCollection().resizeToContents(QgsMargins(1, 2, 3, 4), QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l.pageCollection().pageCount(), 1)

        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().width(), 290.3, 2)
        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().height(), 380.36, 2)
        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters)

        self.assertAlmostEqual(shape1.positionWithUnits().x(), 90.15, 2)
        self.assertAlmostEqual(shape1.positionWithUnits().y(), 20.21, 2)
        self.assertAlmostEqual(shape2.positionWithUnits().x(), 100.15, 2)
        self.assertAlmostEqual(shape2.positionWithUnits().y(), 120.21, 2)
        self.assertAlmostEqual(shape3.positionWithUnits().x(), 210.15, 2)
        self.assertAlmostEqual(shape3.positionWithUnits().y(), 220.21, 2)
        self.assertAlmostEqual(shape4.positionWithUnits().x(), 10.15, 2)
        self.assertAlmostEqual(shape4.positionWithUnits().y(), 310.21, 2)

        # add a second page
        page2 = QgsLayoutItemPage(l)
        page2.setPageSize("A4", QgsLayoutItemPage.Landscape)
        l.pageCollection().addPage(page2)

        # add some guides
        g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(2.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        l.guides().addGuide(g1)
        g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(4.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
        l.guides().addGuide(g2)

        # second page should be removed
        l.pageCollection().resizeToContents(QgsMargins(0, 0, 0, 0), QgsUnitTypes.LayoutCentimeters)
        self.assertEqual(l.pageCollection().pageCount(), 1)

        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().width(), 250.3, 2)
        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().height(), 320.36, 2)
        self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters)

        self.assertAlmostEqual(g1.position().length(), 0.5, 2)
        self.assertAlmostEqual(g2.position().length(), 3.5, 2)