This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
app/sources/speedtimerqmladapter.cpp

96 lines
2.5 KiB
C++

#include "headers/speedtimerqmladapter.h"
SpeedTimerQmlAdapter::SpeedTimerQmlAdapter(QObject *parent) : QObject(parent)
{
this->state = SpeedTimer::IDLE;
connect(pGlobalSpeedTimer, &SpeedTimer::stateChanged, this, &SpeedTimerQmlAdapter::setState);
connect(pGlobalSpeedTimer, &SpeedTimer::startCanceled, this, &SpeedTimerQmlAdapter::startCanceled);
this->refreshTimer = new QTimer();
refreshTimer->setInterval(1);
refreshTimer->setSingleShot(true);
refreshTimer->connect(refreshTimer, &QTimer::timeout, this, &SpeedTimerQmlAdapter::refreshValues);
refreshTimer->start();
}
QString SpeedTimerQmlAdapter::getState(){
switch(state){
case SpeedTimer::IDLE:
return("IDLE");
case SpeedTimer::STARTING:
return("STARTING");
case SpeedTimer::RUNNING:
return("RUNNING");
case SpeedTimer::STOPPED:
return("STOPPED");
}
}
void SpeedTimerQmlAdapter::setState(SpeedTimer::timerState newState){
this->state = newState;
qDebug() << "tmer state changed: " << newState;
emit this->stateChanged(newState);
}
QString SpeedTimerQmlAdapter::getText(){
return(this->text);
}
void SpeedTimerQmlAdapter::refreshValues(){
//qDebug() << this->getState();
QString newText;
switch (this->state) {
case SpeedTimer::IDLE:
newText = "Click Start to start";
break;
case SpeedTimer::STARTING:
newText = "0.000 sec";
break;
case SpeedTimer::RUNNING:
newText = QString::number( pGlobalSpeedTimer->getCurrTime() / 1000.0, 'f', 3 ) + " sec";
break;
case SpeedTimer::STOPPED:
newText = QString::number( pGlobalSpeedTimer->stoppedTime / 1000.0, 'f', 3 ) + " sec";
}
if(this->text != newText){
this->text = newText;
emit textChanged();
}
//qDebug() << this->text;
refreshTimer->start();
}
bool SpeedTimerQmlAdapter::setStarting(){
if(pGlobalSpeedTimer->remoteControlled){
return false;
}
pGlobalSpeedTimer->setState(SpeedTimer::STARTING);
return true;
}
bool SpeedTimerQmlAdapter::start(){
if(pGlobalSpeedTimer->remoteControlled){
return false;
}
pGlobalSpeedTimer->start();
return true;
}
bool SpeedTimerQmlAdapter::stop(QString type){
if(pGlobalSpeedTimer->remoteControlled){
return false;
}
pGlobalSpeedTimer->stop(type);
return true;
}
bool SpeedTimerQmlAdapter::reset(){
if(pGlobalSpeedTimer->remoteControlled){
return false;
}
pGlobalSpeedTimer->reset();
return true;
}