diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp index 4727901..86c41e7 100644 --- a/src/contentmanagerdelegate.cpp +++ b/src/contentmanagerdelegate.cpp @@ -129,7 +129,7 @@ void showDownloadProgress(QPainter *painter, QRect box, const DownloadState& dow double progress = (double) (downloadInfo.progress) / 100; progress = -progress; auto completedLength = downloadInfo.completedLength; - auto downloadSpeed = downloadInfo.downloadSpeed; + auto downloadSpeed = downloadInfo.getDownloadSpeed(); if (downloadInfo.status == DownloadState::PAUSED) { createResumeSymbol(painter, dcl.pauseResumeButtonRect); diff --git a/src/downloadmanagement.cpp b/src/downloadmanagement.cpp index f1011c6..7686d50 100644 --- a/src/downloadmanagement.cpp +++ b/src/downloadmanagement.cpp @@ -37,15 +37,29 @@ DownloadState::Status getDownloadStatus(QString status) } // unnamed namespace -void DownloadState::update(const DownloadInfo& downloadInfos) +void DownloadState::update(const DownloadInfo& info) { - double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble(); - percent *= 100; - percent = QString::number(percent, 'g', 3).toDouble(); - auto completedLength = convertToUnits(downloadInfos["completedLength"].toDouble()); - auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toDouble()) + "/s"; - const auto status = getDownloadStatus(downloadInfos["status"].toString()); - *this = {percent, completedLength, downloadSpeed, status}; + const auto completedBytes = info["completedLength"].toDouble(); + const double percentage = completedBytes / info["totalLength"].toDouble(); + + progress = QString::number(100 * percentage, 'g', 3).toDouble(); + completedLength = convertToUnits(completedBytes); + downloadSpeed = convertToUnits(info["downloadSpeed"].toDouble()) + "/s"; + status = getDownloadStatus(info["status"].toString()); + lastUpdated = std::chrono::steady_clock::now(); +} + +double DownloadState::timeSinceLastUpdate() const +{ + typedef std::chrono::duration Seconds; + + const auto dt = std::chrono::steady_clock::now() - lastUpdated; + return std::chrono::duration_cast(dt).count(); +} + +QString DownloadState::getDownloadSpeed() const +{ + return timeSinceLastUpdate() > 2.0 ? "---" : downloadSpeed; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/downloadmanagement.h b/src/downloadmanagement.h index 23c2256..bd1b3e2 100644 --- a/src/downloadmanagement.h +++ b/src/downloadmanagement.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -18,7 +19,7 @@ typedef QMap DownloadInfo; class DownloadState { -public: +public: // types enum Status { UNKNOWN, WAITING, @@ -27,13 +28,23 @@ public: PAUSED }; + +public: // data + double progress = 0; QString completedLength; - QString downloadSpeed; Status status = UNKNOWN; -public: +public: // functions void update(const DownloadInfo& info); + QString getDownloadSpeed() const; + + // time in seconds since last update + double timeSinceLastUpdate() const; + +private: // data + QString downloadSpeed; + std::chrono::steady_clock::time_point lastUpdated; }; class DownloadManager : public QObject