2015-05-28 18:38:29 +01:00
|
|
|
// Licensed under the Apache-2.0 license. See README.md for details.
|
|
|
|
|
|
|
|
#include "ProgressWidget.h"
|
2022-07-15 17:18:12 +01:00
|
|
|
#include <QEventLoop>
|
2015-05-28 18:38:29 +01:00
|
|
|
#include <QLabel>
|
2022-07-15 17:18:12 +01:00
|
|
|
#include <QProgressBar>
|
2015-05-28 18:38:29 +01:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include "tasks/Task.h"
|
|
|
|
|
2022-07-15 17:18:12 +01:00
|
|
|
ProgressWidget::ProgressWidget(QWidget* parent, bool show_label) : QWidget(parent)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2022-07-15 17:18:12 +01:00
|
|
|
auto* layout = new QVBoxLayout(this);
|
|
|
|
|
|
|
|
if (show_label) {
|
|
|
|
m_label = new QLabel(this);
|
|
|
|
m_label->setWordWrap(true);
|
|
|
|
layout->addWidget(m_label);
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
m_bar = new QProgressBar(this);
|
|
|
|
m_bar->setMinimum(0);
|
|
|
|
m_bar->setMaximum(100);
|
|
|
|
layout->addWidget(m_bar);
|
2022-07-15 17:18:12 +01:00
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
2022-07-15 17:18:12 +01:00
|
|
|
void ProgressWidget::reset()
|
|
|
|
{
|
|
|
|
m_bar->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressWidget::progressFormat(QString format)
|
|
|
|
{
|
|
|
|
if (format.isEmpty())
|
|
|
|
m_bar->setTextVisible(false);
|
|
|
|
else
|
|
|
|
m_bar->setFormat(format);
|
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
void ProgressWidget::watch(const Task* task)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2022-07-15 17:18:12 +01:00
|
|
|
if (!task)
|
|
|
|
return;
|
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
if (m_task)
|
2022-07-15 17:18:12 +01:00
|
|
|
disconnect(m_task, nullptr, this, nullptr);
|
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
m_task = task;
|
2022-07-15 17:18:12 +01:00
|
|
|
|
|
|
|
connect(m_task, &Task::finished, this, &ProgressWidget::handleTaskFinish);
|
|
|
|
connect(m_task, &Task::status, this, &ProgressWidget::handleTaskStatus);
|
2023-04-01 03:25:01 +01:00
|
|
|
// TODO: should we connect &Task::details
|
2022-07-15 17:18:12 +01:00
|
|
|
connect(m_task, &Task::progress, this, &ProgressWidget::handleTaskProgress);
|
|
|
|
connect(m_task, &Task::destroyed, this, &ProgressWidget::taskDestroyed);
|
|
|
|
|
2022-12-16 23:19:33 +00:00
|
|
|
if (m_task->isRunning())
|
|
|
|
show();
|
|
|
|
else
|
|
|
|
connect(m_task, &Task::started, this, &ProgressWidget::show);
|
2022-07-15 17:18:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
void ProgressWidget::start(const Task* task)
|
2022-07-15 17:18:12 +01:00
|
|
|
{
|
|
|
|
watch(task);
|
2015-05-28 18:38:29 +01:00
|
|
|
if (!m_task->isRunning())
|
2022-11-25 12:23:46 +00:00
|
|
|
QMetaObject::invokeMethod(const_cast<Task*>(m_task), "start", Qt::QueuedConnection);
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
2022-07-15 17:18:12 +01:00
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
bool ProgressWidget::exec(std::shared_ptr<Task> task)
|
|
|
|
{
|
|
|
|
QEventLoop loop;
|
2022-07-15 17:18:12 +01:00
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
connect(task.get(), &Task::finished, &loop, &QEventLoop::quit);
|
2022-07-15 17:18:12 +01:00
|
|
|
|
|
|
|
start(task.get());
|
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
if (task->isRunning())
|
|
|
|
loop.exec();
|
2022-07-15 17:18:12 +01:00
|
|
|
|
2017-06-26 00:14:32 +01:00
|
|
|
return task->wasSuccessful();
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-15 17:18:12 +01:00
|
|
|
void ProgressWidget::show()
|
|
|
|
{
|
|
|
|
setHidden(false);
|
|
|
|
}
|
|
|
|
void ProgressWidget::hide()
|
|
|
|
{
|
|
|
|
setHidden(true);
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:38:29 +01:00
|
|
|
void ProgressWidget::handleTaskFinish()
|
|
|
|
{
|
2022-07-15 17:18:12 +01:00
|
|
|
if (!m_task->wasSuccessful() && m_label)
|
2015-05-28 18:38:29 +01:00
|
|
|
m_label->setText(m_task->failReason());
|
2022-07-15 17:18:12 +01:00
|
|
|
|
|
|
|
if (m_hide_if_inactive)
|
|
|
|
hide();
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
2022-07-15 17:18:12 +01:00
|
|
|
void ProgressWidget::handleTaskStatus(const QString& status)
|
2015-05-28 18:38:29 +01:00
|
|
|
{
|
2022-07-15 17:18:12 +01:00
|
|
|
if (m_label)
|
|
|
|
m_label->setText(status);
|
2015-05-28 18:38:29 +01:00
|
|
|
}
|
|
|
|
void ProgressWidget::handleTaskProgress(qint64 current, qint64 total)
|
|
|
|
{
|
|
|
|
m_bar->setMaximum(total);
|
|
|
|
m_bar->setValue(current);
|
|
|
|
}
|
|
|
|
void ProgressWidget::taskDestroyed()
|
|
|
|
{
|
|
|
|
m_task = nullptr;
|
|
|
|
}
|