Skip to content
Permalink
Browse files

DeleteContext instead of having cascade parameter in delete functions…

… and QMap for response
  • Loading branch information
signedav committed May 7, 2020
1 parent c4154b1 commit 79d86012ae7938b7a713481ada52ddac7966274a
@@ -374,6 +374,18 @@ Constructor for LayerOptions.

};

struct DeleteContext
{

explicit DeleteContext( bool cascade = false );
%Docstring
Constructor for DeleteContext.
%End

QMap<QgsVectorLayer *, QgsFeatureIds> handledFeatures;
bool cascade;
};

explicit QgsVectorLayer( const QString &path = QString(), const QString &baseName = QString(),
const QString &providerLib = "ogr", const QgsVectorLayer::LayerOptions &options = QgsVectorLayer::LayerOptions() );
%Docstring
@@ -1259,7 +1271,7 @@ Deletes a vertex from a feature.
.. versionadded:: 2.14
%End

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

@@ -1944,7 +1956,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, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 );
bool deleteFeature( QgsFeatureId fid, DeleteContext *context = 0 );
%Docstring
Deletes a feature from the layer (but does not commit it).

@@ -1959,7 +1971,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, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 );
bool deleteFeatures( const QgsFeatureIds &fids, DeleteContext *context = 0 );
%Docstring
Deletes a set of features from the layer (but does not commit it)

@@ -207,7 +207,7 @@ created if its fields are not empty.
.. versionadded:: 3.0
%End

bool deleteFeature( QgsFeatureId fid, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 ) const;
bool deleteFeature( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context = 0 ) const;
%Docstring
Deletes a feature from joined layers. The feature id given in
parameter is the one coming from the target layer.
@@ -220,7 +220,7 @@ parameter is the one coming from the target layer.
.. versionadded:: 3.0
%End

bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = 0 ) const;
bool deleteFeatures( const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context = 0 ) const;
%Docstring
Deletes a list of features from joined layers. Feature ids given
in a parameter are those coming from the target layer.
@@ -9003,7 +9003,8 @@ void QgisApp::deleteSelected( QgsMapLayer *layer, QWidget *parent, bool checkFea

vlayer->beginEditCommand( tr( "Features deleted" ) );
int deletedCount = 0;
if ( !vlayer->deleteSelectedFeatures( &deletedCount, true ) )
QgsVectorLayer::DeleteContext context { true };
if ( !vlayer->deleteSelectedFeatures( &deletedCount, &context ) )
{
visibleMessageBar()->pushMessage( tr( "Problem deleting features" ),
tr( "A problem occurred during deletion from layer \"%1\". %n feature(s) not deleted.", nullptr, numberOfSelectedFeatures - deletedCount ).arg( vlayer->name() ),
@@ -864,7 +864,8 @@ void QgsAttributeTableDialog::setFilterExpression( const QString &filterString,
void QgsAttributeTableDialog::deleteFeature( const QgsFeatureId fid )
{
QgsDebugMsg( QStringLiteral( "Delete %1" ).arg( fid ) );
mLayer->deleteFeature( fid, true );
QgsVectorLayer::DeleteContext context { true };
mLayer->deleteFeature( fid, &context );
}

void QgsAttributeTableDialog::showContextMenu( QgsActionMenu *menu, const QgsFeatureId fid )
@@ -1140,7 +1140,7 @@ QgsVectorLayer::EditResult QgsVectorLayer::deleteVertex( QgsFeatureId featureId,
}


bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures )
bool QgsVectorLayer::deleteSelectedFeatures( int *deletedCount, QgsVectorLayer::DeleteContext *context )
{
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, handledFeatures ); // removes from selection
deleted += deleteFeature( fid, context ); // removes from selection
}

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

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

if ( cascade )
if ( context && context->cascade )
{
if ( handledFeatures->contains( this ) )
if ( context->handledFeatures.contains( this ) )
{
QgsFeatureIds handledFeatureIds = handledFeatures->value( this );
QgsFeatureIds handledFeatureIds = context->handledFeatures.value( this );
if ( handledFeatureIds.contains( fid ) )
{
// break recursion
@@ -3183,13 +3183,13 @@ bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, const bool cascade,
{
// add feature id
handledFeatureIds << fid;
handledFeatures->insert( this, handledFeatureIds );
context->handledFeatures.insert( this, handledFeatureIds );
}
}
else
{
// add layer and feature id
handledFeatures->insert( this, QgsFeatureIds() << fid );
context->handledFeatures.insert( this, QgsFeatureIds() << fid );
}

const QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( this );
@@ -3209,25 +3209,25 @@ bool QgsVectorLayer::deleteFeatureCascade( QgsFeatureId fid, const bool cascade,
}

relation.referencingLayer()->startEditing();
relation.referencingLayer()->deleteFeatures( childFeatureIds, cascade, handledFeatures );
relation.referencingLayer()->deleteFeatures( childFeatureIds, context );
}
}
}

if ( mJoinBuffer->containsJoins() )
mJoinBuffer->deleteFeature( fid, cascade, handledFeatures );
mJoinBuffer->deleteFeature( fid, context );

bool res = mEditBuffer->deleteFeature( fid );

return res;
}

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

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

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

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

if ( res )
{
@@ -504,6 +504,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

};

/**
* Context for deleting features
* \since QGIS 3.14
*/
struct DeleteContext
{

/**
* Constructor for DeleteContext.
*/
explicit DeleteContext( bool cascade = false ): cascade( cascade ) {}

QMap<QgsVectorLayer *, QgsFeatureIds> handledFeatures;
bool cascade ;
};

/**
* Constructor - creates a vector layer
*
@@ -1283,7 +1299,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, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );
Q_INVOKABLE bool deleteSelectedFeatures( int *deletedCount = nullptr, DeleteContext *context = nullptr );

/**
* Adds a ring to polygon/multipolygon features
@@ -1823,7 +1839,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, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );
bool deleteFeature( QgsFeatureId fid, DeleteContext *context = nullptr );

/**
* Deletes a set of features from the layer (but does not commit it)
@@ -1838,7 +1854,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, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr );
bool deleteFeatures( const QgsFeatureIds &fids, DeleteContext *context = nullptr );

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

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

#ifdef SIP_RUN
QgsVectorLayer( const QgsVectorLayer &rhs );
@@ -663,12 +663,12 @@ bool QgsVectorLayerJoinBuffer::changeAttributeValues( QgsFeatureId fid, const Qg
return success;
}

bool QgsVectorLayerJoinBuffer::deleteFeature( QgsFeatureId fid, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures ) const
bool QgsVectorLayerJoinBuffer::deleteFeature( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context ) const
{
return deleteFeatures( QgsFeatureIds() << fid, cascade, handledFeatures );
return deleteFeatures( QgsFeatureIds() << fid, context );
}

bool QgsVectorLayerJoinBuffer::deleteFeatures( const QgsFeatureIds &fids, const bool cascade, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures ) const
bool QgsVectorLayerJoinBuffer::deleteFeatures( const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context ) const
{
if ( !containsJoins() )
return false;
@@ -683,7 +683,7 @@ bool QgsVectorLayerJoinBuffer::deleteFeatures( const QgsFeatureIds &fids, const
{
const QgsFeature joinFeature = joinedFeatureOf( &info, mLayer->getFeature( fid ) );
if ( joinFeature.isValid() )
info.joinLayer()->deleteFeature( joinFeature.id(), cascade, handledFeatures );
info.joinLayer()->deleteFeature( joinFeature.id(), context );
}
}
}
@@ -202,7 +202,7 @@ class CORE_EXPORT QgsVectorLayerJoinBuffer : public QObject, public QgsFeatureSi
*
* \since QGIS 3.0
*/
bool deleteFeature( QgsFeatureId fid, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr ) const;
bool deleteFeature( QgsFeatureId fid, QgsVectorLayer::DeleteContext *context = nullptr ) const;

/**
* Deletes a list of features from joined layers. Feature ids given
@@ -214,7 +214,7 @@ class CORE_EXPORT QgsVectorLayerJoinBuffer : public QObject, public QgsFeatureSi
*
* \since QGIS 3.0
*/
bool deleteFeatures( const QgsFeatureIds &fids, const bool cascade = false, QMap<QgsVectorLayer *, QgsFeatureIds> *handledFeatures = nullptr ) const;
bool deleteFeatures( const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context = nullptr ) const;

signals:

@@ -730,7 +730,8 @@ void QgsRelationEditorWidget::deleteFeatures( const QgsFeatureIds &featureids )

if ( deleteFeatures )
{
layer->deleteFeatures( featureids, true );
QgsVectorLayer::DeleteContext context { true };
layer->deleteFeatures( featureids, &context );
updateUi();
}
}
@@ -791,7 +792,7 @@ void QgsRelationEditorWidget::unlinkFeatures( const QgsFeatureIds &featureids )
QgsDebugMsgLevel( FID_TO_STRING( f.id() ), 4 );
}

mRelation.referencingLayer()->deleteFeatures( fids, false );
mRelation.referencingLayer()->deleteFeatures( fids );

updateUi();
}

0 comments on commit 79d8601

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