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 win32:LIBS += -L$$SCSTWLIBRARIES_LIB_OUTPUT_DIR -lScStwLibraries1
android {
ANDROID_EXTRA_LIBS += $$SCSTWLIBRARIES_LIB_OUTPUT_DIR/libScStwLibraries.so
}
INCLUDEPATH += "$$PWD" INCLUDEPATH += "$$PWD"
INCLUDEPATH += "$$PWD"/headers INCLUDEPATH += "$$PWD"/headers

View file

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

View file

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

View file

@ -63,6 +63,7 @@ public:
CANCELLED, /*!< Timer was cancelled */ CANCELLED, /*!< Timer was cancelled */
DISABLED /*!< Timer is disabled */ DISABLED /*!< Timer is disabled */
}; };
Q_ENUM(TimerState);
/*! /*!
* \brief The StopReason enum contains all possible reasons for a stop * \brief The StopReason enum contains all possible reasons for a stop
@ -186,6 +187,12 @@ public slots:
*/ */
double getReactionTime(); 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 * \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); this->nextActionTimer = new QTimer(this);
nextActionTimer->setSingleShot(true); nextActionTimer->setSingleShot(true);
this->nextActionLoop = new QEventLoop(); this->nextActionLoop = new QEventLoop();
this->nextStartAction = None;
connect(this->nextActionTimer, &QTimer::timeout, this->nextActionLoop, &QEventLoop::quit); connect(this->nextActionTimer, &QTimer::timeout, this->nextActionLoop, &QEventLoop::quit);
connect(this, &ScStwRace::nextStartActionChanged, this, &ScStwRace::nextStartActionDetailsChanged);
// write default settings // write default settings
this->startActionSettings.insert(Start, {{"Enabled", true}, {"Delay", 1}}); this->startActionSettings.insert(Start, {{"Enabled", true}, {"Delay", 1}});
@ -187,15 +189,15 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
return true; return true;
} }
this->nextStartAction = StartAction(thisAction + 1); if(thisAction < Start) {
qDebug() << "next action is: " << nextStartAction; this->nextStartAction = StartAction(thisAction + 1);
qDebug() << "next action is: " << nextStartAction;
int nextActionDelay = -1; int nextActionDelay = -1;
if(this->startActionSettings.contains(this->nextStartAction) && this->startActionSettings[this->nextStartAction]["Enabled"].toBool()) { if(this->startActionSettings.contains(this->nextStartAction) && this->startActionSettings[this->nextStartAction]["Enabled"].toBool()) {
nextActionDelay = this->startActionSettings[this->nextStartAction]["Delay"].toInt(); nextActionDelay = this->startActionSettings[this->nextStartAction]["Delay"].toInt();
} }
if(this->nextStartAction <= Start) {
// perform next action // perform next action
this->nextActionTimer->start(nextActionDelay > 0 ? nextActionDelay:1); this->nextActionTimer->start(nextActionDelay > 0 ? nextActionDelay:1);
if(nextActionDelay > 0) if(nextActionDelay > 0)
@ -269,7 +271,7 @@ void ScStwRace::refreshTimerStates() {
bool raceIsOver = true; bool raceIsOver = true;
foreach(ScStwTimer * timer, this->timers){ 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 // if the timer is not in stoped state
raceIsOver = false; raceIsOver = false;
break; break;
@ -307,9 +309,9 @@ void ScStwRace::handleTimerEnable(ScStwTimer* timer) {
} }
} }
QVariantMap ScStwRace::getNextStartActionDetails() { QVariantList ScStwRace::getNextStartActionDetails() {
int nextActionDelay = 0; int nextActionDelay = 0;
double nextActionDelayProg = 0; double nextActionDelayProg = -1;
if(this->nextStartAction == AtYourMarks || this->nextStartAction == Ready) { if(this->nextStartAction == AtYourMarks || this->nextStartAction == Ready) {
// get the total delay and the delay progress of the next action timer // get the total delay and the delay progress of the next action timer
@ -321,7 +323,11 @@ QVariantMap ScStwRace::getNextStartActionDetails() {
nextActionDelayProg = 1 - (remaining / nextActionDelay); 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) { 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::startCanceled, this, &ScStwRace::cancelStart);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates); connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates);
connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable); connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
return true; return true;
@ -385,6 +393,7 @@ QVariantList ScStwRace::getTimerDetailList() {
tmpTimer.insert("state", timer->getState()); tmpTimer.insert("state", timer->getState());
tmpTimer.insert("currentTime", timer->getCurrentTime()); tmpTimer.insert("currentTime", timer->getCurrentTime());
tmpTimer.insert("reactionTime", timer->getReactionTime()); tmpTimer.insert("reactionTime", timer->getReactionTime());
tmpTimer.insert("text", timer->getText());
tmpTimers.append(tmpTimer); tmpTimers.append(tmpTimer);
} }

View file

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

View file

@ -177,6 +177,42 @@ double ScStwTimer::getReactionTime() {
return this->reactionTime; 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) { void ScStwTimer::setDisabled(bool disabled) {
if(disabled) if(disabled)
this->setState(DISABLED); this->setState(DISABLED);