Skip to content

Commit ec488ff

Browse files
i-am-spacekelson42
authored andcommitted
fix: added time left to complete download in library
1 parent acf878b commit ec488ff

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/contentmanagerdelegate.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ void createCancelButton(QPainter *painter, const QRect& r)
8484
painter->setFont(oldFont);
8585
}
8686

87-
void createDownloadStats(QPainter *painter, QRect box, QString downloadSpeed, QString completedLength)
87+
// Modified to draw three lines: download speed, completed length and estimated time remaining.
88+
void createDownloadStats(QPainter *painter, QRect box, QString downloadSpeed, QString completedLength, QString timeRemaining)
8889
{
8990
QPen pen;
9091
int x = box.x();
@@ -95,10 +96,12 @@ void createDownloadStats(QPainter *painter, QRect box, QString downloadSpeed, QS
9596
painter->setPen(pen);
9697
auto oldFont = painter->font();
9798
painter->setFont(QFont("Selawik", 8));
98-
QRect nRect(x - 20, y - 10, w, h);
99-
painter->drawText(nRect,Qt::AlignCenter | Qt::AlignJustify, downloadSpeed);
100-
QRect fRect(x - 20, y + 10, w, h);
101-
painter->drawText(fRect,Qt::AlignCenter | Qt::AlignJustify, completedLength);
99+
QRect speedRect(x - 20, y - 10, w, h);
100+
painter->drawText(speedRect, Qt::AlignCenter | Qt::AlignJustify, downloadSpeed);
101+
QRect completedRect(x - 20, y + 10, w, h);
102+
painter->drawText(completedRect, Qt::AlignCenter | Qt::AlignJustify, completedLength);
103+
QRect timeRect(x - 20, y + 30, w, h);
104+
painter->drawText(timeRect, Qt::AlignCenter | Qt::AlignJustify, timeRemaining);
102105
painter->setFont(oldFont);
103106
}
104107

@@ -131,13 +134,15 @@ void showDownloadProgress(QPainter *painter, QRect box, const DownloadState& dow
131134
progress = -progress;
132135
auto completedLength = downloadInfo.completedLength;
133136
auto downloadSpeed = downloadInfo.getDownloadSpeed();
137+
auto timeRemaining = downloadInfo.getEstimatedTimeRemaining();
134138

135139
if (downloadInfo.getStatus() == DownloadState::PAUSED) {
136140
createResumeSymbol(painter, dcl.pauseResumeButtonRect);
137141
createCancelButton(painter, dcl.cancelButtonRect);
138142
} else if (downloadInfo.getStatus() == DownloadState::DOWNLOADING) {
139143
createPauseSymbol(painter, dcl.pauseResumeButtonRect);
140-
createDownloadStats(painter, box, downloadSpeed, completedLength);
144+
// Pass the estimated time remaining to display it
145+
createDownloadStats(painter, box, downloadSpeed, completedLength, timeRemaining);
141146
}
142147

143148
QPen pen;

src/downloadmanagement.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ DownloadState::Status getDownloadStatus(QString status)
3535
return DownloadState::UNKNOWN;
3636
}
3737

38+
QString formatTimeRemaining(double seconds)
39+
{
40+
int totalSec = int(seconds);
41+
int hours = totalSec / 3600;
42+
int minutes = (totalSec % 3600) / 60;
43+
int secs = totalSec % 60;
44+
if (hours > 0)
45+
return QString("%1:%2:%3")
46+
.arg(hours, 2, 10, QChar('0'))
47+
.arg(minutes, 2, 10, QChar('0'))
48+
.arg(secs, 2, 10, QChar('0'));
49+
else
50+
return QString("%1:%2")
51+
.arg(minutes, 2, 10, QChar('0'))
52+
.arg(secs, 2, 10, QChar('0'));
53+
}
54+
3855
} // unnamed namespace
3956

4057
bool DownloadState::isLateUpdateInfo(const DownloadInfo& info) const
@@ -49,11 +66,17 @@ bool DownloadState::isLateUpdateInfo(const DownloadInfo& info) const
4966
void DownloadState::update(const DownloadInfo& info)
5067
{
5168
const auto completedBytes = info["completedLength"].toDouble();
52-
const double percentage = completedBytes / info["totalLength"].toDouble();
69+
const double totalBytes = info["totalLength"].toDouble();
70+
const double percentage = completedBytes / totalBytes;
5371

5472
progress = QString::number(100 * percentage, 'g', 3).toDouble();
5573
completedLength = convertToUnits(completedBytes);
56-
downloadSpeed = convertToUnits(info["downloadSpeed"].toDouble()) + "/s";
74+
double speedVal = info["downloadSpeed"].toDouble();
75+
downloadSpeed = convertToUnits(speedVal) + "/s";
76+
// Compute estimated time remaining
77+
double remainingSeconds = (speedVal > 0) ? ((totalBytes - completedBytes) / speedVal) : 0;
78+
estimatedTimeRemaining = (speedVal > 0) ? formatTimeRemaining(remainingSeconds)
79+
: "---";
5780
if ( !isLateUpdateInfo(info) ) {
5881
status = getDownloadStatus(info["status"].toString());
5982
}
@@ -229,12 +252,20 @@ DownloadInfo DownloadManager::getDownloadInfo(QString bookId) const
229252
const auto d = mp_downloader->getDownload(b.getDownloadId());
230253
d->updateStatus(true);
231254

255+
// Calculate estimated time remaining.
256+
double total = d->getTotalLength();
257+
double completed = d->getCompletedLength();
258+
double speed = d->getDownloadSpeed(); // bytes per second
259+
double remainingSeconds = (speed > 0) ? ((total - completed) / speed) : 0;
260+
QString timeRemaining = (speed > 0) ? formatTimeRemaining(remainingSeconds) : "---";
261+
232262
return {
233-
{ "status" , downloadStatus2QString(d->getStatus()) },
234-
{ "completedLength" , QString::number(d->getCompletedLength()) },
235-
{ "totalLength" , QString::number(d->getTotalLength()) },
236-
{ "downloadSpeed" , QString::number(d->getDownloadSpeed()) },
237-
{ "path" , QString::fromStdString(d->getPath()) }
263+
{ "status" , downloadStatus2QString(d->getStatus()) },
264+
{ "completedLength" , QString::number(d->getCompletedLength()) },
265+
{ "totalLength" , QString::number(d->getTotalLength()) },
266+
{ "downloadSpeed" , QString::number(d->getDownloadSpeed()) },
267+
{ "estimatedTimeRemaining", timeRemaining },
268+
{ "path" , QString::fromStdString(d->getPath()) }
238269
};
239270
}
240271

src/downloadmanagement.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class DownloadState
7979

8080
double progress = 0;
8181
QString completedLength;
82+
// New member for estimated time remaining
83+
QString estimatedTimeRemaining;
8284

8385
public: // functions
8486
void update(const DownloadInfo& info);
@@ -96,6 +98,9 @@ class DownloadState
9698
// time in seconds since last update
9799
double timeSinceLastUpdate() const;
98100

101+
// Optional accessor:
102+
QString getEstimatedTimeRemaining() const { return estimatedTimeRemaining; }
103+
99104
private: // data
100105
Status status = UNKNOWN;
101106
QString downloadSpeed;

0 commit comments

Comments
 (0)