feat: make Task a QRunnable
This makes it possible to run a task in another thread. I added a variable to toggle debug prints because they seem to trigger an assertion on Qt internals when the task in on another thread. Of course, this isn't awesome, but can wait until we improve our logging. Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
2dcff83be7
commit
2d63c86022
@ -37,8 +37,9 @@
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
Task::Task(QObject *parent) : QObject(parent)
|
Task::Task(QObject *parent, bool show_debug) : QObject(parent), m_show_debug(show_debug)
|
||||||
{
|
{
|
||||||
|
setAutoDelete(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Task::setStatus(const QString &new_status)
|
void Task::setStatus(const QString &new_status)
|
||||||
@ -63,26 +64,31 @@ void Task::start()
|
|||||||
{
|
{
|
||||||
case State::Inactive:
|
case State::Inactive:
|
||||||
{
|
{
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "starting for the first time";
|
qDebug() << "Task" << describe() << "starting for the first time";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case State::AbortedByUser:
|
case State::AbortedByUser:
|
||||||
{
|
{
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "restarting for after being aborted by user";
|
qDebug() << "Task" << describe() << "restarting for after being aborted by user";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case State::Failed:
|
case State::Failed:
|
||||||
{
|
{
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "restarting for after failing at first";
|
qDebug() << "Task" << describe() << "restarting for after failing at first";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case State::Succeeded:
|
case State::Succeeded:
|
||||||
{
|
{
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "restarting for after succeeding at first";
|
qDebug() << "Task" << describe() << "restarting for after succeeding at first";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case State::Running:
|
case State::Running:
|
||||||
{
|
{
|
||||||
|
if (m_show_debug)
|
||||||
qWarning() << "The launcher tried to start task" << describe() << "while it was already running!";
|
qWarning() << "The launcher tried to start task" << describe() << "while it was already running!";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -118,6 +124,7 @@ void Task::emitAborted()
|
|||||||
}
|
}
|
||||||
m_state = State::AbortedByUser;
|
m_state = State::AbortedByUser;
|
||||||
m_failReason = "Aborted.";
|
m_failReason = "Aborted.";
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "aborted.";
|
qDebug() << "Task" << describe() << "aborted.";
|
||||||
emit aborted();
|
emit aborted();
|
||||||
emit finished();
|
emit finished();
|
||||||
@ -132,6 +139,7 @@ void Task::emitSucceeded()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_state = State::Succeeded;
|
m_state = State::Succeeded;
|
||||||
|
if (m_show_debug)
|
||||||
qDebug() << "Task" << describe() << "succeeded";
|
qDebug() << "Task" << describe() << "succeeded";
|
||||||
emit succeeded();
|
emit succeeded();
|
||||||
emit finished();
|
emit finished();
|
||||||
|
@ -35,9 +35,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QRunnable>
|
||||||
|
|
||||||
#include "QObjectPtr.h"
|
#include "QObjectPtr.h"
|
||||||
|
|
||||||
class Task : public QObject {
|
class Task : public QObject, public QRunnable {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
using Ptr = shared_qobject_ptr<Task>;
|
using Ptr = shared_qobject_ptr<Task>;
|
||||||
@ -45,7 +47,7 @@ class Task : public QObject {
|
|||||||
enum class State { Inactive, Running, Succeeded, Failed, AbortedByUser };
|
enum class State { Inactive, Running, Succeeded, Failed, AbortedByUser };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Task(QObject* parent = 0);
|
explicit Task(QObject* parent = 0, bool show_debug_log = true);
|
||||||
virtual ~Task() = default;
|
virtual ~Task() = default;
|
||||||
|
|
||||||
bool isRunning() const;
|
bool isRunning() const;
|
||||||
@ -95,6 +97,9 @@ class Task : public QObject {
|
|||||||
void stepStatus(QString status);
|
void stepStatus(QString status);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
// QRunnable's interface
|
||||||
|
void run() override { start(); }
|
||||||
|
|
||||||
virtual void start();
|
virtual void start();
|
||||||
virtual bool abort() { if(canAbort()) emitAborted(); return canAbort(); };
|
virtual bool abort() { if(canAbort()) emitAborted(); return canAbort(); };
|
||||||
|
|
||||||
@ -117,4 +122,7 @@ class Task : public QObject {
|
|||||||
QString m_status;
|
QString m_status;
|
||||||
int m_progress = 0;
|
int m_progress = 0;
|
||||||
int m_progressTotal = 100;
|
int m_progressTotal = 100;
|
||||||
|
|
||||||
|
// TODO: Nuke in favor of QLoggingCategory
|
||||||
|
bool m_show_debug = true;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user