18#ifndef QGSTASKMANAGER_H
19#define QGSTASKMANAGER_H
25#include <QElapsedTimer>
29#include <QReadWriteLock>
33class QgsTaskRunnableWrapper;
100 void setDescription(
const QString &description );
135 qint64 elapsedTime()
const;
146 virtual void cancel();
199 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
206 QList< QgsMapLayer * > dependentLayers()
const;
217 bool waitForFinished(
int timeout = 30000 );
281 virtual void finished(
bool result ) { Q_UNUSED( result ) }
288 bool isCanceled()
const;
297 void setProgress(
double progress );
300 void subTaskStatusChanged(
int status );
304 QString mDescription;
306 TaskStatus mStatus = Queued;
308 TaskStatus mOverallStatus = Queued;
314 QMutex mNotFinishedMutex;
320 QSemaphore mNotStartedMutex;
323 double mProgress = 0.0;
325 double mTotalProgress = 0.0;
326 bool mShouldTerminate =
false;
327 mutable QMutex mShouldTerminateMutex;
332 SubTask(
QgsTask *task,
const QgsTaskList &dependencies, SubTaskDependency dependency )
334 , dependencies( dependencies )
335 , dependency( dependency )
337 QgsTask *task =
nullptr;
339 SubTaskDependency dependency;
341 QList< SubTask > mSubTasks;
345 QElapsedTimer mElapsedTime;
364 void processSubTasksForHold();
373 void processSubTasksForCompletion();
375 void processSubTasksForTermination();
463 QList<QgsTask *>
tasks()
const;
587 void taskProgressChanged(
double progress );
588 void taskStatusChanged(
int status );
589 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
594 TaskInfo(
QgsTask *
task =
nullptr,
int priority = 0 );
595 void createRunnable();
599 QgsTaskRunnableWrapper *runnable =
nullptr;
602 QThreadPool *mThreadPool =
nullptr;
604 bool mInitialized =
false;
606 mutable QRecursiveMutex *mTaskMutex;
608 QMap< long, TaskInfo > mTasks;
609 QMap< QgsTask *, long> mMapTaskPtrToId;
610 QMap< long, QgsTaskList > mTaskDependencies;
611 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
614 long mNextTaskId = 1;
617 QSet< QgsTask * > mActiveTasks;
619 QSet< QgsTask * > mParentTasks;
621 QSet< QgsTask * > mSubTasks;
623 QSet< QgsTask * > mPendingDeletion;
625 long addTaskPrivate(
QgsTask *task,
QgsTaskList dependencies,
bool isSubTask,
int priority );
627 bool cleanupAndDeleteTask(
QgsTask *task );
640 void cancelDependentTasks(
long taskId );
642 bool resolveDependencies(
long taskId, QSet< long > &results )
const;
645 bool hasCircularDependencies(
long taskId )
const;
681 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.