2021-12-10 02:32:00 +00:00
|
|
|
// Copyright 2020 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2020-03-29 22:28:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QProgressDialog>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include "Common/Flag.h"
|
|
|
|
|
|
|
|
class ParallelProgressDialog final : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-04-25 00:18:28 +00:00
|
|
|
ParallelProgressDialog(const ParallelProgressDialog&) = delete;
|
|
|
|
ParallelProgressDialog& operator=(const ParallelProgressDialog&) = delete;
|
|
|
|
ParallelProgressDialog(ParallelProgressDialog&&) = delete;
|
|
|
|
ParallelProgressDialog& operator=(ParallelProgressDialog&&) = delete;
|
|
|
|
|
2020-03-29 22:28:16 +00:00
|
|
|
// Only use this from the main thread
|
|
|
|
template <typename... Args>
|
|
|
|
ParallelProgressDialog(Args&&... args) : m_dialog{std::forward<Args>(args)...}
|
|
|
|
{
|
|
|
|
setParent(m_dialog.parent());
|
|
|
|
m_dialog.setWindowFlags(m_dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
ConnectSignalsAndSlots();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only use this from the main thread
|
|
|
|
QProgressDialog* GetRaw() { return &m_dialog; }
|
|
|
|
|
|
|
|
// All of the following can be called from any thread
|
|
|
|
void Cancel() { emit CancelSignal(); }
|
|
|
|
void Reset() { emit ResetSignal(); }
|
2020-10-19 10:39:10 +00:00
|
|
|
void SetCancelButtonText(const QString& text) { emit SetCancelButtonTextSignal(text); }
|
2020-03-29 22:28:16 +00:00
|
|
|
void SetLabelText(const QString& text) { emit SetLabelTextSignal(text); }
|
|
|
|
void SetMaximum(int maximum) { emit SetMaximumSignal(maximum); }
|
|
|
|
void SetMinimum(int minimum) { emit SetMinimumSignal(minimum); }
|
|
|
|
void SetMinimumDuration(int ms) { emit SetMinimumDurationSignal(ms); }
|
|
|
|
void SetRange(int minimum, int maximum) { emit SetRangeSignal(minimum, maximum); }
|
2020-07-08 17:47:44 +00:00
|
|
|
void SetValue(int progress) { emit SetValueSignal(progress); }
|
2020-03-29 22:28:16 +00:00
|
|
|
|
|
|
|
// Can be called from any thread
|
2023-06-12 13:38:08 +00:00
|
|
|
bool WasCanceled() const { return m_was_cancelled.IsSet(); }
|
2020-03-29 22:28:16 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void CancelSignal();
|
|
|
|
void ResetSignal();
|
|
|
|
void SetCancelButtonTextSignal(const QString& cancel_button_text);
|
|
|
|
void SetLabelTextSignal(const QString& text);
|
|
|
|
void SetMaximumSignal(int maximum);
|
|
|
|
void SetMinimumSignal(int minimum);
|
|
|
|
void SetMinimumDurationSignal(int ms);
|
|
|
|
void SetRangeSignal(int minimum, int maximum);
|
|
|
|
void SetValueSignal(int progress);
|
|
|
|
|
|
|
|
void Canceled();
|
|
|
|
void Finished(int result);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void OnCancelled() { m_was_cancelled.Set(); }
|
|
|
|
|
2020-07-08 17:47:44 +00:00
|
|
|
void SetValueSlot(int progress)
|
|
|
|
{
|
|
|
|
// Normally we would've been able to just call setValue instead of having this wrapper
|
|
|
|
// around it, but due to the https://bugreports.qt.io/browse/QTBUG-10561 stack overflow,
|
|
|
|
// we can't. Short summary of the stack overflow: setValue calls processEvents,
|
|
|
|
// which calls SetValueSlot if there is another SetValueSlot event in the queue,
|
|
|
|
// which would call setValue if it wasn't for the check below, and so on.
|
|
|
|
|
|
|
|
m_last_received_progress = progress;
|
|
|
|
|
|
|
|
if (m_is_setting_value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_is_setting_value = true;
|
|
|
|
|
2020-07-09 13:42:41 +00:00
|
|
|
int last_set_progress;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
last_set_progress = m_last_received_progress;
|
2020-07-08 17:47:44 +00:00
|
|
|
m_dialog.setValue(m_last_received_progress);
|
2020-07-09 13:42:41 +00:00
|
|
|
} while (m_last_received_progress != last_set_progress);
|
2020-07-08 17:47:44 +00:00
|
|
|
|
|
|
|
m_is_setting_value = false;
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:28:16 +00:00
|
|
|
private:
|
|
|
|
template <typename Func1, typename Func2>
|
|
|
|
void ConnectSignal(Func1 signal, Func2 slot)
|
|
|
|
{
|
|
|
|
QObject::connect(this, signal, &m_dialog, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func1, typename Func2>
|
|
|
|
void ConnectSlot(Func1 signal, Func2 slot)
|
|
|
|
{
|
|
|
|
QObject::connect(&m_dialog, signal, this, slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectSignalsAndSlots()
|
|
|
|
{
|
|
|
|
ConnectSignal(&ParallelProgressDialog::CancelSignal, &QProgressDialog::cancel);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::ResetSignal, &QProgressDialog::reset);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetCancelButtonTextSignal,
|
|
|
|
&QProgressDialog::setCancelButtonText);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetLabelTextSignal, &QProgressDialog::setLabelText);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetMaximumSignal, &QProgressDialog::setMaximum);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetMinimumSignal, &QProgressDialog::setMinimum);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetMinimumDurationSignal,
|
|
|
|
&QProgressDialog::setMinimumDuration);
|
|
|
|
ConnectSignal(&ParallelProgressDialog::SetRangeSignal, &QProgressDialog::setRange);
|
2020-07-08 17:47:44 +00:00
|
|
|
|
|
|
|
QObject::connect(this, &ParallelProgressDialog::SetValueSignal, this,
|
|
|
|
&ParallelProgressDialog::SetValueSlot);
|
2020-03-29 22:28:16 +00:00
|
|
|
|
|
|
|
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::OnCancelled);
|
|
|
|
|
|
|
|
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::Canceled);
|
|
|
|
ConnectSlot(&QProgressDialog::finished, &ParallelProgressDialog::Finished);
|
|
|
|
}
|
|
|
|
|
|
|
|
QProgressDialog m_dialog;
|
|
|
|
Common::Flag m_was_cancelled;
|
2023-06-12 13:36:49 +00:00
|
|
|
int m_last_received_progress = 0;
|
2020-07-08 17:47:44 +00:00
|
|
|
bool m_is_setting_value = false;
|
2020-03-29 22:28:16 +00:00
|
|
|
};
|