refactor: propogate only only one StepProgress at a time
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
This commit is contained in:
parent
fe36471b8d
commit
0fb6a2836b
@ -35,13 +35,15 @@
|
|||||||
*/
|
*/
|
||||||
#include "ConcurrentTask.h"
|
#include "ConcurrentTask.h"
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QDebug>
|
||||||
#include "tasks/Task.h"
|
#include "tasks/Task.h"
|
||||||
|
|
||||||
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
|
ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
|
||||||
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
|
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
|
||||||
{ setObjectName(task_name); }
|
{
|
||||||
|
setObjectName(task_name);
|
||||||
|
}
|
||||||
|
|
||||||
ConcurrentTask::~ConcurrentTask()
|
ConcurrentTask::~ConcurrentTask()
|
||||||
{
|
{
|
||||||
@ -126,18 +128,17 @@ void ConcurrentTask::startNext()
|
|||||||
|
|
||||||
Task::Ptr next = m_queue.dequeue();
|
Task::Ptr next = m_queue.dequeue();
|
||||||
|
|
||||||
connect(next.get(), &Task::succeeded, this, [this, next](){ subTaskSucceeded(next); });
|
connect(next.get(), &Task::succeeded, this, [this, next]() { subTaskSucceeded(next); });
|
||||||
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
|
connect(next.get(), &Task::failed, this, [this, next](QString msg) { subTaskFailed(next, msg); });
|
||||||
|
|
||||||
connect(next.get(), &Task::status, this, [this, next](QString msg){ subTaskStatus(next, msg); });
|
connect(next.get(), &Task::status, this, [this, next](QString msg) { subTaskStatus(next, msg); });
|
||||||
connect(next.get(), &Task::details, this, [this, next](QString msg){ subTaskDetails(next, msg); });
|
connect(next.get(), &Task::details, this, [this, next](QString msg) { subTaskDetails(next, msg); });
|
||||||
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgressList tp){ subTaskStepProgress(next, tp); });
|
connect(next.get(), &Task::stepProgress, this, [this, next](TaskStepProgress tp) { subTaskStepProgress(next, tp); });
|
||||||
|
|
||||||
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total){ subTaskProgress(next, current, total); });
|
connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });
|
||||||
|
|
||||||
m_doing.insert(next.get(), next);
|
m_doing.insert(next.get(), next);
|
||||||
m_task_progress.insert(next->getUid(), std::make_shared<TaskStepProgress>(TaskStepProgress({next->getUid()})));
|
m_task_progress.insert(next->getUid(), std::make_shared<TaskStepProgress>(TaskStepProgress({ next->getUid() })));
|
||||||
|
|
||||||
|
|
||||||
updateState();
|
updateState();
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
@ -158,10 +159,12 @@ void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
|
|||||||
m_succeeded.insert(task.get(), task);
|
m_succeeded.insert(task.get(), task);
|
||||||
|
|
||||||
m_doing.remove(task.get());
|
m_doing.remove(task.get());
|
||||||
m_task_progress.value(task->getUid())->state = TaskStepState::Succeeded;
|
auto task_progress = m_task_progress.value(task->getUid());
|
||||||
|
task_progress->state = TaskStepState::Succeeded;
|
||||||
|
|
||||||
disconnect(task.get(), 0, this, 0);
|
disconnect(task.get(), 0, this, 0);
|
||||||
|
|
||||||
|
emit stepProgress(*task_progress.get());
|
||||||
updateState();
|
updateState();
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
startNext();
|
startNext();
|
||||||
@ -173,10 +176,13 @@ void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
|
|||||||
m_failed.insert(task.get(), task);
|
m_failed.insert(task.get(), task);
|
||||||
|
|
||||||
m_doing.remove(task.get());
|
m_doing.remove(task.get());
|
||||||
m_task_progress.value(task->getUid())->state = TaskStepState::Failed;
|
|
||||||
|
auto task_progress = m_task_progress.value(task->getUid());
|
||||||
|
task_progress->state = TaskStepState::Failed;
|
||||||
|
|
||||||
disconnect(task.get(), 0, this, 0);
|
disconnect(task.get(), 0, this, 0);
|
||||||
|
|
||||||
|
emit stepProgress(*task_progress.get());
|
||||||
updateState();
|
updateState();
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
startNext();
|
startNext();
|
||||||
@ -184,68 +190,71 @@ void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
|
|||||||
|
|
||||||
void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
|
void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
|
||||||
{
|
{
|
||||||
auto taskProgress = m_task_progress.value(task->getUid());
|
auto task_progress = m_task_progress.value(task->getUid());
|
||||||
taskProgress->status = msg;
|
task_progress->status = msg;
|
||||||
taskProgress->state = TaskStepState::Running;
|
task_progress->state = TaskStepState::Running;
|
||||||
|
|
||||||
|
emit stepProgress(*task_progress.get());
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConcurrentTask::subTaskDetails(Task::Ptr task, const QString& msg)
|
void ConcurrentTask::subTaskDetails(Task::Ptr task, const QString& msg)
|
||||||
{
|
{
|
||||||
auto taskProgress = m_task_progress.value(task->getUid());
|
auto task_progress = m_task_progress.value(task->getUid());
|
||||||
taskProgress->details = msg;
|
task_progress->details = msg;
|
||||||
taskProgress->state = TaskStepState::Running;
|
task_progress->state = TaskStepState::Running;
|
||||||
|
|
||||||
|
emit stepProgress(*task_progress.get());
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 total)
|
void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 total)
|
||||||
{
|
{
|
||||||
auto taskProgress = m_task_progress.value(task->getUid());
|
auto task_progress = m_task_progress.value(task->getUid());
|
||||||
|
|
||||||
taskProgress->current = current;
|
task_progress->current = current;
|
||||||
taskProgress->total = total;
|
task_progress->total = total;
|
||||||
taskProgress->state = TaskStepState::Running;
|
task_progress->state = TaskStepState::Running;
|
||||||
|
|
||||||
|
emit stepProgress(*task_progress.get());
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
updateState();
|
updateState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgressList task_step_progress)
|
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress task_progress)
|
||||||
{
|
{
|
||||||
for (auto progress : task_step_progress) {
|
if (!m_task_progress.contains(task_progress.uid)) {
|
||||||
if (!m_task_progress.contains(progress->uid)) {
|
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
|
||||||
m_task_progress.insert(progress->uid, progress);
|
|
||||||
} else {
|
} else {
|
||||||
auto tp = m_task_progress.value(progress->uid);
|
auto tp = m_task_progress.value(task_progress.uid);
|
||||||
tp->current = progress->current;
|
tp->current = task_progress.current;
|
||||||
tp->total = progress->total;
|
tp->total = task_progress.total;
|
||||||
tp->status = progress->status;
|
tp->status = task_progress.status;
|
||||||
tp->details = progress->details;
|
tp->details = task_progress.details;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
emit stepProgress(task_progress);
|
||||||
updateStepProgress();
|
updateStepProgress();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConcurrentTask::updateStepProgress()
|
void ConcurrentTask::updateStepProgress()
|
||||||
{
|
{
|
||||||
qint64 current = 0, total = 0;
|
qint64 current = 0, total = 0;
|
||||||
for ( auto taskProgress : m_task_progress ) {
|
for (auto taskProgress : m_task_progress) {
|
||||||
current += taskProgress->current;
|
current += taskProgress->current;
|
||||||
total += taskProgress->total;
|
total += taskProgress->total;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_stepProgress = current;
|
m_stepProgress = current;
|
||||||
m_stepTotalProgress = total;
|
m_stepTotalProgress = total;
|
||||||
emit stepProgress(m_task_progress.values());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConcurrentTask::updateState()
|
void ConcurrentTask::updateState()
|
||||||
{
|
{
|
||||||
if (totalSize() > 1) {
|
if (totalSize() > 1) {
|
||||||
setProgress(m_done.count(), totalSize());
|
setProgress(m_done.count(), totalSize());
|
||||||
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)").arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
|
setStatus(tr("Executing %1 task(s) (%2 out of %3 are done)")
|
||||||
|
.arg(QString::number(m_doing.count()), QString::number(m_done.count()), QString::number(totalSize())));
|
||||||
} else {
|
} else {
|
||||||
setProgress(m_stepProgress, m_stepTotalProgress);
|
setProgress(m_stepProgress, m_stepTotalProgress);
|
||||||
QString status = tr("Please wait ...");
|
QString status = tr("Please wait ...");
|
||||||
|
@ -77,7 +77,7 @@ slots:
|
|||||||
void subTaskStatus(Task::Ptr task, const QString &msg);
|
void subTaskStatus(Task::Ptr task, const QString &msg);
|
||||||
void subTaskDetails(Task::Ptr task, const QString &msg);
|
void subTaskDetails(Task::Ptr task, const QString &msg);
|
||||||
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
void subTaskProgress(Task::Ptr task, qint64 current, qint64 total);
|
||||||
void subTaskStepProgress(Task::Ptr task, TaskStepProgressList task_step_progress);
|
void subTaskStepProgress(Task::Ptr task, TaskStepProgress task_step_progress);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// NOTE: This is not thread-safe.
|
// NOTE: This is not thread-safe.
|
||||||
|
@ -161,7 +161,7 @@ void Task::emitSucceeded()
|
|||||||
emit finished();
|
emit finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Task::propogateStepProgress(TaskStepProgressList task_progress)
|
void Task::propogateStepProgress(TaskStepProgress task_progress)
|
||||||
{
|
{
|
||||||
emit stepProgress(task_progress);
|
emit stepProgress(task_progress);
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,7 @@ enum class TaskStepState {
|
|||||||
Waiting,
|
Waiting,
|
||||||
Running,
|
Running,
|
||||||
Failed,
|
Failed,
|
||||||
Succeeded,
|
Succeeded
|
||||||
Finished
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(TaskStepState)
|
Q_DECLARE_METATYPE(TaskStepState)
|
||||||
@ -61,7 +60,7 @@ struct TaskStepProgress {
|
|||||||
QString status = "";
|
QString status = "";
|
||||||
QString details = "";
|
QString details = "";
|
||||||
TaskStepState state = TaskStepState::Waiting;
|
TaskStepState state = TaskStepState::Waiting;
|
||||||
bool isDone() { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded) || (state == TaskStepState::Finished); }
|
bool isDone() { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded); }
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(TaskStepProgress)
|
Q_DECLARE_METATYPE(TaskStepProgress)
|
||||||
@ -127,7 +126,7 @@ class Task : public QObject, public QRunnable {
|
|||||||
void failed(QString reason);
|
void failed(QString reason);
|
||||||
void status(QString status);
|
void status(QString status);
|
||||||
void details(QString details);
|
void details(QString details);
|
||||||
void stepProgress(TaskStepProgressList task_progress); //
|
void stepProgress(TaskStepProgress task_progress); //
|
||||||
|
|
||||||
/** Emitted when the canAbort() status has changed.
|
/** Emitted when the canAbort() status has changed.
|
||||||
*/
|
*/
|
||||||
@ -150,7 +149,7 @@ class Task : public QObject, public QRunnable {
|
|||||||
virtual void emitAborted();
|
virtual void emitAborted();
|
||||||
virtual void emitFailed(QString reason = "");
|
virtual void emitFailed(QString reason = "");
|
||||||
|
|
||||||
virtual void propogateStepProgress(TaskStepProgressList task_progress);
|
virtual void propogateStepProgress(TaskStepProgress task_progress);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void setStatus(const QString& status);
|
void setStatus(const QString& status);
|
||||||
|
@ -46,8 +46,8 @@
|
|||||||
|
|
||||||
|
|
||||||
// map a value in a numaric range of an arbatray type to between 0 and INT_MAX
|
// map a value in a numaric range of an arbatray type to between 0 and INT_MAX
|
||||||
// for getting the best percision out of the qt progress bar
|
// for getting the best precision out of the qt progress bar
|
||||||
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
|
template<typename T, std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
|
||||||
std::tuple<int, int> map_int_zero_max(T current, T range_max, T range_min)
|
std::tuple<int, int> map_int_zero_max(T current, T range_max, T range_min)
|
||||||
{
|
{
|
||||||
int int_max = std::numeric_limits<int>::max();
|
int int_max = std::numeric_limits<int>::max();
|
||||||
@ -213,7 +213,7 @@ void ProgressDialog::addTaskProgress(TaskStepProgress* progress)
|
|||||||
ui->taskProgressLayout->addWidget(task_bar);
|
ui->taskProgressLayout->addWidget(task_bar);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgressDialog::changeStepProgress(TaskStepProgressList task_progress)
|
void ProgressDialog::changeStepProgress(TaskStepProgress task_progress)
|
||||||
{
|
{
|
||||||
m_is_multi_step = true;
|
m_is_multi_step = true;
|
||||||
if(ui->taskProgressScrollArea->isHidden()) {
|
if(ui->taskProgressScrollArea->isHidden()) {
|
||||||
@ -221,28 +221,26 @@ void ProgressDialog::changeStepProgress(TaskStepProgressList task_progress)
|
|||||||
updateSize();
|
updateSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto tp : task_progress) {
|
if (!taskProgress.contains(task_progress.uid))
|
||||||
if (!taskProgress.contains(tp->uid))
|
addTaskProgress(&task_progress);
|
||||||
addTaskProgress(tp.get());
|
auto task_bar = taskProgress.value(task_progress.uid);
|
||||||
auto task_bar = taskProgress.value(tp->uid);
|
|
||||||
|
|
||||||
|
|
||||||
auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(tp->current, tp->total, 0);
|
auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(task_progress.current, task_progress.total, 0);
|
||||||
if (tp->total <= 0) {
|
if (task_progress.total <= 0) {
|
||||||
task_bar->setRange(0, 0);
|
task_bar->setRange(0, 0);
|
||||||
} else {
|
} else {
|
||||||
task_bar->setRange(0, mapped_total);
|
task_bar->setRange(0, mapped_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
task_bar->setValue(mapped_current);
|
task_bar->setValue(mapped_current);
|
||||||
task_bar->setStatus(tp->status);
|
task_bar->setStatus(task_progress.status);
|
||||||
task_bar->setDetails(tp->details);
|
task_bar->setDetails(task_progress.details);
|
||||||
|
|
||||||
if (tp->isDone()) {
|
if (task_progress.isDone()) {
|
||||||
task_bar->setVisible(false);
|
task_bar->setVisible(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgressDialog::changeProgress(qint64 current, qint64 total)
|
void ProgressDialog::changeProgress(qint64 current, qint64 total)
|
||||||
|
@ -80,7 +80,7 @@ slots:
|
|||||||
|
|
||||||
void changeStatus(const QString &status);
|
void changeStatus(const QString &status);
|
||||||
void changeProgress(qint64 current, qint64 total);
|
void changeProgress(qint64 current, qint64 total);
|
||||||
void changeStepProgress(TaskStepProgressList task_progress);
|
void changeStepProgress(TaskStepProgress task_progress);
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
Loading…
Reference in New Issue
Block a user