taskdialog: improved signalling
Ensures task errors are picked up and objects are deleted at the most appropriate times.
This commit is contained in:
+24
-20
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user