mirror of
https://github.com/rizinorg/cutter.git
synced 2024-12-20 03:46:11 +00:00
Use QSharedPointer for AsyncTask
This commit is contained in:
parent
7777707062
commit
14cfe74324
@ -6,8 +6,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
|
|
||||||
AnalTask::AnalTask(QObject *parent) :
|
AnalTask::AnalTask() :
|
||||||
AsyncTask(parent)
|
AsyncTask()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class AnalTask : public AsyncTask
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AnalTask(QObject *parent = nullptr);
|
explicit AnalTask();
|
||||||
~AnalTask();
|
~AnalTask();
|
||||||
|
|
||||||
QString getTitle() override { return tr("Initial Analysis"); }
|
QString getTitle() override { return tr("Initial Analysis"); }
|
||||||
|
@ -5,10 +5,10 @@
|
|||||||
#include "ui_AsyncTaskDialog.h"
|
#include "ui_AsyncTaskDialog.h"
|
||||||
|
|
||||||
|
|
||||||
AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent)
|
AsyncTaskDialog::AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent)
|
||||||
: QDialog(parent),
|
: QDialog(parent),
|
||||||
ui(new Ui::AsyncTaskDialog),
|
task(task),
|
||||||
task(task)
|
ui(new Ui::AsyncTaskDialog)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
@ -17,9 +17,8 @@ AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent)
|
|||||||
setWindowTitle(title);
|
setWindowTitle(title);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog);
|
connect(task.data(), &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog);
|
||||||
connect(task, &AsyncTask::finished, this, [this]() {
|
connect(task.data(), &AsyncTask::finished, this, [this]() {
|
||||||
this->task = nullptr;
|
|
||||||
close();
|
close();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -44,10 +43,6 @@ void AsyncTaskDialog::updateLog(const QString &log)
|
|||||||
|
|
||||||
void AsyncTaskDialog::updateProgressTimer()
|
void AsyncTaskDialog::updateProgressTimer()
|
||||||
{
|
{
|
||||||
if(!task) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000;
|
int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000;
|
||||||
int minutesElapsed = secondsElapsed / 60;
|
int minutesElapsed = secondsElapsed / 60;
|
||||||
int hoursElapsed = minutesElapsed / 60;
|
int hoursElapsed = minutesElapsed / 60;
|
||||||
@ -67,7 +62,7 @@ void AsyncTaskDialog::updateProgressTimer()
|
|||||||
|
|
||||||
void AsyncTaskDialog::closeEvent(QCloseEvent *event)
|
void AsyncTaskDialog::closeEvent(QCloseEvent *event)
|
||||||
{
|
{
|
||||||
if (interruptOnClose && task) {
|
if (interruptOnClose) {
|
||||||
task->interrupt();
|
task->interrupt();
|
||||||
task->wait();
|
task->wait();
|
||||||
}
|
}
|
||||||
@ -77,7 +72,5 @@ void AsyncTaskDialog::closeEvent(QCloseEvent *event)
|
|||||||
|
|
||||||
void AsyncTaskDialog::reject()
|
void AsyncTaskDialog::reject()
|
||||||
{
|
{
|
||||||
if (task) {
|
task->interrupt();
|
||||||
task->interrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,18 @@
|
|||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include "utils/AsyncTask.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class AsyncTaskDialog;
|
class AsyncTaskDialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncTask;
|
|
||||||
|
|
||||||
class AsyncTaskDialog : public QDialog
|
class AsyncTaskDialog : public QDialog
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AsyncTaskDialog(AsyncTask *task, QWidget *parent = nullptr);
|
AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent = nullptr);
|
||||||
~AsyncTaskDialog();
|
~AsyncTaskDialog();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
@ -33,7 +33,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Ui::AsyncTaskDialog> ui;
|
std::unique_ptr<Ui::AsyncTaskDialog> ui;
|
||||||
AsyncTask *task;
|
AsyncTask::Ptr task;
|
||||||
QTimer timer;
|
QTimer timer;
|
||||||
|
|
||||||
bool interruptOnClose;
|
bool interruptOnClose;
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
|
||||||
|
#include "utils/AsyncTask.h"
|
||||||
|
|
||||||
#include "OptionsDialog.h"
|
#include "OptionsDialog.h"
|
||||||
#include "ui_OptionsDialog.h"
|
#include "ui_OptionsDialog.h"
|
||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
@ -231,15 +234,17 @@ void OptionsDialog::setupAndStartAnalysis(int level, QList<QString> advanced)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AnalTask *analTask = new AnalTask(main);
|
AnalTask *analTask = new AnalTask();
|
||||||
analTask->setOptions(options);
|
analTask->setOptions(options);
|
||||||
|
|
||||||
connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
|
connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
|
||||||
connect(analTask, &AsyncTask::finished, main, &MainWindow::finalizeOpen);
|
connect(analTask, &AsyncTask::finished, main, &MainWindow::finalizeOpen);
|
||||||
|
|
||||||
Core()->getAsyncTaskManager()->start(analTask);
|
AsyncTask::Ptr analTaskPtr(analTask);
|
||||||
|
|
||||||
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTask);
|
Core()->getAsyncTaskManager()->start(analTaskPtr);
|
||||||
|
|
||||||
|
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTaskPtr);
|
||||||
taskDialog->setAttribute(Qt::WA_DeleteOnClose);
|
taskDialog->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
taskDialog->show();
|
taskDialog->show();
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
|
|
||||||
#include "AsyncTask.h"
|
#include "AsyncTask.h"
|
||||||
|
|
||||||
AsyncTask::AsyncTask(QObject *parent)
|
AsyncTask::AsyncTask()
|
||||||
: QObject(parent),
|
: QObject(nullptr),
|
||||||
QRunnable()
|
QRunnable()
|
||||||
{
|
{
|
||||||
setAutoDelete(true);
|
setAutoDelete(false);
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,12 +44,17 @@ void AsyncTask::prepareRun()
|
|||||||
void AsyncTask::run()
|
void AsyncTask::run()
|
||||||
{
|
{
|
||||||
runningMutex.lock();
|
runningMutex.lock();
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
logBuffer = "";
|
logBuffer = "";
|
||||||
emit logChanged(logBuffer);
|
emit logChanged(logBuffer);
|
||||||
runTask();
|
runTask();
|
||||||
emit finished();
|
|
||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
|
emit finished();
|
||||||
|
|
||||||
runningMutex.unlock();
|
runningMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +74,14 @@ AsyncTaskManager::~AsyncTaskManager()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsyncTaskManager::start(AsyncTask *task)
|
void AsyncTaskManager::start(AsyncTask::Ptr task)
|
||||||
{
|
{
|
||||||
|
tasks.append(task);
|
||||||
task->prepareRun();
|
task->prepareRun();
|
||||||
threadPool->start(task);
|
|
||||||
|
QWeakPointer<AsyncTask> weakPtr = task;
|
||||||
|
connect(task.data(), &AsyncTask::finished, this, [this, weakPtr]() {
|
||||||
|
tasks.removeOne(weakPtr);
|
||||||
|
});
|
||||||
|
threadPool->start(task.data());
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
#include <QThreadPool>
|
#include <QThreadPool>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QElapsedTimer>
|
#include <QElapsedTimer>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
#include <QList>
|
||||||
|
|
||||||
class AsyncTaskManager;
|
class AsyncTaskManager;
|
||||||
|
|
||||||
@ -16,7 +18,9 @@ class AsyncTask : public QObject, public QRunnable
|
|||||||
friend class AsyncTaskManager;
|
friend class AsyncTaskManager;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AsyncTask(QObject *parent = nullptr);
|
using Ptr = QSharedPointer<AsyncTask>;
|
||||||
|
|
||||||
|
AsyncTask();
|
||||||
~AsyncTask();
|
~AsyncTask();
|
||||||
|
|
||||||
void run() override final;
|
void run() override final;
|
||||||
@ -59,12 +63,13 @@ class AsyncTaskManager : public QObject
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QThreadPool *threadPool;
|
QThreadPool *threadPool;
|
||||||
|
QList<AsyncTask::Ptr> tasks;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AsyncTaskManager(QObject *parent = nullptr);
|
explicit AsyncTaskManager(QObject *parent = nullptr);
|
||||||
~AsyncTaskManager();
|
~AsyncTaskManager();
|
||||||
|
|
||||||
void start(AsyncTask *task);
|
void start(AsyncTask::Ptr task);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user