Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't report a postgis import was successful when the user cancels
it mid-way through
  • Loading branch information
nyalldawson committed Jan 31, 2016
1 parent 8981564 commit 7fb9b68
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
6 changes: 2 additions & 4 deletions python/core/qgsvectorlayerimport.sip
Expand Up @@ -3,9 +3,6 @@
There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)

Currently supports only writing to shapefiles, but shouldn't be a problem to add capability
to support other OGR-writable formats.
*/
class QgsVectorLayerImport
{
Expand All @@ -30,7 +27,8 @@ class QProgressDialog;
ErrInvalidLayer,
ErrInvalidProvider,
ErrProviderUnsupportedFeature,
ErrConnectionFailed
ErrConnectionFailed,
ErrUserCancelled, /*!< User cancelled the import*/
};

/** Write contents of vector layer to a different datasource */
Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsvectorlayerimport.cpp
Expand Up @@ -333,11 +333,14 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
progress->setRange( 0, layer->featureCount() );
}

bool cancelled = false;

// write all features
while ( fit.nextFeature( fet ) )
{
if ( progress && progress->wasCanceled() )
{
cancelled = true;
if ( errorMessage )
{
*errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() );
Expand Down Expand Up @@ -436,5 +439,10 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
}
}

return errors == 0 ? NoError : ErrFeatureWriteFailed;
if ( cancelled )
return ErrUserCancelled;
else if ( errors > 0 )
return ErrFeatureWriteFailed;

return NoError;
}
6 changes: 2 additions & 4 deletions src/core/qgsvectorlayerimport.h
Expand Up @@ -29,9 +29,6 @@ class QProgressDialog;
There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...)
Currently supports only writing to shapefiles, but shouldn't be a problem to add capability
to support other OGR-writable formats.
*/
class CORE_EXPORT QgsVectorLayerImport
{
Expand All @@ -50,7 +47,8 @@ class CORE_EXPORT QgsVectorLayerImport
ErrInvalidLayer,
ErrInvalidProvider,
ErrProviderUnsupportedFeature,
ErrConnectionFailed
ErrConnectionFailed,
ErrUserCancelled, /*!< User cancelled the import*/
};

/** Write contents of vector layer to a different datasource */
Expand Down
11 changes: 10 additions & 1 deletion src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -207,6 +207,8 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )

QStringList importResults;
bool hasError = false;
bool cancelled = false;

QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst )
{
Expand Down Expand Up @@ -235,6 +237,8 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )
err = QgsVectorLayerImport::importLayer( srcLayer, uri.uri( false ), "postgres", &srcLayer->crs(), false, &importError, false, nullptr, progress );
if ( err == QgsVectorLayerImport::NoError )
importResults.append( tr( "%1: OK!" ).arg( u.name ) );
else if ( err == QgsVectorLayerImport::ErrUserCancelled )
cancelled = true;
else
{
importResults.append( QString( "%1: %2" ).arg( u.name, importError ) );
Expand All @@ -254,7 +258,12 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )

qApp->restoreOverrideCursor();

if ( hasError )
if ( cancelled )
{
QMessageBox::information( nullptr, tr( "Import to PostGIS database" ), tr( "Import cancelled." ) );
refresh();
}
else if ( hasError )
{
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
output->setTitle( tr( "Import to PostGIS database" ) );
Expand Down

0 comments on commit 7fb9b68

Please sign in to comment.