QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgsvectorfilewritertask.cpp"
20#include "qgsvectorlayer.h"
21
23 : QgsTask( tr( "Saving %1" ).arg( fileName ), QgsTask::CanCancel )
24 , mDestFileName( fileName )
25 , mSinkFlags( sinkFlags )
26 , mOptions( options )
27{
28 if ( mOptions.fieldValueConverter )
29 {
30 // fieldValueConverter is not owned - so we need to clone it here
31 // to ensure it exists for lifetime of task
32 mFieldValueConverter.reset( mOptions.fieldValueConverter->clone() );
33 mOptions.fieldValueConverter = mFieldValueConverter.get();
34 }
35 if ( !mOptions.feedback )
36 {
37 mOwnedFeedback.reset( new QgsFeedback() );
38 mOptions.feedback = mOwnedFeedback.get();
39 }
40
41 if ( layer )
42 {
43 mTransformContext = layer->transformContext();
44 }
45
46 mError = QgsVectorFileWriter::prepareWriteAsVectorFormat( layer, mOptions, mWriterDetails );
47}
48
54
56{
57 if ( mError != QgsVectorFileWriter::NoError )
58 return false;
59
61
62
64 mWriterDetails, mDestFileName, mTransformContext, mOptions, &mNewFilename, &mNewLayer, &mErrorMessage, mSinkFlags );
65 return mError == QgsVectorFileWriter::NoError;
66}
67
69{
70 if ( result )
71 {
72 emit writeComplete( mNewFilename );
73 emit completed( mNewFilename, mNewLayer );
74 }
75 else
76 {
77 emit errorOccurred( mError, mErrorMessage );
78 }
79}
QFlags< SinkFlag > SinkFlags
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
Abstract base class for long running background tasks.
virtual void cancel()
Notifies the task that it should terminate.
void setProgress(double progress)
Sets the task's current progress.
void errorOccurred(int error, const QString &errorMessage)
Emitted when an error occurs which prevented the file being written (or if the task is canceled).
bool run() override
Performs the task's operation.
void completed(const QString &newFilename, const QString &newLayer)
Emitted when writing the layer is successfully completed.
void writeComplete(const QString &newFilename)
Emitted when writing the layer is successfully completed.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
QgsVectorFileWriterTask(QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
Constructor for QgsVectorFileWriterTask.
void cancel() override
Notifies the task that it should terminate.
virtual QgsVectorFileWriter::FieldValueConverter * clone() const
Creates a clone of the FieldValueConverter.
Options to pass to writeAsVectorFormat()
QgsVectorFileWriter::FieldValueConverter * fieldValueConverter
Field value converter.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static Q_DECL_DEPRECATED QgsVectorFileWriter::WriterError writeAsVectorFormatV2(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename=nullptr, QString *newLayer=nullptr, QString *errorMessage=nullptr)
Writes a layer out to a vector file.
Represents a vector layer which manages a vector based data sets.