def testLayerChangeDirtiesProject(self): """ Test that making changes to certain layer properties results in dirty projects """ p = QgsProject() l = QgsVectorLayer(os.path.join(TEST_DATA_DIR, "points.shp"), "points", "ogr") self.assertTrue(l.isValid()) self.assertTrue(p.addMapLayers([l])) p.setDirty(False) l.setCrs(QgsCoordinateReferenceSystem('EPSG:3111')) self.assertTrue(p.isDirty()) p.setDirty(False) l.setName('test') self.assertTrue(p.isDirty()) p.setDirty(False) self.assertTrue(l.setSubsetString('class=\'a\'')) self.assertTrue(p.isDirty())
def testDirtyBlocker(self): # first test manual QgsProjectDirtyBlocker construction p = QgsProject() dirty_spy = QSignalSpy(p.isDirtyChanged) # ^ will do *whatever* it takes to discover the enemy's secret plans! # simple checks p.setDirty(True) self.assertTrue(p.isDirty()) self.assertEqual(len(dirty_spy), 1) self.assertEqual(dirty_spy[-1], [True]) p.setDirty(True) # already dirty self.assertTrue(p.isDirty()) self.assertEqual(len(dirty_spy), 1) p.setDirty(False) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 2) self.assertEqual(dirty_spy[-1], [False]) p.setDirty(True) self.assertTrue(p.isDirty()) self.assertEqual(len(dirty_spy), 3) self.assertEqual(dirty_spy[-1], [True]) # with a blocker blocker = QgsProjectDirtyBlocker(p) # blockers will allow cleaning projects p.setDirty(False) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 4) self.assertEqual(dirty_spy[-1], [False]) # but not dirtying! p.setDirty(True) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 4) self.assertEqual(dirty_spy[-1], [False]) # nested block blocker2 = QgsProjectDirtyBlocker(p) p.setDirty(True) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 4) self.assertEqual(dirty_spy[-1], [False]) del blocker2 p.setDirty(True) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 4) self.assertEqual(dirty_spy[-1], [False]) del blocker p.setDirty(True) self.assertTrue(p.isDirty()) self.assertEqual(len(dirty_spy), 5) self.assertEqual(dirty_spy[-1], [True]) # using python context manager with QgsProject.blockDirtying(p): # cleaning allowed p.setDirty(False) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 6) self.assertEqual(dirty_spy[-1], [False]) # but not dirtying! p.setDirty(True) self.assertFalse(p.isDirty()) self.assertEqual(len(dirty_spy), 6) self.assertEqual(dirty_spy[-1], [False]) # unblocked p.setDirty(True) self.assertTrue(p.isDirty()) self.assertEqual(len(dirty_spy), 7) self.assertEqual(dirty_spy[-1], [True])
def testDirtying(self): project = QgsProject() # writing a new entry should dirty the project project.setDirty(False) self.assertTrue(project.writeEntry('myscope', 'myentry', True)) self.assertTrue(project.isDirty()) # over-writing a pre-existing entry with the same value should _not_ dirty the project project.setDirty(False) self.assertTrue(project.writeEntry('myscope', 'myentry', True)) self.assertFalse(project.isDirty()) # over-writing a pre-existing entry with a different value should dirty the project project.setDirty(False) self.assertTrue(project.writeEntry('myscope', 'myentry', False)) self.assertTrue(project.isDirty()) # removing an existing entry should dirty the project project.setDirty(False) self.assertTrue(project.removeEntry('myscope', 'myentry')) self.assertTrue(project.isDirty()) # removing a non-existing entry should _not_ dirty the project project.setDirty(False) self.assertTrue(project.removeEntry('myscope', 'myentry')) self.assertFalse(project.isDirty()) # setting a project CRS with a new value should dirty the project project.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) project.setDirty(False) project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148')) self.assertTrue(project.isDirty()) # setting a project CRS with the same project CRS should not dirty the project project.setDirty(False) project.setCrs(QgsCoordinateReferenceSystem('EPSG:3148')) self.assertFalse(project.isDirty())