taskdialog: improved signalling

Ensures task errors are picked up and objects are deleted at the most
appropriate times.
This commit is contained in:
itsmattkc
2020-03-28 12:13:51 +11:00
parent b40dd761f5
commit 4dcc672b23
2 changed files with 28 additions and 24 deletions
+24 -20
View File
@@ -13,25 +13,26 @@ TaskDialog::TaskDialog(Task* task, const QString& title, QWidget *parent) :
// Connect the save manager progress signal to the progress bar update on the dialog
connect(task_, &Task::ProgressChanged, this, &TaskDialog::SetProgress, Qt::QueuedConnection);
// Connect cancel signal (must be a direct connection or it'll be queued after the save is already finished)
connect(this, &TaskDialog::Cancelled, this, &TaskDialog::reject, Qt::DirectConnection);
// Connect error reporting
connect(task_, &Task::Failed, this, &TaskDialog::TaskFailed, Qt::QueuedConnection);
// Connect cancel signal (must be a direct connection or it'll be queued after the task has already finished)
connect(this, &TaskDialog::Cancelled, task_, &Task::Cancel, Qt::DirectConnection);
// Connect cleanup functions (ensure everything new'd in this function is deleteLater'd)
connect(task_, &Task::Finished, this, &TaskDialog::accept, Qt::QueuedConnection);
connect(task_, &Task::Finished, this, &TaskDialog::deleteLater, Qt::QueuedConnection);
connect(task_, &Task::Finished, task_, &Task::deleteLater, Qt::QueuedConnection);
connect(task_, &Task::Finished, this, &TaskDialog::close, Qt::QueuedConnection);
// When task is finished, signal thread to quit
connect(task_, &Task::Finished, thread_, &QThread::quit, Qt::QueuedConnection);
// When thread has quit, delete both task and thread
connect(thread_, &QThread::finished, task_, &Task::deleteLater, Qt::QueuedConnection);
connect(thread_, &QThread::finished, thread_, &QThread::deleteLater, Qt::QueuedConnection);
}
void TaskDialog::open()
void TaskDialog::showEvent(QShowEvent *e)
{
ProgressDialog::open();
if (thread_->isRunning()) {
// Task has already started, no need to do anymore
return;
}
QDialog::showEvent(e);
// Create a separate thread to run this task in
thread_->start();
@@ -43,21 +44,24 @@ void TaskDialog::open()
QMetaObject::invokeMethod(task_, "Start", Qt::QueuedConnection);
}
void TaskDialog::accept()
void TaskDialog::closeEvent(QCloseEvent *e)
{
if (task_failed_) {
// Show error if the task failed
if (!task_->IsCancelled() && task_failed_) {
QMessageBox::critical(this,
tr("Task Error"),
tr("Error"),
task_error_,
QMessageBox::Ok);
}
ProgressDialog::accept();
}
void TaskDialog::reject()
{
// Cancel task if it is running
task_->Cancel();
// Standard close function
QDialog::closeEvent(e);
// Clean up this dialog (FIXME: Is this necessary?)
deleteLater();
}
void TaskDialog::TaskFailed(const QString &s)
+4 -4
View File
@@ -6,14 +6,14 @@
class TaskDialog : public ProgressDialog
{
Q_OBJECT
public:
TaskDialog(Task *task, const QString &title, QWidget* parent = nullptr);
public slots:
virtual void open() override;
protected:
virtual void showEvent(QShowEvent* e) override;
virtual void accept() override;
virtual void reject() override;
virtual void closeEvent(QCloseEvent* e) override;
private:
Task* task_;