18 #ifndef QGSTASKMANAGER_H 19 #define QGSTASKMANAGER_H 25 #include <QReadWriteLock> 27 #include "qgis_core.h" 31 class QgsTaskRunnableWrapper;
52 class CORE_EXPORT
QgsTask :
public QObject
74 Q_DECLARE_FLAGS( Flags,
Flag )
81 QgsTask(
const QString &description = QString(), QgsTask::Flags flags = AllFlags );
88 Flags
flags()
const {
return mFlags; }
95 void setDescription(
const QString &description );
106 bool isActive()
const {
return mOverallStatus == Running; }
130 qint64 elapsedTime()
const;
141 virtual void cancel();
163 SubTaskIndependent = 0,
195 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
202 QList< QgsMapLayer * > dependentLayers()
const;
213 bool waitForFinished(
int timeout = 30000 );
223 void progressChanged(
double progress );
231 void statusChanged(
int status );
245 void taskCompleted();
254 void taskTerminated();
266 virtual bool run() = 0;
278 virtual void finished(
bool result ) { Q_UNUSED( result ) }
294 void setProgress(
double progress );
297 void subTaskStatusChanged(
int status );
302 QString mDescription;
312 QMutex mNotFinishedMutex;
315 double mProgress = 0.0;
317 double mTotalProgress = 0.0;
318 bool mShouldTerminate =
false;
325 , dependencies( dependencies )
326 , dependency( dependency )
332 QList< SubTask > mSubTasks;
336 QElapsedTimer mElapsedTime;
354 void processSubTasksForCompletion();
356 void processSubTasksForTermination();
358 void processSubTasksForHold();
361 friend class QgsTaskRunnableWrapper;
362 friend class TestQgsTaskManager;
367 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
402 , dependentTasks( dependentTasks )
442 QgsTask *task(
long id )
const;
447 QList<QgsTask *> tasks()
const;
457 long taskId(
QgsTask *task )
const;
467 bool dependenciesSatisfied(
long taskId )
const;
473 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
482 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
488 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
494 QList< QgsTask * > activeTasks()
const;
501 int countActiveTasks()
const;
509 void triggerTask(
QgsTask *task );
518 void progressChanged(
long taskId,
double progress );
525 void finalTaskProgressChanged(
double progress );
532 void statusChanged(
long taskId,
int status );
538 void taskAdded(
long taskId );
544 void taskAboutToBeDeleted(
long taskId );
550 void allTasksFinished();
556 void countActiveTasksChanged(
int count );
564 void taskTriggered(
QgsTask *task );
568 void taskProgressChanged(
double progress );
569 void taskStatusChanged(
int status );
570 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
576 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
577 void createRunnable();
581 QgsTaskRunnableWrapper *runnable =
nullptr;
584 mutable QMutex *mTaskMutex;
586 QMap< long, TaskInfo > mTasks;
587 QMap< long, QgsTaskList > mTaskDependencies;
588 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
591 long mNextTaskId = 1;
594 QSet< QgsTask * > mActiveTasks;
596 QSet< QgsTask * > mParentTasks;
598 QSet< QgsTask * > mSubTasks;
600 QSet< QgsTask * > mPendingDeletion;
602 long addTaskPrivate(
QgsTask *task,
607 bool cleanupAndDeleteTask(
QgsTask *task );
620 void cancelDependentTasks(
long taskId );
622 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
625 bool hasCircularDependencies(
long taskId )
const;
627 friend class TestQgsTaskManager;
630 #endif //QGSTASKMANAGER_H Base class for all map layer types.
bool isActive() const
Returns true if the task is active, ie it is not complete and has not been canceled.
QList< QgsTask *> QgsTaskList
List of QgsTask objects.
Subtask must complete before parent can begin.
TaskStatus
Status of tasks.
bool isCanceled() const
Will return true if task should terminate ASAP.
SubTaskDependency
Controls how subtasks relate to their parent task.
QgsTaskList dependentTasks
List of dependent tasks which must be completed before task can run.
Task was terminated or errored.
QString description() const
Returns the task's description.
Definition of a task for inclusion in the manager.
Task is queued but on hold and will not be started.
Abstract base class for long running background tasks.
bool canCancel() const
Returns true if the task can be canceled.
Task successfully completed.
Task manager for managing a set of long-running QgsTask tasks.
Task is queued and has not begun.
TaskDefinition(QgsTask *task, const QgsTaskList &dependentTasks=QgsTaskList())
Constructor for TaskDefinition.
double progress() const
Returns the task's progress (between 0.0 and 100.0)
Task is currently running.
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
virtual void finished(bool result)
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
TaskStatus status() const
Returns the current task status.
Flags flags() const
Returns the flags associated with the task.