From f09d32bd3d6f4b2fb69e63da40e761e0361acb3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 27 May 2018 18:28:48 +0200 Subject: [PATCH] Interrupt AsyncTask from AsyncTaskDialog --- src/dialogs/AsyncTaskDialog.cpp | 26 +++++++++++++++++++++++++- src/dialogs/AsyncTaskDialog.h | 8 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/dialogs/AsyncTaskDialog.cpp b/src/dialogs/AsyncTaskDialog.cpp index 15084b1d..87ce3c49 100644 --- a/src/dialogs/AsyncTaskDialog.cpp +++ b/src/dialogs/AsyncTaskDialog.cpp @@ -18,7 +18,10 @@ AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent) } connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog); - connect(task, &AsyncTask::finished, this, &QWidget::close); + connect(task, &AsyncTask::finished, this, [this]() { + this->task = nullptr; + close(); + }); updateLog(); @@ -41,6 +44,10 @@ void AsyncTaskDialog::updateLog() void AsyncTaskDialog::updateProgressTimer() { + if(!task) { + return; + } + int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000; int minutesElapsed = secondsElapsed / 60; int hoursElapsed = minutesElapsed / 60; @@ -57,3 +64,20 @@ void AsyncTaskDialog::updateProgressTimer() 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(); + } +} diff --git a/src/dialogs/AsyncTaskDialog.h b/src/dialogs/AsyncTaskDialog.h index 4d0fd5e4..b5cf3c5a 100644 --- a/src/dialogs/AsyncTaskDialog.h +++ b/src/dialogs/AsyncTaskDialog.h @@ -21,14 +21,22 @@ public: AsyncTaskDialog(AsyncTask *task, QWidget *parent = nullptr); ~AsyncTaskDialog(); +public slots: + void reject() override; + private slots: void updateLog(); void updateProgressTimer(); +protected: + void closeEvent(QCloseEvent *event) override; + private: std::unique_ptr ui; AsyncTask *task; QTimer timer; + + bool interruptOnClose; }; #endif //ASYNCTASKDIALOG_H