def testGenerateUniqueTitle(self): project = QgsProject() manager = QgsLayoutManager(project) self.assertEqual(manager.generateUniqueTitle(QgsMasterLayoutInterface.PrintLayout), 'Layout 1') self.assertEqual(manager.generateUniqueTitle(QgsMasterLayoutInterface.Report), 'Report 1') layout = QgsPrintLayout(project) layout.setName(manager.generateUniqueTitle()) manager.addLayout(layout) self.assertEqual(manager.generateUniqueTitle(), 'Layout 2') self.assertEqual(manager.generateUniqueTitle(QgsMasterLayoutInterface.Report), 'Report 1') layout2 = QgsPrintLayout(project) layout2.setName(manager.generateUniqueTitle()) manager.addLayout(layout2) self.assertEqual(manager.generateUniqueTitle(), 'Layout 3') report1 = QgsReport(project) report1.setName(manager.generateUniqueTitle(QgsMasterLayoutInterface.Report)) manager.addLayout(report1) self.assertEqual(manager.generateUniqueTitle(QgsMasterLayoutInterface.Report), 'Report 2') manager.clear() self.assertEqual(manager.generateUniqueTitle(), 'Layout 1') self.assertEqual(manager.generateUniqueTitle(QgsMasterLayoutInterface.Report), 'Report 1')
def testCombo(self): project = QgsProject() manager = QgsLayoutManager(project) layout = QgsPrintLayout(project) layout.setName('ccc') self.assertTrue(manager.addLayout(layout)) layout2 = QgsPrintLayout(project) layout2.setName('bbb') self.assertTrue(manager.addLayout(layout2)) r = QgsReport(project) r.setName('ddd') manager.addLayout(r) combo = QgsLayoutComboBox(None, manager) spy = QSignalSpy(combo.layoutChanged) self.assertEqual(combo.count(), 3) self.assertEqual(combo.itemText(0), 'bbb') self.assertEqual(combo.itemText(1), 'ccc') self.assertEqual(combo.itemText(2), 'ddd') self.assertEqual(combo.layout(0), layout2) self.assertEqual(combo.layout(1), layout) self.assertEqual(combo.layout(2), r) combo.setCurrentLayout(None) self.assertEqual(combo.currentLayout(), None) self.assertEqual(len(spy), 1) combo.setCurrentLayout(layout) self.assertEqual(combo.currentLayout(), layout) self.assertEqual(len(spy), 2) combo.setCurrentLayout(r) self.assertEqual(combo.currentLayout(), r) self.assertEqual(len(spy), 3) combo.setCurrentLayout(layout2) self.assertEqual(combo.currentLayout(), layout2) self.assertEqual(len(spy), 4) combo.setAllowEmptyLayout(True) self.assertEqual(combo.count(), 4) self.assertEqual(combo.itemText(0), '') self.assertEqual(combo.itemText(1), 'bbb') self.assertEqual(combo.itemText(2), 'ccc') self.assertEqual(combo.itemText(3), 'ddd') combo.setCurrentLayout(None) self.assertEqual(combo.currentIndex(), 0) combo.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts) self.assertEqual(combo.count(), 3) self.assertEqual(combo.itemText(0), '') self.assertEqual(combo.itemText(1), 'bbb') self.assertEqual(combo.itemText(2), 'ccc') combo.setFilters(QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(combo.filters(), QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(combo.count(), 2) self.assertEqual(combo.itemText(0), '') self.assertEqual(combo.itemText(1), 'ddd')
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 testProxyModel(self): project = QgsProject() manager = QgsLayoutManager(project) model = QgsLayoutManagerModel(manager) proxy = QgsLayoutManagerProxyModel() proxy.setSourceModel(model) self.assertEqual(proxy.rowCount(QModelIndex()), 0) self.assertEqual( proxy.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(model.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) layout = QgsPrintLayout(project) layout.setName('ccc') self.assertTrue(manager.addLayout(layout)) self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'ccc') self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) layout2 = QgsPrintLayout(project) layout2.setName('bbb') self.assertTrue(manager.addLayout(layout2)) self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'ccc') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) layout.setName('aaa') self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) model.setAllowEmptyLayout(True) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) r = QgsReport(project) r.setName('ddd') manager.addLayout(r) self.assertEqual(proxy.rowCount(QModelIndex()), 4) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) self.assertEqual( proxy.data(proxy.index(3, 0, QModelIndex()), Qt.DisplayRole), 'ddd') self.assertEqual( proxy.data(proxy.index(3, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), r) proxy.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts) self.assertEqual(proxy.filters(), QgsLayoutManagerProxyModel.FilterPrintLayouts) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual( proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) proxy.setFilters(QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.filters(), QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual( proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'ddd') self.assertEqual( proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), r) proxy.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts | QgsLayoutManagerProxyModel.FilterReports) self.assertEqual( proxy.filters(), QgsLayoutManagerProxyModel.FilterPrintLayouts | QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.rowCount(QModelIndex()), 4)
def testProxyModel(self): project = QgsProject() manager = QgsLayoutManager(project) model = QgsLayoutManagerModel(manager) proxy = QgsLayoutManagerProxyModel() proxy.setSourceModel(model) self.assertEqual(proxy.rowCount(QModelIndex()), 0) self.assertEqual(proxy.data(model.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(model.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) layout = QgsPrintLayout(project) layout.setName('ccc') self.assertTrue(manager.addLayout(layout)) self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'ccc') self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) layout2 = QgsPrintLayout(project) layout2.setName('bbb') self.assertTrue(manager.addLayout(layout2)) self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'ccc') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) layout.setName('aaa') self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) model.setAllowEmptyLayout(True) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) r = QgsReport(project) r.setName('ddd') manager.addLayout(r) self.assertEqual(proxy.rowCount(QModelIndex()), 4) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) self.assertEqual(proxy.data(proxy.index(3, 0, QModelIndex()), Qt.DisplayRole), 'ddd') self.assertEqual(proxy.data(proxy.index(3, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), r) proxy.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts) self.assertEqual(proxy.filters(), QgsLayoutManagerProxyModel.FilterPrintLayouts) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'aaa') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout) self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), Qt.DisplayRole), 'bbb') self.assertEqual(proxy.data(proxy.index(2, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), layout2) proxy.setFilters(QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.filters(), QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), Qt.DisplayRole), None) self.assertEqual(proxy.data(proxy.index(0, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), None) self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), Qt.DisplayRole), 'ddd') self.assertEqual(proxy.data(proxy.index(1, 0, QModelIndex()), QgsLayoutManagerModel.LayoutRole), r) proxy.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts | QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.filters(), QgsLayoutManagerProxyModel.FilterPrintLayouts | QgsLayoutManagerProxyModel.FilterReports) self.assertEqual(proxy.rowCount(QModelIndex()), 4)