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; }
126 virtual void cancel();
148 SubTaskIndependent = 0,
180 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
187 QList< QgsMapLayer * > dependentLayers()
const;
198 bool waitForFinished(
unsigned long timeout = 30000 );
208 void progressChanged(
double progress );
216 void statusChanged(
int status );
230 void taskCompleted();
239 void taskTerminated();
251 virtual bool run() = 0;
263 virtual void finished(
bool result ) { Q_UNUSED( result ); }
279 void setProgress(
double progress );
282 void subTaskStatusChanged(
int status );
287 QString mDescription;
297 QMutex mNotFinishedMutex;
300 double mProgress = 0.0;
302 double mTotalProgress = 0.0;
303 bool mShouldTerminate =
false;
306 QWaitCondition mTaskFinished;
312 , dependencies( dependencies )
313 , dependency( dependency )
319 QList< SubTask > mSubTasks;
339 void processSubTasksForCompletion();
341 void processSubTasksForTermination();
343 void processSubTasksForHold();
346 friend class QgsTaskRunnableWrapper;
347 friend class TestQgsTaskManager;
352 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
387 , dependentTasks( dependentTasks )
427 QgsTask *task(
long id )
const;
432 QList<QgsTask *> tasks()
const;
442 long taskId(
QgsTask *task )
const;
452 bool dependenciesSatisfied(
long taskId )
const;
458 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
467 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
473 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
479 QList< QgsTask * > activeTasks()
const;
486 int countActiveTasks()
const;
494 void triggerTask(
QgsTask *task );
503 void progressChanged(
long taskId,
double progress );
510 void finalTaskProgressChanged(
double progress );
517 void statusChanged(
long taskId,
int status );
523 void taskAdded(
long taskId );
529 void taskAboutToBeDeleted(
long taskId );
535 void allTasksFinished();
541 void countActiveTasksChanged(
int count );
549 void taskTriggered(
QgsTask *task );
553 void taskProgressChanged(
double progress );
554 void taskStatusChanged(
int status );
555 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
561 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
562 void createRunnable();
566 QgsTaskRunnableWrapper *runnable =
nullptr;
569 mutable QMutex *mTaskMutex;
571 QMap< long, TaskInfo > mTasks;
572 QMap< long, QgsTaskList > mTaskDependencies;
573 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
576 long mNextTaskId = 1;
579 QSet< QgsTask * > mActiveTasks;
581 QSet< QgsTask * > mParentTasks;
583 QSet< QgsTask * > mSubTasks;
585 QSet< QgsTask * > mPendingDeletion;
587 long addTaskPrivate(
QgsTask *task,
592 bool cleanupAndDeleteTask(
QgsTask *task );
605 void cancelDependentTasks(
long taskId );
607 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
610 bool hasCircularDependencies(
long taskId )
const;
612 friend class TestQgsTaskManager;
615 #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.