Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix attributes on copy paste while QGIS stays responsive
  • Loading branch information
github-actions[bot] committed Jul 6, 2020
1 parent bb0ef11 commit 717d838
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
49 changes: 33 additions & 16 deletions src/app/qgisapp.cpp
Expand Up @@ -10202,26 +10202,43 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
{
newFeatures.clear();

QgsFixAttributeDialog *dialog = new QgsFixAttributeDialog( pasteVectorLayer, invalidFeatures, this );
int feedback = dialog->exec();
QgsAttributeEditorContext context( createAttributeEditorContext() );
context.setAllowCustomUi( false );
context.setFormMode( QgsAttributeEditorContext::StandaloneDialog );

switch ( feedback )
QgsFixAttributeDialog *dialog = new QgsFixAttributeDialog( pasteVectorLayer, invalidFeatures, this, context );

connect( dialog, &QgsFixAttributeDialog::finished, this, [ = ]( int 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;
}
QgsFeatureList features = newFeatures;
switch ( feedback )
{
case QgsFixAttributeDialog::PasteValid:
//paste valid and fixed, vanish unfixed
features << validFeatures << dialog->fixedFeatures();
break;
case QgsFixAttributeDialog::PasteAll:
//paste all, even unfixed
features << validFeatures << dialog->fixedFeatures() << dialog->unfixedFeatures();
break;
}
pasteFeatures( pasteVectorLayer, invalidGeometriesCount, nTotalFeatures, features );
dialog->deleteLater();
} );
dialog->show();
return;
}
}
pasteVectorLayer->addFeatures( newFeatures );

pasteFeatures( pasteVectorLayer, invalidGeometriesCount, nTotalFeatures, newFeatures );
}

void QgisApp::pasteFeatures( QgsVectorLayer *pasteVectorLayer, int invalidGeometriesCount, int nTotalFeatures, QgsFeatureList &features )
{
pasteVectorLayer->addFeatures( features );
QgsFeatureIds newIds;
newIds.reserve( newFeatures.size() );
for ( const QgsFeature &f : qgis::as_const( newFeatures ) )
newIds.reserve( features.size() );
for ( const QgsFeature &f : qgis::as_const( features ) )
{
newIds << f.id();
}
Expand All @@ -10230,7 +10247,7 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
pasteVectorLayer->endEditCommand();
pasteVectorLayer->updateExtents();

int nCopiedFeatures = newFeatures.count();
int nCopiedFeatures = features.count();
Qgis::MessageLevel level = ( nCopiedFeatures == 0 || invalidGeometriesCount > 0 ) ? Qgis::Warning : Qgis::Info;
QString message;
if ( nCopiedFeatures == 0 )
Expand Down
7 changes: 7 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -927,6 +927,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
(defaults to the active layer on the legend)
*/
void pasteFromClipboard( QgsMapLayer *destinationLayer = nullptr );

//! copies features on the clipboard to a new vector layer
void pasteAsNewVector();
//! copies features on the clipboard to a new memory vector layer
Expand Down Expand Up @@ -2186,6 +2187,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
void resolveVectorLayerWeakRelations( QgsVectorLayer *vectorLayer );

/**
* Pastes the \a features to the \a pasteVectorLayer and gives feedback to the user
* according to \a invalidGeometryCount and \a nTotalFeatures
*/
void pasteFeatures( QgsVectorLayer *pasteVectorLayer, int invalidGeometriesCount, int nTotalFeatures, QgsFeatureList &features );


QgisAppStyleSheet *mStyleSheetBuilder = nullptr;

Expand Down
10 changes: 4 additions & 6 deletions src/app/qgsfixattributedialog.cpp
Expand Up @@ -20,21 +20,19 @@

#include <QtWidgets/QPushButton>

QgsFixAttributeDialog::QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList &features, QWidget *parent )

QgsFixAttributeDialog::QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList &features, QWidget *parent, const QgsAttributeEditorContext &context )
: QDialog( parent )
, mFeatures( features )
{
init( vl );
init( vl, context );
}

void QgsFixAttributeDialog::init( QgsVectorLayer *layer )
void QgsFixAttributeDialog::init( QgsVectorLayer *layer, const QgsAttributeEditorContext &context )
{
QgsAttributeEditorContext context;
setWindowTitle( tr( "%1 - Fix Pasted Features" ).arg( layer->name() ) );
setLayout( new QGridLayout() );
layout()->setMargin( 0 );
context.setFormMode( QgsAttributeEditorContext::StandaloneDialog );
context.setVectorLayerTools( QgisApp::instance()->vectorLayerTools() );

mUnfixedFeatures = mFeatures;
mCurrentFeature = mFeatures.begin();
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsfixattributedialog.h
Expand Up @@ -52,7 +52,7 @@ class APP_EXPORT QgsFixAttributeDialog : public QDialog
/**
* Constructor for QgsFixAttributeDialog
*/
QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList &features, QWidget *parent SIP_TRANSFERTHIS = nullptr );
QgsFixAttributeDialog( QgsVectorLayer *vl, QgsFeatureList &features, QWidget *parent SIP_TRANSFERTHIS = nullptr, const QgsAttributeEditorContext &context = QgsAttributeEditorContext() );

/**
* Returns fixed features
Expand All @@ -69,7 +69,7 @@ class APP_EXPORT QgsFixAttributeDialog : public QDialog
void reject() override;

private:
void init( QgsVectorLayer *layer );
void init( QgsVectorLayer *layer, const QgsAttributeEditorContext &context );
QString descriptionText();

QgsFeatureList mFeatures;
Expand Down

0 comments on commit 717d838

Please sign in to comment.