From 93b1cea0d30e635cad7988dd65578bbbc1abecc7 Mon Sep 17 00:00:00 2001 From: MikaĆ«l Capelle Date: Thu, 11 Jun 2020 20:09:00 +0200 Subject: Rollback to using QProcessEvents but use WaitForMoreEvents. --- src/installationmanager.cpp | 50 ++++++++++++++++++++++++--------------------- src/installationmanager.h | 7 +------ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index b1eaf1d3..8500fec0 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -180,37 +180,34 @@ bool InstallationManager::extractFiles(QString extractPath, QString title, bool installationProgress->setAutoReset(false); // Connect signals emitted by the extraction callback to the progress dialog slots: - connect(this, &InstallationManager::progressUpdate, installationProgress, &QProgressDialog::setValue); - connect(this, &InstallationManager::progressFileChange, installationProgress, &QProgressDialog::setLabelText); + QEventLoop loop; + connect(this, &InstallationManager::progressUpdate, &loop, &QEventLoop::wakeUp, Qt::QueuedConnection); // Cancelling progress only cancel the extraction, we do not force exiting the event-loop: connect(installationProgress, &QProgressDialog::canceled, [this]() { m_ArchiveHandler->cancel(); }); - installationProgress->show(); + int currentProgress = 0; + QString currentFileName; // The callbacks: - auto progressCallback = [this](auto progressType, uint64_t current, uint64_t total) { + auto progressCallback = [this, ¤tProgress](auto progressType, uint64_t current, uint64_t total) { if (progressType == Archive::ProgressType::EXTRACTION) { - int progress = static_cast(100 * current / total); - emit progressUpdate(progress); + currentProgress = static_cast(100 * current / total); + emit progressUpdate(); } }; - Archive::FileChangeCallback fileChangeCallback = [this](auto changeType, std::wstring const& file) { + Archive::FileChangeCallback fileChangeCallback = [this, ¤tFileName](auto changeType, std::wstring const& file) { if (changeType == Archive::FileChangeType::EXTRACTION_START) { - emit progressFileChange(QString::fromStdWString(file)); + currentFileName = QString::fromStdWString(file); + emit progressUpdate(); } }; - // Future watcher to exit the loop: + // unpack only the files we need for the installer QFutureWatcher futureWatcher; - - QEventLoop loop(this); - connect( - &futureWatcher, &QFutureWatcher::finished, - &loop, &QEventLoop::quit, + connect(&futureWatcher, &QFutureWatcher::finished, + &loop, &QEventLoop::wakeUp, Qt::QueuedConnection); - - // unpack only the files we need for the installer futureWatcher.setFuture(QtConcurrent::run([&]() -> bool { return m_ArchiveHandler->extract( extractPath.toStdWString(), @@ -220,14 +217,21 @@ bool InstallationManager::extractFiles(QString extractPath, QString title, bool ); })); - // Wait for future to complete: - loop.exec(); + installationProgress->setModal(true); + installationProgress->show(); + + do { + if (currentProgress != installationProgress->value()) { + installationProgress->setValue(currentProgress); + } + if (currentFileName != installationProgress->labelText()) { + installationProgress->setLabelText(currentFileName); + } + loop.processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents); + } while (!futureWatcher.isFinished()); + + installationProgress->hide(); - // There might still be signal in the queue at this point but the progress dialog is - // going to be destroyed so we must disconnect the signals: - disconnect(this, &InstallationManager::progressUpdate, installationProgress, &QProgressDialog::setValue); - disconnect(this, &InstallationManager::progressFileChange, installationProgress, &QProgressDialog::setLabelText); - future = futureWatcher.future(); } diff --git a/src/installationmanager.h b/src/installationmanager.h index 3da74ec4..695f2ed6 100644 --- a/src/installationmanager.h +++ b/src/installationmanager.h @@ -223,12 +223,7 @@ signals: /** * @brief Progress update from the extraction. */ - void progressUpdate(int percentage); - - /** - * @brief File change update from the extraction. - */ - void progressFileChange(QString const& value); + void progressUpdate(); private: -- cgit v1.3.1