def testReadWriteXml(self): p = QgsProject() ptLayer = QgsVectorLayer("Point?crs=epsg:4326&field=country:string(20)&field=state:string(20)&field=town:string(20)", "points", "memory") p.addMapLayer(ptLayer) r = QgsReport(p) r.setName('my report') # add a header r.setHeaderEnabled(True) report_header = QgsLayout(p) report_header.setUnits(QgsUnitTypes.LayoutInches) r.setHeader(report_header) # add a footer r.setFooterEnabled(True) report_footer = QgsLayout(p) report_footer.setUnits(QgsUnitTypes.LayoutMeters) r.setFooter(report_footer) # add some subsections child1 = QgsReportSectionLayout() child1_body = QgsLayout(p) child1_body.setUnits(QgsUnitTypes.LayoutPoints) child1.setBody(child1_body) child2 = QgsReportSectionLayout() child2_body = QgsLayout(p) child2_body.setUnits(QgsUnitTypes.LayoutPixels) child2.setBody(child2_body) child1.appendChild(child2) child2a = QgsReportSectionFieldGroup() child2a_body = QgsLayout(p) child2a_body.setUnits(QgsUnitTypes.LayoutInches) child2a.setBody(child2a_body) child2a.setField('my field') child2a.setLayer(ptLayer) child1.appendChild(child2a) r.appendChild(child1) doc = QDomDocument("testdoc") elem = r.writeLayoutXml(doc, QgsReadWriteContext()) r2 = QgsReport(p) self.assertTrue(r2.readLayoutXml(elem, doc, QgsReadWriteContext())) self.assertEqual(r2.name(), 'my report') self.assertTrue(r2.headerEnabled()) self.assertEqual(r2.header().units(), QgsUnitTypes.LayoutInches) self.assertTrue(r2.footerEnabled()) self.assertEqual(r2.footer().units(), QgsUnitTypes.LayoutMeters) self.assertEqual(r2.childCount(), 1) self.assertEqual(r2.childSection(0).body().units(), QgsUnitTypes.LayoutPoints) self.assertEqual(r2.childSection(0).childCount(), 2) self.assertEqual(r2.childSection(0).childSection(0).body().units(), QgsUnitTypes.LayoutPixels) self.assertEqual(r2.childSection(0).childSection(1).body().units(), QgsUnitTypes.LayoutInches) self.assertEqual(r2.childSection(0).childSection(1).field(), 'my field') self.assertEqual(r2.childSection(0).childSection(1).layer(), ptLayer)
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')
def testLayoutScalePixels(self): p = QgsProject() l = QgsLayout(p) l.setUnits(QgsUnitTypes.LayoutPixels) view = QgsLayoutView() view.setCurrentLayout(l) view.setZoomLevel(1) # should be no transform, since 100% with pixel units should be pixel-pixel self.assertEqual(view.transform().m11(), 1) view.setZoomLevel(0.5) self.assertEqual(view.transform().m11(), 0.5)
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) 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)
def testReadWriteXml(self): p = QgsProject() ptLayer = QgsVectorLayer( "Point?crs=epsg:4326&field=country:string(20)&field=state:string(20)&field=town:string(20)", "points", "memory") p.addMapLayer(ptLayer) r = QgsReport(p) r.setName('my report') # add a header r.setHeaderEnabled(True) report_header = QgsLayout(p) report_header.setUnits(QgsUnitTypes.LayoutInches) r.setHeader(report_header) # add a footer r.setFooterEnabled(True) report_footer = QgsLayout(p) report_footer.setUnits(QgsUnitTypes.LayoutMeters) r.setFooter(report_footer) # add some subsections child1 = QgsReportSectionLayout() child1_body = QgsLayout(p) child1_body.setUnits(QgsUnitTypes.LayoutPoints) child1.setBody(child1_body) child2 = QgsReportSectionLayout() child2_body = QgsLayout(p) child2_body.setUnits(QgsUnitTypes.LayoutPixels) child2.setBody(child2_body) child1.appendChild(child2) child2a = QgsReportSectionFieldGroup() child2a_body = QgsLayout(p) child2a_body.setUnits(QgsUnitTypes.LayoutInches) child2a.setBody(child2a_body) child2a.setField('my field') child2a.setLayer(ptLayer) child1.appendChild(child2a) r.appendChild(child1) doc = QDomDocument("testdoc") elem = r.writeLayoutXml(doc, QgsReadWriteContext()) r2 = QgsReport(p) self.assertTrue(r2.readLayoutXml(elem, doc, QgsReadWriteContext())) self.assertEqual(r2.name(), 'my report') self.assertTrue(r2.headerEnabled()) self.assertEqual(r2.header().units(), QgsUnitTypes.LayoutInches) self.assertTrue(r2.footerEnabled()) self.assertEqual(r2.footer().units(), QgsUnitTypes.LayoutMeters) self.assertEqual(r2.childCount(), 1) self.assertEqual( r2.childSection(0).body().units(), QgsUnitTypes.LayoutPoints) self.assertEqual(r2.childSection(0).childCount(), 2) self.assertEqual( r2.childSection(0).childSection(0).body().units(), QgsUnitTypes.LayoutPixels) self.assertEqual( r2.childSection(0).childSection(1).body().units(), QgsUnitTypes.LayoutInches) self.assertEqual( r2.childSection(0).childSection(1).field(), 'my field') self.assertEqual(r2.childSection(0).childSection(1).layer(), ptLayer)