Some changes for things to work with qml

This commit is contained in:
Dorian Zedler 2020-04-17 19:57:00 +02:00
parent c262935377
commit 73d62d01d8
Signed by: dorian
GPG key ID: 989DE36109AFA354
8 changed files with 88 additions and 22 deletions

View file

@ -12,5 +12,9 @@ unix:LIBS += -L$$SCSTWLIBRARIES_LIB_OUTPUT_DIR -lScStwLibraries
win32:LIBS += -L$$SCSTWLIBRARIES_LIB_OUTPUT_DIR -lScStwLibraries1
android {
ANDROID_EXTRA_LIBS += $$SCSTWLIBRARIES_LIB_OUTPUT_DIR/libScStwLibraries.so
}
INCLUDEPATH += "$$PWD"
INCLUDEPATH += "$$PWD"/headers

View file

@ -137,7 +137,7 @@ public:
* \see ScStw::BaseStationSetting
* \see ScStw::baseStationSettingFromString()
*/
static QString baseStationSettingToString(BaseStationSetting s);
Q_INVOKABLE static QString baseStationSettingToString(BaseStationSetting s);
/*!
* \brief Function to convert an int to a SignalKey
@ -148,8 +148,8 @@ public:
*/
static SignalKey signalKeyFromInt(int i);
private:
ScStw() : QObject(nullptr) {};
private:
};
#endif // SCSTW_HPP

View file

@ -12,15 +12,24 @@ class ScStwRace : public QObject
{
Q_OBJECT
Q_PROPERTY(RaceState state READ getState NOTIFY stateChanged)
Q_PROPERTY(QVariantList timers READ getTimerDetailList NOTIFY timersChanged)
Q_PROPERTY(QVariantList nextStartActionDetails READ getNextStartActionDetails NOTIFY nextStartActionDetailsChanged)
public:
explicit ScStwRace(QObject *parent = nullptr);
enum RaceState { IDLE, STARTING, WAITING, RUNNING, STOPPED };
Q_ENUM(RaceState)
enum StartAction { None = -1, AtYourMarks, Ready, Start };
enum StartAction { None = -1, AtYourMarks = 0, Ready = 1, Start = 2 };
Q_ENUM(StartAction)
enum NextStartActionDetailAttributes {
NextStartAction = 0,
NextStartActionTotalDelay = 1,
NextStartActionDelayProgress = 2
};
Q_ENUM(NextStartActionDetailAttributes);
private:
RaceState state;
@ -53,12 +62,16 @@ public slots:
void handleTimerStop();
int reset();
void cancelStart(bool falseStart);
QVariantMap getNextStartActionDetails();
// setters
bool writeStartActionSetting(StartAction action, bool enabled, int delay);
bool setSoundVolume(double volume);
bool addTimer(ScStwTimer *timer);
// getters
RaceState getState();
StartAction getNextStartAction();
QVariantList getNextStartActionDetails();
QVariantList getTimerDetailList();
private slots:
@ -72,6 +85,8 @@ signals:
void resetTimers();
void stateChanged(RaceState state);
void nextStartActionChanged();
void nextStartActionDetailsChanged();
void timersChanged();
};

View file

@ -63,6 +63,7 @@ public:
CANCELLED, /*!< Timer was cancelled */
DISABLED /*!< Timer is disabled */
};
Q_ENUM(TimerState);
/*!
* \brief The StopReason enum contains all possible reasons for a stop
@ -186,6 +187,12 @@ public slots:
*/
double getReactionTime();
/*!
* \brief Function to get the text, a timer display is supposed to show
* \return The text to show
*/
QString getText();
/*!
* \brief Function to set if the timer is supposed to be disabled
*

View file

@ -11,8 +11,10 @@ ScStwRace::ScStwRace(QObject *parent) : QObject(parent)
this->nextActionTimer = new QTimer(this);
nextActionTimer->setSingleShot(true);
this->nextActionLoop = new QEventLoop();
this->nextStartAction = None;
connect(this->nextActionTimer, &QTimer::timeout, this->nextActionLoop, &QEventLoop::quit);
connect(this, &ScStwRace::nextStartActionChanged, this, &ScStwRace::nextStartActionDetailsChanged);
// write default settings
this->startActionSettings.insert(Start, {{"Enabled", true}, {"Delay", 1}});
@ -187,6 +189,7 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
return true;
}
if(thisAction < Start) {
this->nextStartAction = StartAction(thisAction + 1);
qDebug() << "next action is: " << nextStartAction;
@ -195,7 +198,6 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
nextActionDelay = this->startActionSettings[this->nextStartAction]["Delay"].toInt();
}
if(this->nextStartAction <= Start) {
// perform next action
this->nextActionTimer->start(nextActionDelay > 0 ? nextActionDelay:1);
if(nextActionDelay > 0)
@ -269,7 +271,7 @@ void ScStwRace::refreshTimerStates() {
bool raceIsOver = true;
foreach(ScStwTimer * timer, this->timers){
if(timer->getState() < ScStwTimer::WON){
if(timer->getState() < ScStwTimer::WON && timer->getState() != ScStwTimer::WAITING){
// if the timer is not in stoped state
raceIsOver = false;
break;
@ -307,9 +309,9 @@ void ScStwRace::handleTimerEnable(ScStwTimer* timer) {
}
}
QVariantMap ScStwRace::getNextStartActionDetails() {
QVariantList ScStwRace::getNextStartActionDetails() {
int nextActionDelay = 0;
double nextActionDelayProg = 0;
double nextActionDelayProg = -1;
if(this->nextStartAction == AtYourMarks || this->nextStartAction == Ready) {
// get the total delay and the delay progress of the next action timer
@ -321,7 +323,11 @@ QVariantMap ScStwRace::getNextStartActionDetails() {
nextActionDelayProg = 1 - (remaining / nextActionDelay);
}
return {{"nextAction", this->nextStartAction}, {"nextActionDelay", nextActionDelay}, {"nextActionDelayProg", nextActionDelayProg}};
return {
this->nextStartAction,
nextActionDelay,
nextActionDelayProg
};
}
bool ScStwRace::writeStartActionSetting(StartAction action, bool enabled, int delay) {
@ -362,6 +368,8 @@ bool ScStwRace::addTimer(ScStwTimer *timer) {
connect(timer, &ScStwTimer::startCanceled, this, &ScStwRace::cancelStart);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates);
connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
return true;
@ -385,6 +393,7 @@ QVariantList ScStwRace::getTimerDetailList() {
tmpTimer.insert("state", timer->getState());
tmpTimer.insert("currentTime", timer->getCurrentTime());
tmpTimer.insert("reactionTime", timer->getReactionTime());
tmpTimer.insert("text", timer->getText());
tmpTimers.append(tmpTimer);
}

View file

@ -38,6 +38,7 @@ bool ScStwSoundPlayer::play(int action, double volume, double *timeOfStart) {
return false;
// stop playback
if(this->audioOutput->state() == QAudio::ActiveState)
this->audioOutput->stop();
// update currently playing action
@ -61,8 +62,6 @@ bool ScStwSoundPlayer::play(int action, double volume, double *timeOfStart) {
// start
this->audioOutput->start(playbackFile);
qDebug() << "playback stared, took " << this->audioOutput->elapsedUSecs();
// pass the time of start if requested
if(timeOfStart != nullptr) {
*timeOfStart = QDateTime::currentMSecsSinceEpoch() - (this->audioOutput->elapsedUSecs() / 1000);
@ -111,7 +110,6 @@ bool ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
// wait until the audio output reports the sound is over
waitLoop->exec();
qDebug() << "waitLoop exited!! elapsed: " << this->audioOutput->elapsedUSecs() << " processed: " << this->audioOutput->processedUSecs();
// wait until the sound is actually over
// the timeOffset is the buffer time before the audio started!
int timeOffset = ((this->audioOutput->processedUSecs() - this->audioOutput->elapsedUSecs()) / 1000);
@ -128,8 +126,6 @@ bool ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
*timeOfStop = QDateTime::currentMSecsSinceEpoch() - latency;
}
qDebug() << "finished now";
return true;
}
@ -138,7 +134,6 @@ void ScStwSoundPlayer::handleStateChanged(QAudio::State newState)
switch (newState) {
case QAudio::IdleState:
// Finished playing (no more data)
qDebug() << "sound reported over!! elapsed: " << this->audioOutput->elapsedUSecs() << " processed: " << this->audioOutput->processedUSecs();
waitLoop->exit();
break;

View file

@ -177,6 +177,42 @@ double ScStwTimer::getReactionTime() {
return this->reactionTime;
}
QString ScStwTimer::getText() {
//qDebug() << this->getState();
QString newText;
switch (this->state) {
case ScStwTimer::IDLE:
newText = "0.000 sec";
break;
case ScStwTimer::STARTING:
newText = "0.000 sec";
break;
case ScStwTimer::WAITING:
newText = "please wait...";
break;
case ScStwTimer::RUNNING:
newText = QString::number( this->getCurrentTime() / 1000.0, 'f', 3 ) + " sec";
break;
case ScStwTimer::WON:
newText = QString::number( this->getCurrentTime() / 1000.0, 'f', 3 ) + " sec";
break;
case ScStwTimer::LOST:
newText = QString::number( this->getCurrentTime() / 1000.0, 'f', 3 ) + " sec";
break;
case ScStwTimer::FAILED:
newText = "false start";
break;
case ScStwTimer::CANCELLED:
newText = "cancelled";
break;
case ScStwTimer::DISABLED:
newText = "---";
break;
}
return newText;
}
void ScStwTimer::setDisabled(bool disabled) {
if(disabled)
this->setState(DISABLED);