-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Background saving of vector layers
Switches the vector layer save operation to a background task using the task manager framework. Allows layers to saved without blocking the interface.
- Loading branch information
1 parent
92091c5
commit 95d000f
Showing
11 changed files
with
220 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class QgsVectorFileWriterTask : QgsTask | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsvectorfilewritertask.h> | ||
%End | ||
|
||
public: | ||
|
||
QgsVectorFileWriterTask( QgsVectorLayer* layer, | ||
const QString& fileName, | ||
const QgsVectorFileWriter::SaveVectorOptions& options ); | ||
|
||
virtual void cancel(); | ||
|
||
signals: | ||
|
||
void writeComplete( const QString& newFilename ); | ||
void errorOccurred( int error, const QString& errorMessage ); | ||
|
||
protected: | ||
|
||
virtual bool run(); | ||
virtual void finished( bool result ); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*************************************************************************** | ||
qgsvectorfilewritertask.cpp | ||
--------------------------- | ||
begin : Feb 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsvectorfilewritertask.h" | ||
|
||
|
||
QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer* layer, const QString& fileName, const QgsVectorFileWriter::SaveVectorOptions& options ) | ||
: QgsTask( tr( "Saving %1 " ).arg( fileName ), QgsTask::CanCancel ) | ||
, mLayer( layer ) | ||
, mDestFileName( fileName ) | ||
, mOptions( options ) | ||
{ | ||
if ( !mOptions.feedback ) | ||
{ | ||
mOwnedFeedback.reset( new QgsFeedback() ); | ||
mOptions.feedback = mOwnedFeedback.get(); | ||
} | ||
} | ||
|
||
void QgsVectorFileWriterTask::cancel() | ||
{ | ||
mOptions.feedback->cancel(); | ||
QgsTask::cancel(); | ||
} | ||
|
||
bool QgsVectorFileWriterTask::run() | ||
{ | ||
if ( !mLayer ) | ||
return false; | ||
|
||
|
||
mError = QgsVectorFileWriter::writeAsVectorFormat( | ||
mLayer, mDestFileName, mOptions, &mNewFilename, &mErrorMessage ); | ||
return mError == QgsVectorFileWriter::NoError; | ||
} | ||
|
||
void QgsVectorFileWriterTask::finished( bool result ) | ||
{ | ||
if ( result ) | ||
emit writeComplete( mNewFilename ); | ||
else | ||
emit errorOccurred( mError, mErrorMessage ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*************************************************************************** | ||
qgsvectorfilewritertask.h | ||
------------------------- | ||
begin : Feb 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSVECTORFILEWRITERTASK_H | ||
#define QGSVECTORFILEWRITERTASK_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgsvectorfilewriter.h" | ||
#include "qgstaskmanager.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
/** | ||
* \class QgsVectorFileWriterTask | ||
* \ingroup core | ||
* QgsTask task which performs a QgsVectorFileWriter layer saving operation as a background | ||
* task. This can be used to save a vector layer out to a file without blocking the | ||
* QGIS interface. | ||
* \note Added in QGIS 3.0 | ||
*/ | ||
class CORE_EXPORT QgsVectorFileWriterTask : public QgsTask | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
/** | ||
* Constructor for QgsVectorFileWriterTask. Takes a source \a layer, destination \a fileName | ||
* and save \a options. | ||
*/ | ||
QgsVectorFileWriterTask( QgsVectorLayer* layer, | ||
const QString& fileName, | ||
const QgsVectorFileWriter::SaveVectorOptions& options ); | ||
|
||
virtual void cancel() override; | ||
|
||
signals: | ||
|
||
/** | ||
* Emitted when writing the layer is successfully completed. The \a newFilename | ||
* parameter indicates the file path for the written file. | ||
*/ | ||
void writeComplete( const QString& newFilename ); | ||
|
||
/** | ||
* Emitted when an error occurs which prevented the file being written (or if | ||
* the task is canceled). The writing \a error and \a errorMessage will be reported. | ||
*/ | ||
void errorOccurred( int error, const QString& errorMessage ); | ||
|
||
protected: | ||
|
||
virtual bool run() override; | ||
virtual void finished( bool result ) override; | ||
|
||
private: | ||
|
||
QPointer< QgsVectorLayer > mLayer = nullptr; | ||
|
||
QString mDestFileName; | ||
|
||
std::unique_ptr< QgsFeedback > mOwnedFeedback; | ||
|
||
QgsVectorFileWriter::WriterError mError = QgsVectorFileWriter::NoError; | ||
|
||
QString mNewFilename; | ||
QString mErrorMessage; | ||
|
||
QgsVectorFileWriter::SaveVectorOptions mOptions; | ||
}; | ||
|
||
#endif |