taskdialog: complete implementation of TaskDialog

This commit is contained in:
itsmattkc
2020-02-17 02:14:11 +11:00
parent 705b7f3d86
commit 73ba2f4c96
3 changed files with 120 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
# Olive - Non-Linear Video Editor
# Copyright (C) 2019 Olive Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(OLIVE_SOURCES
${OLIVE_SOURCES}
dialog/task/task.h
dialog/task/task.cpp
PARENT_SCOPE
)
+67
View File
@@ -0,0 +1,67 @@
#include "task.h"
#include <QMessageBox>
#include <QThread>
TaskDialog::TaskDialog(Task* task, const QString& title, QWidget *parent) :
ProgressDialog(task->GetTitle(), title, parent),
task_(task),
task_failed_(false)
{
thread_ = new QThread();
// 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 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, thread_, &QThread::quit, Qt::QueuedConnection);
connect(task_, &Task::Finished, thread_, &QThread::deleteLater, Qt::QueuedConnection);
}
void TaskDialog::open()
{
ProgressDialog::open();
if (thread_->isRunning()) {
// Task has already started, no need to do anymore
return;
}
// Create a separate thread to run this task in
thread_->start(QThread::LowPriority);
// Move the task to this thread
task_->moveToThread(thread_);
// Start the task
QMetaObject::invokeMethod(task_, "Start", Qt::QueuedConnection);
}
void TaskDialog::accept()
{
if (task_failed_) {
QMessageBox::critical(this,
tr("Task Error"),
task_error_,
QMessageBox::Ok);
}
ProgressDialog::accept();
}
void TaskDialog::reject()
{
task_->Cancel();
}
void TaskDialog::TaskFailed(const QString &s)
{
task_failed_ = true;
task_error_ = s;
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef TASKDIALOG_H
#define TASKDIALOG_H
#include "dialog/progress/progress.h"
#include "task/task.h"
class TaskDialog : public ProgressDialog
{
public:
TaskDialog(Task *task, const QString &title, QWidget* parent = nullptr);
public slots:
virtual void open() override;
virtual void accept() override;
virtual void reject() override;
private:
Task* task_;
QThread* thread_;
bool task_failed_;
QString task_error_;
private slots:
void TaskFailed(const QString& s);
};
#endif // TASKDIALOG_H