boxingtimer.cpp
#include <QObject>
#include <QTime>
#include "boxingtimer.h"
#include "boxingsettings.h"
BoxingTimer::BoxingTimer() :
mRound(0),
mStatus(Status::Stop),
mOldStatus(Status::Default) {
this->setTimerType(Qt::VeryCoarseTimer);
settings = new BoxingSettings();
mRemainingMilliseconds = settings->getRoundMilliseconds();
QObject::connect(this, SIGNAL(statusChanged()), this, SLOT(applyStatus()));
QObject::connect(this, SIGNAL(timeout()), this, SLOT(updateRemainingMilliseconds()));
}
const QString BoxingTimer::getPreset() {
return this->settings->getPreset();
}
void BoxingTimer::setRound(const int &round) {
this->mRound = round;
emit roundsToStringChanged();
}
void BoxingTimer::setRemainingMilliseconds(const int &milliseconds) {
this->mRemainingMilliseconds = milliseconds;
emit remainingTimeToStringChanged();
}
void BoxingTimer::setStatus(const Status &status) {
if (this->mStatus == Status::Start || this->mStatus == Status::Rest) {
this->mOldStatus = this->mStatus;
}
this->mStatus = status;
emit statusChanged();
}
void BoxingTimer::stop() {
QTimer::stop();
emit activeChanged();
}
void BoxingTimer::start(const int &milliseconds) {
QTimer::start(milliseconds);
emit activeChanged();
}
void BoxingTimer::applyStatus() {
switch (this->mStatus) {
case Status::Reset:
if (this->mOldStatus == Status::Start) {
this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
} else if (this->mOldStatus == Status::Rest) {
this->setRemainingMilliseconds(this->settings->getRestMilliseconds());
}
if (this->isActive()) {
this->setStatus(this->mOldStatus);
}
break;
case Status::Start:
case Status::Rest:
this->start(1000);
if (this->mRound == 0) {
this->nextRound();
}
break;
case Status::Stop:
this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
this->setRound(0);
case Status::Pause:
this->stop();
break;
default:
break;
}
}
void BoxingTimer::updateRemainingMilliseconds() {
this->setRemainingMilliseconds(this->mRemainingMilliseconds - 1000);
if (this->mRemainingMilliseconds < 0) {
if (this->mStatus == Status::Start) {
this->mStatus = Status::Rest;
this->setRemainingMilliseconds(this->settings->getRestMilliseconds());
} else if(this->mStatus == Status::Rest) {
this->mStatus = Status::Start;
this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
this->nextRound();
}
}
}
void BoxingTimer::nextRound() {
int newRound = this->mRound + 1;
if (newRound > this->settings->getRounds()) {
this->setStatus(Status::Stop);
} else {
this->setRound(newRound);
}
}
const QString BoxingTimer::roundsToString() {
return QString::number(this->mRound) + "/" + QString::number(this->settings->getRounds());
}
const QString BoxingTimer::remainingTimeToString() {
return QTime::fromMSecsSinceStartOfDay(this->mRemainingMilliseconds).toString(QString("mm:ss"));
}
BoxingTimer::BoxingTimer
BoxingTimer()
Default constructor.
Definition: boxingtimer.cpp:7
BoxingTimer::roundsToString
const QString roundsToString()
Convert round and total rounds for view.
Definition: boxingtimer.cpp:119
BoxingSettings
Definition: boxingsettings.h:6