18 #ifndef QGSTASKMANAGER_H
19 #define QGSTASKMANAGER_H
25 #include <QReadWriteLock>
27 #include <QElapsedTimer>
29 #include "qgis_core.h"
33 class QgsTaskRunnableWrapper;
54 class CORE_EXPORT
QgsTask :
public QObject
75 CancelWithoutPrompt = 1 << 2,
80 Q_DECLARE_FLAGS( Flags, Flag )
87 QgsTask(
const QString &description = QString(), QgsTask::Flags flags = AllFlags );
94 Flags
flags()
const {
return mFlags; }
101 void setDescription(
const QString &description );
112 bool isActive()
const {
return mOverallStatus == Running; }
136 qint64 elapsedTime()
const;
147 virtual void cancel();
169 SubTaskIndependent = 0,
194 SubTaskDependency subTaskDependency = SubTaskIndependent );
201 void setDependentLayers(
const QList<QgsMapLayer *> &dependentLayers );
208 QList< QgsMapLayer * > dependentLayers()
const;
219 bool waitForFinished(
int timeout = 30000 );
229 void progressChanged(
double progress );
237 void statusChanged(
int status );
251 void taskCompleted();
260 void taskTerminated();
272 virtual bool run() = 0;
284 virtual void finished(
bool result ) { Q_UNUSED( result ) }
291 bool isCanceled()
const;
300 void setProgress(
double progress );
303 void subTaskStatusChanged(
int status );
308 QString mDescription;
310 TaskStatus mStatus = Queued;
312 TaskStatus mOverallStatus = Queued;
318 QMutex mNotFinishedMutex;
324 QSemaphore mNotStartedMutex;
327 double mProgress = 0.0;
329 double mTotalProgress = 0.0;
330 bool mShouldTerminate =
false;
331 mutable QMutex mShouldTerminateMutex;
336 SubTask(
QgsTask *task,
const QgsTaskList &dependencies, SubTaskDependency dependency )
338 , dependencies( dependencies )
339 , dependency( dependency )
343 SubTaskDependency dependency;
345 QList< SubTask > mSubTasks;
349 QElapsedTimer mElapsedTime;
368 void processSubTasksForHold();
371 friend class QgsTaskRunnableWrapper;
372 friend class TestQgsTaskManager;
376 void processSubTasksForCompletion();
378 void processSubTasksForTermination();
418 , dependentTasks( dependentTasks )
458 QgsTask *task(
long id )
const;
463 QList<QgsTask *> tasks()
const;
473 long taskId(
QgsTask *task )
const;
483 bool dependenciesSatisfied(
long taskId )
const;
489 QSet< long > dependencies(
long taskId )
const SIP_SKIP;
498 QList< QgsMapLayer * > dependentLayers(
long taskId )
const;
504 QList< QgsTask * > tasksDependentOnLayer(
QgsMapLayer *layer )
const;
510 QList< QgsTask * > activeTasks()
const;
520 int countActiveTasks(
bool includeHidden =
true )
const;
528 void triggerTask(
QgsTask *task );
537 void progressChanged(
long taskId,
double progress );
544 void finalTaskProgressChanged(
double progress );
551 void statusChanged(
long taskId,
int status );
557 void taskAdded(
long taskId );
563 void taskAboutToBeDeleted(
long taskId );
569 void allTasksFinished();
575 void countActiveTasksChanged(
int count );
583 void taskTriggered(
QgsTask *task );
587 void taskProgressChanged(
double progress );
588 void taskStatusChanged(
int status );
589 void layersWillBeRemoved(
const QList<QgsMapLayer *> &layers );
595 TaskInfo(
QgsTask *task =
nullptr,
int priority = 0 );
596 void createRunnable();
600 QgsTaskRunnableWrapper *runnable =
nullptr;
603 bool mInitialized =
false;
605 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
606 mutable QMutex *mTaskMutex;
608 mutable QRecursiveMutex *mTaskMutex;
611 QMap< long, TaskInfo > mTasks;
612 QMap< long, QgsTaskList > mTaskDependencies;
613 QMap< long, QgsWeakMapLayerPointerList > mLayerDependencies;
616 long mNextTaskId = 1;
619 QSet< QgsTask * > mActiveTasks;
621 QSet< QgsTask * > mParentTasks;
623 QSet< QgsTask * > mSubTasks;
625 QSet< QgsTask * > mPendingDeletion;
627 long addTaskPrivate(
QgsTask *task,
632 bool cleanupAndDeleteTask(
QgsTask *task );
645 void cancelDependentTasks(
long taskId );
647 bool resolveDependencies(
long firstTaskId,
long currentTaskId, QSet< long > &results )
const;
650 bool hasCircularDependencies(
long taskId )
const;
652 friend class TestQgsTaskManager;
655 #endif //QGSTASKMANAGER_H