def testRemoveLayout(self): project = QgsProject() layout = QgsLayout(project) layout.setName('test layout') self.manager = QgsLayoutManager(project) layout_removed_spy = QSignalSpy(self.manager.layoutRemoved) layout_about_to_be_removed_spy = QSignalSpy( self.manager.layoutAboutToBeRemoved) # tests that layout still exists when layoutAboutToBeRemoved is fired self.manager.layoutAboutToBeRemoved.connect( self.layoutAboutToBeRemoved) # not added, should fail self.assertFalse(self.manager.removeLayout(layout)) self.assertEqual(len(layout_removed_spy), 0) self.assertEqual(len(layout_about_to_be_removed_spy), 0) self.assertTrue(self.manager.addLayout(layout)) self.assertEqual(self.manager.layouts(), [layout]) self.assertTrue(self.manager.removeLayout(layout)) self.assertEqual(len(self.manager.layouts()), 0) self.assertEqual(len(layout_removed_spy), 1) self.assertEqual(layout_removed_spy[0][0], 'test layout') self.assertEqual(len(layout_about_to_be_removed_spy), 1) self.assertEqual(layout_about_to_be_removed_spy[0][0], 'test layout') self.assertTrue(self.aboutFired) self.manager = None
def testRemoveLayout(self): project = QgsProject() layout = QgsLayout(project) layout.setName('test layout') self.manager = QgsLayoutManager(project) layout_removed_spy = QSignalSpy(self.manager.layoutRemoved) layout_about_to_be_removed_spy = QSignalSpy(self.manager.layoutAboutToBeRemoved) # tests that layout still exists when layoutAboutToBeRemoved is fired self.manager.layoutAboutToBeRemoved.connect(self.layoutAboutToBeRemoved) # not added, should fail self.assertFalse(self.manager.removeLayout(layout)) self.assertEqual(len(layout_removed_spy), 0) self.assertEqual(len(layout_about_to_be_removed_spy), 0) self.assertTrue(self.manager.addLayout(layout)) self.assertEqual(self.manager.layouts(), [layout]) self.assertTrue(self.manager.removeLayout(layout)) self.assertEqual(len(self.manager.layouts()), 0) self.assertEqual(len(layout_removed_spy), 1) self.assertEqual(layout_removed_spy[0][0], 'test layout') self.assertEqual(len(layout_about_to_be_removed_spy), 1) self.assertEqual(layout_about_to_be_removed_spy[0][0], 'test layout') self.assertTrue(self.aboutFired) self.manager = None
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) l.setReferenceMap(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') self.assertEqual(l2.referenceMap().id(), 'zzyyzz')
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 testGenerateUniqueTitle(self): project = QgsProject() manager = QgsLayoutManager(project) self.assertEqual(manager.generateUniqueTitle(), 'Layout 1') layout = QgsLayout(project) layout.setName(manager.generateUniqueTitle()) manager.addLayout(layout) self.assertEqual(manager.generateUniqueTitle(), 'Layout 2') layout2 = QgsLayout(project) layout2.setName(manager.generateUniqueTitle()) manager.addLayout(layout2) self.assertEqual(manager.generateUniqueTitle(), 'Layout 3') manager.clear() self.assertEqual(manager.generateUniqueTitle(), 'Layout 1')
def testAddLayout(self): project = QgsProject() layout = QgsLayout(project) layout.setName('test layout') manager = QgsLayoutManager(project) layout_about_to_be_added_spy = QSignalSpy(manager.layoutAboutToBeAdded) layout_added_spy = QSignalSpy(manager.layoutAdded) self.assertTrue(manager.addLayout(layout)) self.assertEqual(len(layout_about_to_be_added_spy), 1) self.assertEqual(layout_about_to_be_added_spy[0][0], 'test layout') self.assertEqual(len(layout_added_spy), 1) self.assertEqual(layout_added_spy[0][0], 'test layout') # adding it again should fail self.assertFalse(manager.addLayout(layout)) # try adding a second layout layout2 = QgsLayout(project) layout2.setName('test layout2') self.assertTrue(manager.addLayout(layout2)) self.assertEqual(len(layout_added_spy), 2) self.assertEqual(layout_about_to_be_added_spy[1][0], 'test layout2') self.assertEqual(len(layout_about_to_be_added_spy), 2) self.assertEqual(layout_added_spy[1][0], 'test layout2') # adding a layout with duplicate name should fail layout3 = QgsLayout(project) layout3.setName('test layout2') self.assertFalse(manager.addLayout(layout3))
def testRenameSignal(self): project = QgsProject() manager = QgsLayoutManager(project) layout = QgsLayout(project) layout.setName('c1') manager.addLayout(layout) layout2 = QgsLayout(project) layout2.setName('c2') manager.addLayout(layout2) layout_renamed_spy = QSignalSpy(manager.layoutRenamed) layout.setName('d1') self.assertEqual(len(layout_renamed_spy), 1) # self.assertEqual(layout_renamed_spy[0][0], layout) self.assertEqual(layout_renamed_spy[0][1], 'd1') layout2.setName('d2') self.assertEqual(len(layout_renamed_spy), 2) # self.assertEqual(layout_renamed_spy[1][0], layout2) self.assertEqual(layout_renamed_spy[1][1], 'd2')
def testReadWriteXml(self): """ Test reading and writing layout manager state to XML """ project = QgsProject() manager = QgsLayoutManager(project) # add a bunch of compositions composition = QgsComposition(project) composition.setName('test composition') composition2 = QgsComposition(project) composition2.setName('test composition2') composition3 = QgsComposition(project) composition3.setName('test composition3') manager.addComposition(composition) manager.addComposition(composition2) manager.addComposition(composition3) # add a bunch of layouts layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addLayout(layout) manager.addLayout(layout2) manager.addLayout(layout3) # save to xml doc = QDomDocument("testdoc") elem = manager.writeXml(doc) doc.appendChild(elem) # restore from xml project2 = QgsProject() manager2 = QgsLayoutManager(project2) self.assertTrue(manager2.readXml(elem, doc)) self.assertEqual(len(manager2.compositions()), 3) names = [c.name() for c in manager2.compositions()] self.assertEqual( set(names), {'test composition', 'test composition2', 'test composition3'}) self.assertEqual(len(manager2.layouts()), 3) names = [c.name() for c in manager2.layouts()] self.assertCountEqual(names, ['test layout', 'test layout2', 'test layout3'])
def testReadWriteXml(self): """ Test reading and writing layout manager state to XML """ project = QgsProject() manager = QgsLayoutManager(project) # add a bunch of compositions composition = QgsComposition(project) composition.setName('test composition') composition2 = QgsComposition(project) composition2.setName('test composition2') composition3 = QgsComposition(project) composition3.setName('test composition3') manager.addComposition(composition) manager.addComposition(composition2) manager.addComposition(composition3) # add a bunch of layouts layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addLayout(layout) manager.addLayout(layout2) manager.addLayout(layout3) # save to xml doc = QDomDocument("testdoc") elem = manager.writeXml(doc) doc.appendChild(elem) # restore from xml project2 = QgsProject() manager2 = QgsLayoutManager(project2) self.assertTrue(manager2.readXml(elem, doc)) self.assertEqual(len(manager2.compositions()), 3) names = [c.name() for c in manager2.compositions()] self.assertEqual(set(names), {'test composition', 'test composition2', 'test composition3'}) self.assertEqual(len(manager2.layouts()), 3) names = [c.name() for c in manager2.layouts()] self.assertCountEqual(names, ['test layout', 'test layout2', 'test layout3'])
def testLayouts(self): project = QgsProject() manager = QgsLayoutManager(project) layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addLayout(layout) self.assertEqual(manager.layouts(), [layout]) manager.addLayout(layout2) self.assertEqual(set(manager.layouts()), {layout, layout2}) manager.addLayout(layout3) self.assertEqual(set(manager.layouts()), {layout, layout2, layout3})
def testLayoutsByName(self): project = QgsProject() manager = QgsLayoutManager(project) # add a bunch of layouts layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addLayout(layout) manager.addLayout(layout2) manager.addLayout(layout3) self.assertFalse(manager.layoutByName('asdf')) self.assertEqual(manager.layoutByName('test layout'), layout) self.assertEqual(manager.layoutByName('test layout2'), layout2) self.assertEqual(manager.layoutByName('test layout3'), layout3)
def testClear(self): project = QgsProject() manager = QgsLayoutManager(project) # add a bunch of compositions composition = QgsComposition(project) composition.setName('test composition') composition2 = QgsComposition(project) composition2.setName('test composition2') composition3 = QgsComposition(project) composition3.setName('test composition3') # add a bunch of layouts layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addComposition(composition) manager.addComposition(composition2) manager.addComposition(composition3) manager.addLayout(layout) manager.addLayout(layout2) manager.addLayout(layout3) composition_removed_spy = QSignalSpy(manager.compositionRemoved) composition_about_to_be_removed_spy = QSignalSpy( manager.compositionAboutToBeRemoved) layout_removed_spy = QSignalSpy(manager.layoutRemoved) layout_about_to_be_removed_spy = QSignalSpy( manager.layoutAboutToBeRemoved) manager.clear() self.assertEqual(len(manager.compositions()), 0) self.assertEqual(len(composition_removed_spy), 3) self.assertEqual(len(composition_about_to_be_removed_spy), 3) self.assertEqual(len(manager.layouts()), 0) self.assertEqual(len(layout_removed_spy), 3) self.assertEqual(len(layout_about_to_be_removed_spy), 3)
def testClear(self): project = QgsProject() manager = QgsLayoutManager(project) # add a bunch of compositions composition = QgsComposition(project) composition.setName('test composition') composition2 = QgsComposition(project) composition2.setName('test composition2') composition3 = QgsComposition(project) composition3.setName('test composition3') # add a bunch of layouts layout = QgsLayout(project) layout.setName('test layout') layout2 = QgsLayout(project) layout2.setName('test layout2') layout3 = QgsLayout(project) layout3.setName('test layout3') manager.addComposition(composition) manager.addComposition(composition2) manager.addComposition(composition3) manager.addLayout(layout) manager.addLayout(layout2) manager.addLayout(layout3) composition_removed_spy = QSignalSpy(manager.compositionRemoved) composition_about_to_be_removed_spy = QSignalSpy(manager.compositionAboutToBeRemoved) layout_removed_spy = QSignalSpy(manager.layoutRemoved) layout_about_to_be_removed_spy = QSignalSpy(manager.layoutAboutToBeRemoved) manager.clear() self.assertEqual(len(manager.compositions()), 0) self.assertEqual(len(composition_removed_spy), 3) self.assertEqual(len(composition_about_to_be_removed_spy), 3) self.assertEqual(len(manager.layouts()), 0) self.assertEqual(len(layout_removed_spy), 3) self.assertEqual(len(layout_about_to_be_removed_spy), 3)