Skip to content
Permalink
Browse files
Merge pull request #45675 from jakimowb/issue_45228
Fixes #45228: QgsVectorLayer::featuresDeleted is only called once
  • Loading branch information
elpaso committed Oct 28, 2021
2 parents 4a4e006 + 579c683 commit d3c25645942ffa5370d2f2723f88582133dc3c8a
Showing with 38 additions and 1 deletion.
  1. +9 −1 src/core/vector/qgsvectorlayer.cpp
  2. +3 −0 src/core/vector/qgsvectorlayer.h
  3. +26 −0 tests/src/python/test_qgsvectorlayer.py
@@ -3475,7 +3475,15 @@ bool QgsVectorLayer::commitChanges( bool stopEditing )
if ( !mAllowCommit )
return false;

mCommitChangesActive = true;
bool success = mEditBuffer->commitChanges( mCommitErrors );
mCommitChangesActive = false;

if ( !mDeletedFids.empty() )
{
emit featuresDeleted( mDeletedFids );
mDeletedFids.clear();
}

if ( success )
{
@@ -5286,7 +5294,7 @@ void QgsVectorLayer::onJoinedFieldsChanged()

void QgsVectorLayer::onFeatureDeleted( QgsFeatureId fid )
{
if ( mEditCommandActive )
if ( mEditCommandActive || mCommitChangesActive )
{
mDeletedFids << fid;
}
@@ -2951,6 +2951,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! True while an undo command is active
bool mEditCommandActive = false;

//! True while a commit is active
bool mCommitChangesActive = false;

bool mReadExtentFromXml;
QgsRectangle mXmlExtent;

@@ -3660,6 +3660,32 @@ def onFeaturesDeleted(deleted_fids):
self.assertEqual(layer.featureCount(), 0)
self.assertEqual(layer.selectedFeatureIds(), [])

def testCommitChangesReportsDeletedFeatureIDs(self):
"""
Tests if commitChanges emits "featuresDeleted" with all deleted feature IDs,
e.g. in case (negative) temporary FIDs are converted into (positive) persistent FIDs.
"""
temp_fids = []

def onFeaturesDeleted(deleted_fids):
self.assertEqual(len(deleted_fids), len(temp_fids),
msg=f'featuresDeleted returned {len(deleted_fids)} instead of 2 deleted feature IDs: '
f'{deleted_fids}')
for d in deleted_fids:
self.assertTrue(d in temp_fids)

layer = QgsVectorLayer("point?crs=epsg:4326&field=name:string", "Scratch point layer", "memory")
layer.featuresDeleted.connect(onFeaturesDeleted)

layer.startEditing()
layer.beginEditCommand('add 2 features')
layer.addFeature(QgsFeature(layer.fields()))
layer.addFeature(QgsFeature(layer.fields()))
layer.endEditCommand()
temp_fids.extend(layer.allFeatureIds())

layer.commitChanges()

def testSubsetStringInvalidLayer(self):
"""
Test that subset strings can be set on invalid layers, and retrieved later...

0 comments on commit d3c2564

Please sign in to comment.