diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-06-05 20:36:35 +0200 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-06-05 20:36:35 +0200 |
| commit | e49ce3ed21b10b9af21caaabd8c2f1339fe59076 (patch) | |
| tree | 3145e1793d3ea36b17dbe92a8075c6b341b09f77 | |
| parent | 8fa905cde626067f7cb4dcec473049228b831d9d (diff) | |
Use QEventLoop to process events when extracting instead of manual loop with processEvents.
| -rw-r--r-- | src/installationmanager.cpp | 45 | ||||
| -rw-r--r-- | src/installationmanager.h | 13 |
2 files changed, 36 insertions, 22 deletions
diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index f4b19c71..7f4ad864 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -155,24 +155,28 @@ bool InstallationManager::extractFiles(QString extractPath, QString title, bool // extracted... installationProgress->setAutoReset(false); - installationProgress->show(); + // 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); - auto start = std::chrono::system_clock::now(); + // Cancelling progress only cancel the extraction, we do not force exiting the event-loop: + connect(installationProgress, &QProgressDialog::canceled, [this]() { m_ArchiveHandler->cancel(); }); + + installationProgress->show(); // Variable updated by the callbacks: - int progress = 0; - QString progressFile; QString errorMessage; // The callbacks: - auto progressCallback = [&progress](auto progressType, uint64_t current, uint64_t total) { + auto progressCallback = [this](auto progressType, uint64_t current, uint64_t total) { if (progressType == Archive::ProgressType::EXTRACTION) { - progress = static_cast<int>(100 * current / total); + int progress = static_cast<int>(100 * current / total); + emit progressUpdate(progress); } }; - Archive::FileChangeCallback fileChangeCallback = [&progressFile](auto changeType, std::wstring const& file) { + Archive::FileChangeCallback fileChangeCallback = [this](auto changeType, std::wstring const& file) { if (changeType == Archive::FileChangeType::EXTRACTION_START) { - progressFile = QString::fromStdWString(file); + emit progressFileChange(QString::fromStdWString(file)); } }; auto errorCallback = [&errorMessage, this](std::wstring const& message) { @@ -180,6 +184,8 @@ bool InstallationManager::extractFiles(QString extractPath, QString title, bool errorMessage = QString::fromStdWString(message); }; + auto start = std::chrono::system_clock::now(); + // unpack only the files we need for the installer QFuture<bool> future = QtConcurrent::run([&]() -> bool { return m_ArchiveHandler->extract( @@ -190,20 +196,15 @@ bool InstallationManager::extractFiles(QString extractPath, QString title, bool ); }); - // Wait for the extraction to complete (or be cancelled): - do { - if (installationProgress->wasCanceled()) { - m_ArchiveHandler->cancel(); - installationProgress->reset(); - } - if (progress != installationProgress->value()) { - installationProgress->setValue(progress); - } - if (showFilenames && progressFile != installationProgress->labelText()) { - installationProgress->setLabelText(progressFile); - } - QCoreApplication::processEvents(); - } while (!future.isFinished()); + // Future watcher to exit the loop: + QFutureWatcher<bool> futureWatcher; + futureWatcher.setFuture(future); + + QEventLoop loop(this); + connect(&futureWatcher, &QFutureWatcher<bool>::finished, &loop, &QEventLoop::quit); + + // Wait for future to complete: + loop.exec(); // Check the result: if (!future.result()) { diff --git a/src/installationmanager.h b/src/installationmanager.h index 1b0c6d73..1d94151f 100644 --- a/src/installationmanager.h +++ b/src/installationmanager.h @@ -213,8 +213,21 @@ private slots: signals: + /** + * @brief Emitted when a password is requested from the archive wrapper. + */ void passwordRequested(); + /** + * @brief Progress update from the extraction. + */ + void progressUpdate(int percentage); + + /** + * @brief File change update from the extraction. + */ + void progressFileChange(QString const& value); + private: struct ByPriority { |
