QGIS API Documentation  3.0.2-Girona (307d082)
qgsvectorfilewritertask.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorfilewritertask.cpp
3  ---------------------------
4  begin : Feb 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
20 
22  : QgsTask( tr( "Saving %1" ).arg( fileName ), QgsTask::CanCancel )
23  , mDestFileName( fileName )
24  , mOptions( options )
25 {
26  if ( mOptions.fieldValueConverter )
27  {
28  // fieldValueConverter is not owned - so we need to clone it here
29  // to ensure it exists for lifetime of task
30  mFieldValueConverter.reset( mOptions.fieldValueConverter->clone() );
31  mOptions.fieldValueConverter = mFieldValueConverter.get();
32  }
33  if ( !mOptions.feedback )
34  {
35  mOwnedFeedback.reset( new QgsFeedback() );
36  mOptions.feedback = mOwnedFeedback.get();
37  }
38 
39  mError = QgsVectorFileWriter::prepareWriteAsVectorFormat( layer, mOptions, mWriterDetails );
40 }
41 
43 {
44  mOptions.feedback->cancel();
46 }
47 
49 {
50  if ( mError != QgsVectorFileWriter::NoError )
51  return false;
52 
54 
55 
57  mWriterDetails, mDestFileName, mOptions, &mNewFilename, &mErrorMessage );
58  return mError == QgsVectorFileWriter::NoError;
59 }
60 
62 {
63  if ( result )
64  emit writeComplete( mNewFilename );
65  else
66  emit errorOccurred( mError, mErrorMessage );
67 }
QgsVectorFileWriterTask(QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options)
Constructor for QgsVectorFileWriterTask.
void setProgress(double progress)
Sets the task's current progress.
void cancel() override
Notifies the task that it should terminate.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:85
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
Options to pass to writeAsVectorFormat()
virtual QgsVectorFileWriter::FieldValueConverter * clone() const
Creates a clone of the FieldValueConverter.
void errorOccurred(int error, const QString &errorMessage)
Emitted when an error occurs which prevented the file being written (or if the task is canceled)...
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void writeComplete(const QString &newFilename)
Emitted when writing the layer is successfully completed.
bool run() override
Performs the task's operation.
Abstract base class for long running background tasks.
virtual void cancel()
Notifies the task that it should terminate.
static QgsVectorFileWriter::WriterError writeAsVectorFormat(QgsVectorLayer *layer, const QString &fileName, const QString &fileEncoding, const QgsCoordinateReferenceSystem &destCRS=QgsCoordinateReferenceSystem(), const QString &driverName="GPKG", bool onlySelected=false, QString *errorMessage=nullptr, const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), bool skipAttributeCreation=false, QString *newFilename=nullptr, QgsVectorFileWriter::SymbologyExport symbologyExport=QgsVectorFileWriter::NoSymbology, double symbologyScale=1.0, const QgsRectangle *filterExtent=nullptr, QgsWkbTypes::Type overrideGeometryType=QgsWkbTypes::Unknown, bool forceMulti=false, bool includeZ=false, const QgsAttributeList &attributes=QgsAttributeList(), QgsVectorFileWriter::FieldValueConverter *fieldValueConverter=nullptr)
Write contents of vector layer to an (OGR supported) vector formt.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
QgsVectorFileWriter::FieldValueConverter * fieldValueConverter
Field value converter.
Task can be canceled.
QgsFeedback * feedback
Optional feedback object allowing cancelation of layer save.
Represents a vector layer which manages a vector based data sets.