This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
forked from jeremysanders/okularplugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
progresswidget.cpp
165 lines (132 loc) · 5.02 KB
/
progresswidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright (C) 2012 André Frimberger [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "progresswidget.h"
#include <KDebug>
ProgressWidget::ProgressWidget(QWidget *parent) : QWidget(parent), m_ui(new Ui::ProgressWidget()),
m_lastTimestamp(0), m_bytesDownloadedTotalLastInterval(0),
m_progressBarValue(0), m_avgDownloadSpeedWeighted(0),
m_vecDownloadSpeeds(QVector<float>())
{
m_ui->setupUi(this);
m_ui->lblErrorMessage->setText("");
m_ui->lblDownloadSpeed->setText("");
m_ui->lblRemainingDlTime->setText("");
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateDownloadSpeed()));
m_timer->setInterval(1500);
m_timer->start();
}
void ProgressWidget::updateDownloadSpeed()
{
qint64 currTimestamp = QDateTime::currentMSecsSinceEpoch();
if (m_lastTimestamp == 0) {
m_lastTimestamp = currTimestamp - m_timer->interval();
}
qint64 intervalLength = currTimestamp - m_lastTimestamp;
qint64 amountDownloadedTotal = m_progressBarValue;
qint64 amountDownloaded = amountDownloadedTotal - m_bytesDownloadedTotalLastInterval;
qint64 amountRemaining = m_ui->progressBar->maximum() - amountDownloadedTotal;
float currDownloadSpeed = (float)amountDownloaded / (float)intervalLength;
if (m_avgDownloadSpeedWeighted == 0)
m_avgDownloadSpeedWeighted = currDownloadSpeed;
if (m_vecDownloadSpeeds.size() >= 180) {
m_vecDownloadSpeeds.pop_front();
}
m_vecDownloadSpeeds.push_back(currDownloadSpeed);
#ifdef DEBUG_PROGRESSWIDGET
m_vecDownloadSpeedsForEstimation.push_back(currDownloadSpeed);
#endif
float avgDownloadSpeed = 0;
for (int i=0; i<m_vecDownloadSpeeds.size(); ++i) {
avgDownloadSpeed += m_vecDownloadSpeeds[i];
}
avgDownloadSpeed /= m_vecDownloadSpeeds.size();
m_avgDownloadSpeedWeighted = 0.02 * currDownloadSpeed + 0.70 * m_avgDownloadSpeedWeighted + 0.28 * avgDownloadSpeed;
QTime remainingDlTime = QTime();
remainingDlTime = remainingDlTime.addSecs(amountRemaining / (float) m_avgDownloadSpeedWeighted / 1000.0f);
#ifdef DEBUG_PROGRESSWIDGET
kWarning() << "remaining msces = " << amountRemaining / m_avgDownloadSpeedWeighted;
kWarning() << remainingDlTime;
kWarning() << "remainingTime = " << remainingDlTime.toString();
kWarning() << "currDownload Speed = " << currDownloadSpeed;
if (m_vecDownloadSpeedsForEstimation.size() % 60 == 0) {
calcEstimationError();
}
#endif
m_ui->lblRemainingDlTime->setText(remainingDlTime.toString("h:mm:ss"));
m_ui->lblDownloadSpeed->setText(QString::number(m_avgDownloadSpeedWeighted, 'f', 2) + " KB/s");
m_bytesDownloadedTotalLastInterval = amountDownloadedTotal;
m_lastTimestamp = currTimestamp;
}
#ifdef DEBUG_PROGRESSWIDGET
void ProgressWidget::calcEstimationError() {
int sumDownloadSpeeds = 0;
for (int i=0; i<m_vecDownloadSpeedsForEstimation.size(); ++i) {
sumDownloadSpeeds += m_vecDownloadSpeedsForEstimation[i];
}
kWarning() << "avg = " << (float) sumDownloadSpeeds / (float) m_vecDownloadSpeedsForEstimation.size();
kWarning() << "deviation = " << (float) sumDownloadSpeeds / (float) m_vecDownloadSpeedsForEstimation.size() - m_avgDownloadSpeedWeighted;
}
#endif
void ProgressWidget::setMaximum(int size)
{
m_ui->progressBar->setMaximum(size);
}
int ProgressWidget::getMaximum()
{
return m_ui->progressBar->maximum();
}
void ProgressWidget::setMinimum(int size)
{
m_ui->progressBar->setMinimum(size);
}
void ProgressWidget::setValue(int size)
{
if (size >= m_ui->progressBar->maximum()) {
m_timer->stop();
} else if (! m_timer->isActive()) {
m_timer->start();
}
m_progressBarValue = size;
m_ui->progressBar->setValue(size);
}
int ProgressWidget::getValue()
{
return m_ui->progressBar->value();
}
void ProgressWidget::setFileName(const QString& fileName)
{
m_ui->lblFilename->setText("Downloading " + fileName + " ...");
m_fileName = fileName;
}
void ProgressWidget::setErrorMessage ( const QString& errMsg )
{
m_ui->lblErrorMessage->setText(errMsg);
}
void ProgressWidget::resetProgressBar() {
m_timer->stop();
m_ui->progressBar->setValue(m_ui->progressBar->minimum());
m_ui->lblDownloadSpeed->setText("");
m_ui->lblRemainingDlTime->setText("");
m_vecDownloadSpeeds.clear();
m_lastTimestamp = 0;
m_bytesDownloadedTotalLastInterval = 0;
m_avgDownloadSpeedWeighted = 0;
}
ProgressWidget::~ProgressWidget()
{
m_timer->stop();
delete m_timer;
delete m_ui;
}