Skip to content
Permalink
Browse files

Add QgsVectorLayer::deleteFeatures() method

  • Loading branch information
m-kuhn committed Nov 30, 2015
1 parent 53b116a commit 68a01c9ab1d080bcf5161176c7ae6f3816bf3287
@@ -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 );

@@ -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.
@@ -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 );


/**
@@ -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();



@@ -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;
@@ -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.
@@ -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 )
{
@@ -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 );

@@ -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;
@@ -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;

0 comments on commit 68a01c9

Please sign in to comment.
You can’t perform that action at this time.