Qt: Rename QtAsyncTaskWithProgress to QtAsyncTaskWithProgressDialog

This commit is contained in:
Stenzek
2025-11-30 17:03:27 +10:00
parent 547d3495ed
commit f3e0fe90d9
5 changed files with 55 additions and 50 deletions

View File

@@ -448,16 +448,17 @@ void GameSummaryWidget::onComputeHashClicked()
m_ui.computeHashes->setEnabled(false);
QtAsyncTaskWithProgress::create(this, TRANSLATE_SV("GameSummaryWidget", "Verifying Image"), {}, true, 1, 0, 0.0f,
[this, path = m_path](ProgressCallback* progress) {
Error error;
CDImageHasher::TrackHashes track_hashes;
const bool result = computeImageHash(path, track_hashes, progress, &error);
const bool cancelled = (!result && progress->IsCancelled());
return
[this, track_hashes = std::move(track_hashes), error = std::move(error), result,
cancelled]() { processHashResults(track_hashes, result, cancelled, error); };
});
QtAsyncTaskWithProgressDialog::create(this, TRANSLATE_SV("GameSummaryWidget", "Verifying Image"), {}, true, 1, 0,
0.0f, [this, path = m_path](ProgressCallback* progress) {
Error error;
CDImageHasher::TrackHashes track_hashes;
const bool result = computeImageHash(path, track_hashes, progress, &error);
const bool cancelled = (!result && progress->IsCancelled());
return [this, track_hashes = std::move(track_hashes),
error = std::move(error), result, cancelled]() {
processHashResults(track_hashes, result, cancelled, error);
};
});
}
bool GameSummaryWidget::computeImageHash(const std::string& path, CDImageHasher::TrackHashes& track_hashes,

View File

@@ -188,7 +188,7 @@ void ISOBrowserWindow::extractFile(const QString& path, IsoReader::ReadMode mode
const std::string status_text =
fmt::format(TRANSLATE_FS("ISOBrowserWindow", "Extracting {}..."), Path::GetFileName(spath));
QtAsyncTaskWithProgress::create(
QtAsyncTaskWithProgressDialog::create(
this, windowTitle().toStdString(), status_text, true, 0, 0, 0.15f,
[this, spath = std::move(spath), save_path = std::move(save_path), mode](ProgressCallback* const progress) mutable {
Error error;

View File

@@ -408,7 +408,7 @@ void QtHost::DownloadFile(QWidget* parent, std::string url, std::string path,
fmt::format(TRANSLATE_FS("QtHost", "Downloading {}..."),
std::string_view(url).substr((url_file_part_pos >= 0) ? (url_file_part_pos + 1) : 0));
QtAsyncTaskWithProgress::create(
QtAsyncTaskWithProgressDialog::create(
parent, TRANSLATE_SV("QtHost", "File Download"), status_text, true, 0, 0, 0.0f,
[url = std::move(url), path = std::move(path),
completion_callback = std::move(completion_callback)](ProgressCallback* const progress) mutable {
@@ -440,7 +440,7 @@ void QtHost::DownloadFile(QWidget* parent, std::string url, std::string path,
http->WaitForAllRequests();
}
QtAsyncTaskWithProgress::CompletionCallback ret;
QtAsyncTaskWithProgressDialog::CompletionCallback ret;
if (completion_callback)
{
ret = [path = std::move(path), completion_callback = std::move(completion_callback), error = std::move(error),

View File

@@ -77,9 +77,10 @@ void QtProgressCallback::connectWidgets(QLabel* const status_label, QProgressBar
}
}
QtAsyncTaskWithProgress::QtAsyncTaskWithProgress(const QString& initial_title, const QString& initial_status_text,
bool cancellable, int range, int value, float show_delay,
QWidget* dialog_parent, WorkCallback callback)
QtAsyncTaskWithProgressDialog::QtAsyncTaskWithProgressDialog(const QString& initial_title,
const QString& initial_status_text, bool cancellable,
int range, int value, float show_delay,
QWidget* dialog_parent, WorkCallback callback)
: m_callback(std::move(callback)), m_show_delay(show_delay)
{
m_dialog = new ProgressDialog(initial_title, initial_status_text, cancellable, range, value, *this, dialog_parent);
@@ -91,7 +92,7 @@ QtAsyncTaskWithProgress::QtAsyncTaskWithProgress(const QString& initial_title, c
}
}
QtAsyncTaskWithProgress::~QtAsyncTaskWithProgress()
QtAsyncTaskWithProgressDialog::~QtAsyncTaskWithProgressDialog()
{
if (m_dialog)
{
@@ -101,9 +102,10 @@ QtAsyncTaskWithProgress::~QtAsyncTaskWithProgress()
}
}
QtAsyncTaskWithProgress::ProgressDialog::ProgressDialog(const QString& initial_title,
const QString& initial_status_text, bool cancellable, int range,
int value, QtAsyncTaskWithProgress& task, QWidget* parent)
QtAsyncTaskWithProgressDialog::ProgressDialog::ProgressDialog(const QString& initial_title,
const QString& initial_status_text, bool cancellable,
int range, int value, QtAsyncTaskWithProgressDialog& task,
QWidget* parent)
: QDialog(parent), m_task(task)
{
if (!initial_title.isEmpty())
@@ -136,13 +138,13 @@ QtAsyncTaskWithProgress::ProgressDialog::ProgressDialog(const QString& initial_t
layout->addWidget(m_button_box);
}
QtAsyncTaskWithProgress::ProgressDialog::~ProgressDialog()
QtAsyncTaskWithProgressDialog::ProgressDialog::~ProgressDialog()
{
DebugAssert(m_task.m_dialog == this);
m_task.m_dialog = nullptr;
}
void QtAsyncTaskWithProgress::ProgressDialog::setCancellable(bool cancellable)
void QtAsyncTaskWithProgressDialog::ProgressDialog::setCancellable(bool cancellable)
{
if (cancellable == m_button_box->isVisible())
return;
@@ -153,28 +155,29 @@ void QtAsyncTaskWithProgress::ProgressDialog::setCancellable(bool cancellable)
m_button_box->setVisible(cancellable);
}
void QtAsyncTaskWithProgress::ProgressDialog::closeEvent(QCloseEvent* event)
void QtAsyncTaskWithProgressDialog::ProgressDialog::closeEvent(QCloseEvent* event)
{
cancelled();
QDialog::closeEvent(event);
}
void QtAsyncTaskWithProgress::ProgressDialog::cancelled()
void QtAsyncTaskWithProgressDialog::ProgressDialog::cancelled()
{
m_task.m_ts_cancelled.store(true, std::memory_order_release);
}
QtAsyncTaskWithProgress* QtAsyncTaskWithProgress::create(QWidget* parent, std::string_view initial_title,
std::string_view initial_status_text, bool cancellable,
int range, int value, float show_delay, WorkCallback callback)
QtAsyncTaskWithProgressDialog* QtAsyncTaskWithProgressDialog::create(QWidget* parent, std::string_view initial_title,
std::string_view initial_status_text,
bool cancellable, int range, int value,
float show_delay, WorkCallback callback)
{
DebugAssert(parent);
// NOTE: Must get connected before queuing, because otherwise you risk a race.
QtAsyncTaskWithProgress* task = new QtAsyncTaskWithProgress(
QtAsyncTaskWithProgressDialog* task = new QtAsyncTaskWithProgressDialog(
QtUtils::StringViewToQString(initial_title), QtUtils::StringViewToQString(initial_status_text), cancellable, range,
value, show_delay, parent, std::move(callback));
connect(task, &QtAsyncTaskWithProgress::completed, parent,
connect(task, &QtAsyncTaskWithProgressDialog::completed, parent,
[task]() { std::get<CompletionCallback>(task->m_callback)(); });
System::QueueAsyncTask([task]() {
@@ -188,22 +191,23 @@ QtAsyncTaskWithProgress* QtAsyncTaskWithProgress::create(QWidget* parent, std::s
return task;
}
QtAsyncTaskWithProgress* QtAsyncTaskWithProgress::create(QWidget* parent, float show_delay, WorkCallback callback)
QtAsyncTaskWithProgressDialog* QtAsyncTaskWithProgressDialog::create(QWidget* parent, float show_delay,
WorkCallback callback)
{
return create(parent, {}, {}, false, 0, 1, show_delay, std::move(callback));
}
void QtAsyncTaskWithProgress::cancel()
void QtAsyncTaskWithProgressDialog::cancel()
{
m_ts_cancelled.store(true, std::memory_order_release);
}
bool QtAsyncTaskWithProgress::IsCancelled() const
bool QtAsyncTaskWithProgressDialog::IsCancelled() const
{
return m_ts_cancelled.load(std::memory_order_acquire);
}
void QtAsyncTaskWithProgress::SetCancellable(bool cancellable)
void QtAsyncTaskWithProgressDialog::SetCancellable(bool cancellable)
{
if (m_cancellable == cancellable)
return;
@@ -216,7 +220,7 @@ void QtAsyncTaskWithProgress::SetCancellable(bool cancellable)
});
}
void QtAsyncTaskWithProgress::SetTitle(const std::string_view title)
void QtAsyncTaskWithProgressDialog::SetTitle(const std::string_view title)
{
Host::RunOnUIThread([this, title = QtUtils::StringViewToQString(title)]() {
if (m_dialog)
@@ -224,7 +228,7 @@ void QtAsyncTaskWithProgress::SetTitle(const std::string_view title)
});
}
void QtAsyncTaskWithProgress::SetStatusText(const std::string_view text)
void QtAsyncTaskWithProgressDialog::SetStatusText(const std::string_view text)
{
if (m_status_text == text)
return;
@@ -243,7 +247,7 @@ void QtAsyncTaskWithProgress::SetStatusText(const std::string_view text)
}
}
void QtAsyncTaskWithProgress::SetProgressRange(u32 range)
void QtAsyncTaskWithProgressDialog::SetProgressRange(u32 range)
{
const u32 prev_range = m_progress_range;
ProgressCallback::SetProgressRange(range);
@@ -263,7 +267,7 @@ void QtAsyncTaskWithProgress::SetProgressRange(u32 range)
}
}
void QtAsyncTaskWithProgress::SetProgressValue(u32 value)
void QtAsyncTaskWithProgressDialog::SetProgressValue(u32 value)
{
const u32 prev_value = m_progress_value;
ProgressCallback::SetProgressValue(value);
@@ -283,7 +287,7 @@ void QtAsyncTaskWithProgress::SetProgressValue(u32 value)
}
}
void QtAsyncTaskWithProgress::CheckForDelayedShow()
void QtAsyncTaskWithProgressDialog::CheckForDelayedShow()
{
DebugAssert(!m_shown);

View File

@@ -42,7 +42,7 @@ private:
std::atomic_bool m_ts_cancelled{false};
};
class QtAsyncTaskWithProgress final : public QObject, private ProgressCallback
class QtAsyncTaskWithProgressDialog final : public QObject, private ProgressCallback
{
Q_OBJECT
@@ -50,27 +50,27 @@ public:
using CompletionCallback = std::function<void()>;
using WorkCallback = std::function<CompletionCallback(ProgressCallback*)>;
static QtAsyncTaskWithProgress* create(QWidget* parent, std::string_view initial_title,
std::string_view initial_status_text, bool cancellable, int range, int value,
float show_delay, WorkCallback callback);
static QtAsyncTaskWithProgress* create(QWidget* parent, float show_delay, WorkCallback callback);
static QtAsyncTaskWithProgressDialog* create(QWidget* parent, std::string_view initial_title,
std::string_view initial_status_text, bool cancellable, int range,
int value, float show_delay, WorkCallback callback);
static QtAsyncTaskWithProgressDialog* create(QWidget* parent, float show_delay, WorkCallback callback);
/// Asynchronously cancel the task. Should only be called from the UI thread.
/// There is no guarantee when the cancel will go through.
void cancel();
Q_SIGNALS:
void completed(QtAsyncTaskWithProgress* self);
void completed(QtAsyncTaskWithProgressDialog* self);
private:
// can't use QProgressDialog, it starts an event in setValue()...
class ProgressDialog final : public QDialog
{
friend QtAsyncTaskWithProgress;
friend QtAsyncTaskWithProgressDialog;
public:
ProgressDialog(const QString& initial_title, const QString& initial_status_text, bool cancellable, int range,
int value, QtAsyncTaskWithProgress& task, QWidget* parent);
int value, QtAsyncTaskWithProgressDialog& task, QWidget* parent);
~ProgressDialog() override;
void setCancellable(bool cancellable);
@@ -85,7 +85,7 @@ private:
void cancelled();
QtAsyncTaskWithProgress& m_task;
QtAsyncTaskWithProgressDialog& m_task;
QLabel* m_status_label = nullptr;
QProgressBar* m_progress_bar = nullptr;
QDialogButtonBox* m_button_box = nullptr;
@@ -94,9 +94,9 @@ private:
friend ProgressDialog;
// constructor hidden, clients should not be creating this directly
QtAsyncTaskWithProgress(const QString& initial_title, const QString& initial_status_text, bool cancellable, int range,
int value, float show_delay, QWidget* dialog_parent, WorkCallback callback);
~QtAsyncTaskWithProgress();
QtAsyncTaskWithProgressDialog(const QString& initial_title, const QString& initial_status_text, bool cancellable,
int range, int value, float show_delay, QWidget* dialog_parent, WorkCallback callback);
~QtAsyncTaskWithProgressDialog();
// progress callback overrides
bool IsCancelled() const override;