1
- /** \ingroup core
1
+ /**
2
+ * \ingroup core
2
3
* \class QgsTask
3
- * \brief Interface class for long running tasks which will be handled by a QgsTaskManager
4
- * \note Added in version 2.16
4
+ * \brief Abstract base class for long running background tasks. Tasks can be controlled directly,
5
+ * or added to a QgsTaskManager for automatic management.
6
+ *
7
+ * Derived classes should implement the process they want to execute in the background
8
+ * within the run() method. This method will be called when the
9
+ * task commences (ie via calling start() ).
10
+ *
11
+ * Long running tasks should periodically check the isCancelled() flag to detect if the task
12
+ * has been cancelled via some external event. If this flag is true then the task should
13
+ * clean up and terminate at the earliest possible convenience.
14
+ *
15
+ * \note Added in version 3.0
5
16
*/
6
17
class QgsTask : QObject
7
18
{
@@ -21,6 +32,14 @@ class QgsTask : QObject
21
32
Terminated, /*!< Task was terminated or errored */
22
33
};
23
34
35
+ //! Result of running the task
36
+ enum TaskResult
37
+ {
38
+ ResultSuccess, //!< Task completed successfully
39
+ ResultFail, //!< Task was terminated within completion
40
+ ResultPending, //!< Task is still running
41
+ };
42
+
24
43
//! Task flags
25
44
enum Flag
26
45
{
@@ -30,106 +49,176 @@ class QgsTask : QObject
30
49
};
31
50
typedef QFlags<QgsTask::Flag> Flags;
32
51
33
- /** Constructor for QgsTask.
52
+ /**
53
+ * Constructor for QgsTask.
34
54
* @param description text description of task
35
55
* @param flags task flags
36
56
*/
37
57
QgsTask( const QString& description = QString(), const Flags& flags = AllFlags );
38
58
39
- //! Returns the flags associated with the task.
59
+ /**
60
+ * Returns the flags associated with the task.
61
+ */
40
62
Flags flags() const;
41
63
42
- //! Returns true if the task can be cancelled.
64
+ /**
65
+ * Returns true if the task can be cancelled.
66
+ */
43
67
bool canCancel() const;
44
68
45
- //! Returns true if the task is active, ie it is not complete and has
46
- //! not been cancelled.
69
+ /**
70
+ * Returns true if the task is active, ie it is not complete and has
71
+ * not been cancelled.
72
+ */
47
73
bool isActive() const;
48
74
49
- //! Returns the current task status.
75
+ /**
76
+ * Returns the current task status.
77
+ */
50
78
TaskStatus status() const;
51
79
52
- //! Returns the task's description.
80
+ /**
81
+ * Returns the task's description.
82
+ */
53
83
QString description() const;
54
84
55
- //! Returns the task's progress (between 0.0 and 100.0)
85
+ /**
86
+ * Returns the task's progress (between 0.0 and 100.0)
87
+ */
56
88
double progress() const;
57
89
58
- public slots:
59
-
60
- //! Starts the task.
90
+ /**
91
+ * Starts the task. Should only be called for tasks which are not being
92
+ * handled by a QgsTaskManager. If the task is managed by a QgsTaskManager
93
+ * then this method should not be called directly, instead it is left to the
94
+ * task manager to start the task when appropriate.
95
+ */
61
96
void start();
62
97
63
- //! Notifies the task that it should terminate.
64
- //! @see isCancelled()
98
+ /**
99
+ * Notifies the task that it should terminate. Calling this is not gauranteed
100
+ * to immediately end the task, rather it sets the isCancelled() flag which
101
+ * task subclasses can check and terminate their operations at an appropriate
102
+ * time.
103
+ * @see isCancelled()
104
+ */
65
105
void cancel();
66
106
67
- //! Called when the task is placed on hold. If the task in not queued
68
- //! (ie it is running or has finished) then calling this has no effect.
69
- //! @see unhold()
107
+ /**
108
+ * Places the task on hold. If the task in not queued
109
+ * (ie it is already running or has finished) then calling this has no effect.
110
+ * Calling this method only has an effect for tasks which are managed
111
+ * by a QgsTaskManager.
112
+ * @see unhold()
113
+ */
70
114
void hold();
71
115
72
- //! Called when the task should be unheld and re-added to the queue. If the
73
- //! task in not currently being held then calling this has no effect.
74
- //! @see unhold()
116
+ /**
117
+ * Releases the task from being held. For tasks managed by a QgsTaskManager
118
+ * calling this will re-add them to the queue. If the
119
+ * task in not currently being held then calling this has no effect.
120
+ * @see hold()
121
+ */
75
122
void unhold();
76
123
77
- //! Sets the task's current progress. If task reports the CanReportProgress flag then
78
- //! the derived class should call this method whenever the task wants to update its
79
- //! progress. Calling will automatically emit the progressChanged signal.
80
- //! @param progress percent of progress, from 0.0 - 100.0
81
- void setProgress( double progress );
82
-
83
- //! Sets the task as completed. Should be called when the task is complete.
84
- //! Calling will automatically emit the statusChanged and taskCompleted signals.
85
- void completed();
86
-
87
- //! Sets the task as stopped. Should be called whenever the task ends for any
88
- //! reason other than successful completion.
89
- //! Calling will automatically emit the statusChanged and taskStopped signals.
90
- void stopped();
91
-
92
124
signals:
93
125
94
- //! Will be emitted by task when its progress changes
95
- //! @param progress percent of progress, from 0.0 - 100.0
96
- //! @note derived classes should not emit this signal directly, instead they should call
97
- //! setProgress()
126
+ /**
127
+ * Will be emitted by task when its progress changes.
128
+ * @param progress percent of progress, from 0.0 - 100.0
129
+ * @note derived classes should not emit this signal directly, instead they should call
130
+ * setProgress()
131
+ */
98
132
void progressChanged( double progress );
99
133
100
- //! Will be emitted by task when its status changes
101
- //! @param status new task status
102
- //! @note derived classes should not emit this signal directly, instead they should call
103
- //! completed() or stopped()
134
+ /**
135
+ * Will be emitted by task when its status changes.
136
+ * @param status new task status
137
+ * @note derived classes should not emit this signal directly, instead they should call
138
+ * completed() or stopped()
139
+ */
104
140
void statusChanged( int status );
105
141
106
- //! Will be emitted by task to indicate its commencement.
107
- //! @note derived classes should not emit this signal directly, it will automatically
108
- //! be emitted when the task begins
142
+ /**
143
+ * Will be emitted by task to indicate its commencement.
144
+ * @note derived classes should not emit this signal directly, it will automatically
145
+ * be emitted when the task begins
146
+ */
109
147
void begun();
110
148
111
- //! Will be emitted by task to indicate its completion.
112
- //! @note derived classes should not emit this signal directly, instead they should call
113
- //! completed()
149
+ /**
150
+ * Will be emitted by task to indicate its successful completion.
151
+ * @note derived classes should not emit this signal directly, instead they should call
152
+ * completed()
153
+ */
114
154
void taskCompleted();
115
155
116
- //! Will be emitted by task if it has terminated for any reason
117
- //! other then completion.
118
- //! @note derived classes should not emit this signal directly, instead they should call
119
- //! stopped()//!
156
+ /**
157
+ * Will be emitted by task if it has terminated for any reason
158
+ * other then completion (eg when a task has been cancelled or encountered
159
+ * an internal error).
160
+ * @note derived classes should not emit this signal directly, instead they should call
161
+ * stopped()
162
+ */
120
163
void taskStopped();
121
164
122
165
protected:
123
166
124
- //! Derived tasks must implement a run() method. This method will be called when the
125
- //! task commences (ie via calling start() ).
126
- virtual void run() = 0;
167
+ /**
168
+ * Performs the task's operation. This method will be called when the task commences
169
+ * (ie via calling start() ), and subclasses should implement the operation they
170
+ * wish to perform in the background within this method.
171
+ *
172
+ * A task can return a ResultSuccess and ResultFail value to indicate that the
173
+ * task has finished and was either completed successfully or terminated before
174
+ * completion.
175
+ *
176
+ * Alternatively, tasks can also return the ResultPending value
177
+ * to indicate that the task is still operating and will manually report its
178
+ * completion by calling completed() or stopped(). This may be useful for
179
+ * tasks which rely on external events for completion, eg downloading a
180
+ * file. In this case Qt slots could be created which are connected to the
181
+ * download completion or termination and which call completed() or stopped()
182
+ * to indicate the task has finished operations.
183
+ * @see completed()
184
+ * @see stopped()
185
+ */
186
+ virtual TaskResult run() = 0;
127
187
128
- //! Will return true if task should terminate ASAP. If the task reports the CanCancel
129
- //! flag, then derived classes' run() methods should periodically check this and
130
- //! terminate in a safe manner.
188
+ /**
189
+ * Will return true if task should terminate ASAP. If the task reports the CanCancel
190
+ * flag, then derived classes' run() methods should periodically check this and
191
+ * terminate in a safe manner.
192
+ */
131
193
bool isCancelled() const;
132
194
195
+ /**
196
+ * Sets the task as completed. Calling this is only required for tasks which
197
+ * returned the ResultPending value as a result of run(). This should be called
198
+ * when the task is complete. Calling will automatically emit the statusChanged
199
+ * and taskCompleted signals.
200
+ */
201
+ void completed();
202
+
203
+ /**
204
+ * Sets the task as stopped. Calling this is only required for tasks which
205
+ * returned the ResultPending value as a result of run().
206
+ * Should be called whenever the task ends for any reason other than successful
207
+ * completion. Calling will automatically emit the statusChanged and taskStopped
208
+ * signals.
209
+ */
210
+ void stopped();
211
+
212
+ protected slots:
213
+
214
+ /**
215
+ * Sets the task's current progress. If task reports the CanReportProgress flag then
216
+ * the derived class should call this method whenever the task wants to update its
217
+ * progress. Calling will automatically emit the progressChanged signal.
218
+ * @param progress percent of progress, from 0.0 - 100.0
219
+ */
220
+ void setProgress( double progress );
221
+
133
222
};
134
223
135
224
QFlags<QgsTask::Flag> operator|(QgsTask::Flag f1, QFlags<QgsTask::Flag> f2);
0 commit comments