18 #ifndef QGSTASKMANAGER_H 19 #define QGSTASKMANAGER_H 26 #include <QReadWriteLock> 28 #include "qgis_core.h" 32 class QgsTaskRunnableWrapper;
53 class CORE_EXPORT
QgsTask :
public QObject
75 Q_DECLARE_FLAGS( Flags,
Flag )
82 QgsTask( const QString &description = QString(),
QgsTask::Flags flags = AllFlags );
89 Flags flags()
const {
return mFlags; }
94 bool canCancel()
const {
return mFlags & CanCancel; }
100 bool isActive()
const {
return mOverallStatus == Running; }
124 qint64 elapsedTime()
const;
135 virtual void cancel();
157 SubTaskIndependent = 0,
189 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
196 QList< QgsMapLayer * > dependentLayers()
const;
207 bool waitForFinished(
int timeout = 30000 );
217 void progressChanged(
double progress );
225 void statusChanged(
int status );
239 void taskCompleted();
248 void taskTerminated();
260 virtual bool run() = 0;
272 virtual void finished(
bool result ) { Q_UNUSED( result ); }
288 void setProgress(
double progress );
291 void subTaskStatusChanged(
int status );
296 QString mDescription;
306 QMutex mNotFinishedMutex;
309 double mProgress = 0.0;
311 double mTotalProgress = 0.0;
312 bool mShouldTerminate =
false;
319 , dependencies( dependencies )
320 , dependency( dependency )
326 QList< SubTask > mSubTasks;
330 QElapsedTimer mElapsedTime;
348 void processSubTasksForCompletion();
350 void processSubTasksForTermination();
352 void processSubTasksForHold();
355 friend class QgsTaskRunnableWrapper;
356 friend class TestQgsTaskManager;
361 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
396 , dependentTasks( dependentTasks )
436 QgsTask *task(
long id )
const;
441 QList<QgsTask *> tasks()
const;
451 long taskId(
QgsTask *task )
const;
461 bool dependenciesSatisfied(
long taskId )
const;
467 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
476 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
482 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
488 QList< QgsTask * > activeTasks()
const;
495 int countActiveTasks()
const;
503 void triggerTask(
QgsTask *task );
512 void progressChanged(
long taskId,
double progress );
519 void finalTaskProgressChanged(
double progress );
526 void statusChanged(
long taskId,
int status );
532 void taskAdded(
long taskId );
538 void taskAboutToBeDeleted(
long taskId );
544 void allTasksFinished();
550 void countActiveTasksChanged(
int count );
558 void taskTriggered(
QgsTask *task );
562 void taskProgressChanged(
double progress );
563 void taskStatusChanged(
int status );
564 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
570 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
571 void createRunnable();
575 QgsTaskRunnableWrapper *runnable =
nullptr;
578 mutable QMutex *mTaskMutex;
580 QMap< long, TaskInfo > mTasks;
581 QMap< long, QgsTaskList > mTaskDependencies;
582 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
585 long mNextTaskId = 1;
588 QSet< QgsTask * > mActiveTasks;
590 QSet< QgsTask * > mParentTasks;
592 QSet< QgsTask * > mSubTasks;
594 QSet< QgsTask * > mPendingDeletion;
596 long addTaskPrivate(
QgsTask *task,
601 bool cleanupAndDeleteTask(
QgsTask *task );
614 void cancelDependentTasks(
long taskId );
616 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
619 bool hasCircularDependencies(
long taskId )
const;
621 friend class TestQgsTaskManager;
624 #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.
double progress() const
Returns the task's progress (between 0.0 and 100.0)
QList< QgsTask * > QgsTaskList
List of QgsTask objects.
Subtask must complete before parent can begin.
TaskStatus
Status of tasks.
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.
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 isCanceled() const
Will return true if task should terminate ASAP.
Task successfully completed.
Task manager for managing a set of long-running QgsTask tasks.
bool canCancel() const
Returns true if the task can be canceled.
QString description() const
Returns the task's description.
Task is queued and has not begun.
TaskDefinition(QgsTask *task, const QgsTaskList &dependentTasks=QgsTaskList())
Constructor for TaskDefinition.
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.