Skip to content

Commit

Permalink
use iterator for looping validFeatures
Browse files Browse the repository at this point in the history
better naming of the buttons and the enums
  • Loading branch information
signedav committed Jan 13, 2020
1 parent e906778 commit 4574268
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
70 changes: 41 additions & 29 deletions src/app/qgisapp.cpp
Expand Up @@ -9591,45 +9591,57 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
QgsFeatureList newFeatures {QgsVectorLayerUtils::createFeatures( pasteVectorLayer, newFeaturesDataList, &context )};

// check constraints
QgsFeatureList validFeatures = newFeatures;
QgsFeatureList invalidFeatures;
for ( const QgsFeature &f : qgis::as_const( validFeatures ) )
bool hasStrongConstraints = false;

for ( const QgsField &field : pasteVectorLayer->fields() )
{
if ( ( field.constraints().constraints() & QgsFieldConstraints::ConstraintUnique && !field.constraints().constraintExpression().isEmpty() && field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) & QgsFieldConstraints::ConstraintStrengthHard )
|| ( field.constraints().constraints() & QgsFieldConstraints::ConstraintNotNull && field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) & QgsFieldConstraints::ConstraintStrengthHard )
|| ( field.constraints().constraints() & QgsFieldConstraints::ConstraintExpression && field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) & QgsFieldConstraints::ConstraintStrengthHard )
)
hasStrongConstraints = true;
}

if ( hasStrongConstraints )
{
for ( int idx = 0; idx < pasteVectorLayer->fields().count(); ++idx )
QgsFeatureList validFeatures = newFeatures;
QgsFeatureList invalidFeatures;
QMutableListIterator<QgsFeature> it( validFeatures );
while ( it.hasNext() )
{
QStringList errors;
if ( !QgsVectorLayerUtils::validateAttribute( pasteVectorLayer, f, idx, errors, QgsFieldConstraints::ConstraintStrengthHard, QgsFieldConstraints::ConstraintOriginNotSet ) )
QgsFeature &f = it.next();
for ( int idx = 0; idx < pasteVectorLayer->fields().count(); ++idx )
{
invalidFeatures << f;
validFeatures.removeOne( f );
break;
QStringList errors;
if ( !QgsVectorLayerUtils::validateAttribute( pasteVectorLayer, f, idx, errors, QgsFieldConstraints::ConstraintStrengthHard, QgsFieldConstraints::ConstraintOriginNotSet ) )
{
invalidFeatures << f;
it.remove();
break;
}
}
}
}

if ( !invalidFeatures.isEmpty() )
{
newFeatures.clear();
if ( !invalidFeatures.isEmpty() )
{
newFeatures.clear();

QgsFixAttributeDialog *dialog = new QgsFixAttributeDialog( pasteVectorLayer, invalidFeatures, this );
int feedback = dialog->exec();
QgsFixAttributeDialog *dialog = new QgsFixAttributeDialog( pasteVectorLayer, invalidFeatures, this );
int feedback = dialog->exec();

if ( feedback == QgsFixAttributeDialog::VanishAll )
{
//vanish all
}
else if ( feedback == QgsFixAttributeDialog::CopyValid )
{
//copy valid and fixed, vanish unfixed
newFeatures << validFeatures << dialog->fixedFeatures();
}
else if ( feedback == QgsFixAttributeDialog::CopyAll )
{
//copy all, even unfixed
newFeatures << validFeatures << dialog->fixedFeatures() << dialog->unfixedFeatures();
switch ( feedback )
{
case QgsFixAttributeDialog::PasteValid:
//paste valid and fixed, vanish unfixed
newFeatures << validFeatures << dialog->fixedFeatures();
break;
case QgsFixAttributeDialog::PasteAll:
//paste all, even unfixed
newFeatures << validFeatures << dialog->fixedFeatures() << dialog->unfixedFeatures();
break;
}
}
}

pasteVectorLayer->addFeatures( newFeatures );
QgsFeatureIds newIds;
newIds.reserve( newFeatures.size() );
Expand Down
20 changes: 10 additions & 10 deletions src/gui/qgsfixattributedialog.cpp
Expand Up @@ -29,7 +29,7 @@ QgsFixAttributeDialog::QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList
void QgsFixAttributeDialog::init( QgsVectorLayer *layer )
{
QgsAttributeEditorContext context;
setWindowTitle( tr( "%1 - Fix Pastet Features" ).arg( layer->name() ) );
setWindowTitle( tr( "%1 - Fix Pasted Features" ).arg( layer->name() ) );
setLayout( new QGridLayout() );
layout()->setMargin( 0 );
context.setFormMode( QgsAttributeEditorContext::StandaloneDialog );
Expand Down Expand Up @@ -57,31 +57,31 @@ void QgsFixAttributeDialog::init( QgsVectorLayer *layer )
layout()->addWidget( mAttributeForm );

QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>();
QPushButton *cancelAllBtn = new QPushButton( tr( "Cancel all" ) );
QPushButton *cancelAllInvalidBtn = new QPushButton( tr( "Cancel all invalid" ) );
QPushButton *storeAllInvalidBtn = new QPushButton( tr( "Store all (even invalid)" ) );
QPushButton *cancelAllBtn = new QPushButton( tr( "Discard All" ) );
QPushButton *cancelAllInvalidBtn = new QPushButton( tr( "Discard All Invalid" ) );
QPushButton *storeAllInvalidBtn = new QPushButton( tr( "Paste All (Including Invalid)" ) );
if ( mFeatures.count() > 1 )
{
buttonBox->addButton( cancelAllBtn, QDialogButtonBox::ActionRole );
buttonBox->addButton( cancelAllInvalidBtn, QDialogButtonBox::ActionRole );
connect( cancelAllBtn, &QAbstractButton::clicked, this, [ = ]()
{
done( VanishAll );
done( DiscardAll );
} );
connect( cancelAllInvalidBtn, &QAbstractButton::clicked, this, [ = ]()
{
done( CopyValid );
done( PasteValid );
} );
buttonBox->button( QDialogButtonBox::Cancel )->setText( tr( "Skip" ) );
}
else
{
storeAllInvalidBtn->setText( tr( "Store anyway" ) );
storeAllInvalidBtn->setText( tr( "Paste Anyway" ) );
}
buttonBox->addButton( storeAllInvalidBtn, QDialogButtonBox::ActionRole );
connect( storeAllInvalidBtn, &QAbstractButton::clicked, this, [ = ]()
{
done( CopyAll );
done( PasteAll );
} );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsFixAttributeDialog::reject );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsFixAttributeDialog::accept );
Expand Down Expand Up @@ -109,7 +109,7 @@ void QgsFixAttributeDialog::accept()
}
else
{
done( CopyValid );
done( PasteValid );
}

mProgressBar->setValue( mCurrentFeature - mFeatures.begin() );
Expand All @@ -126,7 +126,7 @@ void QgsFixAttributeDialog::reject()
}
else
{
done( CopyValid );
done( PasteValid );
}

mProgressBar->setValue( mCurrentFeature - mFeatures.begin() );
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsfixattributedialog.h
Expand Up @@ -44,9 +44,9 @@ class GUI_EXPORT QgsFixAttributeDialog : public QDialog
*/
enum Feedback
{
VanishAll, //!< Feedback to cancel copy of all features (even valid ones)
CopyValid, //!< Feedback to copy the valid features and vanishe the invalid ones
CopyAll //!< Feedback to copy all features, no matter if valid or invalid
DiscardAll, //!< Feedback to discard all features (even valid ones)
PasteValid, //!< Feedback to paste the valid features and vanishe the invalid ones
PasteAll //!< Feedback to paste all features, no matter if valid or invalid
};

/**
Expand Down

0 comments on commit 4574268

Please sign in to comment.