ThreadTask for all your ThreadNeeds

Use only when absolutely required.
This commit is contained in:
Petr Mrázek 2014-01-01 19:21:58 +01:00
parent cf7335186d
commit 0e78d34d4c
5 changed files with 85 additions and 9 deletions

View File

@ -47,11 +47,13 @@ find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Test REQUIRED)
find_package(Qt5Concurrent REQUIRED)
find_package(Qt5LinguistTools REQUIRED)
include_directories(
${Qt5Core_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}
${Qt5Concurrent_INCLUDE_DIRS}
${Qt5Network_INCLUDE_DIRS}
${Qt5Test_INCLUDE_DIRS}
)
@ -398,6 +400,8 @@ logic/EnabledItemFilter.cpp
logic/tasks/ProgressProvider.h
logic/tasks/Task.h
logic/tasks/Task.cpp
logic/tasks/ThreadTask.h
logic/tasks/ThreadTask.cpp
logic/tasks/SequentialTask.h
logic/tasks/SequentialTask.cpp
@ -512,8 +516,8 @@ ADD_EXECUTABLE(MultiMC MACOSX_BUNDLE WIN32 main.cpp ${MULTIMC_RCS})
# Link
TARGET_LINK_LIBRARIES(MultiMC MultiMC_common)
TARGET_LINK_LIBRARIES(MultiMC_common xz-embedded unpack200 quazip libUtil libSettings libGroupView ${MultiMC_LINK_ADDITIONAL_LIBS})
QT5_USE_MODULES(MultiMC Core Widgets Network Xml ${MultiMC_QT_ADDITIONAL_MODULES})
QT5_USE_MODULES(MultiMC_common Core Widgets Network Xml ${MultiMC_QT_ADDITIONAL_MODULES})
QT5_USE_MODULES(MultiMC Core Widgets Network Xml Concurrent ${MultiMC_QT_ADDITIONAL_MODULES})
QT5_USE_MODULES(MultiMC_common Core Widgets Network Xml Concurrent ${MultiMC_QT_ADDITIONAL_MODULES})
ADD_DEPENDENCIES(MultiMC_common MultiMCLauncher JavaCheck)
################################ INSTALLATION AND PACKAGING ################################

View File

@ -90,6 +90,7 @@
#include "logic/assets/AssetsUtils.h"
#include "logic/assets/AssetsMigrateTask.h"
#include <logic/updater/UpdateChecker.h>
#include <logic/tasks/ThreadTask.h>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
@ -1169,14 +1170,17 @@ void MainWindow::checkMigrateLegacyAssets()
{
ProgressDialog migrateDlg(this);
AssetsMigrateTask migrateTask(legacyAssets, &migrateDlg);
{
ThreadTask threadTask(&migrateTask);
if (migrateDlg.exec(&migrateTask))
{
QLOG_INFO() << "Assets migration task completed successfully";
}
else
{
QLOG_INFO() << "Assets migration task reported failure";
if (migrateDlg.exec(&threadTask))
{
QLOG_INFO() << "Assets migration task completed successfully";
}
else
{
QLOG_INFO() << "Assets migration task reported failure";
}
}
}
else

View File

@ -4,6 +4,7 @@
#include <QJsonObject>
#include <QJsonDocument>
#include <QDirIterator>
#include <QCryptographicHash>
#include "gui/dialogs/CustomMessageBox.h"
#include <QDesktopServices>
@ -139,3 +140,4 @@ void AssetsMigrateTask::executeTask()
emitSucceeded();
}
}

View File

@ -0,0 +1,41 @@
#include "ThreadTask.h"
#include <QtConcurrentRun>
ThreadTask::ThreadTask(Task * internal, QObject *parent) : Task(parent), m_internal(internal)
{
}
void ThreadTask::start()
{
connect(m_internal, SIGNAL(failed(QString)), SLOT(iternal_failed(QString)));
connect(m_internal, SIGNAL(progress(qint64,qint64)), SLOT(iternal_progress(qint64,qint64)));
connect(m_internal, SIGNAL(started()), SLOT(iternal_started()));
connect(m_internal, SIGNAL(status(QString)), SLOT(iternal_status(QString)));
connect(m_internal, SIGNAL(succeeded()), SLOT(iternal_succeeded()));
m_running = true;
QtConcurrent::run(m_internal, &Task::start);
}
void ThreadTask::iternal_failed(QString reason)
{
emitFailed(reason);
}
void ThreadTask::iternal_progress(qint64 current, qint64 total)
{
progress(current, total);
}
void ThreadTask::iternal_started()
{
emit started();
}
void ThreadTask::iternal_status(QString status)
{
setStatus(status);
}
void ThreadTask::iternal_succeeded()
{
emitSucceeded();
}

25
logic/tasks/ThreadTask.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include "Task.h"
class ThreadTask : public Task
{
Q_OBJECT
public:
explicit ThreadTask(Task * internal, QObject * parent = nullptr);
protected:
void executeTask() {};
public slots:
virtual void start();
private slots:
void iternal_started();
void iternal_progress(qint64 current, qint64 total);
void iternal_succeeded();
void iternal_failed(QString reason);
void iternal_status(QString status);
private:
Task * m_internal;
};