QGIS API Documentation
3.6.0-Noosa (5873452)
|
Abstract base class for long running background tasks. More...
#include <qgstaskmanager.h>
Public Types | |
enum | Flag { CanCancel = 1 << 1, AllFlags = CanCancel } |
Task flags. More... | |
enum | SubTaskDependency { SubTaskIndependent = 0, ParentDependsOnSubTask } |
Controls how subtasks relate to their parent task. More... | |
enum | TaskStatus { Queued, OnHold, Running, Complete, Terminated } |
Status of tasks. More... | |
Signals | |
void | begun () |
Will be emitted by task to indicate its commencement. More... | |
void | progressChanged (double progress) |
Will be emitted by task when its progress changes. More... | |
void | statusChanged (int status) |
Will be emitted by task when its status changes. More... | |
void | taskCompleted () |
Will be emitted by task to indicate its successful completion. More... | |
void | taskTerminated () |
Will be emitted by task if it has terminated for any reason other then completion (e.g., when a task has been canceled or encountered an internal error). More... | |
Public Member Functions | |
QgsTask (const QString &description=QString(), QgsTask::Flags flags=AllFlags) | |
Constructor for QgsTask. More... | |
~QgsTask () override | |
void | addSubTask (QgsTask *subTask, const QgsTaskList &dependencies=QgsTaskList(), SubTaskDependency subTaskDependency=SubTaskIndependent) |
Adds a subtask to this task. More... | |
bool | canCancel () const |
Returns true if the task can be canceled. More... | |
virtual void | cancel () |
Notifies the task that it should terminate. More... | |
QList< QgsMapLayer *> | dependentLayers () const |
Returns the list of layers on which the task depends. More... | |
QString | description () const |
Returns the task's description. More... | |
qint64 | elapsedTime () const |
Returns the elapsed time since the task commenced, in milliseconds. More... | |
Flags | flags () const |
Returns the flags associated with the task. More... | |
void | hold () |
Places the task on hold. More... | |
bool | isActive () const |
Returns true if the task is active, ie it is not complete and has not been canceled. More... | |
double | progress () const |
Returns the task's progress (between 0.0 and 100.0) More... | |
void | setDependentLayers (const QList< QgsMapLayer *> &dependentLayers) |
Sets a list of layers on which the task depends. More... | |
TaskStatus | status () const |
Returns the current task status. More... | |
void | unhold () |
Releases the task from being held. More... | |
bool | waitForFinished (int timeout=30000) |
Blocks the current thread until the task finishes or a maximum of timeout milliseconds. More... | |
Protected Slots | |
void | setProgress (double progress) |
Sets the task's current progress. More... | |
Protected Member Functions | |
virtual void | finished (bool result) |
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether through successful completion or via early termination). More... | |
bool | isCanceled () const |
Will return true if task should terminate ASAP. More... | |
virtual bool | run ()=0 |
Performs the task's operation. More... | |
Friends | |
class | QgsTaskManager |
class | QgsTaskRunnableWrapper |
class | TestQgsTaskManager |
Abstract base class for long running background tasks.
Tasks can be controlled directly, or added to a QgsTaskManager for automatic management.
Derived classes should implement the process they want to execute in the background within the run() method. This method will be called when the task commences (ie via calling run() ).
Long running tasks should periodically check the isCanceled() flag to detect if the task has been canceled via some external event. If this flag is true then the task should clean up and terminate at the earliest possible convenience.
Definition at line 52 of file qgstaskmanager.h.
enum QgsTask::Flag |
Task flags.
Enumerator | |
---|---|
CanCancel | Task can be canceled. |
AllFlags | Task supports all flags. |
Definition at line 69 of file qgstaskmanager.h.
Controls how subtasks relate to their parent task.
Enumerator | |
---|---|
SubTaskIndependent | Subtask is independent of the parent, and can run before, after or at the same time as the parent. |
ParentDependsOnSubTask | Subtask must complete before parent can begin. |
Definition at line 154 of file qgstaskmanager.h.
enum QgsTask::TaskStatus |
Status of tasks.
Definition at line 59 of file qgstaskmanager.h.
QgsTask::QgsTask | ( | const QString & | description = QString() , |
QgsTask::Flags | flags = AllFlags |
||
) |
Constructor for QgsTask.
description | text description of task |
flags | task flags |
Definition at line 28 of file qgstaskmanager.cpp.
|
override |
Definition at line 34 of file qgstaskmanager.cpp.
void QgsTask::addSubTask | ( | QgsTask * | subTask, |
const QgsTaskList & | dependencies = QgsTaskList() , |
||
SubTaskDependency | subTaskDependency = SubTaskIndependent |
||
) |
Adds a subtask to this task.
Subtasks allow a single task to be created which consists of multiple smaller tasks. Subtasks are not visible or indepedently controllable by users. Ownership of the subtask is transferred. Subtasks can have an optional list of dependent tasks, which must be completed before the subtask can begin. By default subtasks are considered independent of the parent task, ie they can be run either before, after, or at the same time as the parent task. This behavior can be overridden through the subTaskDependency argument. Note that subtasks should NEVER be dependent on their parent task, and violating this constraint will prevent the task from completing successfully.
The parent task must be added to a QgsTaskManager for subtasks to be utilized. Subtasks should not be added manually to a QgsTaskManager, rather, only the parent task should be added to the manager.
Subtasks can be nested, ie a subtask can legally be a parent task itself with its own set of subtasks.
Definition at line 131 of file qgstaskmanager.cpp.
|
signal |
Will be emitted by task to indicate its commencement.
|
inline |
Returns true if the task can be canceled.
Definition at line 93 of file qgstaskmanager.h.
|
virtual |
Notifies the task that it should terminate.
Calling this is not guaranteed to immediately end the task, rather it sets the isCanceled() flag which task subclasses can check and terminate their operations at an appropriate time. Any subtasks owned by this task will also be canceled. Derived classes must ensure that the base class implementation is called from any overridden version.
Reimplemented in QgsVectorLayerExporterTask, QgsMapRendererTask, QgsVirtualLayerTask, QgsNetworkContentFetcherTask, QgsRasterFileWriterTask, QgsProcessingAlgRunnerTask, and QgsVectorFileWriterTask.
Definition at line 79 of file qgstaskmanager.cpp.
QList< QgsMapLayer * > QgsTask::dependentLayers | ( | ) | const |
Returns the list of layers on which the task depends.
The task will automatically be canceled if any of these layers are about to be removed.
Definition at line 139 of file qgstaskmanager.cpp.
|
inline |
Returns the task's description.
Definition at line 109 of file qgstaskmanager.h.
qint64 QgsTask::elapsedTime | ( | ) | const |
Returns the elapsed time since the task commenced, in milliseconds.
The value is undefined for tasks which have not begun.
Definition at line 45 of file qgstaskmanager.cpp.
|
inlineprotectedvirtual |
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether through successful completion or via early termination).
The result argument reflects whether the task was successfully completed or not. This method is always called from the main thread, so it is safe to create widgets and perform other operations which require the main thread. However, the GUI will be blocked for the duration of this method so tasks should avoid performing any lengthy operations here.
Reimplemented in QgsVectorLayerExporterTask, QgsMapRendererTask, QgsVectorFileWriterTask, QgsRasterFileWriterTask, and QgsProcessingAlgRunnerTask.
Definition at line 271 of file qgstaskmanager.h.
|
inline |
Returns the flags associated with the task.
Definition at line 88 of file qgstaskmanager.h.
void QgsTask::hold | ( | ) |
Places the task on hold.
If the task in not queued (ie it is already running or has finished) then calling this has no effect. Calling this method only has an effect for tasks which are managed by a QgsTaskManager.
Definition at line 102 of file qgstaskmanager.cpp.
|
inline |
Returns true if the task is active, ie it is not complete and has not been canceled.
Definition at line 99 of file qgstaskmanager.h.
|
inlineprotected |
Will return true if task should terminate ASAP.
If the task reports the CanCancel flag, then derived classes' run() methods should periodically check this and terminate in a safe manner.
Definition at line 278 of file qgstaskmanager.h.
|
inline |
Returns the task's progress (between 0.0 and 100.0)
Definition at line 114 of file qgstaskmanager.h.
|
signal |
Will be emitted by task when its progress changes.
progress | percent of progress, from 0.0 - 100.0 |
|
protectedpure virtual |
Performs the task's operation.
This method will be called when the task commences (ie via calling start() ), and subclasses should implement the operation they wish to perform in the background within this method.
A task must return a boolean value to indicate whether the task was completed successfully or terminated before completion.
Implemented in QgsVectorLayerExporterTask, QgsMapRendererTask, QgsVectorFileWriterTask, QgsRasterFileWriterTask, QgsNetworkContentFetcherTask, QgsVirtualLayerTask, QgsProcessingAlgRunnerTask, QgsProxyProgressTask, and QgsVectorLayerFeatureCounter.
void QgsTask::setDependentLayers | ( | const QList< QgsMapLayer *> & | dependentLayers | ) |
Sets a list of layers on which the task depends.
The task will automatically be canceled if any of these layers are about to be removed.
Definition at line 168 of file qgstaskmanager.cpp.
|
protectedslot |
Sets the task's current progress.
The derived class should call this method whenever the task wants to update its progress. Calling will automatically emit the progressChanged signal.
progress | percent of progress, from 0.0 - 100.0 |
Definition at line 204 of file qgstaskmanager.cpp.
|
inline |
Returns the current task status.
Definition at line 104 of file qgstaskmanager.h.
|
signal |
Will be emitted by task when its status changes.
status | new task status |
|
signal |
Will be emitted by task to indicate its successful completion.
|
signal |
Will be emitted by task if it has terminated for any reason other then completion (e.g., when a task has been canceled or encountered an internal error).
void QgsTask::unhold | ( | ) |
Releases the task from being held.
For tasks managed by a QgsTaskManager calling this will re-add them to the queue. If the task in not currently being held then calling this has no effect.
Definition at line 116 of file qgstaskmanager.cpp.
bool QgsTask::waitForFinished | ( | int | timeout = 30000 | ) |
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
If timeout is 0
the thread will be blocked forever. In case of a timeout, the task will still be running. In case the task already is finished, the method will return immediately while returning true
.
The result will be false if the wait timed out and true in any other case.
Definition at line 144 of file qgstaskmanager.cpp.
|
friend |
Definition at line 353 of file qgstaskmanager.h.
|
friend |
Definition at line 354 of file qgstaskmanager.h.
|
friend |
Definition at line 355 of file qgstaskmanager.h.