Add AsyncTaskDialog

This commit is contained in:
Florian Märkl 2018-05-26 20:49:57 +02:00
parent 68e57f536f
commit 6dd71958e2
9 changed files with 175 additions and 10 deletions

View File

@ -47,7 +47,7 @@ void AnalTask::runTask()
binLoadAddr = Core()->math(ui->entry_loadOffset->text()); binLoadAddr = Core()->math(ui->entry_loadOffset->text());
ut64 mapAddr = Core()->math(ui->entry_mapOffset->text()); // Where to map the file once loaded (-m) 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) // Set the CPU details (handle auto)
Core()->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(), Core()->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(),
@ -106,7 +106,9 @@ void AnalTask::runTask()
Core()->setConfig("prj.simple", true); Core()->setConfig("prj.simple", true);
// Start analysis // Start analysis
emit updateProgress(tr("Analysis in progress...")); log(tr("Analysis in progress...\n"));
Core()->analyze(this->level, this->advanced); Core()->analyze(this->level, this->advanced);
emit updateProgress(tr("Analysis complete!"));
log(tr("Analysis complete!"));
} }

View File

@ -20,10 +20,9 @@ public:
void interruptAndWait(); void interruptAndWait();
protected: protected:
void runTask(); void runTask() override;
signals: signals:
void updateProgress(QString str);
void openFileFailed(); void openFileFailed();
private: private:

View File

@ -161,7 +161,8 @@ SOURCES += \
utils/JsonModel.cpp \ utils/JsonModel.cpp \
dialogs/VersionInfoDialog.cpp \ dialogs/VersionInfoDialog.cpp \
widgets/ZignaturesWidget.cpp \ widgets/ZignaturesWidget.cpp \
utils/AsyncTask.cpp utils/AsyncTask.cpp \
dialogs/AsyncTaskDialog.cpp
HEADERS += \ HEADERS += \
Cutter.h \ Cutter.h \
@ -237,7 +238,8 @@ HEADERS += \
utils/JsonModel.h \ utils/JsonModel.h \
dialogs/VersionInfoDialog.h \ dialogs/VersionInfoDialog.h \
widgets/ZignaturesWidget.h \ widgets/ZignaturesWidget.h \
utils/AsyncTask.h utils/AsyncTask.h \
dialogs/AsyncTaskDialog.h
FORMS += \ FORMS += \
dialogs/AboutDialog.ui \ dialogs/AboutDialog.ui \
@ -280,7 +282,8 @@ FORMS += \
widgets/JupyterWidget.ui \ widgets/JupyterWidget.ui \
dialogs/R2PluginsDialog.ui \ dialogs/R2PluginsDialog.ui \
dialogs/VersionInfoDialog.ui \ dialogs/VersionInfoDialog.ui \
widgets/ZignaturesWidget.ui widgets/ZignaturesWidget.ui \
dialogs/AsyncTaskDialog.ui
RESOURCES += \ RESOURCES += \
resources.qrc \ resources.qrc \

View File

@ -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());
}

View File

@ -0,0 +1,30 @@
#ifndef ASYNCTASKDIALOG_H
#define ASYNCTASKDIALOG_H
#include <QDialog>
#include <memory>
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::AsyncTaskDialog> ui;
AsyncTask *task;
};
#endif //ASYNCTASKDIALOG_H

View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AsyncTaskDialog</class>
<widget class="QDialog" name="AsyncTaskDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="timeLabel">
<property name="text">
<string>Time</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="maximum">
<number>0</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="logTextEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AsyncTaskDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AsyncTaskDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -2,6 +2,7 @@
#include "ui_OptionsDialog.h" #include "ui_OptionsDialog.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "dialogs/NewFileDialog.h" #include "dialogs/NewFileDialog.h"
#include "dialogs/AsyncTaskDialog.h"
#include "utils/Helpers.h" #include "utils/Helpers.h"
#include <QSettings> #include <QSettings>
@ -169,11 +170,12 @@ void OptionsDialog::setupAndStartAnalysis(int level, QList<QString> advanced)
updateProgressTimer(); updateProgressTimer();
connect(&analTimer, SIGNAL(timeout()), this, SLOT(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); connect(&analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
analTask.setSettings(main, level, advanced); analTask.setSettings(main, level, advanced);
Core()->getAsyncTaskManager()->start(&analTask); Core()->getAsyncTaskManager()->start(&analTask);
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(&analTask, main);
taskDialog->show();
} }
void OptionsDialog::updateProgressTimer() void OptionsDialog::updateProgressTimer()

View File

@ -44,12 +44,20 @@ void AsyncTask::run()
{ {
runningMutex.lock(); runningMutex.lock();
running = true; running = true;
logBuffer = "";
emit logChanged(logBuffer);
runTask(); runTask();
emit finished(); emit finished();
running = false; running = false;
runningMutex.unlock(); runningMutex.unlock();
} }
void AsyncTask::log(QString s)
{
logBuffer += s;
emit logChanged(logBuffer);
}
AsyncTaskManager::AsyncTaskManager(QObject *parent) AsyncTaskManager::AsyncTaskManager(QObject *parent)
: QObject(parent) : QObject(parent)
{ {

View File

@ -26,17 +26,24 @@ public:
bool isInterrupted() { return interrupted; } bool isInterrupted() { return interrupted; }
bool isRunning() { return running; } bool isRunning() { return running; }
const QString &getLog() { return logBuffer; }
protected: protected:
virtual void runTask() =0; virtual void runTask() =0;
void log(QString s);
signals: signals:
void finished(); void finished();
void logChanged(const QString &log);
private: private:
bool running; bool running;
bool interrupted; bool interrupted;
QMutex runningMutex; QMutex runningMutex;
QString logBuffer;
void prepareRun(); void prepareRun();
}; };