Skip to content

Commit 3999a37

Browse files
committed
Remove QgsTaskManager singleton, and instead attach an instance
to QgsApplication. ie instead of QgsTaskManager.instance(), use QgsApplication.taskManager()
1 parent b6b7a7f commit 3999a37

File tree

10 files changed

+44
-48
lines changed

10 files changed

+44
-48
lines changed

python/core/qgsapplication.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
374374
* @note added in 2.4 */
375375
static void setMaxThreads( int maxThreads );
376376

377+
/**
378+
* Returns the application's task manager, used for managing application
379+
* wide background task handling.
380+
* @note added in QGIS 3.0
381+
*/
382+
static QgsTaskManager* taskManager();
383+
377384
%If(ANDROID)
378385
//dummy method to workaround sip generation issue issue
379386
bool x11EventFilter ( XEvent * event );

python/core/qgstaskmanager.sip

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ class QgsTaskManager : QObject
251251
%End
252252
public:
253253

254-
/** Returns the global task manager instance pointer, creating the object on the first call.
255-
*/
256-
static QgsTaskManager * instance();
257-
258254
/** Constructor for QgsTaskManager.
259255
* @param parent parent QObject
260256
*/

src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ void QgisApp::createStatusBar()
24102410
connect( mMapCanvas, SIGNAL( renderStarting() ), this, SLOT( canvasRefreshStarted() ) );
24112411
connect( mMapCanvas, SIGNAL( mapCanvasRefreshed() ), this, SLOT( canvasRefreshFinished() ) );
24122412

2413-
mTaskManagerWidget = new QgsTaskManagerStatusBarWidget( QgsTaskManager::instance(), statusBar() );
2413+
mTaskManagerWidget = new QgsTaskManagerStatusBarWidget( QgsApplication::taskManager(), statusBar() );
24142414
statusBar()->addPermanentWidget( mTaskManagerWidget, 0 );
24152415

24162416
// Bumped the font up one point size since 8 was too

src/core/qgsapplication.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsexpression.h"
2626
#include "qgsactionscoperegistry.h"
2727
#include "qgsruntimeprofiler.h"
28+
#include "qgstaskmanager.h"
2829

2930
#include <QDir>
3031
#include <QFile>
@@ -105,6 +106,9 @@ QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, const
105106
{
106107
sPlatformName = platformName;
107108

109+
// don't use initializer lists or scoped pointers - as more objects are added here we
110+
// will need to be careful with the order of creation/destruction
111+
mTaskManager = new QgsTaskManager();
108112
mProfiler = new QgsRuntimeProfiler();
109113
mActionScopeRegistry = new QgsActionScopeRegistry();
110114

@@ -239,6 +243,7 @@ void QgsApplication::init( QString customConfigPath )
239243
QgsApplication::~QgsApplication()
240244
{
241245
delete mActionScopeRegistry;
246+
delete mTaskManager;
242247
delete mProfiler;
243248
}
244249

@@ -1407,6 +1412,11 @@ void QgsApplication::setMaxThreads( int maxThreads )
14071412
QgsDebugMsg( QString( "set QThreadPool max thread count to %1" ).arg( QThreadPool::globalInstance()->maxThreadCount() ) );
14081413
}
14091414

1415+
QgsTaskManager* QgsApplication::taskManager()
1416+
{
1417+
return instance()->mTaskManager;
1418+
}
1419+
14101420
void QgsApplication::emitSettingsChanged()
14111421
{
14121422
emit settingsChanged();

src/core/qgsapplication.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
class QgsActionScopeRegistry;
2626
class QgsRuntimeProfiler;
27+
class QgsTaskManager;
2728

2829
/** \ingroup core
2930
* Extends QApplication to provide access to QGIS specific resources such
@@ -366,6 +367,13 @@ class CORE_EXPORT QgsApplication : public QApplication
366367
* @note added in 2.4 */
367368
static void setMaxThreads( int maxThreads );
368369

370+
/**
371+
* Returns the application's task manager, used for managing application
372+
* wide background task handling.
373+
* @note added in QGIS 3.0
374+
*/
375+
static QgsTaskManager* taskManager();
376+
369377
#ifdef ANDROID
370378
//dummy method to workaround sip generation issue issue
371379
bool x11EventFilter( XEvent * event )
@@ -456,6 +464,7 @@ class CORE_EXPORT QgsApplication : public QApplication
456464

457465
QgsActionScopeRegistry* mActionScopeRegistry;
458466
QgsRuntimeProfiler* mProfiler;
467+
QgsTaskManager* mTaskManager;
459468
};
460469

461470
#endif

src/core/qgstaskmanager.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@ void QgsTask::terminated()
110110
// QgsTaskManager
111111
//
112112

113-
// Static calls to enforce singleton behaviour
114-
QgsTaskManager *QgsTaskManager::sInstance = nullptr;
115-
QgsTaskManager *QgsTaskManager::instance()
116-
{
117-
if ( !sInstance )
118-
{
119-
sInstance = new QgsTaskManager();
120-
}
121-
122-
return sInstance;
123-
}
124-
125113
QgsTaskManager::QgsTaskManager( QObject* parent )
126114
: QObject( parent )
127115
, mTaskMutex( new QMutex( QMutex::Recursive ) )

src/core/qgstaskmanager.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ typedef QList< QgsTask* > QgsTaskList;
273273
/** \ingroup core
274274
* \class QgsTaskManager
275275
* \brief Task manager for managing a set of long-running QgsTask tasks. This class can be created directly,
276-
* or accessed via a global instance.
276+
* or accessed via QgsApplication::taskManager().
277277
* \note Added in version 3.0
278278
*/
279279
class CORE_EXPORT QgsTaskManager : public QObject
@@ -282,10 +282,6 @@ class CORE_EXPORT QgsTaskManager : public QObject
282282

283283
public:
284284

285-
/** Returns the global task manager instance pointer, creating the object on the first call.
286-
*/
287-
static QgsTaskManager * instance();
288-
289285
/** Constructor for QgsTaskManager.
290286
* @param parent parent QObject
291287
*/
@@ -405,8 +401,6 @@ class CORE_EXPORT QgsTaskManager : public QObject
405401

406402
private:
407403

408-
static QgsTaskManager *sInstance;
409-
410404
struct TaskInfo
411405
{
412406
TaskInfo( QgsTask* task = nullptr )

tests/src/core/testqgsapplication.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ void TestQgsApplication::platformName()
8383
QCOMPARE( QgsApplication::platform(), QString( "desktop" ) );
8484
}
8585

86-
8786
void TestQgsApplication::checkPaths()
8887
{
8988
QString myPath = QgsApplication::authorsFilePath();

tests/src/core/testqgstaskmanager.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class FinishTask : public QgsTask
150150
class TestQgsTaskManager : public QObject
151151
{
152152
Q_OBJECT
153+
public:
153154

154155
private slots:
155156
void initTestCase();// will be called before the first testfunction is executed.
@@ -159,7 +160,6 @@ class TestQgsTaskManager : public QObject
159160
void task();
160161
void taskResult();
161162
void taskFinished();
162-
void createInstance();
163163
void addTask();
164164
//void taskTerminationBeforeDelete();
165165
void taskId();
@@ -171,8 +171,6 @@ class TestQgsTaskManager : public QObject
171171
void dependancies();
172172
void layerDependencies();
173173

174-
private:
175-
176174
};
177175

178176
void TestQgsTaskManager::initTestCase()
@@ -279,12 +277,6 @@ void TestQgsTaskManager::taskResult()
279277
QCOMPARE( task->status(), QgsTask::Terminated );
280278
}
281279

282-
void TestQgsTaskManager::createInstance()
283-
{
284-
QgsTaskManager* manager = QgsTaskManager::instance();
285-
QVERIFY( manager );
286-
}
287-
288280
void TestQgsTaskManager::addTask()
289281
{
290282
//create an empty manager

tests/src/python/test_qgstaskmanager.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
from qgis.core import (
1919
QgsTask,
20-
QgsTaskManager
20+
QgsTaskManager,
21+
QgsApplication
2122
)
2223
from qgis.PyQt.QtCore import (QCoreApplication)
2324

@@ -91,7 +92,7 @@ def testTaskFromFunction(self):
9192
""" test creating task from function """
9293

9394
task = QgsTask.fromFunction('test task', run, 20)
94-
QgsTaskManager.instance().addTask(task)
95+
QgsApplication.taskManager().addTask(task)
9596
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
9697
pass
9798

@@ -101,7 +102,7 @@ def testTaskFromFunction(self):
101102

102103
# try a task which cancels itself
103104
bad_task = QgsTask.fromFunction('test task2', run, None)
104-
QgsTaskManager.instance().addTask(bad_task)
105+
QgsApplication.taskManager().addTask(bad_task)
105106
while bad_task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
106107
pass
107108

@@ -113,7 +114,7 @@ def testTaskFromFunctionWithKwargs(self):
113114
""" test creating task from function using kwargs """
114115

115116
task = QgsTask.fromFunction('test task3', run_with_kwargs, result=5, password=1)
116-
QgsTaskManager.instance().addTask(task)
117+
QgsApplication.taskManager().addTask(task)
117118
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
118119
pass
119120

@@ -124,14 +125,14 @@ def testTaskFromFunctionWithKwargs(self):
124125
def testTaskFromFunctionIsCancellable(self):
125126
""" test that task from function can check cancelled status """
126127
bad_task = QgsTask.fromFunction('test task4', cancellable)
127-
QgsTaskManager.instance().addTask(bad_task)
128+
QgsApplication.taskManager().addTask(bad_task)
128129
while bad_task.status() != QgsTask.Running:
129130
pass
130131

131132
bad_task.cancel()
132133
while bad_task.status() == QgsTask.Running:
133134
pass
134-
while QgsTaskManager.instance().countActiveTasks() > 0:
135+
while QgsApplication.taskManager().countActiveTasks() > 0:
135136
QCoreApplication.processEvents()
136137

137138
self.assertEqual(bad_task.status(), QgsTask.Terminated)
@@ -140,7 +141,7 @@ def testTaskFromFunctionIsCancellable(self):
140141
def testTaskFromFunctionCanSetProgress(self):
141142
""" test that task from function can set progress """
142143
task = QgsTask.fromFunction('test task5', progress_function)
143-
QgsTaskManager.instance().addTask(task)
144+
QgsApplication.taskManager().addTask(task)
144145
while task.status() != QgsTask.Running:
145146
pass
146147

@@ -152,16 +153,16 @@ def testTaskFromFunctionCanSetProgress(self):
152153
task.cancel()
153154
while task.status() == QgsTask.Running:
154155
pass
155-
while QgsTaskManager.instance().countActiveTasks() > 0:
156+
while QgsApplication.taskManager().countActiveTasks() > 0:
156157
QCoreApplication.processEvents()
157158

158159
def testTaskFromFunctionFinished(self):
159160
""" test that task from function can have callback finished function"""
160161
task = QgsTask.fromFunction('test task', run_no_result, on_finished=finished_no_val)
161-
QgsTaskManager.instance().addTask(task)
162+
QgsApplication.taskManager().addTask(task)
162163
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
163164
pass
164-
while QgsTaskManager.instance().countActiveTasks() > 0:
165+
while QgsApplication.taskManager().countActiveTasks() > 0:
165166
QCoreApplication.processEvents()
166167

167168
# check that the finished function was called
@@ -172,10 +173,10 @@ def testTaskFromFunctionFinished(self):
172173
def testTaskFromFunctionFinishedWithVal(self):
173174
""" test that task from function can have callback finished function and is passed result values"""
174175
task = QgsTask.fromFunction('test task', run_single_val_result, on_finished=finished_single_value_result)
175-
QgsTaskManager.instance().addTask(task)
176+
QgsApplication.taskManager().addTask(task)
176177
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
177178
pass
178-
while QgsTaskManager.instance().countActiveTasks() > 0:
179+
while QgsApplication.taskManager().countActiveTasks() > 0:
179180
QCoreApplication.processEvents()
180181

181182
# check that the finished function was called
@@ -186,10 +187,10 @@ def testTaskFromFunctionFinishedWithVal(self):
186187
def testTaskFromFunctionFinishedWithMultipleValues(self):
187188
""" test that task from function can have callback finished function and is passed multiple result values"""
188189
task = QgsTask.fromFunction('test task', run_multiple_val_result, on_finished=finished_multiple_value_result)
189-
QgsTaskManager.instance().addTask(task)
190+
QgsApplication.taskManager().addTask(task)
190191
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
191192
pass
192-
while QgsTaskManager.instance().countActiveTasks() > 0:
193+
while QgsApplication.taskManager().countActiveTasks() > 0:
193194
QCoreApplication.processEvents()
194195

195196
# check that the finished function was called

0 commit comments

Comments
 (0)