18#ifndef QGSTASKMANAGER_H
19#define QGSTASKMANAGER_H
25#include <QElapsedTimer>
29#include <QReadWriteLock>
33class QgsTaskRunnableWrapper;
101 void setDescription(
const QString &description );
136 qint64 elapsedTime()
const;
147 virtual void cancel();
194 SubTaskDependency subTaskDependency = SubTaskIndependent );
201 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
208 QList< QgsMapLayer * > dependentLayers()
const;
219 bool waitForFinished(
int timeout = 30000 );
284 virtual void finished(
bool result ) { Q_UNUSED( result ) }
291 bool isCanceled()
const;
300 void setProgress(
double progress );
303 void subTaskStatusChanged(
int status );
308 QString mDescription;
310 TaskStatus mStatus = Queued;
312 TaskStatus mOverallStatus = Queued;
318 QMutex mNotFinishedMutex;
324 QSemaphore mNotStartedMutex;
327 double mProgress = 0.0;
329 double mTotalProgress = 0.0;
330 bool mShouldTerminate =
false;
331 mutable QMutex mShouldTerminateMutex;
336 SubTask(
QgsTask *task,
const QgsTaskList &dependencies, SubTaskDependency dependency )
338 , dependencies( dependencies )
339 , dependency( dependency )
341 QgsTask *task =
nullptr;
343 SubTaskDependency dependency;
345 QList< SubTask > mSubTasks;
349 QElapsedTimer mElapsedTime;
368 void processSubTasksForHold();
377 void processSubTasksForCompletion();
379 void processSubTasksForTermination();
471 QList<QgsTask *>
tasks()
const;
595 void taskProgressChanged(
double progress );
596 void taskStatusChanged(
int status );
597 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
603 TaskInfo(
QgsTask *
task =
nullptr,
int priority = 0 );
604 void createRunnable();
608 QgsTaskRunnableWrapper *runnable =
nullptr;
611 QThreadPool *mThreadPool =
nullptr;
613 bool mInitialized =
false;
615 mutable QRecursiveMutex *mTaskMutex;
617 QMap< long, TaskInfo > mTasks;
618 QMap< QgsTask *, long> mMapTaskPtrToId;
619 QMap< long, QgsTaskList > mTaskDependencies;
620 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
623 long mNextTaskId = 1;
626 QSet< QgsTask * > mActiveTasks;
628 QSet< QgsTask * > mParentTasks;
630 QSet< QgsTask * > mSubTasks;
632 QSet< QgsTask * > mPendingDeletion;
634 long addTaskPrivate(
QgsTask *task,
639 bool cleanupAndDeleteTask(
QgsTask *task );
652 void cancelDependentTasks(
long taskId );
654 bool resolveDependencies(
long taskId, QSet< long > &results )
const;
657 bool hasCircularDependencies(
long taskId )
const;
691 void cancel()
override;
Base class for all map layer types.
QList< QgsTask * > tasks() const
Returns all tasks tracked by the manager.
void finalTaskProgressChanged(double progress)
Will be emitted when only a single task remains to complete and that task has reported a progress cha...
void statusChanged(long taskId, int status)
Will be emitted when a task reports a status change.
void taskAdded(long taskId)
Emitted when a new task has been added to the manager.
friend class TestQgsTaskManager
QList< QgsTask * > activeTasks() const
Returns a list of the active (queued or running) tasks.
QgsTaskManager(QObject *parent=nullptr)
Constructor for QgsTaskManager.
void taskAboutToBeDeleted(long taskId)
Emitted when a task is about to be deleted.
long taskId(QgsTask *task) const
Returns the unique task ID corresponding to a task managed by the class.
int count() const
Returns the number of tasks tracked by the manager.
QList< QgsTask * > tasksDependentOnLayer(QgsMapLayer *layer) const
Returns a list of tasks which depend on a layer.
void allTasksFinished()
Emitted when all tasks are complete.
bool dependenciesSatisfied(long taskId) const
Returns true if all dependencies for the specified task are satisfied.
QThreadPool * threadPool()
Returns the threadpool utilized by the task manager.
void cancelAll()
Instructs all tasks tracked by the manager to terminate.
QSet< long > dependencies(long taskId) const
Returns the set of task IDs on which a task is dependent.
QgsTask * task(long id) const
Returns the task with matching ID.
void progressChanged(long taskId, double progress)
Will be emitted when a task reports a progress change.
int countActiveTasks(bool includeHidden=true) const
Returns the number of active (queued or running) tasks.
void triggerTask(QgsTask *task)
Triggers a task, e.g.
void countActiveTasksChanged(int count)
Emitted when the number of active tasks changes.
QList< QgsMapLayer * > dependentLayers(long taskId) const
Returns a list of layers on which as task is dependent.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskTriggered(QgsTask *task)
Emitted when a task is triggered.
A task that is composed of sub-tasks to be executed in a serial way.
QList< QgsTask * > mSubTasksSerial
QgsTaskWithSerialSubTasks(const QString &desc=QString())
Constructor.
bool run() override
Performs the task's operation.
Abstract base class for long running background tasks.
TaskStatus status() const
Returns the current task status.
Flags flags() const
Returns the flags associated with the task.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
double progress() const
Returns the task's progress (between 0.0 and 100.0).
virtual void finished(bool result)
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
virtual bool run()=0
Performs the task's operation.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
friend class TestQgsTaskManager
friend class QgsTaskRunnableWrapper
void begun()
Will be emitted by task to indicate its commencement.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
bool isActive() const
Returns true if the task is active, ie it is not complete and has not been canceled.
@ Hidden
Hide task from GUI.
@ CanCancel
Task can be canceled.
@ AllFlags
Task supports all flags.
@ CancelWithoutPrompt
Task can be canceled without any users prompts, e.g. when closing a project or QGIS.
@ Silent
Don't show task updates (such as completion/failure messages) as operating-system level notifications...
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....
void statusChanged(int status)
Will be emitted by task when its status changes.
friend class QgsTaskWithSerialSubTasks
friend class QgsTaskManager
TaskStatus
Status of tasks.
@ Terminated
Task was terminated or errored.
@ Queued
Task is queued and has not begun.
@ OnHold
Task is queued but on hold and will not be started.
@ Running
Task is currently running.
@ Complete
Task successfully completed.
QString description() const
Returns the task's description.
SubTaskDependency
Controls how subtasks relate to their parent task.
@ 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.
bool canCancel() const
Returns true if the task can be canceled.
#define SIP_ENUM_BASETYPE(type)
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
QList< QgsTask * > QgsTaskList
List of QgsTask objects.
Definition of a task for inclusion in the manager.
TaskDefinition(QgsTask *task, const QgsTaskList &dependentTasks=QgsTaskList())
Constructor for TaskDefinition.
QgsTaskList dependentTasks
List of dependent tasks which must be completed before task can run.