Don't use a timer to update the download dialog

This commit is contained in:
Dan Paulat 2024-03-26 22:51:11 -05:00
parent 6448826d60
commit 7012040c32

View file

@ -6,7 +6,6 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QPushButton> #include <QPushButton>
#include <QTimer>
namespace scwx namespace scwx
{ {
@ -18,31 +17,14 @@ namespace ui
class DownloadDialog::Impl class DownloadDialog::Impl
{ {
public: public:
explicit Impl(DownloadDialog* self) : self_ {self} explicit Impl() {};
{
updateTimer_.setSingleShot(true);
updateTimer_.setInterval(0);
QObject::connect(&updateTimer_,
&QTimer::timeout,
self_,
[this]() { UpdateProgress(); });
};
~Impl() = default; ~Impl() = default;
void UpdateProgress();
DownloadDialog* self_;
boost::timer::cpu_timer timer_ {}; boost::timer::cpu_timer timer_ {};
QTimer updateTimer_ {};
std::ptrdiff_t downloadedBytes_ {};
std::ptrdiff_t totalBytes_ {};
}; };
DownloadDialog::DownloadDialog(QWidget* parent) : DownloadDialog::DownloadDialog(QWidget* parent) :
ProgressDialog(parent), p {std::make_unique<Impl>(this)} ProgressDialog(parent), p {std::make_unique<Impl>()}
{ {
auto buttonBox = button_box(); auto buttonBox = button_box();
buttonBox->setStandardButtons(QDialogButtonBox::StandardButton::Ok | buttonBox->setStandardButtons(QDialogButtonBox::StandardButton::Ok |
@ -50,6 +32,7 @@ DownloadDialog::DownloadDialog(QWidget* parent) :
buttonBox->button(QDialogButtonBox::StandardButton::Ok) buttonBox->button(QDialogButtonBox::StandardButton::Ok)
->setText("Install Now"); ->setText("Install Now");
setWindowTitle(tr("Download File"));
SetRange(0, 100); SetRange(0, 100);
} }
@ -68,6 +51,7 @@ void DownloadDialog::StartDownload()
->button(QDialogButtonBox::StandardButton::Ok) ->button(QDialogButtonBox::StandardButton::Ok)
->setVisible(false); ->setVisible(false);
SetValue(0);
SetBottomLabelText(tr("Waiting for download to begin...")); SetBottomLabelText(tr("Waiting for download to begin..."));
p->timer_.start(); p->timer_.start();
show(); show();
@ -75,41 +59,17 @@ void DownloadDialog::StartDownload()
void DownloadDialog::UpdateProgress(std::ptrdiff_t downloadedBytes, void DownloadDialog::UpdateProgress(std::ptrdiff_t downloadedBytes,
std::ptrdiff_t totalBytes) 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; using namespace std::chrono_literals;
const std::ptrdiff_t downloadedBytes = downloadedBytes_; const std::chrono::nanoseconds elapsed {p->timer_.elapsed().wall};
const std::ptrdiff_t totalBytes = totalBytes_;
const std::chrono::nanoseconds elapsed {timer_.elapsed().wall};
const double percentComplete = const double percentComplete =
(totalBytes > 0.0) ? static_cast<double>(downloadedBytes) / totalBytes : (totalBytes > 0.0) ? static_cast<double>(downloadedBytes) / totalBytes :
0.0; 0.0;
const int progressValue = static_cast<int>(percentComplete * 100.0); const int progressValue = static_cast<int>(percentComplete * 100.0);
self_->SetValue(progressValue); SetValue(progressValue);
const std::chrono::seconds timeRemaining = const std::chrono::seconds timeRemaining =
(percentComplete > 0.0) ? (percentComplete > 0.0) ?
@ -126,7 +86,18 @@ void DownloadDialog::Impl::UpdateProgress()
hoursRemaining.count(), hoursRemaining.count(),
timeRemaining); 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 } // namespace ui