Ejemplo n.º 1
0
    def testRemoveComposition(self):
        project = QgsProject()
        composition = QgsComposition(project)
        composition.setName('test composition')

        self.manager = QgsLayoutManager(project)
        composition_removed_spy = QSignalSpy(self.manager.compositionRemoved)
        composition_about_to_be_removed_spy = QSignalSpy(self.manager.compositionAboutToBeRemoved)
        # tests that composition still exists when compositionAboutToBeRemoved is fired
        self.manager.compositionAboutToBeRemoved.connect(self.aboutToBeRemoved)

        # not added, should fail
        self.assertFalse(self.manager.removeComposition(composition))
        self.assertEqual(len(composition_removed_spy), 0)
        self.assertEqual(len(composition_about_to_be_removed_spy), 0)

        self.assertTrue(self.manager.addComposition(composition))
        self.assertEqual(self.manager.compositions(), [composition])
        self.assertTrue(self.manager.removeComposition(composition))
        self.assertEqual(len(self.manager.compositions()), 0)
        self.assertEqual(len(composition_removed_spy), 1)
        self.assertEqual(composition_removed_spy[0][0], 'test composition')
        self.assertEqual(len(composition_about_to_be_removed_spy), 1)
        self.assertEqual(composition_about_to_be_removed_spy[0][0], 'test composition')
        self.assertTrue(self.aboutFired)
        self.manager = None
Ejemplo n.º 2
0
    def testSaveAsTemplate(self):
        """
        Test saving composition as template
        """
        project = QgsProject()
        manager = QgsLayoutManager(project)
        doc = QDomDocument("testdoc")
        self.assertFalse(manager.saveAsTemplate('not in manager', doc))

        composition = QgsComposition(project)
        composition.setName('test composition')
        manager.addComposition(composition)
        self.assertTrue(manager.saveAsTemplate('test composition', doc))
Ejemplo n.º 3
0
    def testSaveRestore(self):
        # test that properties are restored correctly from XML
        composition = QgsComposition(QgsProject.instance())
        composition.setName('test composition')

        doc = QDomDocument("testdoc")
        elem = doc.createElement("qgis")
        doc.appendChild(elem)
        elem = doc.createElement("composer")
        self.assertTrue(composition.writeXml(elem, doc))

        composition2 = QgsComposition(QgsProject.instance())
        self.assertTrue(composition2.readXml(elem, doc))

        self.assertEqual(composition.name(), 'test composition')
Ejemplo n.º 4
0
    def testGenerateUniqueTitle(self):
        project = QgsProject()
        manager = QgsLayoutManager(project)
        self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')

        composition = QgsComposition(project)
        composition.setName(manager.generateUniqueTitle())
        manager.addComposition(composition)

        self.assertEqual(manager.generateUniqueTitle(), 'Composer 2')
        composition2 = QgsComposition(project)
        composition2.setName(manager.generateUniqueTitle())
        manager.addComposition(composition2)

        self.assertEqual(manager.generateUniqueTitle(), 'Composer 3')
        manager.clear()
        self.assertEqual(manager.generateUniqueTitle(), 'Composer 1')
Ejemplo n.º 5
0
    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)

        # 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'})
Ejemplo n.º 6
0
    def testAddComposition(self):
        project = QgsProject()
        composition = QgsComposition(project)
        composition.setName('test composition')

        manager = QgsLayoutManager(project)

        composition_about_to_be_added_spy = QSignalSpy(manager.compositionAboutToBeAdded)
        composition_added_spy = QSignalSpy(manager.compositionAdded)
        self.assertTrue(manager.addComposition(composition))
        self.assertEqual(len(composition_about_to_be_added_spy), 1)
        self.assertEqual(composition_about_to_be_added_spy[0][0], 'test composition')
        self.assertEqual(len(composition_added_spy), 1)
        self.assertEqual(composition_added_spy[0][0], 'test composition')

        # adding it again should fail
        self.assertFalse(manager.addComposition(composition))

        # try adding a second composition
        composition2 = QgsComposition(project)
        composition2.setName('test composition2')
        self.assertTrue(manager.addComposition(composition2))
        self.assertEqual(len(composition_added_spy), 2)
        self.assertEqual(composition_about_to_be_added_spy[1][0], 'test composition2')
        self.assertEqual(len(composition_about_to_be_added_spy), 2)
        self.assertEqual(composition_added_spy[1][0], 'test composition2')

        # adding a composition with duplicate name should fail
        composition3 = QgsComposition(project)
        composition3.setName('test composition2')
        self.assertFalse(manager.addComposition(composition3))
Ejemplo n.º 7
0
    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)

        # 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'})
Ejemplo n.º 8
0
    def testAddComposition(self):
        project = QgsProject()
        composition = QgsComposition(project)
        composition.setName('test composition')

        manager = QgsLayoutManager(project)

        composition_about_to_be_added_spy = QSignalSpy(
            manager.compositionAboutToBeAdded)
        composition_added_spy = QSignalSpy(manager.compositionAdded)
        self.assertTrue(manager.addComposition(composition))
        self.assertEqual(len(composition_about_to_be_added_spy), 1)
        self.assertEqual(composition_about_to_be_added_spy[0][0],
                         'test composition')
        self.assertEqual(len(composition_added_spy), 1)
        self.assertEqual(composition_added_spy[0][0], 'test composition')

        # adding it again should fail
        self.assertFalse(manager.addComposition(composition))

        # try adding a second composition
        composition2 = QgsComposition(project)
        composition2.setName('test composition2')
        self.assertTrue(manager.addComposition(composition2))
        self.assertEqual(len(composition_added_spy), 2)
        self.assertEqual(composition_about_to_be_added_spy[1][0],
                         'test composition2')
        self.assertEqual(len(composition_about_to_be_added_spy), 2)
        self.assertEqual(composition_added_spy[1][0], 'test composition2')

        # adding a composition with duplicate name should fail
        composition3 = QgsComposition(project)
        composition3.setName('test composition2')
        self.assertFalse(manager.addComposition(composition3))
Ejemplo n.º 9
0
    def testDuplicateComposition(self):
        """
        Test duplicating compositions
        """
        project = QgsProject()
        manager = QgsLayoutManager(project)
        doc = QDomDocument("testdoc")
        self.assertFalse(manager.duplicateComposition('not in manager', 'dest'))

        composition = QgsComposition(project)
        composition.setName('test composition')
        composition.setPaperSize(100, 200)
        manager.addComposition(composition)
        # duplicate name
        self.assertFalse(manager.duplicateComposition('test composition', 'test composition'))

        result = manager.duplicateComposition('test composition', 'dupe composition')
        self.assertTrue(result)
        # make sure result in stored in manager
        self.assertEqual(result, manager.compositionByName('dupe composition'))
        self.assertEqual(result.name(), 'dupe composition')
        self.assertEqual(result.paperHeight(), 200)
        self.assertEqual(result.paperWidth(), 100)
Ejemplo n.º 10
0
    def testDuplicateComposition(self):
        """
        Test duplicating compositions
        """
        project = QgsProject()
        manager = QgsLayoutManager(project)
        doc = QDomDocument("testdoc")
        self.assertFalse(manager.duplicateComposition('not in manager', 'dest'))

        composition = QgsComposition(project)
        composition.setName('test composition')
        composition.setPaperSize(100, 200)
        manager.addComposition(composition)
        # duplicate name
        self.assertFalse(manager.duplicateComposition('test composition', 'test composition'))

        result = manager.duplicateComposition('test composition', 'dupe composition')
        self.assertTrue(result)
        # make sure result in stored in manager
        self.assertEqual(result, manager.compositionByName('dupe composition'))
        self.assertEqual(result.name(), 'dupe composition')
        self.assertEqual(result.paperHeight(), 200)
        self.assertEqual(result.paperWidth(), 100)
Ejemplo n.º 11
0
    def testRenameSignal(self):
        project = QgsProject()
        manager = QgsLayoutManager(project)
        composition = QgsComposition(project)
        composition.setName('c1')
        manager.addComposition(composition)
        composition2 = QgsComposition(project)
        composition2.setName('c2')
        manager.addComposition(composition2)

        composition_renamed_spy = QSignalSpy(manager.compositionRenamed)
        composition.setName('d1')
        self.assertEqual(len(composition_renamed_spy), 1)
        self.assertEqual(composition_renamed_spy[0][0], composition)
        self.assertEqual(composition_renamed_spy[0][1], 'd1')
        composition2.setName('d2')
        self.assertEqual(len(composition_renamed_spy), 2)
        self.assertEqual(composition_renamed_spy[1][0], composition2)
        self.assertEqual(composition_renamed_spy[1][1], 'd2')
Ejemplo n.º 12
0
    def testRenameSignal(self):
        project = QgsProject()
        manager = QgsLayoutManager(project)
        composition = QgsComposition(project)
        composition.setName('c1')
        manager.addComposition(composition)
        composition2 = QgsComposition(project)
        composition2.setName('c2')
        manager.addComposition(composition2)

        composition_renamed_spy = QSignalSpy(manager.compositionRenamed)
        composition.setName('d1')
        self.assertEqual(len(composition_renamed_spy), 1)
        self.assertEqual(composition_renamed_spy[0][0], composition)
        self.assertEqual(composition_renamed_spy[0][1], 'd1')
        composition2.setName('d2')
        self.assertEqual(len(composition_renamed_spy), 2)
        self.assertEqual(composition_renamed_spy[1][0], composition2)
        self.assertEqual(composition_renamed_spy[1][1], 'd2')
Ejemplo n.º 13
0
    def testCompositions(self):
        project = QgsProject()
        manager = QgsLayoutManager(project)
        composition = QgsComposition(project)
        composition.setName('test composition')
        composition2 = QgsComposition(project)
        composition2.setName('test composition2')
        composition3 = QgsComposition(project)
        composition3.setName('test composition3')

        manager.addComposition(composition)
        self.assertEqual(manager.compositions(), [composition])
        manager.addComposition(composition2)
        self.assertEqual(set(manager.compositions()), {composition, composition2})
        manager.addComposition(composition3)
        self.assertEqual(set(manager.compositions()), {composition, composition2, composition3})
Ejemplo n.º 14
0
    def testCompositions(self):
        project = QgsProject()
        manager = QgsLayoutManager(project)
        composition = QgsComposition(project)
        composition.setName('test composition')
        composition2 = QgsComposition(project)
        composition2.setName('test composition2')
        composition3 = QgsComposition(project)
        composition3.setName('test composition3')

        manager.addComposition(composition)
        self.assertEqual(manager.compositions(), [composition])
        manager.addComposition(composition2)
        self.assertEqual(set(manager.compositions()), {composition, composition2})
        manager.addComposition(composition3)
        self.assertEqual(set(manager.compositions()), {composition, composition2, composition3})
Ejemplo n.º 15
0
    def testCompositionByName(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')

        manager.addComposition(composition)
        manager.addComposition(composition2)
        manager.addComposition(composition3)

        self.assertFalse(manager.compositionByName('asdf'))
        self.assertEqual(manager.compositionByName('test composition'), composition)
        self.assertEqual(manager.compositionByName('test composition2'), composition2)
        self.assertEqual(manager.compositionByName('test composition3'), composition3)
Ejemplo n.º 16
0
    def testCompositionByName(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')

        manager.addComposition(composition)
        manager.addComposition(composition2)
        manager.addComposition(composition3)

        self.assertFalse(manager.compositionByName('asdf'))
        self.assertEqual(manager.compositionByName('test composition'), composition)
        self.assertEqual(manager.compositionByName('test composition2'), composition2)
        self.assertEqual(manager.compositionByName('test composition3'), composition3)
Ejemplo n.º 17
0
    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')

        manager.addComposition(composition)
        manager.addComposition(composition2)
        manager.addComposition(composition3)

        composition_removed_spy = QSignalSpy(manager.compositionRemoved)
        composition_about_to_be_removed_spy = QSignalSpy(manager.compositionAboutToBeRemoved)
        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)
Ejemplo n.º 18
0
    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')

        manager.addComposition(composition)
        manager.addComposition(composition2)
        manager.addComposition(composition3)

        composition_removed_spy = QSignalSpy(manager.compositionRemoved)
        composition_about_to_be_removed_spy = QSignalSpy(manager.compositionAboutToBeRemoved)
        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)