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; }
93 bool canCancel()
const {
return mFlags & CanCancel; }
99 bool isActive()
const {
return mOverallStatus == Running; }
123 qint64 elapsedTime()
const;
134 virtual void cancel();
156 SubTaskIndependent = 0,
188 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
195 QList< QgsMapLayer * > dependentLayers()
const;
206 bool waitForFinished(
int timeout = 30000 );
216 void progressChanged(
double progress );
224 void statusChanged(
int status );
238 void taskCompleted();
247 void taskTerminated();
259 virtual bool run() = 0;
271 virtual void finished(
bool result ) { Q_UNUSED( result ) }
287 void setProgress(
double progress );
290 void subTaskStatusChanged(
int status );
295 QString mDescription;
305 QMutex mNotFinishedMutex;
308 double mProgress = 0.0;
310 double mTotalProgress = 0.0;
311 bool mShouldTerminate =
false;
318 , dependencies( dependencies )
319 , dependency( dependency )
325 QList< SubTask > mSubTasks;
329 QElapsedTimer mElapsedTime;
347 void processSubTasksForCompletion();
349 void processSubTasksForTermination();
351 void processSubTasksForHold();
354 friend class QgsTaskRunnableWrapper;
355 friend class TestQgsTaskManager;
360 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
395 , dependentTasks( dependentTasks )
435 QgsTask *task(
long id )
const;
440 QList<QgsTask *> tasks()
const;
450 long taskId(
QgsTask *task )
const;
460 bool dependenciesSatisfied(
long taskId )
const;
466 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
475 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
481 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
487 QList< QgsTask * > activeTasks()
const;
494 int countActiveTasks()
const;
502 void triggerTask(
QgsTask *task );
511 void progressChanged(
long taskId,
double progress );
518 void finalTaskProgressChanged(
double progress );
525 void statusChanged(
long taskId,
int status );
531 void taskAdded(
long taskId );
537 void taskAboutToBeDeleted(
long taskId );
543 void allTasksFinished();
549 void countActiveTasksChanged(
int count );
557 void taskTriggered(
QgsTask *task );
561 void taskProgressChanged(
double progress );
562 void taskStatusChanged(
int status );
563 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
569 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
570 void createRunnable();
574 QgsTaskRunnableWrapper *runnable =
nullptr;
577 mutable QMutex *mTaskMutex;
579 QMap< long, TaskInfo > mTasks;
580 QMap< long, QgsTaskList > mTaskDependencies;
581 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
584 long mNextTaskId = 1;
587 QSet< QgsTask * > mActiveTasks;
589 QSet< QgsTask * > mParentTasks;
591 QSet< QgsTask * > mSubTasks;
593 QSet< QgsTask * > mPendingDeletion;
595 long addTaskPrivate(
QgsTask *task,
600 bool cleanupAndDeleteTask(
QgsTask *task );
613 void cancelDependentTasks(
long taskId );
615 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
618 bool hasCircularDependencies(
long taskId )
const;
620 friend class TestQgsTaskManager;
623 #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.