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
73 CancelWithoutPrompt = 1 << 2,
76 Q_DECLARE_FLAGS( Flags, Flag )
83 QgsTask(
const QString &description = QString(), QgsTask::Flags flags = AllFlags );
90 Flags
flags()
const {
return mFlags; }
97 void setDescription(
const QString &description );
108 bool isActive()
const {
return mOverallStatus == Running; }
132 qint64 elapsedTime()
const;
143 virtual void cancel();
165 SubTaskIndependent = 0,
190 SubTaskDependency subTaskDependency = SubTaskIndependent );
197 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
204 QList< QgsMapLayer * > dependentLayers()
const;
215 bool waitForFinished(
int timeout = 30000 );
225 void progressChanged(
double progress );
233 void statusChanged(
int status );
247 void taskCompleted();
256 void taskTerminated();
268 virtual bool run() = 0;
280 virtual void finished(
bool result ) { Q_UNUSED( result ) }
287 bool isCanceled()
const;
296 void setProgress(
double progress );
299 void subTaskStatusChanged(
int status );
304 QString mDescription;
306 TaskStatus mStatus = Queued;
308 TaskStatus mOverallStatus = Queued;
314 QMutex mNotFinishedMutex;
320 QSemaphore mNotStartedMutex;
323 double mProgress = 0.0;
325 double mTotalProgress = 0.0;
326 bool mShouldTerminate =
false;
327 mutable QMutex mShouldTerminateMutex;
332 SubTask(
QgsTask *task,
const QgsTaskList &dependencies, SubTaskDependency dependency )
334 , dependencies( dependencies )
335 , dependency( dependency )
339 SubTaskDependency dependency;
341 QList< SubTask > mSubTasks;
345 QElapsedTimer mElapsedTime;
364 void processSubTasksForHold();
367 friend class QgsTaskRunnableWrapper;
368 friend class TestQgsTaskManager;
372 void processSubTasksForCompletion();
374 void processSubTasksForTermination();
379 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
414 , dependentTasks( dependentTasks )
454 QgsTask *task(
long id )
const;
459 QList<QgsTask *> tasks()
const;
469 long taskId(
QgsTask *task )
const;
479 bool dependenciesSatisfied(
long taskId )
const;
485 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
494 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
500 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
506 QList< QgsTask * > activeTasks()
const;
513 int countActiveTasks()
const;
521 void triggerTask(
QgsTask *task );
530 void progressChanged(
long taskId,
double progress );
537 void finalTaskProgressChanged(
double progress );
544 void statusChanged(
long taskId,
int status );
550 void taskAdded(
long taskId );
556 void taskAboutToBeDeleted(
long taskId );
562 void allTasksFinished();
568 void countActiveTasksChanged(
int count );
576 void taskTriggered(
QgsTask *task );
580 void taskProgressChanged(
double progress );
581 void taskStatusChanged(
int status );
582 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
588 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
589 void createRunnable();
593 QgsTaskRunnableWrapper *runnable =
nullptr;
596 bool mInitialized =
false;
598 mutable QMutex *mTaskMutex;
600 QMap< long, TaskInfo > mTasks;
601 QMap< long, QgsTaskList > mTaskDependencies;
602 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
605 long mNextTaskId = 1;
608 QSet< QgsTask * > mActiveTasks;
610 QSet< QgsTask * > mParentTasks;
612 QSet< QgsTask * > mSubTasks;
614 QSet< QgsTask * > mPendingDeletion;
616 long addTaskPrivate(
QgsTask *task,
621 bool cleanupAndDeleteTask(
QgsTask *task );
634 void cancelDependentTasks(
long taskId );
636 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
639 bool hasCircularDependencies(
long taskId )
const;
641 friend class TestQgsTaskManager;
644 #endif //QGSTASKMANAGER_H