Skip to content

Commit e35420a

Browse files
committed
Force QgsTask::run() to fully complete
Remove support for signal based completion/termination Also unfortunately disable a lot of the test suite as a result, since it's not easily translatable
1 parent 268e512 commit e35420a

File tree

5 files changed

+106
-168
lines changed

5 files changed

+106
-168
lines changed

python/core/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ def run(self):
214214
except Exception as ex:
215215
# report error
216216
self.exception = ex
217-
return QgsTask.ResultFail
217+
return False
218218

219-
return QgsTask.ResultSuccess
219+
return True
220220

221221
def finished(self, result):
222222
if not self.on_finished:
223223
return
224224

225-
if result == QgsTask.ResultFail and self.exception is None:
225+
if not result and self.exception is None:
226226
self.exception = Exception('Task cancelled')
227227

228228
try:

python/core/qgstaskmanager.sip

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ class QgsTask : QObject
3232
Terminated, /*!< Task was terminated or errored */
3333
};
3434

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-
4335
//! Task flags
4436
enum Flag
4537
{
@@ -154,8 +146,8 @@ class QgsTask : QObject
154146
/**
155147
* Will be emitted by task when its status changes.
156148
* @param status new task status
157-
* @note derived classes should not emit this signal directly, instead they should call
158-
* completed() or terminated()
149+
* @note derived classes should not emit this signal directly, it will automatically
150+
* be emitted
159151
*/
160152
void statusChanged( int status );
161153

@@ -168,17 +160,17 @@ class QgsTask : QObject
168160

169161
/**
170162
* Will be emitted by task to indicate its successful completion.
171-
* @note derived classes should not emit this signal directly, instead they should call
172-
* completed()
163+
* @note derived classes should not emit this signal directly, it will automatically
164+
* be emitted
173165
*/
174166
void taskCompleted();
175167

176168
/**
177169
* Will be emitted by task if it has terminated for any reason
178170
* other then completion (eg when a task has been cancelled or encountered
179171
* an internal error).
180-
* @note derived classes should not emit this signal directly, instead they should call
181-
* terminated()
172+
* @note derived classes should not emit this signal directly, it will automatically
173+
* be emitted
182174
*/
183175
void taskTerminated();
184176

@@ -189,21 +181,10 @@ class QgsTask : QObject
189181
* (ie via calling start() ), and subclasses should implement the operation they
190182
* wish to perform in the background within this method.
191183
*
192-
* A task can return a ResultSuccess and ResultFail value to indicate that the
193-
* task has finished and was either completed successfully or terminated before
194-
* completion.
195-
*
196-
* Alternatively, tasks can also return the ResultPending value
197-
* to indicate that the task is still operating and will manually report its
198-
* completion by calling completed() or terminated(). This may be useful for
199-
* tasks which rely on external events for completion, eg downloading a
200-
* file. In this case Qt slots could be created which are connected to the
201-
* download completion or termination and which call completed() or terminated()
202-
* to indicate the task has finished operations.
203-
* @see completed()
204-
* @see terminated()
184+
* A task must return a boolean value to indicate whether the
185+
* task was completed successfully or terminated before completion.
205186
*/
206-
virtual TaskResult run() = 0;
187+
virtual bool run() = 0;
207188

208189
/**
209190
* If the task is managed by a QgsTaskManager, this will be called after the
@@ -215,7 +196,7 @@ class QgsTask : QObject
215196
* for the duration of this method so tasks should avoid performing any
216197
* lengthy operations here.
217198
*/
218-
virtual void finished( TaskResult result );
199+
virtual void finished( bool result );
219200

220201
/**
221202
* Will return true if task should terminate ASAP. If the task reports the CanCancel
@@ -224,23 +205,6 @@ class QgsTask : QObject
224205
*/
225206
bool isCancelled() const;
226207

227-
/**
228-
* Sets the task as completed. Calling this is only required for tasks which
229-
* returned the ResultPending value as a result of run(). This should be called
230-
* when the task is complete. Calling will automatically emit the statusChanged
231-
* and taskCompleted signals.
232-
*/
233-
void completed();
234-
235-
/**
236-
* Sets the task as terminated. Calling this is only required for tasks which
237-
* returned the ResultPending value as a result of run().
238-
* Should be called whenever the task ends for any reason other than successful
239-
* completion. Calling will automatically emit the statusChanged and taskTerminated
240-
* signals.
241-
*/
242-
void terminated();
243-
244208
protected slots:
245209

246210
/**

src/core/qgstaskmanager.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,13 @@ void QgsTask::start()
6262
// force initial emission of progressChanged, but respect if task has had initial progress manually set
6363
setProgress( mProgress );
6464

65-
TaskResult result = run();
66-
switch ( result )
65+
if ( run() )
6766
{
68-
case ResultSuccess:
69-
completed();
70-
break;
71-
72-
case ResultFail:
73-
terminated();
74-
break;
75-
76-
case ResultPending:
77-
// nothing to do - task will call completed() or stopped()
78-
// in it's own time
79-
break;
80-
67+
completed();
68+
}
69+
else
70+
{
71+
terminated();
8172
}
8273
}
8374

@@ -625,8 +616,7 @@ void QgsTaskManager::taskStatusChanged( int status )
625616

626617
if ( status == QgsTask::Terminated || status == QgsTask::Complete )
627618
{
628-
QgsTask::TaskResult result = status == QgsTask::Complete ? QgsTask::ResultSuccess
629-
: QgsTask::ResultFail;
619+
bool result = status == QgsTask::Complete;
630620
task->finished( result );
631621
}
632622

src/core/qgstaskmanager.h

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ class CORE_EXPORT QgsTask : public QObject
6161
Terminated, //!< Task was terminated or errored
6262
};
6363

64-
//! Result of running the task
65-
enum TaskResult
66-
{
67-
ResultSuccess = 0, //!< Task completed successfully
68-
ResultFail, //!< Task was terminated within completion
69-
ResultPending, //!< Task is still running
70-
};
71-
7264
//! Task flags
7365
enum Flag
7466
{
@@ -186,8 +178,8 @@ class CORE_EXPORT QgsTask : public QObject
186178
/**
187179
* Will be emitted by task when its status changes.
188180
* @param status new task status
189-
* @note derived classes should not emit this signal directly, instead they should call
190-
* completed() or terminated()
181+
* @note derived classes should not emit this signal directly, it will automatically
182+
* be emitted
191183
*/
192184
void statusChanged( int status );
193185

@@ -200,17 +192,17 @@ class CORE_EXPORT QgsTask : public QObject
200192

201193
/**
202194
* Will be emitted by task to indicate its successful completion.
203-
* @note derived classes should not emit this signal directly, instead they should call
204-
* completed()
195+
* @note derived classes should not emit this signal directly, it will automatically
196+
* be emitted
205197
*/
206198
void taskCompleted();
207199

208200
/**
209201
* Will be emitted by task if it has terminated for any reason
210202
* other then completion (eg when a task has been cancelled or encountered
211203
* an internal error).
212-
* @note derived classes should not emit this signal directly, instead they should call
213-
* terminated()
204+
* @note derived classes should not emit this signal directly, it will automatically
205+
* be emitted
214206
*/
215207
void taskTerminated();
216208

@@ -221,21 +213,10 @@ class CORE_EXPORT QgsTask : public QObject
221213
* (ie via calling start() ), and subclasses should implement the operation they
222214
* wish to perform in the background within this method.
223215
*
224-
* A task can return a ResultSuccess and ResultFail value to indicate that the
225-
* task has finished and was either completed successfully or terminated before
226-
* completion.
227-
*
228-
* Alternatively, tasks can also return the ResultPending value
229-
* to indicate that the task is still operating and will manually report its
230-
* completion by calling completed() or terminated(). This may be useful for
231-
* tasks which rely on external events for completion, eg downloading a
232-
* file. In this case Qt slots could be created which are connected to the
233-
* download completion or termination and which call completed() or terminated()
234-
* to indicate the task has finished operations.
235-
* @see completed()
236-
* @see terminated()
237-
*/
238-
virtual TaskResult run() = 0;
216+
* A task must return a boolean value to indicate whether the
217+
* task was completed successfully or terminated before completion.
218+
*/
219+
virtual bool run() = 0;
239220

240221
/**
241222
* If the task is managed by a QgsTaskManager, this will be called after the
@@ -247,7 +228,7 @@ class CORE_EXPORT QgsTask : public QObject
247228
* for the duration of this method so tasks should avoid performing any
248229
* lengthy operations here.
249230
*/
250-
virtual void finished( TaskResult result ) { Q_UNUSED( result ); }
231+
virtual void finished( bool result ) { Q_UNUSED( result ); }
251232

252233
/**
253234
* Will return true if task should terminate ASAP. If the task reports the CanCancel
@@ -256,29 +237,11 @@ class CORE_EXPORT QgsTask : public QObject
256237
*/
257238
bool isCancelled() const { return mShouldTerminate; }
258239

259-
/**
260-
* Sets the task as completed. Calling this is only required for tasks which
261-
* returned the ResultPending value as a result of run(). This should be called
262-
* when the task is complete. Calling will automatically emit the statusChanged
263-
* and taskCompleted signals.
264-
*/
265-
void completed();
266-
267-
/**
268-
* Sets the task as terminated. Calling this is only required for tasks which
269-
* returned the ResultPending value as a result of run().
270-
* Should be called whenever the task ends for any reason other than successful
271-
* completion. Calling will automatically emit the statusChanged and taskTerminated
272-
* signals.
273-
*/
274-
void terminated();
275-
276240
protected slots:
277241

278242
/**
279-
* Sets the task's current progress. If task reports the CanReportProgress flag then
280-
* the derived class should call this method whenever the task wants to update its
281-
* progress. Calling will automatically emit the progressChanged signal.
243+
* Sets the task's current progress. The derived class should call this method whenever
244+
* the task wants to update its progress. Calling will automatically emit the progressChanged signal.
282245
* @param progress percent of progress, from 0.0 - 100.0
283246
*/
284247
void setProgress( double progress );
@@ -321,6 +284,16 @@ class CORE_EXPORT QgsTask : public QObject
321284
*/
322285
void start();
323286

287+
/**
288+
* Called when the task has completed successfully.
289+
*/
290+
void completed();
291+
292+
/**
293+
* Called when the task has failed, as either a result of an internal failure or via cancellation.
294+
*/
295+
void terminated();
296+
324297
void processSubTasksForCompletion();
325298

326299
void processSubTasksForTermination();

0 commit comments

Comments
 (0)