-
Notifications
You must be signed in to change notification settings - Fork 0
/
elapsedtimer.cpp
83 lines (71 loc) · 2.14 KB
/
elapsedtimer.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
#include "elapsedtimer.h"
ElapsedTimer::ElapsedTimer(QWidget *parent)
: QLabel(parent)
{
timer = new QTime();
setTextFormat(Qt::PlainText);
setTextInteractionFlags(Qt::NoTextInteraction);
setText(QString("0:00/0:00"));
setVisible(false);
}
ElapsedTimer::~ElapsedTimer()
{
delete timer;
}
int ElapsedTimer::ms()
{
return(timer->elapsed());
}
// this uses a parameter to store the results rather than a return value in
// order to avoid mallocing the struct every time this function is called
// (which, potentially, is frequently).
void ElapsedTimer::secsToHMS(unsigned int secs, timeStruct_t *hms)
{
unsigned int mins = 0;
hms->sec = secs % SECS_PER_MIN;
if ( secs >= SECS_PER_MIN )
{
mins = (secs / SECS_PER_MIN);
hms->min = mins % MINS_PER_HOUR;
if ( secs >= SECS_PER_HOUR )
{
hms->hour = secs / SECS_PER_HOUR;
}
}
}
void ElapsedTimer::update(unsigned long long progress, unsigned long long total)
{
timeStruct_t tTime, eTime;
unsigned int baseSecs = timer->elapsed() / MS_PER_SEC;
unsigned int totalSecs = (unsigned int)((float)baseSecs * ( (float)total/(float)progress ));
// convert seconds to hours:minues:seconds
secsToHMS(baseSecs, &eTime);
secsToHMS(totalSecs, &tTime);
// build the display string
const QChar & fillChar = QLatin1Char( '0' );
QString qs = QString("%1:%2/").arg(eTime.min, 2, 10, fillChar).arg(eTime.sec, 2, 10, fillChar);
if (eTime.hour > 0)
{
qs.prepend(QString("%1:").arg(eTime.hour, 2, 10, fillChar));
}
if (tTime.hour > 0)
{
qs += (QString("%1:").arg(tTime.hour, 2, 10, fillChar));
}
qs += (QString("%1:%2 ").arg(tTime.min, 2, 10, fillChar).arg(tTime.sec, 2, 10, fillChar));
// added a space following the times to separate the text slightly from the right edge of the status bar...
// there's probably a more "QT-correct" way to do that (like, margins or something),
// but this was simple and effective.
// display
setText(qs);
}
void ElapsedTimer::start()
{
setVisible(true);
timer->start();
}
void ElapsedTimer::stop()
{
setVisible(false);
setText(QString("0:00/0:00 "));
}