| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <utility> |
| 7 |
|
| 8 |
#include <QObject> |
| 9 |
#include <QProgressDialog> |
| 10 |
#include <QString> |
| 11 |
|
| 12 |
#include "Common/Flag.h" |
| 13 |
|
| 14 |
class ParallelProgressDialog final : public QObject |
| 15 |
{ |
| 16 |
Q_OBJECT |
| 17 |
|
| 18 |
public: |
| 19 |
ParallelProgressDialog(const ParallelProgressDialog&) = delete; |
| 20 |
ParallelProgressDialog& operator=(const ParallelProgressDialog&) = delete; |
| 21 |
ParallelProgressDialog(ParallelProgressDialog&&) = delete; |
| 22 |
ParallelProgressDialog& operator=(ParallelProgressDialog&&) = delete; |
| 23 |
|
| 24 |
|
| 25 |
template <typename... Args> |
| 26 |
ParallelProgressDialog(Args&&... args) : m_dialog{std::forward<Args>(args)...} |
| 27 |
{ |
| 28 |
setParent(m_dialog.parent()); |
| 29 |
ConnectSignalsAndSlots(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
QProgressDialog* GetRaw() { return &m_dialog; } |
| 34 |
|
| 35 |
|
| 36 |
void Cancel() { emit CancelSignal(); } |
| 37 |
void Reset() { emit ResetSignal(); } |
| 38 |
void SetCancelButtonText(const QString& text) { emit SetCancelButtonTextSignal(text); } |
| 39 |
void SetLabelText(const QString& text) { emit SetLabelTextSignal(text); } |
| 40 |
void SetMaximum(int maximum) { emit SetMaximumSignal(maximum); } |
| 41 |
void SetMinimum(int minimum) { emit SetMinimumSignal(minimum); } |
| 42 |
void SetMinimumDuration(int ms) { emit SetMinimumDurationSignal(ms); } |
| 43 |
void SetRange(int minimum, int maximum) { emit SetRangeSignal(minimum, maximum); } |
| 44 |
void SetValue(int progress) { emit SetValueSignal(progress); } |
| 45 |
|
| 46 |
|
| 47 |
bool WasCanceled() const { return m_was_cancelled.IsSet(); } |
| 48 |
|
| 49 |
signals: |
| 50 |
void CancelSignal(); |
| 51 |
void ResetSignal(); |
| 52 |
void SetCancelButtonTextSignal(const QString& cancel_button_text); |
| 53 |
void SetLabelTextSignal(const QString& text); |
| 54 |
void SetMaximumSignal(int maximum); |
| 55 |
void SetMinimumSignal(int minimum); |
| 56 |
void SetMinimumDurationSignal(int ms); |
| 57 |
void SetRangeSignal(int minimum, int maximum); |
| 58 |
void SetValueSignal(int progress); |
| 59 |
|
| 60 |
void Canceled(); |
| 61 |
void Finished(int result); |
| 62 |
|
| 63 |
private slots: |
| 64 |
void OnCancelled() { m_was_cancelled.Set(); } |
| 65 |
|
| 66 |
void SetValueSlot(int progress) |
| 67 |
{ |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
m_last_received_progress = progress; |
| 75 |
|
| 76 |
if (m_is_setting_value) |
| 77 |
return; |
| 78 |
|
| 79 |
m_is_setting_value = true; |
| 80 |
|
| 81 |
int last_set_progress; |
| 82 |
do |
| 83 |
{ |
| 84 |
last_set_progress = m_last_received_progress; |
| 85 |
m_dialog.setValue(m_last_received_progress); |
| 86 |
} while (m_last_received_progress != last_set_progress); |
| 87 |
|
| 88 |
m_is_setting_value = false; |
| 89 |
} |
| 90 |
|
| 91 |
private: |
| 92 |
template <typename Func1, typename Func2> |
| 93 |
void ConnectSignal(Func1 signal, Func2 slot) |
| 94 |
{ |
| 95 |
QObject::connect(this, signal, &m_dialog, slot); |
| 96 |
} |
| 97 |
|
| 98 |
template <typename Func1, typename Func2> |
| 99 |
void ConnectSlot(Func1 signal, Func2 slot) |
| 100 |
{ |
| 101 |
QObject::connect(&m_dialog, signal, this, slot); |
| 102 |
} |
| 103 |
|
| 104 |
void ConnectSignalsAndSlots() |
| 105 |
{ |
| 106 |
ConnectSignal(&ParallelProgressDialog::CancelSignal, &QProgressDialog::cancel); |
| 107 |
ConnectSignal(&ParallelProgressDialog::ResetSignal, &QProgressDialog::reset); |
| 108 |
ConnectSignal(&ParallelProgressDialog::SetCancelButtonTextSignal, |
| 109 |
&QProgressDialog::setCancelButtonText); |
| 110 |
ConnectSignal(&ParallelProgressDialog::SetLabelTextSignal, &QProgressDialog::setLabelText); |
| 111 |
ConnectSignal(&ParallelProgressDialog::SetMaximumSignal, &QProgressDialog::setMaximum); |
| 112 |
ConnectSignal(&ParallelProgressDialog::SetMinimumSignal, &QProgressDialog::setMinimum); |
| 113 |
ConnectSignal(&ParallelProgressDialog::SetMinimumDurationSignal, |
| 114 |
&QProgressDialog::setMinimumDuration); |
| 115 |
ConnectSignal(&ParallelProgressDialog::SetRangeSignal, &QProgressDialog::setRange); |
| 116 |
|
| 117 |
QObject::connect(this, &ParallelProgressDialog::SetValueSignal, this, |
| 118 |
&ParallelProgressDialog::SetValueSlot); |
| 119 |
|
| 120 |
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::OnCancelled); |
| 121 |
|
| 122 |
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::Canceled); |
| 123 |
ConnectSlot(&QProgressDialog::finished, &ParallelProgressDialog::Finished); |
| 124 |
} |
| 125 |
|
| 126 |
QProgressDialog m_dialog; |
| 127 |
Common::Flag m_was_cancelled; |
| 128 |
int m_last_received_progress = 0; |
| 129 |
bool m_is_setting_value = false; |
| 130 |
}; |