-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.cpp
More file actions
88 lines (76 loc) · 2.11 KB
/
clock.cpp
File metadata and controls
88 lines (76 loc) · 2.11 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
#include "clock.hpp"
#include <QTimer>
Clock::Clock (QObject* parent)
: QObject{parent}
, m_timer{new QTimer(this)}
, m_secondsRemains{m_parameters.workTime}
, m_shortBreaks{0}
, m_currentStage{ClockStage::Work}
, m_parameters{} {
connect(m_timer, &QTimer::timeout, this, &Clock::onSecondTimeout);
}
int Clock::secondsRemains () const {
return m_secondsRemains;
}
void Clock::start (ClockStage stage) {
switch (stage) {
case ClockStage::Work:
m_secondsRemains = m_parameters.workTime;
break;
case ClockStage::ShortBreak:
m_secondsRemains = m_parameters.shortBreakTime;
break;
case ClockStage::LongBreak:
m_secondsRemains = m_parameters.longBreakTime;
break;
default:
return;
}
m_currentStage = stage;
m_timer->start(1000);
}
void Clock::stop () {
m_timer->stop();
}
void Clock::onSecondTimeout () {
--m_secondsRemains;
if (m_secondsRemains == 0) {
m_timer->stop();
onStageCompeted();
}
emit secondTimeout();
}
void Clock::onStageCompeted () {
if (m_currentStage == ClockStage::Work) {
if (m_shortBreaks >= parameters().maxShortBreaks) {
m_currentStage = ClockStage::LongBreak;
m_shortBreaks = 0;
} else {
m_currentStage = ClockStage::ShortBreak;
++m_shortBreaks;
}
} else {
m_currentStage = ClockStage::Work;
}
emit stageCompeted();
}
ClockParameters Clock::parameters () const {
return m_parameters;
}
void Clock::setParameters (const ClockParameters& parameters) {
m_parameters = parameters;
m_secondsRemains = m_parameters.workTime;
}
ClockStage Clock::nextStage () const {
return m_currentStage;
}
void Clock::setNextStage (ClockStage stage) {
m_timer->stop();
if (stage == ClockStage::Work) {
m_secondsRemains = parameters().workTime;
} else if (stage == ClockStage::ShortBreak) {
m_secondsRemains = parameters().shortBreakTime;
} else if (stage == ClockStage::LongBreak) {
m_secondsRemains = parameters().longBreakTime;
}
}