Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_34] Remove layers from the edit buffer group #59906

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ Constructor for :py:class:`QgsEditBufferGroup`
void addLayer( QgsVectorLayer *layer );
%Docstring
Add a layer to this edit buffer group.
%End

void removeLayer( QgsVectorLayer *layer );
%Docstring
Remove a layer from this edit buffer group.

.. versionadded:: 3.42
%End

void clear();
%Docstring
Remove all layers from this edit buffer group
Remove all layers from this edit buffer group.
%End

QSet<QgsVectorLayer *> layers() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ Constructor for :py:class:`QgsEditBufferGroup`
void addLayer( QgsVectorLayer *layer );
%Docstring
Add a layer to this edit buffer group.
%End

void removeLayer( QgsVectorLayer *layer );
%Docstring
Remove a layer from this edit buffer group.

.. versionadded:: 3.42
%End

void clear();
%Docstring
Remove all layers from this edit buffer group
Remove all layers from this edit buffer group.
%End

QSet<QgsVectorLayer *> layers() const;
Expand Down
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()
Loading