Skip to content

Commit dcecf44

Browse files
committedDec 5, 2016
Add support for placing queued tasks on hold
1 parent cf5eeb7 commit dcecf44

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed
 

‎python/core/qgstaskmanager.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class QgsTask : QObject
1515
enum TaskStatus
1616
{
1717
Queued, /*!< Task is queued and has not begun */
18+
OnHold, /*!< Task is queued but on hold and will not be started */
1819
Running, /*!< Task is currently running */
1920
Complete, /*!< Task successfully completed */
2021
Terminated, /*!< Task was terminated or errored */
@@ -63,6 +64,16 @@ class QgsTask : QObject
6364
//! @see isCancelled()
6465
void cancel();
6566

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()
70+
void hold();
71+
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()
75+
void unhold();
76+
6677
//! Sets the task's current progress. If task reports the CanReportProgress flag then
6778
//! the derived class should call this method whenever the task wants to update its
6879
//! progress. Calling will automatically emit the progressChanged signal.

‎src/core/qgstaskmanager.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ void QgsTask::cancel()
4545
mShouldTerminate = true;
4646
}
4747

48+
void QgsTask::hold()
49+
{
50+
if ( mStatus == Queued )
51+
{
52+
mStatus = OnHold;
53+
emit statusChanged( OnHold );
54+
}
55+
}
56+
57+
void QgsTask::unhold()
58+
{
59+
if ( mStatus == OnHold )
60+
{
61+
mStatus = Queued;
62+
emit statusChanged( Queued );
63+
}
64+
}
65+
4866
void QgsTask::setProgress( double progress )
4967
{
5068
mProgress = progress;
@@ -107,9 +125,9 @@ long QgsTaskManager::addTask( QgsTask* task )
107125
connect( task, SIGNAL( progressChanged( double ) ), this, SLOT( taskProgressChanged( double ) ) );
108126
connect( task, SIGNAL( statusChanged( int ) ), this, SLOT( taskStatusChanged( int ) ) );
109127

110-
mTasks[ mNextTaskId ].future = QtConcurrent::run( task, &QgsTask::start );
111-
112128
emit taskAdded( mNextTaskId );
129+
processQueue();
130+
113131
return mNextTaskId++;
114132
}
115133

@@ -202,6 +220,7 @@ void QgsTaskManager::taskStatusChanged( int status )
202220
return;
203221

204222
emit statusChanged( id, status );
223+
processQueue();
205224
}
206225

207226
bool QgsTaskManager::cleanupAndDeleteTask( QgsTask *task )
@@ -228,3 +247,15 @@ bool QgsTaskManager::cleanupAndDeleteTask( QgsTask *task )
228247
task->deleteLater();
229248
return true;
230249
}
250+
251+
void QgsTaskManager::processQueue()
252+
{
253+
for ( QMap< long, TaskInfo >::iterator it = mTasks.begin(); it != mTasks.end(); ++it )
254+
{
255+
QgsTask* task = it.value().task;
256+
if ( task && task->status() == QgsTask::Queued )
257+
{
258+
mTasks[ it.key()].future = QtConcurrent::run( task, &QgsTask::start );
259+
}
260+
}
261+
}

‎src/core/qgstaskmanager.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CORE_EXPORT QgsTask : public QObject
3939
enum TaskStatus
4040
{
4141
Queued, /*!< Task is queued and has not begun */
42+
OnHold, /*!< Task is queued but on hold and will not be started */
4243
Running, /*!< Task is currently running */
4344
Complete, /*!< Task successfully completed */
4445
Terminated, /*!< Task was terminated or errored */
@@ -87,6 +88,16 @@ class CORE_EXPORT QgsTask : public QObject
8788
//! @see isCancelled()
8889
void cancel();
8990

91+
//! Called when the task is placed on hold. If the task in not queued
92+
//! (ie it is running or has finished) then calling this has no effect.
93+
//! @see unhold()
94+
void hold();
95+
96+
//! Called when the task should be unheld and re-added to the queue. If the
97+
//! task in not currently being held then calling this has no effect.
98+
//! @see unhold()
99+
void unhold();
100+
90101
//! Sets the task's current progress. If task reports the CanReportProgress flag then
91102
//! the derived class should call this method whenever the task wants to update its
92103
//! progress. Calling will automatically emit the progressChanged signal.
@@ -267,6 +278,10 @@ class CORE_EXPORT QgsTaskManager : public QObject
267278

268279
bool cleanupAndDeleteTask( QgsTask* task );
269280

281+
//! Process the queue of outstanding jobs and starts up any
282+
//! which are ready to go.
283+
void processQueue();
284+
270285
};
271286

272287
#endif //QGSTASKMANAGER_H

‎tests/src/core/testqgstaskmanager.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class TestQgsTaskManager : public QObject
8484
void taskId();
8585
void progressChanged();
8686
void statusChanged();
87+
void holdTask();
8788

8889
private:
8990

@@ -334,6 +335,22 @@ void TestQgsTaskManager::statusChanged()
334335
QCOMPARE( static_cast< QgsTask::TaskStatus >( spy.last().at( 1 ).toInt() ), QgsTask::Complete );
335336
}
336337

338+
void TestQgsTaskManager::holdTask()
339+
{
340+
QgsTaskManager manager;
341+
TestTask* task = new TestTask();
342+
//hold task
343+
task->hold();
344+
manager.addTask( task );
345+
//should not be started
346+
QCOMPARE( task->status(), QgsTask::OnHold );
347+
348+
task->unhold();
349+
// wait for task to spin up
350+
while ( task->status() == QgsTask::Queued ) {}
351+
QCOMPARE( task->status(), QgsTask::Running );
352+
}
353+
337354

338355
QTEST_MAIN( TestQgsTaskManager )
339356
#include "testqgstaskmanager.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.