Skip to content
Permalink
Browse files
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 3999a3710f7f7c30a07890d256db7ed278a52255
@@ -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 );
@@ -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
*/
@@ -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
@@ -25,6 +25,7 @@
#include "qgsexpression.h"
#include "qgsactionscoperegistry.h"
#include "qgsruntimeprofiler.h"
#include "qgstaskmanager.h"

#include <QDir>
#include <QFile>
@@ -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();

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

@@ -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();
@@ -24,6 +24,7 @@

class QgsActionScopeRegistry;
class QgsRuntimeProfiler;
class QgsTaskManager;

/** \ingroup core
* Extends QApplication to provide access to QGIS specific resources such
@@ -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 )
@@ -456,6 +464,7 @@ class CORE_EXPORT QgsApplication : public QApplication

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

#endif
@@ -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 ) )
@@ -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
@@ -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
*/
@@ -405,8 +401,6 @@ class CORE_EXPORT QgsTaskManager : public QObject

private:

static QgsTaskManager *sInstance;

struct TaskInfo
{
TaskInfo( QgsTask* task = nullptr )
@@ -83,7 +83,6 @@ void TestQgsApplication::platformName()
QCOMPARE( QgsApplication::platform(), QString( "desktop" ) );
}


void TestQgsApplication::checkPaths()
{
QString myPath = QgsApplication::authorsFilePath();
@@ -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.
@@ -159,7 +160,6 @@ class TestQgsTaskManager : public QObject
void task();
void taskResult();
void taskFinished();
void createInstance();
void addTask();
//void taskTerminationBeforeDelete();
void taskId();
@@ -171,8 +171,6 @@ class TestQgsTaskManager : public QObject
void dependancies();
void layerDependencies();

private:

};

void TestQgsTaskManager::initTestCase()
@@ -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
@@ -17,7 +17,8 @@

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

@@ -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

@@ -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

@@ -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

@@ -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)
@@ -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

@@ -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
@@ -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
@@ -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

0 comments on commit 3999a37

Please sign in to comment.