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