From d62ec9c867b448276ab9873ddd3f320487ab5572 Mon Sep 17 00:00:00 2001 From: Silarn Date: Tue, 1 May 2018 21:50:20 -0500 Subject: Allow downloads to display speed and complete % --- src/downloadlistwidget.cpp | 4 +++- src/downloadlistwidgetcompact.cpp | 3 ++- src/downloadmanager.cpp | 30 +++++++++++++++++++++++------- src/downloadmanager.h | 4 ++-- 4 files changed, 30 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/downloadlistwidget.cpp b/src/downloadlistwidget.cpp index cb7a49c3..2af74cc2 100644 --- a/src/downloadlistwidget.cpp +++ b/src/downloadlistwidget.cpp @@ -53,6 +53,7 @@ DownloadListWidgetDelegate::DownloadListWidgetDelegate(DownloadManager *manager, m_InstallLabel = m_ItemWidget->findChild("installLabel"); m_InstallLabel->setVisible(false); + m_Progress->setTextVisible(true); connect(manager, SIGNAL(stateChanged(int,DownloadManager::DownloadState)), this, SLOT(stateChanged(int,DownloadManager::DownloadState))); connect(manager, SIGNAL(downloadRemoved(int)), this, SLOT(resetCache(int))); @@ -159,7 +160,8 @@ void DownloadListWidgetDelegate::paintRegularDownload(int downloadIndex) const } else { m_InstallLabel->setVisible(false); m_Progress->setVisible(true); - m_Progress->setValue(m_Manager->getProgress(downloadIndex)); + m_Progress->setValue(m_Manager->getProgress(downloadIndex).first); + m_Progress->setFormat(m_Manager->getProgress(downloadIndex).second); } } diff --git a/src/downloadlistwidgetcompact.cpp b/src/downloadlistwidgetcompact.cpp index b390bff4..898d400a 100644 --- a/src/downloadlistwidgetcompact.cpp +++ b/src/downloadlistwidgetcompact.cpp @@ -138,7 +138,8 @@ void DownloadListWidgetCompactDelegate::paintRegularDownload(int downloadIndex) } else { m_DoneLabel->setVisible(false); m_Progress->setVisible(true); - m_Progress->setValue(m_Manager->getProgress(downloadIndex)); + m_Progress->setValue(m_Manager->getProgress(downloadIndex).first); + m_Progress->setFormat(m_Manager->getProgress(downloadIndex).second); } } diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 77a4cf98..097a1168 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -62,7 +62,7 @@ DownloadManager::DownloadInfo *DownloadManager::DownloadInfo::createNew(const Mo info->m_DownloadID = s_NextDownloadID++; info->m_StartTime.start(); info->m_PreResumeSize = 0LL; - info->m_Progress = 0; + info->m_Progress = std::make_pair(0, "0 bytes/sec"); info->m_ResumePos = 0; info->m_FileInfo = new ModRepositoryFileInfo(*fileInfo); info->m_Urls = URLs; @@ -852,7 +852,7 @@ qint64 DownloadManager::getFileSize(int index) const } -int DownloadManager::getProgress(int index) const +std::pair DownloadManager::getProgress(int index) const { if ((index < 0) || (index >= m_ActiveDownloads.size())) { throw MyException(tr("progress: invalid download index %1").arg(index)); @@ -1076,12 +1076,28 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) if (bytesTotal > info->m_TotalSize) { info->m_TotalSize = bytesTotal; } - int oldProgress = info->m_Progress; - info->m_Progress = ((info->m_ResumePos + bytesReceived) * 100) / (info->m_ResumePos + bytesTotal); - TaskProgressManager::instance().updateProgress(info->m_TaskProgressId, bytesReceived, bytesTotal); - if (oldProgress != info->m_Progress) { - emit update(index); + int oldProgress = info->m_Progress.first; + info->m_Progress.first = ((info->m_ResumePos + bytesReceived) * 100) / (info->m_ResumePos + bytesTotal); + + // calculate the download speed + double speed = bytesReceived * 1000.0 / info->m_StartTime.elapsed(); + QString unit; + if (speed < 1024) { + unit = "bytes/sec"; + } + else if (speed < 1024 * 1024) { + speed /= 1024; + unit = "kB/s"; + } + else { + speed /= 1024 * 1024; + unit = "MB/s"; } + + info->m_Progress.second = QString::fromLatin1("%1% - %2 %3").arg(info->m_Progress.first).arg(speed, 3, 'f', 1).arg(unit); + + TaskProgressManager::instance().updateProgress(info->m_TaskProgressId, bytesReceived, bytesTotal); + emit update(index); } } } catch (const std::bad_alloc&) { diff --git a/src/downloadmanager.h b/src/downloadmanager.h index 308d365b..0ff77011 100644 --- a/src/downloadmanager.h +++ b/src/downloadmanager.h @@ -75,7 +75,7 @@ private: QNetworkReply *m_Reply; QTime m_StartTime; qint64 m_PreResumeSize; - int m_Progress; + std::pair m_Progress; DownloadState m_State; int m_CurrentUrl; QStringList m_Urls; @@ -259,7 +259,7 @@ public: * @param index index of the file to look up * @return progress of the download in percent (integer) **/ - int getProgress(int index) const; + std::pair getProgress(int index) const; /** * @brief retrieve the current state of the download -- cgit v1.3.1