Skip to content

Commit

Permalink
Add QgsVectorLayer::deleteFeatures() method
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Nov 30, 2015
1 parent 53b116a commit 68a01c9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
11 changes: 9 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -755,8 +755,6 @@ class QgsVectorLayer : QgsMapLayer
@param f feature to add
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
@return True in case of success and False in case of error

// TODO QGIS3: QgsFeature& f /InOut/
*/
bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );

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

/**
* Deletes a set of features from the layer (but does not commit it)
* @param fids The feature ids to delete
*
* @return false if the layer is not in edit mode or does not support deleting
* in case of an active transaction depends on the provider implementation
*/
bool deleteFeatures( QgsFeatureIds fids );

/**
Attempts to commit any changes to disk. Returns the result of the attempt.
If a commit fails, the in-memory changes are left alone.
Expand Down
23 changes: 13 additions & 10 deletions python/core/qgsvectorlayereditbuffer.sip
Expand Up @@ -12,33 +12,36 @@ class QgsVectorLayerEditBuffer : QObject
~QgsVectorLayerEditBuffer();

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


/** Adds a feature
@param f feature to add
@return True in case of success and False in case of error
*/
bool addFeature( QgsFeature& f );
virtual bool addFeature( QgsFeature& f );

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

/** Delete a feature from the layer (but does not commit it) */
bool deleteFeature( QgsFeatureId fid );
virtual bool deleteFeature( QgsFeatureId fid );

/** Deletes a set of features from the layer (but does not commit it) */
virtual bool deleteFeatures( QgsFeatureIds fid );

/** Change feature's geometry */
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );

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

/** Add an attribute field (but does not commit it)
returns true if the field was added */
bool addAttribute( const QgsField &field );
virtual bool addAttribute( const QgsField &field );

/** Delete an attribute field (but does not commit it) */
bool deleteAttribute( int attr );
virtual bool deleteAttribute( int attr );


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

/** Stop editing and discard the edits */
void rollBack();
virtual void rollBack();



Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2332,6 +2332,21 @@ bool QgsVectorLayer::deleteFeature( QgsFeatureId fid )
return res;
}

bool QgsVectorLayer::deleteFeatures( QgsFeatureIds fids )
{
if ( !mEditBuffer )
return false;

bool res = mEditBuffer->deleteFeatures( fids );

if ( res )
mSelectedFeatureIds.subtract( fid ); // remove it from selection

updateExtents();

return res;
}

QgsAttributeList QgsVectorLayer::pkAttributeList() const
{
QgsAttributeList pkAttributesList;
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1276,6 +1276,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Delete a feature from the layer (but does not commit it) */
bool deleteFeature( QgsFeatureId fid );

/**
* Deletes a set of features from the layer (but does not commit it)
* @param fids The feature ids to delete
*
* @return false if the layer is not in edit mode or does not support deleting
* in case of an active transaction depends on the provider implementation
*/
bool deleteFeatures( QgsFeatureIds fids );

/**
Attempts to commit any changes to disk. Returns the result of the attempt.
If a commit fails, the in-memory changes are left alone.
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsvectorlayereditbuffer.cpp
Expand Up @@ -161,6 +161,17 @@ bool QgsVectorLayerEditBuffer::deleteFeature( QgsFeatureId fid )
return true;
}

bool QgsVectorLayerEditBuffer::deleteFeatures( QgsFeatureIds fids )
{
if ( !( L->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
return false;

Q_FOREACH ( const QgsFeatureId& fid, fids )
deleteFeature( fid );

return true;
}


bool QgsVectorLayerEditBuffer::changeGeometry( QgsFeatureId fid, QgsGeometry* geom )
{
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayereditbuffer.h
Expand Up @@ -53,6 +53,9 @@ class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject
/** Delete a feature from the layer (but does not commit it) */
virtual bool deleteFeature( QgsFeatureId fid );

/** Deletes a set of features from the layer (but does not commit it) */
virtual bool deleteFeatures( QgsFeatureIds fid );

/** Change feature's geometry */
virtual bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );

Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsvectorlayereditpassthrough.cpp
Expand Up @@ -54,6 +54,18 @@ bool QgsVectorLayerEditPassthrough::deleteFeature( QgsFeatureId fid )
return false;
}

bool QgsVectorLayerEditPassthrough::deleteFeatures( QgsFeatureIds fids )
{
if ( L->dataProvider()->deleteFeatures( fids ) )
{
Q_FOREACH ( const QgsFeatureId& fid, fids )
emit featureDeleted( fid );

return true;
}
return false;
}

bool QgsVectorLayerEditPassthrough::changeGeometry( QgsFeatureId fid, QgsGeometry* geom )
{
QgsGeometryMap geomMap;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayereditpassthrough.h
Expand Up @@ -28,6 +28,7 @@ class CORE_EXPORT QgsVectorLayerEditPassthrough : public QgsVectorLayerEditBuffe
bool addFeature( QgsFeature& f ) override;
bool addFeatures( QgsFeatureList& features ) override;
bool deleteFeature( QgsFeatureId fid ) override;
bool deleteFeatures( QgsFeatureIds fids ) override;
bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom ) override;
bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() ) override;
bool addAttribute( const QgsField &field ) override;
Expand Down

0 comments on commit 68a01c9

Please sign in to comment.