Skip to content
Permalink
Browse files

the function to delete without updating the extent and deleting casca…

…de is called deleteFeatureCascade

handledFeatures is given to store all the layers and features affected by the delete operation
recursion is blocked by checking handled features
  • Loading branch information
signedav committed May 7, 2020
1 parent f352793 commit 023f16f696e5fcb395c4de40639b314c83b6e0be
Showing with 35 additions and 16 deletions.
  1. +3 −3 python/core/auto_generated/qgsvectorlayer.sip.in
  2. +28 −9 src/core/qgsvectorlayer.cpp
  3. +4 −4 src/core/qgsvectorlayer.h
@@ -1259,7 +1259,7 @@ Deletes a vertex from a feature.
.. versionadded:: 2.14
%End

bool deleteSelectedFeatures( int *deletedCount = 0, const bool cascade = false );
bool deleteSelectedFeatures( int *deletedCount = 0, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 );
%Docstring
Deletes the selected features

@@ -1944,7 +1944,7 @@ Deletes a list of attribute fields (but does not commit it)
virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = 0 ) ${SIP_FINAL};


bool deleteFeature( QgsFeatureId fid, const bool cascade = false );
bool deleteFeature( QgsFeatureId fid, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 );
%Docstring
Deletes a feature from the layer (but does not commit it).

@@ -1959,7 +1959,7 @@ Deletes a feature from the layer (but does not commit it).
changes can be discarded by calling rollBack().
%End

bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false );
bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 );
%Docstring
Deletes a set of features from the layer (but does not commit it)

@@ -1140,7 +1140,7 @@ QgsVectorLayer::EditResult QgsVectorLayer::deleteVertex( QgsFeatureId featureId,
}


bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, const bool cascade )
bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures )
{
if ( !mValid || !mDataProvider || !( mDataProvider->capabilities() & QgsVectorDataProvider::DeleteFeatures ) )
{
@@ -1159,7 +1159,7 @@ bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, const bool casca
const auto constSelectedFeatures = selectedFeatures;
for ( QgsFeatureId fid : constSelectedFeatures )
{
deleted += deleteFeature( fid, cascade ); // removes from selection
deleted += deleteFeature( fid, cascade, handledFeatures ); // removes from selection
}

triggerRepaint();
@@ -3164,13 +3164,33 @@ bool QgsVectorLayer::deleteAttributes( const QList<int> &attrs )
return deleted;
}

bool QgsVectorLayer::deleteFeatureWithDependencies( QgsFeatureId fid, const bool cascade )
bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures )
{
if ( !mEditBuffer )
return false;

if ( cascade )
{
if ( handledFeatures->contains( this ) )
{
QgsFeatureIds handledFeatureIds = handledFeatures->value( this );
if ( handledFeatureIds.contains( fid ) )
{
// break recursion
return false;
}
else
{
// add feature id
handledFeatureIds << fid;
handledFeatures->insert( this, handledFeatureIds );
}
}
else
{
// add layer and feature id
handledFeatures->insert( this, QgsFeatureIds() << fid );
}

const QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( this );

@@ -3188,9 +3208,8 @@ bool QgsVectorLayer::deleteFeatureWithDependencies( QgsFeatureId fid, const bool
childFeatureIds.insert( childFeature.id() );
}

//set childlayer editable
relation.referencingLayer()->startEditing();
relation.referencingLayer()->deleteFeatures( childFeatureIds );
relation.referencingLayer()->deleteFeatures( childFeatureIds, cascade, handledFeatures );
}
}
}
@@ -3203,12 +3222,12 @@ bool QgsVectorLayer::deleteFeatureWithDependencies( QgsFeatureId fid, const bool
return res;
}

bool QgsVectorLayer::deleteFeature( QgsFeatureId fid, const bool cascade )
bool QgsVectorLayer::deleteFeature( QgsFeatureId fid, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures )
{
if ( !mEditBuffer )
return false;

bool res = deleteFeatureWithDependencies( fid, cascade );
bool res = deleteFeatureCascade( fid, cascade, handledFeatures );

if ( res )
{
@@ -3219,12 +3238,12 @@ bool QgsVectorLayer::deleteFeature( QgsFeatureId fid, const bool cascade )
return res;
}

bool QgsVectorLayer::deleteFeatures( const QgsFeatureIds &fids, const bool cascade )
bool QgsVectorLayer::deleteFeatures( const QgsFeatureIds &fids, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures )
{
bool res = true;
const auto constFids = fids;
for ( QgsFeatureId fid : constFids )
res = deleteFeatureWithDependencies( fid, cascade ) && res;
res = deleteFeatureCascade( fid, cascade, handledFeatures ) && res;

if ( res )
{
@@ -1283,7 +1283,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
*
* \returns TRUE in case of success and FALSE otherwise
*/
Q_INVOKABLE bool deleteSelectedFeatures( int *deletedCount = nullptr, const bool cascade = false );
Q_INVOKABLE bool deleteSelectedFeatures( int *deletedCount = nullptr, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );

/**
* Adds a ring to polygon/multipolygon features
@@ -1823,7 +1823,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* to the underlying data provider until a commitChanges() call is made. Any uncommitted
* changes can be discarded by calling rollBack().
*/
bool deleteFeature( QgsFeatureId fid, const bool cascade = false );
bool deleteFeature( QgsFeatureId fid, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );

/**
* Deletes a set of features from the layer (but does not commit it)
@@ -1838,7 +1838,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
* to the underlying data provider until a commitChanges() call is made. Any uncommitted
* changes can be discarded by calling rollBack().
*/
bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false );
bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );

/**
* Attempts to commit to the underlying data provider any buffered changes made since the
@@ -2695,7 +2695,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
//! Read simple labeling from layer's custom properties (QGIS 2.x projects)
QgsAbstractVectorLayerLabeling *readLabelingFromCustomProperties();

bool deleteFeatureWithDependencies( QgsFeatureId fid, const bool cascade = false );
bool deleteFeatureCascade( QgsFeatureId fid, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );

#ifdef SIP_RUN
QgsVectorLayer( const QgsVectorLayer &rhs );

0 comments on commit 023f16f

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