Skip to content
Permalink
Browse files

On a delete, check for possiblity that features of a referencing or j…

…oining layer could be impacted and print warning.

It's a general warning, and not depending on what features should be deleted. This to avoid long time for checking all features in the relations.
  • Loading branch information
signedav committed May 8, 2020
1 parent aa82247 commit 342521e8416055765815d9bf1be4e8a8c78a87a3
@@ -9001,6 +9001,16 @@ void QgisApp::deleteSelected( QgsMapLayer *layer, QWidget *parent, bool checkFea
}
}

if ( QgsVectorLayerUtils::impactsCascadeFeatures(vlayer, QgsProject::instance() ) )
{
// for extra safety to make sure we know that the delete can have impact on children and joins
int res = QMessageBox::warning( mMapCanvas, tr( "Possible impact on descendants of layer \"%1\"").arg( vlayer->name() ),
tr( "A delete on this layer could have an impact on referencing or joined layers and their descendants. Would you still like to continue?" ),
QMessageBox::Yes | QMessageBox::No );
if ( res != QMessageBox::Yes )
return;
}

vlayer->beginEditCommand( tr( "Features deleted" ) );
int deletedCount = 0;
QgsVectorLayer::DeleteContext context { true };
@@ -864,6 +864,16 @@ void QgsAttributeTableDialog::setFilterExpression( const QString &filterString,
void QgsAttributeTableDialog::deleteFeature( const QgsFeatureId fid )
{
QgsDebugMsg( QStringLiteral( "Delete %1" ).arg( fid ) );
if ( QgsVectorLayerUtils::impactsCascadeFeatures(mLayer, QgsProject::instance() ) )
{
// for extra safety to make sure we know that the delete can have impact on children and joins
int res = QMessageBox::warning( this, tr( "Possible impact on descendants of layer \"%1\"").arg( mLayer->name() ),
tr( "A delete on this layer could have an impact on referencing or joined layers and their descendants. Would you still like to continue?" ),
QMessageBox::Yes | QMessageBox::No );
if ( res != QMessageBox::Yes )
return;
}

QgsVectorLayer::DeleteContext context { true };
mLayer->deleteFeature( fid, &context );
//if it effected more than one layer, print feedback for all descendants
@@ -934,3 +934,30 @@ QString QgsVectorLayerUtils::getFeatureDisplayString( const QgsVectorLayer *laye

return displayString;
}

bool QgsVectorLayerUtils::impactsCascadeFeatures(const QgsVectorLayer *layer, const QgsProject *project)
{
if ( !layer )
return false;

const QList<QgsRelation> relations = project->relationManager()->referencedRelations( layer );
for ( const QgsRelation &relation : relations )
{
if ( relation.strength() == QgsRelation::Composition )
{
return true;
}
}

layer->joinBuffer()->containsJoins();
const auto constVectorJoins = layer->joinBuffer()->vectorJoins();
for ( const QgsVectorLayerJoinInfo &info : constVectorJoins )
{
if ( info.isEditable() && info.hasCascadedDelete() )
{
return true;
}
}

return false;
}
@@ -319,6 +319,15 @@ class CORE_EXPORT QgsVectorLayerUtils
* \since QGIS 3.12
*/
static QString getFeatureDisplayString( const QgsVectorLayer *layer, const QgsFeature &feature );

/**
* \returns true if the \a is connected as parent in at least one composition relation of the \a project
* or contains joins, where cascade delete is set.
*
* \since QGIS 3.14
*/
static bool impactsCascadeFeatures(const QgsVectorLayer *layer, const QgsProject *project);

};


@@ -728,6 +728,16 @@ void QgsRelationEditorWidget::deleteFeatures( const QgsFeatureIds &featureids )
layer = mRelation.referencingLayer();
}

if ( QgsVectorLayerUtils::impactsCascadeFeatures(layer, QgsProject::instance() ) )
{
// for extra safety to make sure we know that the delete can have impact on children and joins
int res = QMessageBox::warning( this, tr( "Possible impact on descendants of layer \"%1\"").arg( layer->name() ),
tr( "A delete on this layer could have an impact on referencing or joined layers and their descendants. Would you still like to continue?" ),
QMessageBox::Yes | QMessageBox::No );
if ( res == QMessageBox::No )
deleteFeatures = false;
}

if ( deleteFeatures )
{
QgsVectorLayer::DeleteContext context { true };

0 comments on commit 342521e

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