From 7012040c320f91cd2e1882cbb5c213771538a11c Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Tue, 26 Mar 2024 22:51:11 -0500 Subject: [PATCH] Don't use a timer to update the download dialog --- scwx-qt/source/scwx/qt/ui/download_dialog.cpp | 65 +++++-------------- 1 file changed, 18 insertions(+), 47 deletions(-) diff --git a/scwx-qt/source/scwx/qt/ui/download_dialog.cpp b/scwx-qt/source/scwx/qt/ui/download_dialog.cpp index 30763f4e..235ad9bf 100644 --- a/scwx-qt/source/scwx/qt/ui/download_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/download_dialog.cpp @@ -6,7 +6,6 @@ #include #include #include -#include namespace scwx { @@ -18,31 +17,14 @@ namespace ui class DownloadDialog::Impl { public: - explicit Impl(DownloadDialog* self) : self_ {self} - { - updateTimer_.setSingleShot(true); - updateTimer_.setInterval(0); - - QObject::connect(&updateTimer_, - &QTimer::timeout, - self_, - [this]() { UpdateProgress(); }); - }; + explicit Impl() {}; ~Impl() = default; - void UpdateProgress(); - - DownloadDialog* self_; - boost::timer::cpu_timer timer_ {}; - QTimer updateTimer_ {}; - - std::ptrdiff_t downloadedBytes_ {}; - std::ptrdiff_t totalBytes_ {}; }; DownloadDialog::DownloadDialog(QWidget* parent) : - ProgressDialog(parent), p {std::make_unique(this)} + ProgressDialog(parent), p {std::make_unique()} { auto buttonBox = button_box(); buttonBox->setStandardButtons(QDialogButtonBox::StandardButton::Ok | @@ -50,6 +32,7 @@ DownloadDialog::DownloadDialog(QWidget* parent) : buttonBox->button(QDialogButtonBox::StandardButton::Ok) ->setText("Install Now"); + setWindowTitle(tr("Download File")); SetRange(0, 100); } @@ -68,6 +51,7 @@ void DownloadDialog::StartDownload() ->button(QDialogButtonBox::StandardButton::Ok) ->setVisible(false); + SetValue(0); SetBottomLabelText(tr("Waiting for download to begin...")); p->timer_.start(); show(); @@ -75,41 +59,17 @@ void DownloadDialog::StartDownload() void DownloadDialog::UpdateProgress(std::ptrdiff_t downloadedBytes, std::ptrdiff_t totalBytes) -{ - p->downloadedBytes_ = downloadedBytes; - p->totalBytes_ = totalBytes; - - // Use a one-shot timer to trigger an update, preventing multiple updates per - // frame - p->updateTimer_.start(); -} - -void DownloadDialog::FinishDownload() -{ - button_box()->button(QDialogButtonBox::StandardButton::Ok)->setVisible(true); -} - -void DownloadDialog::CancelDownload() -{ - SetValue(0); - SetBottomLabelText(tr("Error occurred while downloading")); -} - -void DownloadDialog::Impl::UpdateProgress() { using namespace std::chrono_literals; - const std::ptrdiff_t downloadedBytes = downloadedBytes_; - const std::ptrdiff_t totalBytes = totalBytes_; - - const std::chrono::nanoseconds elapsed {timer_.elapsed().wall}; + const std::chrono::nanoseconds elapsed {p->timer_.elapsed().wall}; const double percentComplete = (totalBytes > 0.0) ? static_cast(downloadedBytes) / totalBytes : 0.0; const int progressValue = static_cast(percentComplete * 100.0); - self_->SetValue(progressValue); + SetValue(progressValue); const std::chrono::seconds timeRemaining = (percentComplete > 0.0) ? @@ -126,7 +86,18 @@ void DownloadDialog::Impl::UpdateProgress() hoursRemaining.count(), timeRemaining); - self_->SetBottomLabelText(QString::fromStdString(progressText)); + SetBottomLabelText(QString::fromStdString(progressText)); +} + +void DownloadDialog::FinishDownload() +{ + button_box()->button(QDialogButtonBox::StandardButton::Ok)->setVisible(true); +} + +void DownloadDialog::CancelDownload() +{ + SetValue(0); + SetBottomLabelText(tr("Error occurred while downloading")); } } // namespace ui