QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsvectorlayer.h"
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  if ( layer )
40  {
41  mTransformContext = layer->transformContext();
42  }
43 
44  mError = QgsVectorFileWriter::prepareWriteAsVectorFormat( layer, mOptions, mWriterDetails );
45 }
46 
48 {
49  mOptions.feedback->cancel();
51 }
52 
54 {
55  if ( mError != QgsVectorFileWriter::NoError )
56  return false;
57 
59 
60 
62  mWriterDetails, mDestFileName, mTransformContext, mOptions, &mNewFilename, &mNewLayer, &mErrorMessage );
63  return mError == QgsVectorFileWriter::NoError;
64 }
65 
67 {
68  if ( result )
69  {
70  emit writeComplete( mNewFilename );
71  emit completed( mNewFilename, mNewLayer );
72  }
73  else
74  {
75  emit errorOccurred( mError, mErrorMessage );
76  }
77 }
QgsVectorFileWriter::SaveVectorOptions
Options to pass to writeAsVectorFormat()
Definition: qgsvectorfilewriter.h:458
QgsVectorFileWriterTask::QgsVectorFileWriterTask
QgsVectorFileWriterTask(QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options)
Constructor for QgsVectorFileWriterTask.
Definition: qgsvectorfilewritertask.cpp:21
QgsVectorFileWriterTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition: qgsvectorfilewritertask.cpp:47
QgsVectorFileWriter::FieldValueConverter::clone
virtual QgsVectorFileWriter::FieldValueConverter * clone() const
Creates a clone of the FieldValueConverter.
Definition: qgsvectorfilewriter.cpp:73
QgsFeedback::cancel
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:121
QgsVectorFileWriterTask::run
bool run() override
Performs the task's operation.
Definition: qgsvectorfilewritertask.cpp:53
QgsVectorFileWriterTask::errorOccurred
void errorOccurred(int error, const QString &errorMessage)
Emitted when an error occurs which prevented the file being written (or if the task is canceled).
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsVectorFileWriter::SaveVectorOptions::feedback
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
Definition: qgsvectorfilewriter.h:532
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsVectorFileWriter::SaveVectorOptions::fieldValueConverter
QgsVectorFileWriter::FieldValueConverter * fieldValueConverter
Field value converter.
Definition: qgsvectorfilewriter.h:529
QgsVectorFileWriterTask::completed
void completed(const QString &newFilename, const QString &newLayer)
Emitted when writing the layer is successfully completed.
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition: qgstaskmanager.cpp:232
QgsFeedback::progressChanged
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
QgsVectorFileWriterTask::writeComplete
void writeComplete(const QString &newFilename)
Emitted when writing the layer is successfully completed.
qgsvectorlayer.h
qgsvectorfilewritertask.h
QgsMapLayer::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
Definition: qgsmaplayer.cpp:951
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorFileWriter::NoError
@ NoError
Definition: qgsvectorfilewriter.h:171
QgsVectorFileWriter::writeAsVectorFormatV2
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.
Definition: qgsvectorfilewriter.cpp:3438
QgsVectorFileWriterTask::finished
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Definition: qgsvectorfilewritertask.cpp:66
QgsTask
Abstract base class for long running background tasks. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:54