Boxing Timer  1.0
boxingtimer.cpp
1 #include <QObject>
2 #include <QTime>
3 
4 #include "boxingtimer.h"
5 #include "boxingsettings.h"
6 
8  mRound(0),
9  mStatus(Status::Stop),
10  mOldStatus(Status::Default) {
11  this->setTimerType(Qt::VeryCoarseTimer);
12 
13  settings = new BoxingSettings();
14 
15  mRemainingMilliseconds = settings->getRoundMilliseconds();
16 
17  QObject::connect(this, SIGNAL(statusChanged()), this, SLOT(applyStatus()));
18  QObject::connect(this, SIGNAL(timeout()), this, SLOT(updateRemainingMilliseconds()));
19 }
20 
21 const QString BoxingTimer::getPreset() {
22  return this->settings->getPreset();
23 }
24 
25 void BoxingTimer::setRound(const int &round) {
26  this->mRound = round;
27 
28  emit roundsToStringChanged();
29 }
30 
31 void BoxingTimer::setRemainingMilliseconds(const int &milliseconds) {
32  this->mRemainingMilliseconds = milliseconds;
33 
34  emit remainingTimeToStringChanged();
35 }
36 
37 void BoxingTimer::setStatus(const Status &status) {
38  if (this->mStatus == Status::Start || this->mStatus == Status::Rest) {
39  this->mOldStatus = this->mStatus;
40  }
41 
42  this->mStatus = status;
43 
44  emit statusChanged();
45 }
46 
47 void BoxingTimer::stop() {
48  QTimer::stop();
49 
50  emit activeChanged();
51 }
52 
53 void BoxingTimer::start(const int &milliseconds) {
54  QTimer::start(milliseconds);
55 
56  emit activeChanged();
57 }
58 
59 void BoxingTimer::applyStatus() {
60  switch (this->mStatus) {
61  case Status::Reset:
62  if (this->mOldStatus == Status::Start) {
63  this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
64  } else if (this->mOldStatus == Status::Rest) {
65  this->setRemainingMilliseconds(this->settings->getRestMilliseconds());
66  }
67 
68  if (this->isActive()) {
69  this->setStatus(this->mOldStatus);
70  }
71 
72  break;
73  case Status::Start:
74  case Status::Rest:
75  this->start(1000);
76 
77  if (this->mRound == 0) {
78  this->nextRound();
79  }
80 
81  break;
82  case Status::Stop:
83  this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
84  this->setRound(0);
85  case Status::Pause:
86  this->stop();
87 
88  break;
89  default:
90  break;
91  }
92 }
93 
94 void BoxingTimer::updateRemainingMilliseconds() {
95  this->setRemainingMilliseconds(this->mRemainingMilliseconds - 1000);
96 
97  if (this->mRemainingMilliseconds < 0) {
98  if (this->mStatus == Status::Start) {
99  this->mStatus = Status::Rest;
100  this->setRemainingMilliseconds(this->settings->getRestMilliseconds());
101  } else if(this->mStatus == Status::Rest) {
102  this->mStatus = Status::Start;
103  this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
104 
105  this->nextRound();
106  }
107  }
108 }
109 
110 void BoxingTimer::nextRound() {
111  int newRound = this->mRound + 1;
112  if (newRound > this->settings->getRounds()) {
113  this->setStatus(Status::Stop);
114  } else {
115  this->setRound(newRound);
116  }
117 }
118 
120  return QString::number(this->mRound) + "/" + QString::number(this->settings->getRounds());
121 }
122 
123 const QString BoxingTimer::remainingTimeToString() {
124  return QTime::fromMSecsSinceStartOfDay(this->mRemainingMilliseconds).toString(QString("mm:ss"));
125 }
BoxingTimer()
Default constructor.
Definition: boxingtimer.cpp:7
const QString roundsToString()
Convert round and total rounds for view.