|
| 1 | +/*************************************************************************** |
| 2 | + qgsfixattributedialog.cpp |
| 3 | + --------------------- |
| 4 | + begin : January 2020 |
| 5 | + copyright : (C) 2020 by David Signer |
| 6 | + email : david at opengis dot ch |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | +#include "qgsfixattributedialog.h" |
| 16 | + |
| 17 | +#include "qgsattributeform.h" |
| 18 | +#include "qgsapplication.h" |
| 19 | + |
| 20 | +#include <QtWidgets/QPushButton> |
| 21 | + |
| 22 | +QgsFixAttributeDialog::QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList &features, QWidget *parent ) |
| 23 | + : QDialog( parent ) |
| 24 | + , mFeatures( features ) |
| 25 | +{ |
| 26 | + init( vl ); |
| 27 | +} |
| 28 | + |
| 29 | +void QgsFixAttributeDialog::init( QgsVectorLayer *layer ) |
| 30 | +{ |
| 31 | + QgsAttributeEditorContext context; |
| 32 | + setWindowTitle( tr( "%1 - Fix Pasted Features" ).arg( layer->name() ) ); |
| 33 | + setLayout( new QGridLayout() ); |
| 34 | + layout()->setMargin( 0 ); |
| 35 | + context.setFormMode( QgsAttributeEditorContext::StandaloneDialog ); |
| 36 | + |
| 37 | + mUnfixedFeatures = mFeatures; |
| 38 | + mCurrentFeature = mFeatures.begin(); |
| 39 | + |
| 40 | + QGridLayout *infoLayout = new QGridLayout(); |
| 41 | + QWidget *infoBox = new QWidget(); |
| 42 | + infoBox->setLayout( infoLayout ); |
| 43 | + layout()->addWidget( infoBox ); |
| 44 | + |
| 45 | + mDescription = new QLabel( descriptionText() ); |
| 46 | + mDescription->setVisible( mFeatures.count() > 1 ); |
| 47 | + infoLayout->addWidget( mDescription ); |
| 48 | + mProgressBar = new QProgressBar(); |
| 49 | + mProgressBar->setOrientation( Qt::Horizontal ); |
| 50 | + mProgressBar->setRange( 0, mFeatures.count() ); |
| 51 | + mProgressBar->setVisible( mFeatures.count() > 1 ); |
| 52 | + infoLayout->addWidget( mProgressBar ); |
| 53 | + QgsFeature feature; |
| 54 | + mAttributeForm = new QgsAttributeForm( layer, *mCurrentFeature, context, this ); |
| 55 | + mAttributeForm->setMode( QgsAttributeEditorContext::SingleEditMode ); |
| 56 | + mAttributeForm->disconnectButtonBox(); |
| 57 | + layout()->addWidget( mAttributeForm ); |
| 58 | + |
| 59 | + QDialogButtonBox *buttonBox = mAttributeForm->findChild<QDialogButtonBox *>(); |
| 60 | + QPushButton *cancelAllBtn = new QPushButton( tr( "Discard All" ) ); |
| 61 | + QPushButton *cancelAllInvalidBtn = new QPushButton( tr( "Discard All Invalid" ) ); |
| 62 | + QPushButton *storeAllInvalidBtn = new QPushButton( tr( "Paste All (Including Invalid)" ) ); |
| 63 | + if ( mFeatures.count() > 1 ) |
| 64 | + { |
| 65 | + buttonBox->addButton( cancelAllBtn, QDialogButtonBox::ActionRole ); |
| 66 | + buttonBox->addButton( cancelAllInvalidBtn, QDialogButtonBox::ActionRole ); |
| 67 | + connect( cancelAllBtn, &QAbstractButton::clicked, this, [ = ]() |
| 68 | + { |
| 69 | + done( DiscardAll ); |
| 70 | + } ); |
| 71 | + connect( cancelAllInvalidBtn, &QAbstractButton::clicked, this, [ = ]() |
| 72 | + { |
| 73 | + done( PasteValid ); |
| 74 | + } ); |
| 75 | + buttonBox->button( QDialogButtonBox::Cancel )->setText( tr( "Skip" ) ); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + storeAllInvalidBtn->setText( tr( "Paste Anyway" ) ); |
| 80 | + } |
| 81 | + buttonBox->addButton( storeAllInvalidBtn, QDialogButtonBox::ActionRole ); |
| 82 | + connect( storeAllInvalidBtn, &QAbstractButton::clicked, this, [ = ]() |
| 83 | + { |
| 84 | + done( PasteAll ); |
| 85 | + } ); |
| 86 | + connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsFixAttributeDialog::reject ); |
| 87 | + connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsFixAttributeDialog::accept ); |
| 88 | + connect( layer, &QObject::destroyed, this, &QWidget::close ); |
| 89 | + |
| 90 | + focusNextChild(); |
| 91 | +} |
| 92 | + |
| 93 | +QString QgsFixAttributeDialog::descriptionText() |
| 94 | +{ |
| 95 | + return tr( "%1 of %2 features processed (%3 fixed, %4 skipped)" ).arg( mCurrentFeature - mFeatures.begin() ).arg( mFeatures.count() ).arg( mFixedFeatures.count() ).arg( mCurrentFeature - mFeatures.begin() - mFixedFeatures.count() ); |
| 96 | +} |
| 97 | + |
| 98 | +void QgsFixAttributeDialog::accept() |
| 99 | +{ |
| 100 | + mAttributeForm->save(); |
| 101 | + mFixedFeatures << mAttributeForm->feature(); |
| 102 | + mUnfixedFeatures.removeOne( *mCurrentFeature ); |
| 103 | + |
| 104 | + //next feature |
| 105 | + ++mCurrentFeature; |
| 106 | + if ( mCurrentFeature != mFeatures.end() ) |
| 107 | + { |
| 108 | + mAttributeForm->setFeature( *mCurrentFeature ); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + done( PasteValid ); |
| 113 | + } |
| 114 | + |
| 115 | + mProgressBar->setValue( mCurrentFeature - mFeatures.begin() ); |
| 116 | + mDescription->setText( descriptionText() ); |
| 117 | +} |
| 118 | + |
| 119 | +void QgsFixAttributeDialog::reject() |
| 120 | +{ |
| 121 | + //next feature |
| 122 | + ++mCurrentFeature; |
| 123 | + if ( mCurrentFeature != mFeatures.end() ) |
| 124 | + { |
| 125 | + mAttributeForm->setFeature( *mCurrentFeature ); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + done( PasteValid ); |
| 130 | + } |
| 131 | + |
| 132 | + mProgressBar->setValue( mCurrentFeature - mFeatures.begin() ); |
| 133 | + mDescription->setText( descriptionText() ); |
| 134 | +} |
0 commit comments