From 6dd71958e206720bf3220e515d0b49cee247ec09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 26 May 2018 20:49:57 +0200 Subject: [PATCH] Add AsyncTaskDialog --- src/AnalTask.cpp | 8 +-- src/AnalTask.h | 3 +- src/Cutter.pro | 9 ++-- src/dialogs/AsyncTaskDialog.cpp | 26 ++++++++++ src/dialogs/AsyncTaskDialog.h | 30 +++++++++++ src/dialogs/AsyncTaskDialog.ui | 88 +++++++++++++++++++++++++++++++++ src/dialogs/OptionsDialog.cpp | 6 ++- src/utils/AsyncTask.cpp | 8 +++ src/utils/AsyncTask.h | 7 +++ 9 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 src/dialogs/AsyncTaskDialog.cpp create mode 100644 src/dialogs/AsyncTaskDialog.h create mode 100644 src/dialogs/AsyncTaskDialog.ui diff --git a/src/AnalTask.cpp b/src/AnalTask.cpp index af131345..1eccf96e 100644 --- a/src/AnalTask.cpp +++ b/src/AnalTask.cpp @@ -47,7 +47,7 @@ void AnalTask::runTask() binLoadAddr = Core()->math(ui->entry_loadOffset->text()); ut64 mapAddr = Core()->math(ui->entry_mapOffset->text()); // Where to map the file once loaded (-m) - emit updateProgress(tr("Loading binary...")); + log(tr("Loading Binary...\n")); // Set the CPU details (handle auto) Core()->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(), @@ -106,7 +106,9 @@ void AnalTask::runTask() Core()->setConfig("prj.simple", true); // Start analysis - emit updateProgress(tr("Analysis in progress...")); + log(tr("Analysis in progress...\n")); + Core()->analyze(this->level, this->advanced); - emit updateProgress(tr("Analysis complete!")); + + log(tr("Analysis complete!")); } diff --git a/src/AnalTask.h b/src/AnalTask.h index 4a308f8e..cc46b837 100644 --- a/src/AnalTask.h +++ b/src/AnalTask.h @@ -20,10 +20,9 @@ public: void interruptAndWait(); protected: - void runTask(); + void runTask() override; signals: - void updateProgress(QString str); void openFileFailed(); private: diff --git a/src/Cutter.pro b/src/Cutter.pro index ef0edf3e..a00ea74d 100644 --- a/src/Cutter.pro +++ b/src/Cutter.pro @@ -161,7 +161,8 @@ SOURCES += \ utils/JsonModel.cpp \ dialogs/VersionInfoDialog.cpp \ widgets/ZignaturesWidget.cpp \ - utils/AsyncTask.cpp + utils/AsyncTask.cpp \ + dialogs/AsyncTaskDialog.cpp HEADERS += \ Cutter.h \ @@ -237,7 +238,8 @@ HEADERS += \ utils/JsonModel.h \ dialogs/VersionInfoDialog.h \ widgets/ZignaturesWidget.h \ - utils/AsyncTask.h + utils/AsyncTask.h \ + dialogs/AsyncTaskDialog.h FORMS += \ dialogs/AboutDialog.ui \ @@ -280,7 +282,8 @@ FORMS += \ widgets/JupyterWidget.ui \ dialogs/R2PluginsDialog.ui \ dialogs/VersionInfoDialog.ui \ - widgets/ZignaturesWidget.ui + widgets/ZignaturesWidget.ui \ + dialogs/AsyncTaskDialog.ui RESOURCES += \ resources.qrc \ diff --git a/src/dialogs/AsyncTaskDialog.cpp b/src/dialogs/AsyncTaskDialog.cpp new file mode 100644 index 00000000..d26c6bda --- /dev/null +++ b/src/dialogs/AsyncTaskDialog.cpp @@ -0,0 +1,26 @@ + +#include "AsyncTaskDialog.h" +#include "utils/AsyncTask.h" + +#include "ui_AsyncTaskDialog.h" + +AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent) + : QDialog(parent), + ui(new Ui::AsyncTaskDialog), + task(task) +{ + ui->setupUi(this); + + connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog); + + updateLog(); +} + +AsyncTaskDialog::~AsyncTaskDialog() +{ +} + +void AsyncTaskDialog::updateLog() +{ + ui->logTextEdit->setPlainText(task->getLog()); +} diff --git a/src/dialogs/AsyncTaskDialog.h b/src/dialogs/AsyncTaskDialog.h new file mode 100644 index 00000000..ebc2488d --- /dev/null +++ b/src/dialogs/AsyncTaskDialog.h @@ -0,0 +1,30 @@ + +#ifndef ASYNCTASKDIALOG_H +#define ASYNCTASKDIALOG_H + +#include +#include + +namespace Ui { +class AsyncTaskDialog; +} + +class AsyncTask; + +class AsyncTaskDialog : public QDialog +{ + Q_OBJECT + +public: + AsyncTaskDialog(AsyncTask *task, QWidget *parent = nullptr); + ~AsyncTaskDialog(); + +private slots: + void updateLog(); + +private: + std::unique_ptr ui; + AsyncTask *task; +}; + +#endif //ASYNCTASKDIALOG_H diff --git a/src/dialogs/AsyncTaskDialog.ui b/src/dialogs/AsyncTaskDialog.ui new file mode 100644 index 00000000..64d37b6e --- /dev/null +++ b/src/dialogs/AsyncTaskDialog.ui @@ -0,0 +1,88 @@ + + + AsyncTaskDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + Time + + + + + + + 0 + + + Qt::Horizontal + + + + + + + true + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel + + + + + + + + + buttonBox + accepted() + AsyncTaskDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + AsyncTaskDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/dialogs/OptionsDialog.cpp b/src/dialogs/OptionsDialog.cpp index 6837177e..907cfedc 100644 --- a/src/dialogs/OptionsDialog.cpp +++ b/src/dialogs/OptionsDialog.cpp @@ -2,6 +2,7 @@ #include "ui_OptionsDialog.h" #include "MainWindow.h" #include "dialogs/NewFileDialog.h" +#include "dialogs/AsyncTaskDialog.h" #include "utils/Helpers.h" #include @@ -169,11 +170,12 @@ void OptionsDialog::setupAndStartAnalysis(int level, QList advanced) updateProgressTimer(); connect(&analTimer, SIGNAL(timeout()), this, SLOT(updateProgressTimer())); - // Threads stuff, connect signal/slot - connect(&analTask, &AnalTask::updateProgress, this, &OptionsDialog::updateProgress); connect(&analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed); analTask.setSettings(main, level, advanced); Core()->getAsyncTaskManager()->start(&analTask); + + AsyncTaskDialog *taskDialog = new AsyncTaskDialog(&analTask, main); + taskDialog->show(); } void OptionsDialog::updateProgressTimer() diff --git a/src/utils/AsyncTask.cpp b/src/utils/AsyncTask.cpp index 436f6fd8..d0ae6068 100644 --- a/src/utils/AsyncTask.cpp +++ b/src/utils/AsyncTask.cpp @@ -44,12 +44,20 @@ void AsyncTask::run() { runningMutex.lock(); running = true; + logBuffer = ""; + emit logChanged(logBuffer); runTask(); emit finished(); running = false; runningMutex.unlock(); } +void AsyncTask::log(QString s) +{ + logBuffer += s; + emit logChanged(logBuffer); +} + AsyncTaskManager::AsyncTaskManager(QObject *parent) : QObject(parent) { diff --git a/src/utils/AsyncTask.h b/src/utils/AsyncTask.h index f58dd350..6a4c6708 100644 --- a/src/utils/AsyncTask.h +++ b/src/utils/AsyncTask.h @@ -26,17 +26,24 @@ public: bool isInterrupted() { return interrupted; } bool isRunning() { return running; } + const QString &getLog() { return logBuffer; } + protected: virtual void runTask() =0; + void log(QString s); + signals: void finished(); + void logChanged(const QString &log); private: bool running; bool interrupted; QMutex runningMutex; + QString logBuffer; + void prepareRun(); };