mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 03:30:06 +00:00 
			
		
		
		
	Don't use a timer to update the download dialog
This commit is contained in:
		
							parent
							
								
									6448826d60
								
							
						
					
					
						commit
						7012040c32
					
				
					 1 changed files with 18 additions and 47 deletions
				
			
		|  | @ -6,7 +6,6 @@ | |||
| #include <fmt/format.h> | ||||
| #include <QDialogButtonBox> | ||||
| #include <QPushButton> | ||||
| #include <QTimer> | ||||
| 
 | ||||
| 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<Impl>(this)} | ||||
|     ProgressDialog(parent), p {std::make_unique<Impl>()} | ||||
| { | ||||
|    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<double>(downloadedBytes) / totalBytes : | ||||
|                            0.0; | ||||
|    const int progressValue = static_cast<int>(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
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat