@@ -8597,70 +8597,35 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
8597
8597
pasteVectorLayer->beginEditCommand ( tr ( " Features pasted" ) );
8598
8598
QgsFeatureList features = clipboard ()->transformedCopyOf ( pasteVectorLayer->crs (), pasteVectorLayer->fields () );
8599
8599
int nTotalFeatures = features.count ();
8600
-
8601
- QHash<int , int > remap;
8602
- QgsFields fields = clipboard ()->fields ();
8603
- for ( int idx = 0 ; idx < fields.count (); ++idx )
8604
- {
8605
- int dst = pasteVectorLayer->fields ().lookupField ( fields.at ( idx ).name () );
8606
- if ( dst < 0 )
8607
- continue ;
8608
-
8609
- remap.insert ( idx, dst );
8610
- }
8611
-
8612
8600
QgsExpressionContext context = pasteVectorLayer->createExpressionContext ();
8613
8601
8614
- int invalidGeometriesCount = 0 ;
8602
+ QgsFeatureList compatibleFeatures ( QgsVectorLayerUtils::makeFeaturesCompatible ( features, pasteVectorLayer ) ) ;
8615
8603
QgsFeatureList newFeatures;
8616
- QgsFeatureList::const_iterator featureIt = features.constBegin ();
8617
- while ( featureIt != features.constEnd () )
8604
+ // Count collapsed geometries
8605
+ int invalidGeometriesCount = 0 ;
8606
+
8607
+ for ( const auto &feature : qgis::as_const ( compatibleFeatures ) )
8618
8608
{
8619
- QgsAttributes srcAttr = featureIt->attributes ();
8620
- QgsAttributeMap dstAttr;
8621
8609
8622
- for ( int src = 0 ; src < srcAttr.count (); ++src )
8623
- {
8624
- int dst = remap.value ( src, -1 );
8625
- if ( dst < 0 )
8626
- continue ;
8610
+ QgsGeometry geom = feature.geometry ();
8627
8611
8628
- dstAttr[ dst ] = srcAttr.at ( src );
8629
- }
8630
-
8631
- QgsGeometry geom = featureIt->geometry ();
8632
- bool geomWasInvalid = geom.isEmpty () || geom.isNull ( );
8633
- if ( featureIt->hasGeometry () )
8612
+ if ( !( geom.isEmpty () || geom.isNull ( ) ) )
8634
8613
{
8635
- // convert geometry to match destination layer
8636
- QgsWkbTypes::GeometryType destType = pasteVectorLayer->geometryType ();
8637
- bool destIsMulti = QgsWkbTypes::isMultiType ( pasteVectorLayer->wkbType () );
8638
- if ( pasteVectorLayer->dataProvider () &&
8639
- !pasteVectorLayer->dataProvider ()->doesStrictFeatureTypeCheck () )
8640
- {
8641
- // force destination to multi if provider doesn't do a feature strict check
8642
- destIsMulti = true ;
8643
- }
8644
-
8645
- if ( destType != QgsWkbTypes::UnknownGeometry )
8646
- {
8647
- QgsGeometry newGeometry = geom.convertToType ( destType, destIsMulti );
8648
- if ( newGeometry.isNull () )
8649
- {
8650
- continue ;
8651
- }
8652
- geom = newGeometry;
8653
- }
8654
8614
// avoid intersection if enabled in digitize settings
8655
8615
geom.avoidIntersections ( QgsProject::instance ()->avoidIntersectionsLayers () );
8616
+ // Count collapsed geometries
8617
+ if ( geom.isEmpty () || geom.isNull ( ) )
8618
+ invalidGeometriesCount++;
8656
8619
}
8657
- if ( ! geomWasInvalid && ( geom.isEmpty () || geom.isNull ( ) ) )
8658
- invalidGeometriesCount++;
8659
8620
8621
+ QgsAttributeMap attrMap;
8622
+ for ( int i = 0 ; i < feature.fields ().count (); i++ )
8623
+ {
8624
+ attrMap[i] = feature.attribute ( i );
8625
+ }
8660
8626
// now create new feature using pasted feature as a template. This automatically handles default
8661
8627
// values and field constraints
8662
- newFeatures << QgsVectorLayerUtils::createFeature ( pasteVectorLayer, geom, dstAttr, &context );
8663
- ++featureIt;
8628
+ newFeatures << QgsVectorLayerUtils::createFeature ( pasteVectorLayer, geom, attrMap, &context );
8664
8629
}
8665
8630
pasteVectorLayer->addFeatures ( newFeatures );
8666
8631
QgsFeatureIds newIds;
0 commit comments