Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 023f16f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
6 changes: 3 additions & 3 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -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

Expand Down Expand Up @@ -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).

Expand All @@ -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)

Expand Down
37 changes: 28 additions & 9 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -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 ) )
{
Expand All @@ -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();
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );
}
}
}
Expand All @@ -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 )
{
Expand All @@ -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 )
{
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 023f16f

Please sign in to comment.