Skip to content

Commit a2e5f24

Browse files
committedJan 7, 2020
hack to check behavior if copy a non valid feature against hard constraints
1 parent 23f862b commit a2e5f24

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#include "qgssourceselectproviderregistry.h"
8888
#include "qgssourceselectprovider.h"
8989
#include "qgsprovidermetadata.h"
90+
#include "qgsattributedialog.h"
9091

9192
#include "qgsanalysis.h"
9293
#include "qgsgeometrycheckregistry.h"
@@ -9588,6 +9589,45 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
95889589
// now create new feature using pasted feature as a template. This automatically handles default
95899590
// values and field constraints
95909591
QgsFeatureList newFeatures {QgsVectorLayerUtils::createFeatures( pasteVectorLayer, newFeaturesDataList, &context )};
9592+
9593+
// check against hard constraints
9594+
for ( QgsFeature &f : newFeatures )
9595+
{
9596+
bool valid = true;
9597+
for ( int idx = 0; idx < pasteVectorLayer->fields().count(); ++idx )
9598+
{
9599+
QStringList errors;
9600+
valid = QgsVectorLayerUtils::validateAttribute( pasteVectorLayer, f, idx, errors, QgsFieldConstraints::ConstraintStrengthHard, QgsFieldConstraints::ConstraintOriginNotSet );
9601+
if ( !valid )
9602+
{
9603+
break;
9604+
}
9605+
}
9606+
9607+
//open attribute form including option "cancel copy all" / "cancel copy of invalid" / "store invalid features"
9608+
if ( !valid )
9609+
{
9610+
//QgsFeatureAction action( tr( "Fix feature" ), f, pasteVectorLayer, QString(), -1, QgisApp::instance() );
9611+
//action.editFeature( true );
9612+
QgsAttributeDialog *dialog = new QgsAttributeDialog( pasteVectorLayer, &f, false );
9613+
9614+
if ( !f.isValid() )
9615+
dialog->setMode( QgsAttributeEditorContext::AddFeatureMode );
9616+
9617+
int feedback = dialog->exec();
9618+
9619+
if ( feedback > 0 )
9620+
{
9621+
f.setAttributes( dialog->feature()->attributes() );
9622+
}
9623+
else
9624+
{
9625+
visibleMessageBar()->pushMessage( tr( "Paste features" ), tr( "Do not copy feature" ), Qgis::Warning, messageTimeout() );
9626+
newFeatures.removeOne( f );
9627+
}
9628+
}
9629+
}
9630+
95919631
pasteVectorLayer->addFeatures( newFeatures );
95929632
QgsFeatureIds newIds;
95939633
newIds.reserve( newFeatures.size() );
@@ -9600,7 +9640,7 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
96009640
pasteVectorLayer->endEditCommand();
96019641
pasteVectorLayer->updateExtents();
96029642

9603-
int nCopiedFeatures = features.count();
9643+
int nCopiedFeatures = newFeatures.count();
96049644
Qgis::MessageLevel level = ( nCopiedFeatures == 0 || nCopiedFeatures < nTotalFeatures || invalidGeometriesCount > 0 ) ? Qgis::Warning : Qgis::Info;
96059645
QString message;
96069646
if ( nCopiedFeatures == 0 )

0 commit comments

Comments
 (0)
Please sign in to comment.