Skip to content

Commit

Permalink
Remove layers from the edit buffer group
Browse files Browse the repository at this point in the history
Layers are removed togheter with removal from project
Fix #59828
  • Loading branch information
domi4484 authored and nyalldawson committed Dec 16, 2024
1 parent edd73a9 commit 86b9f69
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/core/project/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,15 @@ void QgsProject::onMapLayersRemoved( const QList<QgsMapLayer *> &layers )

if ( !mBlockSnappingUpdates && mSnappingConfig.removeLayers( layers ) )
emit snappingConfigChanged( mSnappingConfig );

for ( QgsMapLayer *layer : layers )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( ! vlayer )
continue;

mEditBufferGroup.removeLayer( vlayer );
}
}

void QgsProject::cleanTransactionGroups( bool force )
Expand Down
5 changes: 5 additions & 0 deletions src/core/vector/qgsvectorlayereditbuffergroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ void QgsVectorLayerEditBufferGroup::addLayer( QgsVectorLayer *layer )
mLayers.insert( layer );
}

void QgsVectorLayerEditBufferGroup::removeLayer( QgsVectorLayer *layer )
{
mLayers.remove( layer );
}

void QgsVectorLayerEditBufferGroup::clear()
{
mLayers.clear();
Expand Down
9 changes: 8 additions & 1 deletion src/core/vector/qgsvectorlayereditbuffergroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ class CORE_EXPORT QgsVectorLayerEditBufferGroup : public QObject
void addLayer( QgsVectorLayer *layer );

/**
* Remove all layers from this edit buffer group
* Remove a layer from this edit buffer group.
*
* \since QGIS 3.42
*/
void removeLayer( QgsVectorLayer *layer );

/**
* Remove all layers from this edit buffer group.
*/
void clear();

Expand Down
28 changes: 28 additions & 0 deletions tests/src/python/test_qgsvectorlayereditbuffergroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ def testCircularRelations(self):
self.assertTrue(success)
self.assertFalse(editBufferGroup.isEditing())

def testRemoveLayer(self):
memoryLayer_a = QgsVectorLayer(
"Point?crs=epsg:4326&field=id:integer&field=id_b", "testA", "memory"
)
self.assertTrue(memoryLayer_a.isValid())

memoryLayer_b = QgsVectorLayer(
"Point?crs=epsg:4326&field=id:integer&field=id_a", "testB", "memory"
)
self.assertTrue(memoryLayer_b.isValid())

project = QgsProject.instance()
project.addMapLayer(memoryLayer_a)
project.addMapLayer(memoryLayer_b)

project.setTransactionMode(Qgis.TransactionMode.BufferedGroups)

editBufferGroup = project.editBufferGroup()

project.removeMapLayer(memoryLayer_a.id())

self.assertNotIn(memoryLayer_a, editBufferGroup.layers())
self.assertIn(memoryLayer_b, editBufferGroup.layers())

# Chack that no crash happens (#59828)
project.startEditing()
project.commitChanges()


if __name__ == '__main__':
unittest.main()

0 comments on commit 86b9f69

Please sign in to comment.