Skip to content

Commit b6b7a7f

Browse files
committedDec 5, 2016
Remove delete* methods from QgsTaskManager API
On further consideration allowing external control of task deletion is a bad idea.
1 parent ad71dc4 commit b6b7a7f

File tree

4 files changed

+39
-117
lines changed

4 files changed

+39
-117
lines changed
 

‎python/core/qgstaskmanager.sip

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -274,25 +274,6 @@ class QgsTaskManager : QObject
274274
*/
275275
long addTask( QgsTask* task /Transfer/, const QgsTaskList& dependencies = QgsTaskList() );
276276

277-
/** Deletes the specified task, first terminating it if it is currently
278-
* running.
279-
* @param id task ID
280-
* @returns true if task was found and deleted
281-
*/
282-
bool deleteTask( long id );
283-
284-
/** Deletes the specified task, first terminating it if it is currently
285-
* running.
286-
* @param task task to delete
287-
* @returns true if task was contained in manager and deleted
288-
*/
289-
bool deleteTask( QgsTask* task );
290-
291-
/** Deletes all tasks in the manager, first terminating them if they are currently
292-
* running.
293-
*/
294-
void deleteAllTasks();
295-
296277
/** Returns the task with matching ID.
297278
* @param id task ID
298279
* @returns task if found, or nullptr

‎src/core/qgstaskmanager.cpp

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -172,46 +172,6 @@ long QgsTaskManager::addTask( QgsTask* task, const QgsTaskList& dependencies )
172172
return mNextTaskId++;
173173
}
174174

175-
bool QgsTaskManager::deleteTask( long id )
176-
{
177-
QMutexLocker ml( mTaskMutex );
178-
QgsTask* task = mTasks.value( id ).task;
179-
return deleteTask( task );
180-
}
181-
182-
bool QgsTaskManager::deleteTask( QgsTask *task )
183-
{
184-
if ( !task )
185-
return false;
186-
187-
bool result = cleanupAndDeleteTask( task );
188-
189-
// remove from internal task list
190-
QMutexLocker ml( mTaskMutex );
191-
for ( QMap< long, TaskInfo >::iterator it = mTasks.begin(); it != mTasks.end(); )
192-
{
193-
if ( it.value().task == task )
194-
it = mTasks.erase( it );
195-
else
196-
++it;
197-
}
198-
199-
return result;
200-
}
201-
202-
void QgsTaskManager::deleteAllTasks()
203-
{
204-
//first tell all tasks to cancel
205-
cancelAll();
206-
207-
QMutexLocker ml( mTaskMutex );
208-
Q_FOREACH ( QgsTask* task, tasks() )
209-
{
210-
deleteTask( task );
211-
}
212-
emit allTasksFinished();
213-
}
214-
215175
QgsTask* QgsTaskManager::task( long id ) const
216176
{
217177
QMutexLocker ml( mTaskMutex );
@@ -339,6 +299,20 @@ QStringList QgsTaskManager::dependentLayers( long taskId ) const
339299
return mLayerDependencies.value( taskId, QStringList() );
340300
}
341301

302+
QList<QgsTask*> QgsTaskManager::activeTasks() const
303+
{
304+
QMutexLocker ml( mTaskMutex );
305+
QList< QgsTask* > taskList = mActiveTasks;
306+
taskList.detach();
307+
return taskList;
308+
}
309+
310+
int QgsTaskManager::countActiveTasks() const
311+
{
312+
QMutexLocker ml( mTaskMutex );
313+
return mActiveTasks.count();
314+
}
315+
342316
void QgsTaskManager::taskProgressChanged( double progress )
343317
{
344318
QMutexLocker ml( mTaskMutex );

‎src/core/qgstaskmanager.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -305,25 +305,6 @@ class CORE_EXPORT QgsTaskManager : public QObject
305305
*/
306306
long addTask( QgsTask* task, const QgsTaskList& dependencies = QgsTaskList() );
307307

308-
/** Deletes the specified task, first terminating it if it is currently
309-
* running.
310-
* @param id task ID
311-
* @returns true if task was found and deleted
312-
*/
313-
bool deleteTask( long id );
314-
315-
/** Deletes the specified task, first terminating it if it is currently
316-
* running.
317-
* @param task task to delete
318-
* @returns true if task was contained in manager and deleted
319-
*/
320-
bool deleteTask( QgsTask* task );
321-
322-
/** Deletes all tasks in the manager, first terminating them if they are currently
323-
* running.
324-
*/
325-
void deleteAllTasks();
326-
327308
/** Returns the task with matching ID.
328309
* @param id task ID
329310
* @returns task if found, or nullptr
@@ -375,13 +356,13 @@ class CORE_EXPORT QgsTaskManager : public QObject
375356
/** Returns a list of the active (queued or running) tasks.
376357
* @see countActiveTasks()
377358
*/
378-
QList< QgsTask* > activeTasks() const { return mActiveTasks; }
359+
QList< QgsTask* > activeTasks() const;
379360

380361
/** Returns the number of active (queued or running) tasks.
381362
* @see activeTasks()
382363
* @see countActiveTasksChanged()
383364
*/
384-
int countActiveTasks() const { return mActiveTasks.count(); }
365+
int countActiveTasks() const;
385366

386367
signals:
387368

‎tests/src/core/testqgstaskmanager.cpp

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ class TestTerminationTask : public TestTask
7070
}
7171
};
7272

73+
class CancelableTask : public QgsTask
74+
{
75+
Q_OBJECT
76+
77+
public:
78+
79+
~CancelableTask()
80+
{
81+
int i = 1;
82+
i++;
83+
84+
}
85+
86+
protected:
87+
88+
TaskResult run() override
89+
{
90+
while ( !isCancelled() )
91+
{}
92+
return ResultSuccess;
93+
}
94+
};
95+
7396
class SuccessTask : public QgsTask
7497
{
7598
Q_OBJECT
@@ -138,7 +161,6 @@ class TestQgsTaskManager : public QObject
138161
void taskFinished();
139162
void createInstance();
140163
void addTask();
141-
void deleteTask();
142164
//void taskTerminationBeforeDelete();
143165
void taskId();
144166
void progressChanged();
@@ -303,42 +325,6 @@ void TestQgsTaskManager::addTask()
303325
QCOMPARE( spy.last().at( 0 ).toLongLong(), 1LL );
304326
}
305327

306-
void TestQgsTaskManager::deleteTask()
307-
{
308-
//create manager with some tasks
309-
QgsTaskManager manager;
310-
TestTask* task = new TestTask();
311-
TestTask* task2 = new TestTask();
312-
TestTask* task3 = new TestTask();
313-
manager.addTask( task );
314-
manager.addTask( task2 );
315-
manager.addTask( task3 );
316-
317-
QSignalSpy spy( &manager, &QgsTaskManager::taskAboutToBeDeleted );
318-
319-
//try deleting a non-existant task
320-
QVERIFY( !manager.deleteTask( 56 ) );
321-
QCOMPARE( spy.count(), 0 );
322-
323-
//try deleting a task by ID
324-
QVERIFY( manager.deleteTask( 1 ) );
325-
QCOMPARE( manager.tasks().count(), 2 );
326-
QVERIFY( !manager.task( 1 ) );
327-
QCOMPARE( spy.count(), 1 );
328-
QCOMPARE( spy.last().at( 0 ).toLongLong(), 1LL );
329-
330-
//can't delete twice
331-
QVERIFY( !manager.deleteTask( 1 ) );
332-
QCOMPARE( spy.count(), 1 );
333-
334-
//delete task by reference
335-
QVERIFY( manager.deleteTask( task ) );
336-
QCOMPARE( manager.tasks().count(), 1 );
337-
QVERIFY( !manager.task( 0 ) );
338-
QCOMPARE( spy.count(), 2 );
339-
QCOMPARE( spy.last().at( 0 ).toLongLong(), 0LL );
340-
}
341-
342328
#if 0
343329
// we don't run this by default - the sendPostedEvents call is fragile
344330
void TestQgsTaskManager::taskTerminationBeforeDelete()

0 commit comments

Comments
 (0)
Please sign in to comment.