Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove QgsTaskManager singleton, and instead attach an instance
to QgsApplication.

ie instead of QgsTaskManager.instance(), use
QgsApplication.taskManager()
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent b6b7a7f commit 3999a37
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 48 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsapplication.sip
Expand Up @@ -374,6 +374,13 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
* @note added in 2.4 */
static void setMaxThreads( int maxThreads );

/**
* Returns the application's task manager, used for managing application
* wide background task handling.
* @note added in QGIS 3.0
*/
static QgsTaskManager* taskManager();

%If(ANDROID)
//dummy method to workaround sip generation issue issue
bool x11EventFilter ( XEvent * event );
Expand Down
4 changes: 0 additions & 4 deletions python/core/qgstaskmanager.sip
Expand Up @@ -251,10 +251,6 @@ class QgsTaskManager : QObject
%End
public:

/** Returns the global task manager instance pointer, creating the object on the first call.
*/
static QgsTaskManager * instance();

/** Constructor for QgsTaskManager.
* @param parent parent QObject
*/
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -2410,7 +2410,7 @@ void QgisApp::createStatusBar()
connect( mMapCanvas, SIGNAL( renderStarting() ), this, SLOT( canvasRefreshStarted() ) );
connect( mMapCanvas, SIGNAL( mapCanvasRefreshed() ), this, SLOT( canvasRefreshFinished() ) );

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

// Bumped the font up one point size since 8 was too
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsexpression.h"
#include "qgsactionscoperegistry.h"
#include "qgsruntimeprofiler.h"
#include "qgstaskmanager.h"

#include <QDir>
#include <QFile>
Expand Down Expand Up @@ -105,6 +106,9 @@ QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, const
{
sPlatformName = platformName;

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

Expand Down Expand Up @@ -239,6 +243,7 @@ void QgsApplication::init( QString customConfigPath )
QgsApplication::~QgsApplication()
{
delete mActionScopeRegistry;
delete mTaskManager;
delete mProfiler;
}

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

QgsTaskManager* QgsApplication::taskManager()
{
return instance()->mTaskManager;
}

void QgsApplication::emitSettingsChanged()
{
emit settingsChanged();
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -24,6 +24,7 @@

class QgsActionScopeRegistry;
class QgsRuntimeProfiler;
class QgsTaskManager;

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

/**
* Returns the application's task manager, used for managing application
* wide background task handling.
* @note added in QGIS 3.0
*/
static QgsTaskManager* taskManager();

#ifdef ANDROID
//dummy method to workaround sip generation issue issue
bool x11EventFilter( XEvent * event )
Expand Down Expand Up @@ -456,6 +464,7 @@ class CORE_EXPORT QgsApplication : public QApplication

QgsActionScopeRegistry* mActionScopeRegistry;
QgsRuntimeProfiler* mProfiler;
QgsTaskManager* mTaskManager;
};

#endif
12 changes: 0 additions & 12 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -110,18 +110,6 @@ void QgsTask::terminated()
// QgsTaskManager
//

// Static calls to enforce singleton behaviour
QgsTaskManager *QgsTaskManager::sInstance = nullptr;
QgsTaskManager *QgsTaskManager::instance()
{
if ( !sInstance )
{
sInstance = new QgsTaskManager();
}

return sInstance;
}

QgsTaskManager::QgsTaskManager( QObject* parent )
: QObject( parent )
, mTaskMutex( new QMutex( QMutex::Recursive ) )
Expand Down
8 changes: 1 addition & 7 deletions src/core/qgstaskmanager.h
Expand Up @@ -273,7 +273,7 @@ typedef QList< QgsTask* > QgsTaskList;
/** \ingroup core
* \class QgsTaskManager
* \brief Task manager for managing a set of long-running QgsTask tasks. This class can be created directly,
* or accessed via a global instance.
* or accessed via QgsApplication::taskManager().
* \note Added in version 3.0
*/
class CORE_EXPORT QgsTaskManager : public QObject
Expand All @@ -282,10 +282,6 @@ class CORE_EXPORT QgsTaskManager : public QObject

public:

/** Returns the global task manager instance pointer, creating the object on the first call.
*/
static QgsTaskManager * instance();

/** Constructor for QgsTaskManager.
* @param parent parent QObject
*/
Expand Down Expand Up @@ -405,8 +401,6 @@ class CORE_EXPORT QgsTaskManager : public QObject

private:

static QgsTaskManager *sInstance;

struct TaskInfo
{
TaskInfo( QgsTask* task = nullptr )
Expand Down
1 change: 0 additions & 1 deletion tests/src/core/testqgsapplication.cpp
Expand Up @@ -83,7 +83,6 @@ void TestQgsApplication::platformName()
QCOMPARE( QgsApplication::platform(), QString( "desktop" ) );
}


void TestQgsApplication::checkPaths()
{
QString myPath = QgsApplication::authorsFilePath();
Expand Down
10 changes: 1 addition & 9 deletions tests/src/core/testqgstaskmanager.cpp
Expand Up @@ -150,6 +150,7 @@ class FinishTask : public QgsTask
class TestQgsTaskManager : public QObject
{
Q_OBJECT
public:

private slots:
void initTestCase();// will be called before the first testfunction is executed.
Expand All @@ -159,7 +160,6 @@ class TestQgsTaskManager : public QObject
void task();
void taskResult();
void taskFinished();
void createInstance();
void addTask();
//void taskTerminationBeforeDelete();
void taskId();
Expand All @@ -171,8 +171,6 @@ class TestQgsTaskManager : public QObject
void dependancies();
void layerDependencies();

private:

};

void TestQgsTaskManager::initTestCase()
Expand Down Expand Up @@ -279,12 +277,6 @@ void TestQgsTaskManager::taskResult()
QCOMPARE( task->status(), QgsTask::Terminated );
}

void TestQgsTaskManager::createInstance()
{
QgsTaskManager* manager = QgsTaskManager::instance();
QVERIFY( manager );
}

void TestQgsTaskManager::addTask()
{
//create an empty manager
Expand Down
29 changes: 15 additions & 14 deletions tests/src/python/test_qgstaskmanager.py
Expand Up @@ -17,7 +17,8 @@

from qgis.core import (
QgsTask,
QgsTaskManager
QgsTaskManager,
QgsApplication
)
from qgis.PyQt.QtCore import (QCoreApplication)

Expand Down Expand Up @@ -91,7 +92,7 @@ def testTaskFromFunction(self):
""" test creating task from function """

task = QgsTask.fromFunction('test task', run, 20)
QgsTaskManager.instance().addTask(task)
QgsApplication.taskManager().addTask(task)
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass

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

# try a task which cancels itself
bad_task = QgsTask.fromFunction('test task2', run, None)
QgsTaskManager.instance().addTask(bad_task)
QgsApplication.taskManager().addTask(bad_task)
while bad_task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass

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

task = QgsTask.fromFunction('test task3', run_with_kwargs, result=5, password=1)
QgsTaskManager.instance().addTask(task)
QgsApplication.taskManager().addTask(task)
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass

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

bad_task.cancel()
while bad_task.status() == QgsTask.Running:
pass
while QgsTaskManager.instance().countActiveTasks() > 0:
while QgsApplication.taskManager().countActiveTasks() > 0:
QCoreApplication.processEvents()

self.assertEqual(bad_task.status(), QgsTask.Terminated)
Expand All @@ -140,7 +141,7 @@ def testTaskFromFunctionIsCancellable(self):
def testTaskFromFunctionCanSetProgress(self):
""" test that task from function can set progress """
task = QgsTask.fromFunction('test task5', progress_function)
QgsTaskManager.instance().addTask(task)
QgsApplication.taskManager().addTask(task)
while task.status() != QgsTask.Running:
pass

Expand All @@ -152,16 +153,16 @@ def testTaskFromFunctionCanSetProgress(self):
task.cancel()
while task.status() == QgsTask.Running:
pass
while QgsTaskManager.instance().countActiveTasks() > 0:
while QgsApplication.taskManager().countActiveTasks() > 0:
QCoreApplication.processEvents()

def testTaskFromFunctionFinished(self):
""" test that task from function can have callback finished function"""
task = QgsTask.fromFunction('test task', run_no_result, on_finished=finished_no_val)
QgsTaskManager.instance().addTask(task)
QgsApplication.taskManager().addTask(task)
while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
pass
while QgsTaskManager.instance().countActiveTasks() > 0:
while QgsApplication.taskManager().countActiveTasks() > 0:
QCoreApplication.processEvents()

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

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

# check that the finished function was called
Expand Down

0 comments on commit 3999a37

Please sign in to comment.