From fdb17bdc3a1356f09f14b4f8da0221aff6cc5129 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Mon, 19 Oct 2020 11:15:49 +0200 Subject: [PATCH] Fixed issues that ourred when switching between remote and local race mode --- .../headers/client/scstwremoterace.h | 2 + .../sources/client/scstwremoterace.cpp | 41 ++++++++++++++----- .../sources/client/scstwremotesettings.cpp | 5 ++- ScStwLibraries/sources/scstwsetting.cpp | 1 + 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/ScStwLibraries/headers/client/scstwremoterace.h b/ScStwLibraries/headers/client/scstwremoterace.h index 7a3ba79..8c4a372 100644 --- a/ScStwLibraries/headers/client/scstwremoterace.h +++ b/ScStwLibraries/headers/client/scstwremoterace.h @@ -77,6 +77,8 @@ private slots: RaceMode getMode(); bool local(); + void setTimers(QList timers, bool deleteOldTimers); + signals: void scStwClientChanged(); }; diff --git a/ScStwLibraries/sources/client/scstwremoterace.cpp b/ScStwLibraries/sources/client/scstwremoterace.cpp index 9d3912c..65bad83 100644 --- a/ScStwLibraries/sources/client/scstwremoterace.cpp +++ b/ScStwLibraries/sources/client/scstwremoterace.cpp @@ -119,20 +119,17 @@ ScStw::StatusCode ScStwRemoteRace::setTimerDisabled(int timerId, bool disabled) void ScStwRemoteRace::handleClientStateChange() { switch (this->scStwClient->getState()) { - case ScStwClient::CONNECTED: - this->localTimers.clear(); - this->localTimers = this->timers; - this->timers.clear(); + case ScStwClient::INITIALISING: + // disconnect all local timers from race + this->setTimers({}, false); + // delete all obsolete remote timers + for(ScStwRemoteTimer* oldRemoteTimer : this->remoteTimers) + oldRemoteTimer->deleteLater(); this->remoteTimers.clear(); break; case ScStwClient::DISCONNECTED: - foreach(ScStwRemoteTimer *remoteTimer, this->remoteTimers) - remoteTimer->deleteLater(); this->remoteTimers.clear(); - - this->timers.clear(); - this->timers = this->localTimers; - this->localTimers.clear(); + this->setTimers(this->localTimers, true); emit this->timersChanged(); emit this->detailsChanged(); emit this->currentStartDelayChanged(); @@ -284,6 +281,30 @@ bool ScStwRemoteRace::addTimer(ScStwTimer* timer) { return false; } +void ScStwRemoteRace::setTimers(QList timers, bool deleteOldTimers) { + + // disconnect all signals of all current timers + qDebug() << "SETTING TIMERS"; + + foreach(ScStwTimer *existingTimer, this->timers) { + disconnect(existingTimer, &ScStwTimer::stateChanged, this, &ScStwRace::handleTimerStateChange); + disconnect(existingTimer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged); + disconnect(existingTimer, &ScStwTimer::wantsToBeDisabledChanged, this, &ScStwRace::handleTimerWantsToBeDisabledChange); + disconnect(existingTimer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged); + disconnect(existingTimer, &ScStwTimer::readyStateChanged, this, &ScStwRace::handleTimerReadyStateChange); + disconnect(existingTimer, &ScStwTimer::readyStateChanged, this, &ScStwRace::isReadyForNextStateChanged); + + if(deleteOldTimers) + existingTimer->deleteLater(); + } + + this->timers.clear(); + + for(ScStwTimer* timer : timers) { + this->addTimer(timer); + } +} + QVariantMap ScStwRemoteRace::getCurrentStartDelay() { if(this->local()) return ScStwRace::getCurrentStartDelay(); diff --git a/ScStwLibraries/sources/client/scstwremotesettings.cpp b/ScStwLibraries/sources/client/scstwremotesettings.cpp index 4a63c5d..db5afb9 100644 --- a/ScStwLibraries/sources/client/scstwremotesettings.cpp +++ b/ScStwLibraries/sources/client/scstwremotesettings.cpp @@ -65,9 +65,9 @@ bool ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant void ScStwRemoteSettings::handleClientStateChange() { if(this->scStwClient->getState() == ScStwClient::DISCONNECTED) - emit this->settingChanged(-1, 0, QVariant()); + emit this->settingChanged(-1, ScStwSettings::KeyLevel, QVariant()); else if(this->scStwClient->getState() == ScStwClient::CONNECTED) - emit this->settingChanged(-1, 0, QVariant()); + emit this->settingChanged(-1, ScStwSettings::KeyLevel, QVariant()); } void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) { @@ -92,6 +92,7 @@ void ScStwRemoteSettings::setScStwClient(ScStwClient* client) { if(this->scStwClient != nullptr) { this->scStwClient->addSignalSubscription(ScStw::SettingChanged); + connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteSettings::handleClientStateChange); connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteSettings::handleBaseStationSignal); } } diff --git a/ScStwLibraries/sources/scstwsetting.cpp b/ScStwLibraries/sources/scstwsetting.cpp index c499f4f..7880326 100644 --- a/ScStwLibraries/sources/scstwsetting.cpp +++ b/ScStwLibraries/sources/scstwsetting.cpp @@ -51,6 +51,7 @@ void ScStwSetting::handleSettingChange(int key, int keyLevel, QVariant value) { emit this->valueChanged(); } else if(key == -1 && this->key != -1 && this->keyLevel != -1 && this->keyLevel == keyLevel) { + qDebug() << "value changed!!! key: " << key << " new value " << value; this->hasToReload = true; emit this->valueChanged(); }