Skip to content
Permalink
Browse files

Merge pull request #5214 from boundlessgeo/gpkg_import_master_task

Geopackage import multiple files master task
  • Loading branch information
elpaso committed Sep 19, 2017
2 parents f5c4fdf + 1f0e73a commit 45879616072ff32b458ba1e08a3da89e7021e34d
Showing with 44 additions and 10 deletions.
  1. +19 −10 src/providers/ogr/qgsgeopackagedataitems.cpp
  2. +25 −0 src/providers/ogr/qgsgeopackagedataitems.h
@@ -28,6 +28,7 @@
#include "qgsmessageoutput.h"
#include "qgsvectorlayerexporter.h"
#include "qgsgeopackagerasterwritertask.h"
#include "qgstaskmanager.h"

#include <QAction>
#include <QMessageBox>
@@ -202,6 +203,10 @@ bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAct
QStringList importResults;
bool hasError = false;

// Main task
std::unique_ptr< QgsConcurrentFileWriterImportTask > mainTask( new QgsConcurrentFileWriterImportTask( tr( "GeoPackage import" ) ) );
QgsTaskList importTasks;

const auto lst = QgsMimeDataUtils::decodeUriList( data );
for ( const QgsMimeDataUtils::Uri &dropUri : lst )
{
@@ -263,18 +268,19 @@ bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAct
options.insert( QStringLiteral( "update" ), true );
options.insert( QStringLiteral( "overwrite" ), true );
options.insert( QStringLiteral( "layerName" ), dropUri.name );

std::unique_ptr< QgsVectorLayerExporterTask > exportTask( new QgsVectorLayerExporterTask( vectorSrcLayer, uri, QStringLiteral( "ogr" ), vectorSrcLayer->crs(), options, owner ) );
QgsVectorLayerExporterTask *exportTask = new QgsVectorLayerExporterTask( vectorSrcLayer, uri, QStringLiteral( "ogr" ), vectorSrcLayer->crs(), options, owner ) ;
mainTask->addSubTask( exportTask, importTasks );
importTasks << exportTask;
// when export is successful:
connect( exportTask.get(), &QgsVectorLayerExporterTask::exportComplete, this, [ = ]()
connect( exportTask, &QgsVectorLayerExporterTask::exportComplete, this, [ = ]()
{
// this is gross - TODO - find a way to get access to messageBar from data items
QMessageBox::information( nullptr, tr( "Import to GeoPackage database" ), tr( "Import was successful." ) );
refreshConnections();
} );

// when an error occurs:
connect( exportTask.get(), &QgsVectorLayerExporterTask::errorOccurred, this, [ = ]( int error, const QString & errorMessage )
connect( exportTask, &QgsVectorLayerExporterTask::errorOccurred, this, [ = ]( int error, const QString & errorMessage )
{
if ( error != QgsVectorLayerExporter::ErrUserCanceled )
{
@@ -285,22 +291,22 @@ bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAct
}
} );

QgsApplication::taskManager()->addTask( exportTask.release() );
}
else // Import raster
{

std::unique_ptr< QgsGeoPackageRasterWriterTask > exportTask( new QgsGeoPackageRasterWriterTask( dropUri, mPath ) );
QgsGeoPackageRasterWriterTask *exportTask = new QgsGeoPackageRasterWriterTask( dropUri, mPath ) ;
mainTask->addSubTask( exportTask, importTasks );
importTasks << exportTask;
// when export is successful:
connect( exportTask.get(), &QgsGeoPackageRasterWriterTask::writeComplete, this, [ = ]()
connect( exportTask, &QgsGeoPackageRasterWriterTask::writeComplete, this, [ = ]()
{
// this is gross - TODO - find a way to get access to messageBar from data items
QMessageBox::information( nullptr, tr( "Import to GeoPackage database" ), tr( "Import was successful." ) );
refreshConnections();
} );

// when an error occurs:
connect( exportTask.get(), &QgsGeoPackageRasterWriterTask::errorOccurred, this, [ = ]( QgsGeoPackageRasterWriter::WriterError error, const QString & errorMessage )
connect( exportTask, &QgsGeoPackageRasterWriterTask::errorOccurred, this, [ = ]( QgsGeoPackageRasterWriter::WriterError error, const QString & errorMessage )
{
if ( error != QgsGeoPackageRasterWriter::WriterError::ErrUserCanceled )
{
@@ -315,7 +321,6 @@ bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAct
deleteGeoPackageRasterLayer( QStringLiteral( "GPKG:%1:%2" ).arg( mPath, dropUri.name ), deleteErr );
} );

QgsApplication::taskManager()->addTask( exportTask.release() );
}
} // do not overwrite
}
@@ -334,6 +339,10 @@ bool QgsGeoPackageCollectionItem::handleDrop( const QMimeData *data, Qt::DropAct
output->setMessage( tr( "Failed to import some layers!\n\n" ) + importResults.join( QStringLiteral( "\n" ) ), QgsMessageOutput::MessageText );
output->showMessage();
}
if ( ! importTasks.isEmpty() )
{
QgsApplication::taskManager()->addTask( mainTask.release() );
}
return true;
}

@@ -18,6 +18,7 @@
#include "qgsdataitem.h"
#include "qgsdataitemprovider.h"
#include "qgsdataprovider.h"
#include "qgstaskmanager.h"


/**
@@ -156,5 +157,29 @@ class QgsGeoPackageDataItemProvider : public QgsDataItemProvider
};


/**
* \brief The QgsConcurrentFileWriterImportTask class is the parent task for
* importing layers from a drag and drop operation in the browser.
* Individual layers need to be added as individual substask.
*/
class QgsConcurrentFileWriterImportTask : public QgsTask
{
Q_OBJECT

public:

QgsConcurrentFileWriterImportTask( const QString &desc = QString() ) : QgsTask( desc ) {}

void emitProgressChanged( double progress ) { setProgress( progress ); }


protected:

bool run() override
{
return true;
}

};

#endif // QGSGEOPACKAGEDATAITEMS_H

0 comments on commit 4587961

Please sign in to comment.
You can’t perform that action at this time.