Don't report a postgis import was successful when the user cancels

it mid-way through
This commit is contained in:
Nyall Dawson 2016-01-31 22:54:11 +11:00
parent 89815646fb
commit 7fb9b68cfe
4 changed files with 23 additions and 10 deletions

View File

@ -3,9 +3,6 @@
There are two possibilities how to use this class: There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer 1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...) 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 class QgsVectorLayerImport
{ {
@ -30,7 +27,8 @@ class QProgressDialog;
ErrInvalidLayer, ErrInvalidLayer,
ErrInvalidProvider, ErrInvalidProvider,
ErrProviderUnsupportedFeature, ErrProviderUnsupportedFeature,
ErrConnectionFailed ErrConnectionFailed,
ErrUserCancelled, /*!< User cancelled the import*/
}; };
/** Write contents of vector layer to a different datasource */ /** Write contents of vector layer to a different datasource */

View File

@ -333,11 +333,14 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
progress->setRange( 0, layer->featureCount() ); progress->setRange( 0, layer->featureCount() );
} }
bool cancelled = false;
// write all features // write all features
while ( fit.nextFeature( fet ) ) while ( fit.nextFeature( fet ) )
{ {
if ( progress && progress->wasCanceled() ) if ( progress && progress->wasCanceled() )
{ {
cancelled = true;
if ( errorMessage ) if ( errorMessage )
{ {
*errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() ); *errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() );
@ -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;
} }

View File

@ -29,9 +29,6 @@ class QProgressDialog;
There are two possibilities how to use this class: There are two possibilities how to use this class:
1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer 1. static call to QgsVectorFileWriter::writeAsShapefile(...) which saves the whole vector layer
2. create an instance of the class and issue calls to addFeature(...) 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 class CORE_EXPORT QgsVectorLayerImport
{ {
@ -50,7 +47,8 @@ class CORE_EXPORT QgsVectorLayerImport
ErrInvalidLayer, ErrInvalidLayer,
ErrInvalidProvider, ErrInvalidProvider,
ErrProviderUnsupportedFeature, ErrProviderUnsupportedFeature,
ErrConnectionFailed ErrConnectionFailed,
ErrUserCancelled, /*!< User cancelled the import*/
}; };
/** Write contents of vector layer to a different datasource */ /** Write contents of vector layer to a different datasource */

View File

@ -207,6 +207,8 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )
QStringList importResults; QStringList importResults;
bool hasError = false; bool hasError = false;
bool cancelled = false;
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data ); QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst ) Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst )
{ {
@ -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 ); err = QgsVectorLayerImport::importLayer( srcLayer, uri.uri( false ), "postgres", &srcLayer->crs(), false, &importError, false, nullptr, progress );
if ( err == QgsVectorLayerImport::NoError ) if ( err == QgsVectorLayerImport::NoError )
importResults.append( tr( "%1: OK!" ).arg( u.name ) ); importResults.append( tr( "%1: OK!" ).arg( u.name ) );
else if ( err == QgsVectorLayerImport::ErrUserCancelled )
cancelled = true;
else else
{ {
importResults.append( QString( "%1: %2" ).arg( u.name, importError ) ); importResults.append( QString( "%1: %2" ).arg( u.name, importError ) );
@ -254,7 +258,12 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )
qApp->restoreOverrideCursor(); 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(); QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
output->setTitle( tr( "Import to PostGIS database" ) ); output->setTitle( tr( "Import to PostGIS database" ) );