-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathelapsedtimer.cpp
86 lines (69 loc) · 2.19 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
84
85
86
/*
* STM32Programming
*
* Copyright 2015 - 2020 yangfuyuan
*
*
*/
#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());
}
void ElapsedTimer::update(unsigned long long progress,
unsigned long long total) {
unsigned short eSec = 0, eMin = 0, eHour = 0;
unsigned short tSec = 0, tMin = 0, tHour = 0;
unsigned int baseSecs = timer->elapsed() / MS_PER_SEC;
eSec = baseSecs % SECS_PER_MIN;
if (baseSecs >= SECS_PER_MIN) {
eMin = baseSecs / SECS_PER_MIN;
if (baseSecs >= SECS_PER_HOUR) {
eHour = baseSecs / SECS_PER_HOUR;
}
}
unsigned int totalSecs = (unsigned int)((float)baseSecs * ((float)total /
(float)progress));
unsigned int totalMin = 0;
tSec = totalSecs % SECS_PER_MIN;
if (totalSecs >= SECS_PER_MIN) {
totalMin = totalSecs / SECS_PER_MIN;
tMin = totalMin % MINS_PER_HOUR;
if (totalMin > MINS_PER_HOUR) {
tHour = totalMin / MINS_PER_HOUR;
}
}
const QChar &fillChar = QLatin1Char('0');
QString qs = QString("%1:%2/").arg(eMin, 2, 10, fillChar).arg(eSec, 2, 10,
fillChar);
if (eHour > 0) {
qs.prepend(QString("%1:").arg(eHour, 2, 10, fillChar));
}
if (tHour > 0) {
qs += (QString("%1:").arg(tHour, 2, 10, fillChar));
}
qs += (QString("%1:%2 ").arg(tMin, 2, 10, fillChar).arg(tSec, 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.
setText(qs);
QCoreApplication::processEvents();//避免界面冻结
}
void ElapsedTimer::start() {
setVisible(true);
timer->start();
}
void ElapsedTimer::stop() {
setVisible(false);
setText(QString("0:00/0:00"));
}