-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountdowntimer.cpp
More file actions
135 lines (115 loc) · 2.81 KB
/
Copy pathcountdowntimer.cpp
File metadata and controls
135 lines (115 loc) · 2.81 KB
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
#include "countdowntimer.h"
namespace
{
/* Changes the minute countdown timers to seconds. */
int getSeconds(const TimerType type)
{
#define TIME_DEBUG 0
#if TIME_DEBUG
Q_UNUSED(type);
return 1;
#else
switch (type)
{
case TimerType::seconds:
return 1;
case TimerType::minutes:
return 60;
}
qDebug() << "Unhandled case in getSeconds()!";
return 60;
#endif
}
} // namespace
CountdownTimer::CountdownTimer(const TimerType timerType, const int interval, QObject* parent)
: QObject(parent), m_timerType(timerType), m_interval(interval)
{
m_countDownTimer = new QTimer(this);
m_countDownTimer->setSingleShot(true);
m_countDownTimer->setInterval(m_interval * 1000 * getSeconds(m_timerType));
connect(m_countDownTimer, SIGNAL(timeout()), this, SLOT(sendTimeout()));
m_tickTimer = new QTimer(this);
m_tickTimer->start(1000);
connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(sendTick()));
m_elapsedTimer.start();
calculateRemainder();
stop();
}
/* Starts timer */
void CountdownTimer::start()
{
m_paused = false;
m_countDownTimer->stop();
m_countDownTimer->start(m_interval * 1000 * getSeconds(m_timerType));
m_tickTimer->start(1000);
m_elapsedTimer.restart();
sendTick();
}
/* Toggles between paused and unpaused */
void CountdownTimer::pauseUnpause()
{
if (!m_paused)
{
// Pause timer
m_countDownTimer->stop();
m_countDownTimer->setInterval(m_remaining);
m_tickTimer->stop();
m_paused = true;
}
else
{
// Unpause timer
m_countDownTimer->start(m_remaining * 1000);
m_tickTimer->start(1000);
m_elapsedTimer.restart();
m_paused = false;
}
sendTick();
}
/* Stops timer */
void CountdownTimer::stop()
{
m_paused = false;
m_countDownTimer->stop();
m_tickTimer->stop();
emit tick(0);
}
/* Returning number of seconds remaining */
int CountdownTimer::remainingTime()
{
if (m_paused)
{
return m_remaining;
}
calculateRemainder();
return m_remaining;
}
/* Sets new interval */
bool CountdownTimer::setInterval(const int interval)
{
if (interval < 1)
{
qDebug() << "WARNING: The interval is less than 1. interval: " << interval;
return false;
}
m_interval = interval;
return true;
}
/* Sends timeout signal */
void CountdownTimer::sendTimeout()
{
this->stop();
emit timeout();
}
/* Sends tick signal every second.
* Helps with updating UI */
void CountdownTimer::sendTick()
{
emit tick(remainingTime());
}
void CountdownTimer::calculateRemainder()
{
const int interval = m_countDownTimer->interval();
const qint64 elapsed = m_elapsedTimer.elapsed();
m_remaining = qRound(static_cast<double>(interval - elapsed) / 1000);
}