Skip to content

Commit

Permalink
refactor: Qt can handle const& in signals and slots
Browse files Browse the repository at this point in the history
While most Qt types cna use implicit data sharing
pasing our own structs means copies. const& ensure
it's only copied as needed by Qt.

Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
  • Loading branch information
Ryex committed Apr 8, 2023
1 parent 6308234 commit 79afdcf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
10 changes: 6 additions & 4 deletions launcher/tasks/ConcurrentTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void ConcurrentTask::startNext()

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::stepProgress, this, [this, next](TaskStepProgress const& tp) { subTaskStepProgress(next, tp); });

connect(next.get(), &Task::progress, this, [this, next](qint64 current, qint64 total) { subTaskProgress(next, current, total); });

Expand Down Expand Up @@ -224,13 +224,15 @@ void ConcurrentTask::subTaskProgress(Task::Ptr task, qint64 current, qint64 tota
updateState();
}

void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress task_progress)
void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress const& task_progress)
{
Operation op = Operation::ADDED;

if (!m_task_progress.contains(task_progress.uid)) {
m_task_progress.insert(task_progress.uid, std::make_shared<TaskStepProgress>(task_progress));
op = Operation::ADDED;
emit stepProgress(task_progress);
updateStepProgress(task_progress, op);
} else {
auto tp = m_task_progress.value(task_progress.uid);

Expand All @@ -243,10 +245,10 @@ void ConcurrentTask::subTaskStepProgress(Task::Ptr task, TaskStepProgress task_p
tp->details = task_progress.details;

op = Operation::CHANGED;
emit stepProgress(*tp.get());
updateStepProgress(*tp.get(), op);
}

emit stepProgress(task_progress);
updateStepProgress(task_progress, op);
}

void ConcurrentTask::updateStepProgress(TaskStepProgress const& changed_progress, Operation op)
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, TaskStepProgress task_step_progress);
void subTaskStepProgress(Task::Ptr task, TaskStepProgress const& 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(TaskStepProgress task_progress)
void Task::propogateStepProgress(TaskStepProgress const& task_progress)
{
emit stepProgress(task_progress);
}
Expand Down
6 changes: 3 additions & 3 deletions launcher/tasks/Task.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct TaskStepProgress {
QString status = "";
QString details = "";
TaskStepState state = TaskStepState::Waiting;
bool isDone() { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded); }
bool isDone() const { return (state == TaskStepState::Failed) || (state == TaskStepState::Succeeded); }
};

Q_DECLARE_METATYPE(TaskStepProgress)
Expand Down Expand Up @@ -130,7 +130,7 @@ class Task : public QObject, public QRunnable {
void failed(QString reason);
void status(QString status);
void details(QString details);
void stepProgress(TaskStepProgress task_progress); //
void stepProgress(TaskStepProgress const& task_progress); //

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

virtual void propogateStepProgress(TaskStepProgress task_progress);
virtual void propogateStepProgress(TaskStepProgress const& task_progress);

public slots:
void setStatus(const QString& status);
Expand Down
8 changes: 4 additions & 4 deletions launcher/ui/dialogs/ProgressDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ void ProgressDialog::changeStatus(const QString& status)
updateSize();
}

void ProgressDialog::addTaskProgress(TaskStepProgress* progress)
void ProgressDialog::addTaskProgress(TaskStepProgress const& progress)
{
SubTaskProgressBar* task_bar = new SubTaskProgressBar(this);
taskProgress.insert(progress->uid, task_bar);
taskProgress.insert(progress.uid, task_bar);
ui->taskProgressLayout->addWidget(task_bar);
}

void ProgressDialog::changeStepProgress(TaskStepProgress task_progress)
void ProgressDialog::changeStepProgress(TaskStepProgress const& task_progress)
{
m_is_multi_step = true;
if(ui->taskProgressScrollArea->isHidden()) {
Expand All @@ -222,7 +222,7 @@ void ProgressDialog::changeStepProgress(TaskStepProgress task_progress)
}

if (!taskProgress.contains(task_progress.uid))
addTaskProgress(&task_progress);
addTaskProgress(task_progress);
auto task_bar = taskProgress.value(task_progress.uid);


Expand Down
4 changes: 2 additions & 2 deletions 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(TaskStepProgress task_progress);
void changeStepProgress(TaskStepProgress const& task_progress);


private
Expand All @@ -93,7 +93,7 @@ private

private:
bool handleImmediateResult(QDialog::DialogCode &result);
void addTaskProgress(TaskStepProgress* progress);
void addTaskProgress(TaskStepProgress const& progress);

private:
Ui::ProgressDialog *ui;
Expand Down

0 comments on commit 79afdcf

Please sign in to comment.