cutter/src/dialogs/AsyncTaskDialog.cpp

84 lines
1.8 KiB
C++
Raw Normal View History

2018-05-26 18:49:57 +00:00
#include "AsyncTaskDialog.h"
#include "utils/AsyncTask.h"
#include "ui_AsyncTaskDialog.h"
2018-05-27 14:51:01 +00:00
2018-05-26 18:49:57 +00:00
AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent)
: QDialog(parent),
ui(new Ui::AsyncTaskDialog),
task(task)
{
ui->setupUi(this);
2018-05-27 14:51:01 +00:00
QString title = task->getTitle();
if (!title.isNull()) {
setWindowTitle(title);
}
2018-05-26 18:49:57 +00:00
connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog);
connect(task, &AsyncTask::finished, this, [this]() {
this->task = nullptr;
close();
});
2018-05-26 18:49:57 +00:00
2018-05-27 19:57:37 +00:00
updateLog(task->getLog());
2018-05-27 14:51:01 +00:00
connect(&timer, SIGNAL(timeout()), this, SLOT(updateProgressTimer()));
timer.setInterval(1000);
timer.setSingleShot(false);
timer.start();
updateProgressTimer();
2018-05-26 18:49:57 +00:00
}
AsyncTaskDialog::~AsyncTaskDialog()
{
}
2018-05-27 19:57:37 +00:00
void AsyncTaskDialog::updateLog(const QString &log)
2018-05-26 18:49:57 +00:00
{
2018-05-27 19:57:37 +00:00
ui->logTextEdit->setPlainText(log);
2018-05-26 18:49:57 +00:00
}
2018-05-27 14:51:01 +00:00
void AsyncTaskDialog::updateProgressTimer()
{
if(!task) {
return;
}
2018-05-27 14:51:01 +00:00
int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000;
int minutesElapsed = secondsElapsed / 60;
int hoursElapsed = minutesElapsed / 60;
QString label = tr("Running for") + " ";
if (hoursElapsed) {
label += tr("%n hour", "%n hours", hoursElapsed);
label += " ";
}
if (minutesElapsed) {
label += tr("%n minute", "%n minutes", minutesElapsed % 60);
label += " ";
}
label += tr("%n seconds", "%n second", secondsElapsed % 60);
ui->timeLabel->setText(label);
}
void AsyncTaskDialog::closeEvent(QCloseEvent *event)
{
if (interruptOnClose && task) {
task->interrupt();
task->wait();
}
QWidget::closeEvent(event);
}
void AsyncTaskDialog::reject()
{
if (task) {
task->interrupt();
}
}