Skip to content

Commit 68a01c9

Browse files
committed
Add QgsVectorLayer::deleteFeatures() method
1 parent 53b116a commit 68a01c9

8 files changed

+73
-12
lines changed

python/core/qgsvectorlayer.sip

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,6 @@ class QgsVectorLayer : QgsMapLayer
755755
@param f feature to add
756756
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
757757
@return True in case of success and False in case of error
758-
759-
// TODO QGIS3: QgsFeature& f /InOut/
760758
*/
761759
bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );
762760

@@ -1146,6 +1144,15 @@ class QgsVectorLayer : QgsMapLayer
11461144
/** Delete a feature from the layer (but does not commit it) */
11471145
bool deleteFeature( QgsFeatureId fid );
11481146

1147+
/**
1148+
* Deletes a set of features from the layer (but does not commit it)
1149+
* @param fids The feature ids to delete
1150+
*
1151+
* @return false if the layer is not in edit mode or does not support deleting
1152+
* in case of an active transaction depends on the provider implementation
1153+
*/
1154+
bool deleteFeatures( QgsFeatureIds fids );
1155+
11491156
/**
11501157
Attempts to commit any changes to disk. Returns the result of the attempt.
11511158
If a commit fails, the in-memory changes are left alone.

python/core/qgsvectorlayereditbuffer.sip

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,36 @@ class QgsVectorLayerEditBuffer : QObject
1212
~QgsVectorLayerEditBuffer();
1313

1414
/** Returns true if the provider has been modified since the last commit */
15-
bool isModified() const;
15+
virtual bool isModified() const;
1616

1717

1818
/** Adds a feature
1919
@param f feature to add
2020
@return True in case of success and False in case of error
2121
*/
22-
bool addFeature( QgsFeature& f );
22+
virtual bool addFeature( QgsFeature& f );
2323

2424
/** Insert a copy of the given features into the layer (but does not commit it) */
25-
bool addFeatures( QgsFeatureList& features );
25+
virtual bool addFeatures( QgsFeatureList& features );
2626

2727
/** Delete a feature from the layer (but does not commit it) */
28-
bool deleteFeature( QgsFeatureId fid );
28+
virtual bool deleteFeature( QgsFeatureId fid );
29+
30+
/** Deletes a set of features from the layer (but does not commit it) */
31+
virtual bool deleteFeatures( QgsFeatureIds fid );
2932

3033
/** Change feature's geometry */
31-
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
34+
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
3235

3336
/** Changed an attribute value (but does not commit it) */
34-
bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );
37+
virtual bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );
3538

3639
/** Add an attribute field (but does not commit it)
3740
returns true if the field was added */
38-
bool addAttribute( const QgsField &field );
41+
virtual bool addAttribute( const QgsField &field );
3942

4043
/** Delete an attribute field (but does not commit it) */
41-
bool deleteAttribute( int attr );
44+
virtual bool deleteAttribute( int attr );
4245

4346

4447
/**
@@ -56,10 +59,10 @@ class QgsVectorLayerEditBuffer : QObject
5659
Therefore any error message also includes which stage failed so
5760
that the user has some chance of repairing the damage cleanly.
5861
*/
59-
bool commitChanges( QStringList& commitErrors );
62+
virtual bool commitChanges( QStringList& commitErrors );
6063

6164
/** Stop editing and discard the edits */
62-
void rollBack();
65+
virtual void rollBack();
6366

6467

6568

src/core/qgsvectorlayer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,21 @@ bool QgsVectorLayer::deleteFeature( QgsFeatureId fid )
23322332
return res;
23332333
}
23342334

2335+
bool QgsVectorLayer::deleteFeatures( QgsFeatureIds fids )
2336+
{
2337+
if ( !mEditBuffer )
2338+
return false;
2339+
2340+
bool res = mEditBuffer->deleteFeatures( fids );
2341+
2342+
if ( res )
2343+
mSelectedFeatureIds.subtract( fid ); // remove it from selection
2344+
2345+
updateExtents();
2346+
2347+
return res;
2348+
}
2349+
23352350
QgsAttributeList QgsVectorLayer::pkAttributeList() const
23362351
{
23372352
QgsAttributeList pkAttributesList;

src/core/qgsvectorlayer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
12761276
/** Delete a feature from the layer (but does not commit it) */
12771277
bool deleteFeature( QgsFeatureId fid );
12781278

1279+
/**
1280+
* Deletes a set of features from the layer (but does not commit it)
1281+
* @param fids The feature ids to delete
1282+
*
1283+
* @return false if the layer is not in edit mode or does not support deleting
1284+
* in case of an active transaction depends on the provider implementation
1285+
*/
1286+
bool deleteFeatures( QgsFeatureIds fids );
1287+
12791288
/**
12801289
Attempts to commit any changes to disk. Returns the result of the attempt.
12811290
If a commit fails, the in-memory changes are left alone.

src/core/qgsvectorlayereditbuffer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ bool QgsVectorLayerEditBuffer::deleteFeature( QgsFeatureId fid )
161161
return true;
162162
}
163163

164+
bool QgsVectorLayerEditBuffer::deleteFeatures( QgsFeatureIds fids )
165+
{
166+
if ( !( L->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
167+
return false;
168+
169+
Q_FOREACH ( const QgsFeatureId& fid, fids )
170+
deleteFeature( fid );
171+
172+
return true;
173+
}
174+
164175

165176
bool QgsVectorLayerEditBuffer::changeGeometry( QgsFeatureId fid, QgsGeometry* geom )
166177
{

src/core/qgsvectorlayereditbuffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject
5353
/** Delete a feature from the layer (but does not commit it) */
5454
virtual bool deleteFeature( QgsFeatureId fid );
5555

56+
/** Deletes a set of features from the layer (but does not commit it) */
57+
virtual bool deleteFeatures( QgsFeatureIds fid );
58+
5659
/** Change feature's geometry */
5760
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
5861

src/core/qgsvectorlayereditpassthrough.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
5454
return false;
5555
}
5656

57+
bool QgsVectorLayerEditPassthrough::deleteFeatures( QgsFeatureIds fids )
58+
{
59+
if ( L->dataProvider()->deleteFeatures( fids ) )
60+
{
61+
Q_FOREACH ( const QgsFeatureId& fid, fids )
62+
emit featureDeleted( fid );
63+
64+
return true;
65+
}
66+
return false;
67+
}
68+
5769
bool QgsVectorLayerEditPassthrough::changeGeometry( QgsFeatureId fid, QgsGeometry* geom )
5870
{
5971
QgsGeometryMap geomMap;

src/core/qgsvectorlayereditpassthrough.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
2828
bool addFeature( QgsFeature& f ) override;
2929
bool addFeatures( QgsFeatureList& features ) override;
3030
bool deleteFeature( QgsFeatureId fid ) override;
31+
bool deleteFeatures( QgsFeatureIds fids ) override;
3132
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom ) override;
3233
bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ) override;
3334
bool addAttribute( const QgsField &field ) override;

0 commit comments

Comments
 (0)