Skip to content

Commit adff069

Browse files
committedDec 6, 2016
Cleanup QgsTask API for dependent layers
1 parent a52a1e9 commit adff069

File tree

4 files changed

+79
-23
lines changed

4 files changed

+79
-23
lines changed
 

‎python/core/qgstaskmanager.sip

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ class QgsTask : QObject
133133
void addSubTask( QgsTask* subTask /Transfer/, const QgsTaskList& dependencies = QgsTaskList(),
134134
SubTaskDependency subTaskDependency = SubTaskIndependent );
135135

136+
/**
137+
* Sets a list of layer IDs on which the task depends. The task will automatically
138+
* be cancelled if any of these layers are about to be removed.
139+
* @see dependentLayerIds()
140+
*/
141+
void setDependentLayers( const QStringList& dependentLayerIds );
142+
143+
/**
144+
* Returns the list of layer IDs on which the task depends. The task will automatically
145+
* be cancelled if any of these layers are about to be removed.
146+
* @see setDependentLayers()
147+
*/
148+
QStringList dependentLayerIds() const;
149+
136150
signals:
137151

138152
/**
@@ -310,22 +324,20 @@ class QgsTaskManager : QObject
310324
//! @note not available in Python bindings
311325
//QSet< long > dependencies( long taskId ) const;
312326

313-
/** Sets a list of layers on which as task is dependent. The task will automatically
314-
* be cancelled if any of these layers are above to be removed.
315-
* @param taskId task ID
316-
* @param layerIds list of layer IDs
317-
* @see dependentLayers()
318-
*/
319-
void setDependentLayers( long taskId, const QStringList& layerIds );
320-
321327
/** Returns a list of layers on which as task is dependent. The task will automatically
322328
* be cancelled if any of these layers are above to be removed.
323329
* @param taskId task ID
324330
* @returns list of layer IDs
325-
* @see setDependentLayers()
331+
* @see tasksDependentOnLayer()
326332
*/
327333
QStringList dependentLayers( long taskId ) const;
328334

335+
/**
336+
* Returns a list of tasks which depend on a layer.
337+
* @see dependentLayers()
338+
*/
339+
QList< QgsTask* > tasksDependentOnLayer( const QString& layerId ) const;
340+
329341
/** Returns a list of the active (queued or running) tasks.
330342
* @see countActiveTasks()
331343
*/

‎src/core/qgstaskmanager.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ void QgsTask::addSubTask( QgsTask* subTask, const QgsTaskList& dependencies,
136136
connect( subTask, &QgsTask::statusChanged, this, &QgsTask::subTaskStatusChanged );
137137
}
138138

139+
void QgsTask::setDependentLayers( const QStringList& dependentLayerIds )
140+
{
141+
mDependentLayerIds = dependentLayerIds;
142+
}
143+
139144
void QgsTask::subTaskStatusChanged( int status )
140145
{
141146
QgsTask* subTask = qobject_cast< QgsTask* >( sender() );
@@ -371,6 +376,8 @@ long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bo
371376
{
372377
mParentTasks << task;
373378
}
379+
if ( !task->dependentLayerIds().isEmpty() )
380+
mLayerDependencies.insert( taskId, task->dependentLayerIds() );
374381
mTaskMutex->unlock();
375382

376383
connect( task, &QgsTask::statusChanged, this, &QgsTaskManager::taskStatusChanged );
@@ -539,16 +546,27 @@ bool QgsTaskManager::hasCircularDependencies( long taskId ) const
539546
return !resolveDependencies( taskId, taskId, d );
540547
}
541548

542-
void QgsTaskManager::setDependentLayers( long taskId, const QStringList& layerIds )
549+
QStringList QgsTaskManager::dependentLayers( long taskId ) const
543550
{
544551
QMutexLocker ml( mTaskMutex );
545-
mLayerDependencies.insert( taskId, layerIds );
552+
return mLayerDependencies.value( taskId, QStringList() );
546553
}
547554

548-
QStringList QgsTaskManager::dependentLayers( long taskId ) const
555+
QList<QgsTask*> QgsTaskManager::tasksDependentOnLayer( const QString& layerId ) const
549556
{
550557
QMutexLocker ml( mTaskMutex );
551-
return mLayerDependencies.value( taskId, QStringList() );
558+
QList< QgsTask* > tasks;
559+
QMap< long, QStringList >::const_iterator layerIt = mLayerDependencies.constBegin();
560+
for ( ; layerIt != mLayerDependencies.constEnd(); ++layerIt )
561+
{
562+
if ( layerIt.value().contains( layerId ) )
563+
{
564+
QgsTask* layerTask = task( layerIt.key() );
565+
if ( layerTask )
566+
tasks << layerTask;
567+
}
568+
}
569+
return tasks;
552570
}
553571

554572
QList<QgsTask*> QgsTaskManager::activeTasks() const

‎src/core/qgstaskmanager.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ class CORE_EXPORT QgsTask : public QObject
165165
void addSubTask( QgsTask* subTask, const QgsTaskList& dependencies = QgsTaskList(),
166166
SubTaskDependency subTaskDependency = SubTaskIndependent );
167167

168+
/**
169+
* Sets a list of layer IDs on which the task depends. The task will automatically
170+
* be cancelled if any of these layers are about to be removed.
171+
* @see dependentLayerIds()
172+
*/
173+
void setDependentLayers( const QStringList& dependentLayerIds );
174+
175+
/**
176+
* Returns the list of layer IDs on which the task depends. The task will automatically
177+
* be cancelled if any of these layers are about to be removed.
178+
* @see setDependentLayers()
179+
*/
180+
QStringList dependentLayerIds() const { return mDependentLayerIds; }
181+
168182
signals:
169183

170184
/**
@@ -278,6 +292,8 @@ class CORE_EXPORT QgsTask : public QObject
278292
};
279293
QList< SubTask > mSubTasks;
280294

295+
QStringList mDependentLayerIds;
296+
281297

282298
/**
283299
* Starts the task. Should not be public as only QgsTaskManagers can initiate tasks.
@@ -306,6 +322,7 @@ class CORE_EXPORT QgsTask : public QObject
306322

307323
};
308324

325+
309326
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsTask::Flags )
310327

311328
/** \ingroup core
@@ -351,6 +368,7 @@ class CORE_EXPORT QgsTaskManager : public QObject
351368
* to the task manager for proper handling of dependencies.
352369
*/
353370
QgsTaskList dependentTasks;
371+
354372
};
355373

356374
/** Adds a task to the manager. Ownership of the task is transferred
@@ -405,22 +423,20 @@ class CORE_EXPORT QgsTaskManager : public QObject
405423
//! @note not available in Python bindings
406424
QSet< long > dependencies( long taskId ) const;
407425

408-
/** Sets a list of layers on which as task is dependent. The task will automatically
409-
* be cancelled if any of these layers are above to be removed.
410-
* @param taskId task ID
411-
* @param layerIds list of layer IDs
412-
* @see dependentLayers()
413-
*/
414-
void setDependentLayers( long taskId, const QStringList& layerIds );
415-
416426
/** Returns a list of layers on which as task is dependent. The task will automatically
417427
* be cancelled if any of these layers are above to be removed.
418428
* @param taskId task ID
419429
* @returns list of layer IDs
420-
* @see setDependentLayers()
430+
* @see tasksDependentOnLayer()
421431
*/
422432
QStringList dependentLayers( long taskId ) const;
423433

434+
/**
435+
* Returns a list of tasks which depend on a layer.
436+
* @see dependentLayers()
437+
*/
438+
QList< QgsTask* > tasksDependentOnLayer( const QString& layerId ) const;
439+
424440
/** Returns a list of the active (queued or running) tasks.
425441
* @see countActiveTasks()
426442
*/

‎tests/src/core/testqgstaskmanager.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,25 @@ void TestQgsTaskManager::layerDependencies()
10241024
//test that remove layers cancels all tasks which are dependant on them
10251025
TestTask* task = new TestTask();
10261026
task->hold();
1027+
task->setDependentLayers( QStringList() << layer2->id() << layer3->id() );
1028+
QCOMPARE( task->dependentLayerIds(), QStringList() << layer2->id() << layer3->id() );
10271029
long taskId = manager.addTask( task );
1028-
manager.setDependentLayers( taskId, QStringList() << layer2->id() << layer3->id() );
1030+
QCOMPARE( manager.dependentLayers( taskId ), QStringList() << layer2->id() << layer3->id() );
1031+
QVERIFY( manager.tasksDependentOnLayer( "xxx" ).isEmpty() );
1032+
QCOMPARE( manager.tasksDependentOnLayer( layer2->id() ), QList< QgsTask* >() << task );
1033+
QCOMPARE( manager.tasksDependentOnLayer( layer3->id() ), QList< QgsTask* >() << task );
10291034

10301035
QCOMPARE( task->status(), QgsTask::OnHold );
10311036
//removing layer1 should have no effect
10321037
QgsMapLayerRegistry::instance()->removeMapLayers( QList< QgsMapLayer* >() << layer1 );
10331038
QCOMPARE( task->status(), QgsTask::OnHold );
10341039
//removing layer3 should cancel task
10351040
QgsMapLayerRegistry::instance()->removeMapLayers( QList< QgsMapLayer* >() << layer3 );
1041+
while ( task->status() != QgsTask::Terminated )
1042+
{
1043+
QCoreApplication::processEvents();
1044+
}
1045+
flushEvents();
10361046
QCOMPARE( task->status(), QgsTask::Terminated );
10371047

10381048
QgsMapLayerRegistry::instance()->removeMapLayers( QList< QgsMapLayer* >() << layer2 );

0 commit comments

Comments
 (0)
Please sign in to comment.