18 #ifndef QGSTASKMANAGER_H
19 #define QGSTASKMANAGER_H
25 #include <QReadWriteLock>
27 #include <QElapsedTimer>
29 #include "qgis_core.h"
33 class QgsTaskRunnableWrapper;
54 class CORE_EXPORT
QgsTask :
public QObject
75 CancelWithoutPrompt = 1 << 2,
78 Q_DECLARE_FLAGS( Flags, Flag )
85 QgsTask(
const QString &description = QString(), QgsTask::Flags flags = AllFlags );
92 Flags
flags()
const {
return mFlags; }
99 void setDescription(
const QString &description );
110 bool isActive()
const {
return mOverallStatus == Running; }
134 qint64 elapsedTime()
const;
145 virtual void cancel();
167 SubTaskIndependent = 0,
192 SubTaskDependency subTaskDependency = SubTaskIndependent );
199 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
206 QList< QgsMapLayer * > dependentLayers()
const;
217 bool waitForFinished(
int timeout = 30000 );
282 virtual void finished(
bool result ) { Q_UNUSED( result ) }
289 bool isCanceled()
const;
298 void setProgress(
double progress );
301 void subTaskStatusChanged(
int status );
306 QString mDescription;
308 TaskStatus mStatus = Queued;
310 TaskStatus mOverallStatus = Queued;
316 QMutex mNotFinishedMutex;
322 QSemaphore mNotStartedMutex;
325 double mProgress = 0.0;
327 double mTotalProgress = 0.0;
328 bool mShouldTerminate =
false;
329 mutable QMutex mShouldTerminateMutex;
334 SubTask(
QgsTask *task,
const QgsTaskList &dependencies, SubTaskDependency dependency )
336 , dependencies( dependencies )
337 , dependency( dependency )
341 SubTaskDependency dependency;
343 QList< SubTask > mSubTasks;
347 QElapsedTimer mElapsedTime;
366 void processSubTasksForHold();
369 friend class QgsTaskRunnableWrapper;
370 friend class TestQgsTaskManager;
374 void processSubTasksForCompletion();
376 void processSubTasksForTermination();
416 , dependentTasks( dependentTasks )
456 QgsTask *task(
long id )
const;
461 QList<QgsTask *> tasks()
const;
471 long taskId(
QgsTask *task )
const;
481 bool dependenciesSatisfied(
long taskId )
const;
487 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
496 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
502 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
508 QList< QgsTask * > activeTasks()
const;
515 int countActiveTasks()
const;
523 void triggerTask(
QgsTask *task );
582 void taskProgressChanged(
double progress );
583 void taskStatusChanged(
int status );
584 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
590 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
591 void createRunnable();
595 QgsTaskRunnableWrapper *runnable =
nullptr;
598 bool mInitialized =
false;
600 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
601 mutable QMutex *mTaskMutex;
603 mutable QRecursiveMutex *mTaskMutex;
606 QMap< long, TaskInfo > mTasks;
607 QMap< long, QgsTaskList > mTaskDependencies;
608 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
611 long mNextTaskId = 1;
614 QSet< QgsTask * > mActiveTasks;
616 QSet< QgsTask * > mParentTasks;
618 QSet< QgsTask * > mSubTasks;
620 QSet< QgsTask * > mPendingDeletion;
622 long addTaskPrivate(
QgsTask *task,
627 bool cleanupAndDeleteTask(
QgsTask *task );
640 void cancelDependentTasks(
long taskId );
642 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
645 bool hasCircularDependencies(
long taskId )
const;
647 friend class TestQgsTaskManager;
Base class for all map layer types.
Task manager for managing a set of long-running QgsTask tasks.
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.
void taskAboutToBeDeleted(long taskId)
Emitted when a task is about to be deleted.
void allTasksFinished()
Emitted when all tasks are complete.
void progressChanged(long taskId, double progress)
Will be emitted when a task reports a progress change.
void countActiveTasksChanged(int count)
Emitted when the number of active tasks changes.
void taskTriggered(QgsTask *task)
Emitted when a task is triggered.
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.
void begun()
Will be emitted by task to indicate its commencement.
bool isActive() const
Returns true if the task is active, ie it is not complete and has not been canceled.
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.
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.
@ ParentDependsOnSubTask
Subtask must complete before parent can begin.
bool canCancel() const
Returns true if the task can be canceled.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
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.