Skip to content

Commit 32a9e1e

Browse files
committed
QgsTasks can have subtasks
Now, a QgsTask can have subtask QgsTasks set by calling QgsTask::addSubTask. Sub tasks can have their own set of dependent tasks. Subtasks are not visible to users, and users only see the overall progress and status of the parent task. This allows creation of tasks which are themselves built off many smaller component tasks. The task manager will still handle firing up and scheduling the subtasks, so eg subtasks can run in parallel (if their dependancies allow this). Subtasks can themselves have subtasks. This change is designed to allow the processing concept of algorithms and modeller algorithms to be translatable directly to the task manager architecture.
1 parent 3999a37 commit 32a9e1e

File tree

5 files changed

+814
-113
lines changed

5 files changed

+814
-113
lines changed

python/core/qgstaskmanager.sip

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ class QgsTask : QObject
9696
void start();
9797

9898
/**
99-
* Notifies the task that it should terminate. Calling this is not gauranteed
99+
* Notifies the task that it should terminate. Calling this is not guaranteed
100100
* to immediately end the task, rather it sets the isCancelled() flag which
101101
* task subclasses can check and terminate their operations at an appropriate
102-
* time.
102+
* time. Any subtasks owned by this task will also be cancelled.
103103
* @see isCancelled()
104104
*/
105105
void cancel();
@@ -121,6 +121,35 @@ class QgsTask : QObject
121121
*/
122122
void unhold();
123123

124+
//! Controls how subtasks relate to their parent task
125+
enum SubTaskDependency
126+
{
127+
SubTaskIndependent, //!< Subtask is independent of the parent, and can run before, after or at the same time as the parent.
128+
ParentDependsOnSubTask, //!< Subtask must complete before parent can begin
129+
};
130+
131+
/**
132+
* Adds a subtask to this task.
133+
*
134+
* Subtasks allow a single task to be created which
135+
* consists of multiple smaller tasks. Subtasks are not visible or indepedently
136+
* controllable by users. Ownership of the subtask is transferred.
137+
* Subtasks can have an optional list of dependant tasks, which must be completed
138+
* before the subtask can begin. By default subtasks are considered independent
139+
* of the parent task, ie they can be run either before, after, or at the same
140+
* time as the parent task. This behaviour can be overriden through the subTaskDependency
141+
* argument.
142+
*
143+
* The parent task must be added to a QgsTaskManager for subtasks to be utilised.
144+
* Subtasks should not be added manually to a QgsTaskManager, rather, only the parent
145+
* task should be added to the manager.
146+
*
147+
* Subtasks can be nested, ie a subtask can legally be a parent task itself with
148+
* its own set of subtasks.
149+
*/
150+
void addSubTask( QgsTask* subTask /Transfer/, const QgsTaskList& dependencies = QgsTaskList(),
151+
SubTaskDependency subTaskDependency = SubTaskIndependent );
152+
124153
signals:
125154

126155
/**
@@ -258,17 +287,42 @@ class QgsTaskManager : QObject
258287

259288
virtual ~QgsTaskManager();
260289

290+
/**
291+
* Definition of a task for inclusion within a task bundle.
292+
*/
293+
struct TaskDefinition
294+
{
295+
/**
296+
* Constructor for TaskDefinition. Ownership of the task is transferred to the definition.
297+
*/
298+
TaskDefinition( QgsTask* task, QgsTaskList dependencies = QgsTaskList() );
299+
300+
//! Task
301+
QgsTask* task;
302+
303+
/**
304+
* List of dependencies which must be completed before task can run.
305+
* These tasks must be completed before task can run. If any dependent tasks are
306+
* cancelled this task will also be cancelled. Dependent tasks must also be added
307+
* to the task manager for proper handling of dependencies.
308+
*/
309+
QgsTaskList dependencies;
310+
};
311+
261312
/** Adds a task to the manager. Ownership of the task is transferred
262313
* to the manager, and the task manager will be responsible for starting
263314
* the task.
264-
* @param task task to add
265-
* @param dependencies list of dependent tasks. These tasks must be completed
266-
* before task can run. If any dependent tasks are cancelled this task will also
267-
* be cancelled. Dependent tasks must also be added to this task manager for proper
268-
* handling of dependencies.
269315
* @returns unique task ID
270316
*/
271-
long addTask( QgsTask* task /Transfer/, const QgsTaskList& dependencies = QgsTaskList() );
317+
long addTask( QgsTask* task /Transfer/ );
318+
319+
/**
320+
* Adds a task to the manager, using a full task definition (including dependancy
321+
* handling). Ownership of the task is transferred to the manager, and the task
322+
* manager will be responsible for starting the task.
323+
* @returns unique task ID
324+
*/
325+
long addTask( const TaskDefinition& task /Transfer/ );
272326

273327
/** Returns the task with matching ID.
274328
* @param id task ID

0 commit comments

Comments
 (0)