From 08b6b6065a6c0aea8302b68739a5e8d902feba11 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Sat, 6 Oct 2018 10:53:12 +0200 Subject: [PATCH] Show a warning when pasted feature geometry collapsed ... due to due to intersection avoidance Features are still pasted but the user is warned. Fixes #20020 --- src/app/qgisapp.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index bc66e7d79cf..0bc072e6509 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -8611,7 +8611,7 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer ) QgsExpressionContext context = pasteVectorLayer->createExpressionContext(); - + int invalidGeometriesCount = 0; QgsFeatureList newFeatures; QgsFeatureList::const_iterator featureIt = features.constBegin(); while ( featureIt != features.constEnd() ) @@ -8653,13 +8653,14 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer ) // avoid intersection if enabled in digitize settings geom.avoidIntersections( QgsProject::instance()->avoidIntersectionsLayers() ); } + if ( geom.isEmpty() || geom.isNull( ) ) + invalidGeometriesCount++; // now create new feature using pasted feature as a template. This automatically handles default // values and field constraints newFeatures << QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context ); ++featureIt; } - pasteVectorLayer->addFeatures( newFeatures ); QgsFeatureIds newIds; for ( const QgsFeature &f : qgis::as_const( newFeatures ) ) @@ -8673,26 +8674,31 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer ) pasteVectorLayer->updateExtents(); int nCopiedFeatures = features.count(); + Qgis::MessageLevel level = ( nCopiedFeatures == 0 || nCopiedFeatures < nTotalFeatures || invalidGeometriesCount > 0 ) ? Qgis::Warning : Qgis::Info; + QString message; if ( nCopiedFeatures == 0 ) { - messageBar()->pushMessage( tr( "Paste features" ), - tr( "no features could be successfully pasted." ), - Qgis::Warning, messageTimeout() ); - + message = tr( "No features could be successfully pasted." ); } else if ( nCopiedFeatures == nTotalFeatures ) { - messageBar()->pushMessage( tr( "Paste features" ), - tr( "%1 features were successfully pasted." ).arg( nCopiedFeatures ), - Qgis::Info, messageTimeout() ); + message = tr( "%1 features were successfully pasted." ).arg( nCopiedFeatures ); } else { - messageBar()->pushMessage( tr( "Paste features" ), - tr( "%1 of %2 features could be successfully pasted." ).arg( nCopiedFeatures ).arg( nTotalFeatures ), - Qgis::Warning, messageTimeout() ); + message = tr( "%1 of %2 features could be successfully pasted." ).arg( nCopiedFeatures ).arg( nTotalFeatures ); } + // warn the user if the pasted features have invalid geometries + if ( invalidGeometriesCount > 0 ) + message += invalidGeometriesCount == 1 ? tr( " Geometry collapsed due to intersection avoidance." ) : + tr( "%1 geometries collapsed due to intersection avoidance." ) + .arg( invalidGeometriesCount ); + + messageBar()->pushMessage( tr( "Paste features" ), + message, + level, messageTimeout() ); + pasteVectorLayer->triggerRepaint(); }