DolphinQt: Run tasks that use progress dialogs on separate threads
This commit is contained in:
parent
4ff855921e
commit
c6ee767851
|
@ -224,6 +224,7 @@ add_executable(dolphin-emu
|
|||
QtUtils/FlowLayout.h
|
||||
QtUtils/ModalMessageBox.cpp
|
||||
QtUtils/ModalMessageBox.h
|
||||
QtUtils/ParallelProgressDialog.h
|
||||
QtUtils/ImageConverter.cpp
|
||||
QtUtils/ImageConverter.h
|
||||
QtUtils/WindowActivationEventFilter.cpp
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#include <QFileInfo>
|
||||
#include <QHeaderView>
|
||||
#include <QMenu>
|
||||
#include <QProgressDialog>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStyleFactory>
|
||||
#include <QTreeView>
|
||||
|
@ -23,6 +22,7 @@
|
|||
#include "DiscIO/Volume.h"
|
||||
|
||||
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
||||
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
|
||||
#include "DolphinQt/Resources.h"
|
||||
|
||||
#include "UICommon/UICommon.h"
|
||||
|
@ -282,28 +282,34 @@ void FilesystemWidget::ExtractDirectory(const DiscIO::Partition& partition, cons
|
|||
std::unique_ptr<DiscIO::FileInfo> info = filesystem->FindFileInfo(path.toStdString());
|
||||
u32 size = info->GetTotalChildren();
|
||||
|
||||
QProgressDialog* dialog = new QProgressDialog(this);
|
||||
dialog->setWindowFlags(dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
dialog->setMinimum(0);
|
||||
dialog->setMaximum(size);
|
||||
dialog->show();
|
||||
dialog->setWindowTitle(tr("Progress"));
|
||||
ParallelProgressDialog dialog(this);
|
||||
dialog.GetRaw()->setMinimum(0);
|
||||
dialog.GetRaw()->setMaximum(size);
|
||||
dialog.GetRaw()->setWindowTitle(tr("Progress"));
|
||||
|
||||
bool all = path.isEmpty();
|
||||
const bool all = path.isEmpty();
|
||||
|
||||
std::future<void> future = std::async(std::launch::async, [&] {
|
||||
int progress = 0;
|
||||
|
||||
DiscIO::ExportDirectory(
|
||||
*m_volume, partition, *info, true, path.toStdString(), out.toStdString(),
|
||||
[all, dialog](const std::string& current) {
|
||||
dialog->setLabelText(
|
||||
(all ? QObject::tr("Extracting All Files...") : QObject::tr("Extracting Directory..."))
|
||||
[all, &dialog, &progress](const std::string& current) {
|
||||
dialog.SetLabelText(
|
||||
(all ? QObject::tr("Extracting All Files...") :
|
||||
QObject::tr("Extracting Directory..."))
|
||||
.append(QStringLiteral(" %1").arg(QString::fromStdString(current))));
|
||||
dialog->setValue(dialog->value() + 1);
|
||||
dialog.SetValue(++progress);
|
||||
|
||||
QCoreApplication::processEvents();
|
||||
return dialog->wasCanceled();
|
||||
return dialog.WasCanceled();
|
||||
});
|
||||
|
||||
dialog->close();
|
||||
dialog.Reset();
|
||||
});
|
||||
|
||||
dialog.GetRaw()->exec();
|
||||
future.get();
|
||||
}
|
||||
|
||||
void FilesystemWidget::ExtractFile(const DiscIO::Partition& partition, const QString& path,
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QProgressDialog>
|
||||
#include <QPushButton>
|
||||
#include <QTextEdit>
|
||||
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
|
||||
#include "DolphinQt/Config/VerifyWidget.h"
|
||||
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
|
@ -12,12 +14,12 @@
|
|||
#include <QHBoxLayout>
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QProgressDialog>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "DiscIO/Volume.h"
|
||||
#include "DiscIO/VolumeVerifier.h"
|
||||
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
|
||||
|
||||
VerifyWidget::VerifyWidget(std::shared_ptr<DiscIO::Volume> volume) : m_volume(std::move(volume))
|
||||
{
|
||||
|
@ -131,33 +133,44 @@ void VerifyWidget::Verify()
|
|||
// We have to divide the number of processed bytes with something so it won't make ints overflow
|
||||
constexpr int DIVISOR = 0x100;
|
||||
|
||||
QProgressDialog progress(tr("Verifying"), tr("Cancel"), 0, verifier.GetTotalBytes() / DIVISOR,
|
||||
this);
|
||||
progress.setWindowTitle(tr("Verifying"));
|
||||
progress.setWindowFlags(progress.windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||
progress.setMinimumDuration(500);
|
||||
progress.setWindowModality(Qt::WindowModal);
|
||||
ParallelProgressDialog progress(tr("Verifying"), tr("Cancel"), 0,
|
||||
static_cast<int>(verifier.GetTotalBytes() / DIVISOR), this);
|
||||
progress.GetRaw()->setWindowTitle(tr("Verifying"));
|
||||
progress.GetRaw()->setMinimumDuration(500);
|
||||
progress.GetRaw()->setWindowModality(Qt::WindowModal);
|
||||
|
||||
auto future =
|
||||
std::async(std::launch::async,
|
||||
[&verifier, &progress]() -> std::optional<DiscIO::VolumeVerifier::Result> {
|
||||
progress.SetValue(0);
|
||||
verifier.Start();
|
||||
while (verifier.GetBytesProcessed() != verifier.GetTotalBytes())
|
||||
{
|
||||
progress.setValue(verifier.GetBytesProcessed() / DIVISOR);
|
||||
if (progress.wasCanceled())
|
||||
return;
|
||||
progress.SetValue(static_cast<int>(verifier.GetBytesProcessed() / DIVISOR));
|
||||
if (progress.WasCanceled())
|
||||
return std::nullopt;
|
||||
|
||||
verifier.Process();
|
||||
}
|
||||
verifier.Finish();
|
||||
|
||||
DiscIO::VolumeVerifier::Result result = verifier.GetResult();
|
||||
progress.setValue(verifier.GetBytesProcessed() / DIVISOR);
|
||||
const DiscIO::VolumeVerifier::Result result = verifier.GetResult();
|
||||
progress.Reset();
|
||||
|
||||
m_summary_text->setText(QString::fromStdString(result.summary_text));
|
||||
return result;
|
||||
});
|
||||
progress.GetRaw()->exec();
|
||||
|
||||
m_problems->setRowCount(static_cast<int>(result.problems.size()));
|
||||
std::optional<DiscIO::VolumeVerifier::Result> result = future.get();
|
||||
if (!result)
|
||||
return;
|
||||
|
||||
m_summary_text->setText(QString::fromStdString(result->summary_text));
|
||||
|
||||
m_problems->setRowCount(static_cast<int>(result->problems.size()));
|
||||
for (int i = 0; i < m_problems->rowCount(); ++i)
|
||||
{
|
||||
const DiscIO::VolumeVerifier::Problem problem = result.problems[i];
|
||||
const DiscIO::VolumeVerifier::Problem problem = result->problems[i];
|
||||
|
||||
QString severity;
|
||||
switch (problem.severity)
|
||||
|
@ -179,12 +192,12 @@ void VerifyWidget::Verify()
|
|||
SetProblemCellText(i, 1, severity);
|
||||
}
|
||||
|
||||
SetHash(m_crc32_line_edit, result.hashes.crc32);
|
||||
SetHash(m_md5_line_edit, result.hashes.md5);
|
||||
SetHash(m_sha1_line_edit, result.hashes.sha1);
|
||||
SetHash(m_crc32_line_edit, result->hashes.crc32);
|
||||
SetHash(m_md5_line_edit, result->hashes.md5);
|
||||
SetHash(m_sha1_line_edit, result->hashes.sha1);
|
||||
|
||||
if (m_redump_line_edit)
|
||||
m_redump_line_edit->setText(QString::fromStdString(result.redump.message));
|
||||
m_redump_line_edit->setText(QString::fromStdString(result->redump.message));
|
||||
}
|
||||
|
||||
void VerifyWidget::SetProblemCellText(int row, int column, QString text)
|
||||
|
|
|
@ -181,6 +181,7 @@
|
|||
<QtMoc Include="QtUtils\FileOpenEventFilter.h" />
|
||||
<QtMoc Include="QtUtils\FlowLayout.h" />
|
||||
<QtMoc Include="QtUtils\ModalMessageBox.h" />
|
||||
<QtMoc Include="QtUtils\ParallelProgressDialog.h" />
|
||||
<QtMoc Include="QtUtils\WindowActivationEventFilter.h" />
|
||||
<QtMoc Include="QtUtils\WrapInScrollArea.h" />
|
||||
<QtMoc Include="RenderWidget.h" />
|
||||
|
@ -282,6 +283,7 @@
|
|||
<ClCompile Include="$(QtMocOutPrefix)NewBreakpointDialog.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)NewPatchDialog.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)PadMappingDialog.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)ParallelProgressDialog.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)PatchesWidget.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)PatchInstructionDialog.cpp" />
|
||||
<ClCompile Include="$(QtMocOutPrefix)PathPane.cpp" />
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <future>
|
||||
|
||||
#include <QDesktopServices>
|
||||
#include <QDir>
|
||||
|
@ -20,7 +21,6 @@
|
|||
#include <QListView>
|
||||
#include <QMap>
|
||||
#include <QMenu>
|
||||
#include <QProgressDialog>
|
||||
#include <QShortcut>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QTableView>
|
||||
|
@ -46,6 +46,7 @@
|
|||
#include "DolphinQt/MenuBar.h"
|
||||
#include "DolphinQt/QtUtils/DoubleClickEventFilter.h"
|
||||
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
||||
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
|
||||
#include "DolphinQt/Resources.h"
|
||||
#include "DolphinQt/Settings.h"
|
||||
#include "DolphinQt/WiiUpdate.h"
|
||||
|
@ -574,34 +575,50 @@ void GameList::CompressISO(bool decompress)
|
|||
}
|
||||
}
|
||||
|
||||
QProgressDialog progress_dialog(decompress ? tr("Decompressing...") : tr("Compressing..."),
|
||||
tr("Abort"), 0, 100, this);
|
||||
progress_dialog.setWindowModality(Qt::WindowModal);
|
||||
progress_dialog.setWindowFlags(progress_dialog.windowFlags() &
|
||||
~Qt::WindowContextHelpButtonHint);
|
||||
progress_dialog.setWindowTitle(tr("Progress"));
|
||||
ParallelProgressDialog progress_dialog(
|
||||
decompress ? tr("Decompressing...") : tr("Compressing..."), tr("Abort"), 0, 100, this);
|
||||
progress_dialog.GetRaw()->setWindowModality(Qt::WindowModal);
|
||||
progress_dialog.GetRaw()->setWindowTitle(tr("Progress"));
|
||||
|
||||
bool good;
|
||||
std::future<bool> good;
|
||||
|
||||
if (decompress)
|
||||
{
|
||||
if (files.size() > 1)
|
||||
progress_dialog.setLabelText(tr("Decompressing...") + QLatin1Char{'\n'} +
|
||||
{
|
||||
progress_dialog.GetRaw()->setLabelText(
|
||||
tr("Decompressing...") + QLatin1Char{'\n'} +
|
||||
QFileInfo(QString::fromStdString(original_path)).fileName());
|
||||
good = DiscIO::DecompressBlobToFile(original_path, dst_path.toStdString(), &CompressCB,
|
||||
&progress_dialog);
|
||||
}
|
||||
|
||||
good = std::async(std::launch::async, [&] {
|
||||
const bool good = DiscIO::DecompressBlobToFile(original_path, dst_path.toStdString(),
|
||||
&CompressCB, &progress_dialog);
|
||||
progress_dialog.Reset();
|
||||
return good;
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
if (files.size() > 1)
|
||||
progress_dialog.setLabelText(tr("Compressing...") + QLatin1Char{'\n'} +
|
||||
{
|
||||
progress_dialog.GetRaw()->setLabelText(
|
||||
tr("Compressing...") + QLatin1Char{'\n'} +
|
||||
QFileInfo(QString::fromStdString(original_path)).fileName());
|
||||
good = DiscIO::CompressFileToBlob(original_path, dst_path.toStdString(),
|
||||
file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0,
|
||||
16384, &CompressCB, &progress_dialog);
|
||||
}
|
||||
|
||||
if (!good)
|
||||
good = std::async(std::launch::async, [&] {
|
||||
const bool good =
|
||||
DiscIO::CompressFileToBlob(original_path, dst_path.toStdString(),
|
||||
file->GetPlatform() == DiscIO::Platform::WiiDisc ? 1 : 0,
|
||||
16384, &CompressCB, &progress_dialog);
|
||||
progress_dialog.Reset();
|
||||
return good;
|
||||
});
|
||||
}
|
||||
|
||||
progress_dialog.GetRaw()->exec();
|
||||
if (!good.get())
|
||||
{
|
||||
QErrorMessage(this).showMessage(tr("Dolphin failed to complete the requested action."));
|
||||
return;
|
||||
|
@ -934,10 +951,10 @@ static bool CompressCB(const std::string& text, float percent, void* ptr)
|
|||
if (ptr == nullptr)
|
||||
return false;
|
||||
|
||||
auto* progress_dialog = static_cast<QProgressDialog*>(ptr);
|
||||
auto* progress_dialog = static_cast<ParallelProgressDialog*>(ptr);
|
||||
|
||||
progress_dialog->setValue(percent * 100);
|
||||
return !progress_dialog->wasCanceled();
|
||||
progress_dialog->SetValue(percent * 100);
|
||||
return !progress_dialog->WasCanceled();
|
||||
}
|
||||
|
||||
void GameList::OnSectionResized(int index, int, int)
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include <QAbstractEventDispatcher>
|
||||
#include <QApplication>
|
||||
#include <QProgressDialog>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
|
|
|
@ -87,6 +87,7 @@
|
|||
#include "DolphinQt/NetPlay/NetPlaySetupDialog.h"
|
||||
#include "DolphinQt/QtUtils/FileOpenEventFilter.h"
|
||||
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
||||
#include "DolphinQt/QtUtils/ParallelProgressDialog.h"
|
||||
#include "DolphinQt/QtUtils/QueueOnObject.h"
|
||||
#include "DolphinQt/QtUtils/RunOnObject.h"
|
||||
#include "DolphinQt/QtUtils/WindowActivationEventFilter.h"
|
||||
|
@ -1510,23 +1511,21 @@ void MainWindow::OnImportNANDBackup()
|
|||
if (file.isEmpty())
|
||||
return;
|
||||
|
||||
QProgressDialog* dialog = new QProgressDialog(this);
|
||||
dialog->setMinimum(0);
|
||||
dialog->setMaximum(0);
|
||||
dialog->setLabelText(tr("Importing NAND backup"));
|
||||
dialog->setCancelButton(nullptr);
|
||||
ParallelProgressDialog dialog(this);
|
||||
dialog.GetRaw()->setMinimum(0);
|
||||
dialog.GetRaw()->setMaximum(0);
|
||||
dialog.GetRaw()->setLabelText(tr("Importing NAND backup"));
|
||||
dialog.GetRaw()->setCancelButton(nullptr);
|
||||
|
||||
auto beginning = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
|
||||
auto result = std::async(std::launch::async, [&] {
|
||||
std::future<void> result = std::async(std::launch::async, [&] {
|
||||
DiscIO::NANDImporter().ImportNANDBin(
|
||||
file.toStdString(),
|
||||
[&dialog, beginning] {
|
||||
QueueOnObject(dialog, [&dialog, beginning] {
|
||||
dialog->setLabelText(
|
||||
dialog.SetLabelText(
|
||||
tr("Importing NAND backup\n Time elapsed: %1s")
|
||||
.arg((QDateTime::currentDateTime().toMSecsSinceEpoch() - beginning) / 1000));
|
||||
});
|
||||
},
|
||||
[this] {
|
||||
std::optional<std::string> keys_file = RunOnObject(this, [this] {
|
||||
|
@ -1540,10 +1539,10 @@ void MainWindow::OnImportNANDBackup()
|
|||
return *keys_file;
|
||||
return std::string("");
|
||||
});
|
||||
QueueOnObject(dialog, &QProgressDialog::close);
|
||||
dialog.Reset();
|
||||
});
|
||||
|
||||
dialog->exec();
|
||||
dialog.GetRaw()->exec();
|
||||
|
||||
result.wait();
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMenu>
|
||||
#include <QProgressDialog>
|
||||
#include <QPushButton>
|
||||
#include <QSignalBlocker>
|
||||
#include <QSpinBox>
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <utility>
|
||||
|
||||
#include <QObject>
|
||||
#include <QProgressDialog>
|
||||
#include <QString>
|
||||
|
||||
#include "Common/Flag.h"
|
||||
|
||||
class ParallelProgressDialog final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
// 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(); }
|
||||
void SetCancelButtonText(const QString& text) { emit SetCancelButtonText(text); }
|
||||
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); }
|
||||
|
||||
// Can be called from any thread, but only from one thread at a time
|
||||
void SetValue(int progress)
|
||||
{
|
||||
// HACK: To avoid the https://bugreports.qt.io/browse/QTBUG-10561 stack overflow,
|
||||
// limit how often QProgressDialog::setValue can run
|
||||
const auto current_time = std::chrono::steady_clock::now();
|
||||
if (current_time - m_last_setvalue_time >= std::chrono::milliseconds(50))
|
||||
{
|
||||
m_last_setvalue_time = current_time;
|
||||
emit SetValueSignal(progress);
|
||||
}
|
||||
}
|
||||
|
||||
// Can be called from any thread
|
||||
bool WasCanceled() { return m_was_cancelled.IsSet(); }
|
||||
|
||||
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(); }
|
||||
|
||||
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);
|
||||
ConnectSignal(&ParallelProgressDialog::SetValueSignal, &QProgressDialog::setValue);
|
||||
|
||||
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::OnCancelled);
|
||||
|
||||
ConnectSlot(&QProgressDialog::canceled, &ParallelProgressDialog::Canceled);
|
||||
ConnectSlot(&QProgressDialog::finished, &ParallelProgressDialog::Finished);
|
||||
}
|
||||
|
||||
QProgressDialog m_dialog;
|
||||
Common::Flag m_was_cancelled;
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_last_setvalue_time;
|
||||
};
|
Loading…
Reference in New Issue