Skip to content

Commit

Permalink
refactor: propogate only only one StepProgress at a time
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Apr 3, 2023
1 parent 222554d commit d4297b7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 73 deletions.
99 changes: 54 additions & 45 deletions launcher/tasks/ConcurrentTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
*/
#include "ConcurrentTask.h"

#include <QDebug>
#include <QCoreApplication>
#include <QDebug>
#include "tasks/Task.h"

ConcurrentTask::ConcurrentTask(QObject* parent, QString task_name, int max_concurrent)
: Task(parent), m_name(task_name), m_total_max_size(max_concurrent)
{ setObjectName(task_name); }
{
setObjectName(task_name);
}

ConcurrentTask::~ConcurrentTask()
{
Expand Down Expand Up @@ -126,18 +128,17 @@ void ConcurrentTask::startNext()

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::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::stepProgress, this, [this, next](TaskStepProgressList tp){ subTaskStepProgress(next, tp); });
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::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_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();
updateStepProgress();
Expand All @@ -158,10 +159,12 @@ void ConcurrentTask::subTaskSucceeded(Task::Ptr task)
m_succeeded.insert(task.get(), task);

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);

emit stepProgress(*task_progress.get());
updateState();
updateStepProgress();
startNext();
Expand All @@ -173,79 +176,85 @@ void ConcurrentTask::subTaskFailed(Task::Ptr task, const QString& msg)
m_failed.insert(task.get(), task);

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);

emit stepProgress(*task_progress.get());
updateState();
updateStepProgress();
startNext();
}

void ConcurrentTask::subTaskStatus(Task::Ptr task, const QString& msg)
{
auto taskProgress = m_task_progress.value(task->getUid());
taskProgress->status = msg;
taskProgress->state = TaskStepState::Running;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->status = msg;
task_progress->state = TaskStepState::Running;

emit stepProgress(*task_progress.get());
updateStepProgress();
}

void ConcurrentTask::subTaskDetails(Task::Ptr task, const QString& msg)
{
auto taskProgress = m_task_progress.value(task->getUid());
taskProgress->details = msg;
taskProgress->state = TaskStepState::Running;
auto task_progress = m_task_progress.value(task->getUid());
task_progress->details = msg;
task_progress->state = TaskStepState::Running;

emit stepProgress(*task_progress.get());
updateStepProgress();
}

void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 total)
{
auto taskProgress = m_task_progress.value(task->getUid());

taskProgress->current = current;
taskProgress->total = total;
taskProgress->state = TaskStepState::Running;
auto task_progress = m_task_progress.value(task->getUid());

task_progress->current = current;
task_progress->total = total;
task_progress->state = TaskStepState::Running;

emit stepProgress(*task_progress.get());
updateStepProgress();
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(progress->uid)) {
m_task_progress.insert(progress->uid, progress);
} else {
auto tp = m_task_progress.value(progress->uid);
tp->current = progress->current;
tp->total = progress->total;
tp->status = progress->status;
tp->details = progress->details;
}
if (!m_task_progress.contains(task_progress.uid)) {
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
} else {
auto tp = m_task_progress.value(task_progress.uid);
tp->current = task_progress.current;
tp->total = task_progress.total;
tp->status = task_progress.status;
tp->details = task_progress.details;
}

updateStepProgress();

emit stepProgress(task_progress);
updateStepProgress();
}

void ConcurrentTask::updateStepProgress()
{
qint64 current = 0, total = 0;
for ( auto taskProgress : m_task_progress ) {
current += taskProgress->current;
total += taskProgress->total;
}

m_stepProgress = current;
m_stepTotalProgress = total;
emit stepProgress(m_task_progress.values());
qint64 current = 0, total = 0;
for (auto taskProgress : m_task_progress) {
current += taskProgress->current;
total += taskProgress->total;
}

m_stepProgress = current;
m_stepTotalProgress = total;
}

void ConcurrentTask::updateState()
{
if (totalSize() > 1) {
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 {
setProgress(m_stepProgress, m_stepTotalProgress);
QString status = tr("Please wait ...");
Expand Down
2 changes: 1 addition & 1 deletion launcher/tasks/ConcurrentTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected
void subTaskStatus(Task::Ptr task, const QString &msg);
void subTaskDetails(Task::Ptr task, const QString &msg);
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:
// NOTE: This is not thread-safe.
Expand Down
2 changes: 1 addition & 1 deletion launcher/tasks/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void Task::emitSucceeded()
emit finished();
}

void Task::propogateStepProgress(TaskStepProgressList task_progress)
void Task::propogateStepProgress(TaskStepProgress task_progress)
{
emit stepProgress(task_progress);
}
Expand Down
9 changes: 4 additions & 5 deletions launcher/tasks/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ enum class TaskStepState {
Waiting,
Running,
Failed,
Succeeded,
Finished
Succeeded
};

Q_DECLARE_METATYPE(TaskStepState)
Expand All @@ -61,7 +60,7 @@ struct TaskStepProgress {
QString status = "";
QString details = "";
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)
Expand Down Expand Up @@ -127,7 +126,7 @@ class Task : public QObject, public QRunnable {
void failed(QString reason);
void status(QString status);
void details(QString details);
void stepProgress(TaskStepProgressList task_progress); //
void stepProgress(TaskStepProgress task_progress); //

/** Emitted when the canAbort() status has changed.
*/
Expand All @@ -150,7 +149,7 @@ class Task : public QObject, public QRunnable {
virtual void emitAborted();
virtual void emitFailed(QString reason = "");

virtual void propogateStepProgress(TaskStepProgressList task_progress);
virtual void propogateStepProgress(TaskStepProgress task_progress);

public slots:
void setStatus(const QString& status);
Expand Down
38 changes: 18 additions & 20 deletions launcher/ui/dialogs/ProgressDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@


// 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
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
// for getting the best precision out of the qt progress bar
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)
{
int int_max = std::numeric_limits<int>::max();
Expand Down Expand Up @@ -213,36 +213,34 @@ void ProgressDialog::addTaskProgress(TaskStepProgress* progress)
ui->taskProgressLayout->addWidget(task_bar);
}

void ProgressDialog::changeStepProgress(TaskStepProgressList task_progress)
void ProgressDialog::changeStepProgress(TaskStepProgress task_progress)
{
m_is_multi_step = true;
if(ui->taskProgressScrollArea->isHidden()) {
ui->taskProgressScrollArea->setHidden(false);
updateSize();
}

for (auto tp : task_progress) {
if (!taskProgress.contains(tp->uid))
addTaskProgress(tp.get());
auto task_bar = taskProgress.value(tp->uid);
if (!taskProgress.contains(task_progress.uid))
addTaskProgress(&task_progress);
auto task_bar = taskProgress.value(task_progress.uid);


auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(tp->current, tp->total, 0);
if (tp->total <= 0) {
task_bar->setRange(0, 0);
} else {
task_bar->setRange(0, mapped_total);
}

task_bar->setValue(mapped_current);
task_bar->setStatus(tp->status);
task_bar->setDetails(tp->details);
auto const [mapped_current, mapped_total] = map_int_zero_max<qint64>(task_progress.current, task_progress.total, 0);
if (task_progress.total <= 0) {
task_bar->setRange(0, 0);
} else {
task_bar->setRange(0, mapped_total);
}

if (tp->isDone()) {
task_bar->setVisible(false);
}
task_bar->setValue(mapped_current);
task_bar->setStatus(task_progress.status);
task_bar->setDetails(task_progress.details);

if (task_progress.isDone()) {
task_bar->setVisible(false);
}

}

void ProgressDialog::changeProgress(qint64 current, qint64 total)
Expand Down
2 changes: 1 addition & 1 deletion launcher/ui/dialogs/ProgressDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public

void changeStatus(const QString &status);
void changeProgress(qint64 current, qint64 total);
void changeStepProgress(TaskStepProgressList task_progress);
void changeStepProgress(TaskStepProgress task_progress);


private
Expand Down

0 comments on commit d4297b7

Please sign in to comment.