From f336e953eb86f1a47f6376a6e94c30f82982e606 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sat, 11 Jul 2020 20:30:04 +0200 Subject: [PATCH] some fixes, settings are now working correctly --- ScStwLibraries/ScStwLibraries.pri | 12 ++--- .../headers/client/scstwremotesettings.h | 2 +- ScStwLibraries/headers/client/scstwsetting.h | 47 ---------------- ScStwLibraries/headers/scstwsetting.h | 37 +++++++++++++ ScStwLibraries/headers/scstwsettings.h | 47 +++++++++++++--- ScStwLibraries/sources/client/scstwclient.cpp | 6 +-- .../sources/client/scstwremotesettings.cpp | 14 +++-- .../sources/client/scstwsetting.cpp | 54 ------------------- ScStwLibraries/sources/scstwlibraries.cpp | 2 +- ScStwLibraries/sources/scstwsetting.cpp | 31 +++++++++++ ScStwLibraries/sources/scstwsettings.cpp | 50 +++++++++++++---- 11 files changed, 165 insertions(+), 137 deletions(-) delete mode 100644 ScStwLibraries/headers/client/scstwsetting.h create mode 100644 ScStwLibraries/headers/scstwsetting.h delete mode 100644 ScStwLibraries/sources/client/scstwsetting.cpp create mode 100644 ScStwLibraries/sources/scstwsetting.cpp diff --git a/ScStwLibraries/ScStwLibraries.pri b/ScStwLibraries/ScStwLibraries.pri index 781de65..903b9bc 100644 --- a/ScStwLibraries/ScStwLibraries.pri +++ b/ScStwLibraries/ScStwLibraries.pri @@ -23,7 +23,8 @@ SOURCES += \ $$PWD/sources/scstwsoundplayer.cpp \ $$PWD/sources/scstwlibraries.cpp \ $$PWD/sources/scstwrace.cpp \ - $$PWD/sources/scstwtimer.cpp + $$PWD/sources/scstwtimer.cpp \ + $$PWD/sources/scstwsetting.cpp HEADERS += \ $$PWD/headers/ScStw.hpp \ @@ -32,20 +33,19 @@ HEADERS += \ $$PWD/headers/scstwrace.h \ $$PWD/headers/scstwsettings.h \ $$PWD/headers/scstwsoundplayer.h \ - $$PWD/headers/scstwtimer.h + $$PWD/headers/scstwtimer.h \ + $$PWD/headers/scstwsetting.h ScStwLibraries_ClientLibs { SOURCES += \ $$PWD/sources/client/scstwclient.cpp \ $$PWD/sources/client/scstwremotesettings.cpp \ - $$PWD/sources/client/scstwremotemonitorrace.cpp \ - $$PWD/sources/client/scstwsetting.cpp + $$PWD/sources/client/scstwremotemonitorrace.cpp HEADERS += \ $$PWD/headers/client/scstwclient.h \ $$PWD/headers/client/scstwremotesettings.h \ - $$PWD/headers/client/scstwremotemonitorrace.h \ - $$PWD/headers/client/scstwsetting.h + $$PWD/headers/client/scstwremotemonitorrace.h INCLUDEPATH += $$PWD/headers/client } diff --git a/ScStwLibraries/headers/client/scstwremotesettings.h b/ScStwLibraries/headers/client/scstwremotesettings.h index 7f02d6e..9defc47 100644 --- a/ScStwLibraries/headers/client/scstwremotesettings.h +++ b/ScStwLibraries/headers/client/scstwremotesettings.h @@ -20,7 +20,7 @@ public: protected: QVariant readSetting(QString key, int keyInt, int keyLevel); bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1); - void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); + bool setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); private: ScStwClient * scStwClient; diff --git a/ScStwLibraries/headers/client/scstwsetting.h b/ScStwLibraries/headers/client/scstwsetting.h deleted file mode 100644 index 7c95674..0000000 --- a/ScStwLibraries/headers/client/scstwsetting.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef SCSTWQMLSETTING_H -#define SCSTWQMLSETTING_H - -#include -#include - -class ScStwSetting : public QObject -{ - Q_OBJECT - Q_PROPERTY(ScStwSettings* scStwSettings READ getScStwSettings WRITE setScStwSettings NOTIFY scStwSettingsChanged) - Q_PROPERTY(int key READ getKey WRITE setKey NOTIFY keyChanged) - Q_PROPERTY(int keyLevel READ getKeyLevel WRITE setKeyLevel NOTIFY keyLevelChanged) - Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged) -public: - explicit ScStwSetting(QObject *parent = nullptr); - -private: - ScStwSettings* scStwSettings; - int key; - int keyLevel; - QVariant valueCache; - -public slots: - ScStwSettings* getScStwSettings(); - void setScStwSettings(ScStwSettings* scStwSettings); - - int getKey(); - void setKey(int key); - - int getKeyLevel(); - void setKeyLevel(int keyLevel); - - QVariant getValue(); - void setValue(QVariant value); - -private slots: - void handleSettingChange(int key, int keyLevel, QVariant value); - -signals: - void scStwSettingsChanged(); - void keyChanged(); - void keyLevelChanged(); - void valueChanged(); - -}; - -#endif // SCSTWQMLSETTING_H diff --git a/ScStwLibraries/headers/scstwsetting.h b/ScStwLibraries/headers/scstwsetting.h new file mode 100644 index 0000000..e9a3dac --- /dev/null +++ b/ScStwLibraries/headers/scstwsetting.h @@ -0,0 +1,37 @@ +#ifndef SCSTWQMLSETTING_H +#define SCSTWQMLSETTING_H + +#include +#include + +class ScStwSetting : public QObject +{ + Q_OBJECT + Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged) + +protected: + explicit ScStwSetting(QObject *parent = nullptr); + + int key; + int keyLevel; + bool hasToReload; + + virtual bool writeSetting(int, int, QVariant) = 0; + virtual QVariant readSetting(int, int) = 0; + +private: + QVariant valueCache; + +public slots: + QVariant getValue(); + void setValue(QVariant value); + +protected slots: + void handleSettingChange(int key, int keyLevel, QVariant value); + +signals: + void valueChanged(); + +}; + +#endif // SCSTWQMLSETTING_H diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h index a91d5c5..ac5a7fa 100644 --- a/ScStwLibraries/headers/scstwsettings.h +++ b/ScStwLibraries/headers/scstwsettings.h @@ -10,6 +10,7 @@ #include #include #include +#include class ScStwSettings : public QObject { @@ -17,6 +18,8 @@ class ScStwSettings : public QObject public: explicit ScStwSettings(QObject *parent = nullptr); + typedef QString(*keyToStringConverter)(int); + /*! * \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client * @@ -40,21 +43,29 @@ public: }; Q_ENUM(KeyLevelEnum) - Q_INVOKABLE QVariant readSetting(BaseStationSetting key); - Q_INVOKABLE virtual QVariant readSetting(int key, int level); - Q_INVOKABLE bool writeSetting(BaseStationSetting key, QVariant value); - Q_INVOKABLE virtual bool writeSetting(int key, QVariant value, int level); - Q_INVOKABLE void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant); + virtual QVariant readSetting(BaseStationSetting key); + Q_INVOKABLE virtual QVariant readSetting(int key, int keyLevel); + virtual bool writeSetting(BaseStationSetting key, QVariant value); + Q_INVOKABLE virtual bool writeSetting(int key, int keyLevel, QVariant value); + virtual bool setDefaultSetting(BaseStationSetting key, QVariant defaultValue); + Q_INVOKABLE virtual bool setDefaultSetting(int key, int keyLevel, QVariant defaultValue); + + Q_INVOKABLE ScStwSetting * getSetting(int key, int keyLevel); static BaseStationSetting keyFromInt(int i) { QMetaEnum enumeration = QMetaEnum::fromType(); return static_cast(enumeration.keyToValue(enumeration.valueToKey(i))); } + static QString keyToString(int key) { + return QMetaEnum::fromType().valueToKey(key); + } + protected: virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1); virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1); - virtual void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); + virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1); + bool registerKeyLevelHandler(int keyLevel, keyToStringConverter); private: QFile * settingsFile; @@ -62,6 +73,30 @@ private: bool loadSettingsFromFile(); + class ScStwSettingInternal: public ScStwSetting { + public: + ScStwSettingInternal(int key, int keyLevel, ScStwSettings*scStwSettings, QObject *parent):ScStwSetting(parent) { + this->scStwSettings = scStwSettings; + connect(this->scStwSettings, &ScStwSettings::settingChanged, this, &ScStwSettingInternal::handleSettingChange); + this->key = key; + this->keyLevel = keyLevel; + this->handleSettingChange(-1, 0, QVariant()); + } + protected: + ScStwSettings *scStwSettings; + + bool writeSetting(int key, int keyLevel, QVariant value) { + return this->scStwSettings->writeSetting(key, keyLevel, value); + } + + QVariant readSetting(int key, int keyLevel) { + return this->scStwSettings->readSetting(key, keyLevel); + } + }; + + QMap keyToStringConverters; + QMap> internalSettingHandlers; + private slots: bool writeSettingsToFile(); diff --git a/ScStwLibraries/sources/client/scstwclient.cpp b/ScStwLibraries/sources/client/scstwclient.cpp index 0aebd03..2fed168 100644 --- a/ScStwLibraries/sources/client/scstwclient.cpp +++ b/ScStwLibraries/sources/client/scstwclient.cpp @@ -91,7 +91,7 @@ bool ScStwClient::init() { this->apiVersion = initResponse["data"].toMap()["apiVersion"].toString(); qDebug() << "[INFO][CLIENT] base station api version is: " << this->apiVersion; int compareResult = ScStw::firmwareCompare(this->API_VERSION, this->apiVersion); - qDebug() << "compare result is: " << compareResult; + //qDebug() << "compare result is: " << compareResult; if( compareResult == -3 ){ // the client version is out of date!! this->closeConnection(); @@ -373,7 +373,7 @@ void ScStwClient::handleSignal(QVariantMap data) { ScStw::SignalKey signalKey = ScStw::signalKeyFromInt(data["header"].toInt()); - qDebug() << "got signal: " << signalKey << " with data: " << data["data"]; + //qDebug() << "got signal: " << signalKey << " with data: " << data["data"]; switch (signalKey) { case ScStw::ExtensionsChanged: @@ -409,7 +409,7 @@ QVariant ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) { if(reply["status"] != 200){ return "false"; } - return reply["data"].toString(); + return reply["data"]; } void ScStwClient::setIP(QString newIp){ diff --git a/ScStwLibraries/sources/client/scstwremotesettings.cpp b/ScStwLibraries/sources/client/scstwremotesettings.cpp index 4def371..bd8bcae 100644 --- a/ScStwLibraries/sources/client/scstwremotesettings.cpp +++ b/ScStwLibraries/sources/client/scstwremotesettings.cpp @@ -16,28 +16,26 @@ ScStwRemoteSettings::SettingsMode ScStwRemoteSettings::getMode() { } QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) { - if(this->getMode() == LOCAL || keyLevel > 0) + if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel) return ScStwSettings::readSetting(key, keyInt, keyLevel); return this->scStwClient->readRemoteSetting(ScStwSettings::BaseStationSetting(keyInt)); } bool ScStwRemoteSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) { - if(this->getMode() == LOCAL || keyLevel > 0) + if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel) return ScStwSettings::writeSetting(key, value, keyInt, keyLevel); - qDebug() << "changing setting"; ScStw::StatusCode res = this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value); - qDebug() << "changed setting " << res; return res == ScStw::Success; } -void ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) { - if(this->getMode() == LOCAL || keyLevel > 0) +bool ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) { + if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel) return ScStwSettings::setDefaultSetting(key, defaultVariant, keyInt, keyLevel); - return; + return false; } void ScStwRemoteSettings::handleClientStateChange() { @@ -48,7 +46,7 @@ void ScStwRemoteSettings::handleClientStateChange() { void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) { switch (key) { case ScStw::SettingChanged: - emit this->settingChanged(data.toMap()["key"].toInt(), 0, data.toMap()["value"]); + emit this->settingChanged(data.toMap()["key"].toInt(), ScStwSettings::KeyLevel, data.toMap()["value"]); default: break; } diff --git a/ScStwLibraries/sources/client/scstwsetting.cpp b/ScStwLibraries/sources/client/scstwsetting.cpp deleted file mode 100644 index 4f6e6e2..0000000 --- a/ScStwLibraries/sources/client/scstwsetting.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "scstwsetting.h" - -ScStwSetting::ScStwSetting(QObject *parent) : QObject(parent) -{ -} - -ScStwSettings* ScStwSetting::getScStwSettings(){ - return this->scStwSettings; -} -void ScStwSetting::setScStwSettings(ScStwSettings* scStwSettings) { - if(this->scStwSettings != scStwSettings && scStwSettings != nullptr) { - this->scStwSettings = scStwSettings; - connect(this->scStwSettings, &ScStwSettings::settingChanged, this, &ScStwSetting::handleSettingChange); - emit this->scStwSettingsChanged(); - } -} - -int ScStwSetting::getKey() { - return this->key; -} -void ScStwSetting::setKey(int key) { - if(this->key != key) { - this->key = key; - this->handleSettingChange(this->key, this->keyLevel, this->scStwSettings->readSetting(this->key, this->keyLevel)); - emit this->keyChanged(); - } -} - -int ScStwSetting::getKeyLevel() { - return this->keyLevel; -} -void ScStwSetting::setKeyLevel(int keyLevel) { - if(this->keyLevel != keyLevel) { - this->keyLevel = keyLevel; - this->handleSettingChange(this->key, this->keyLevel, this->scStwSettings->readSetting(this->key, this->keyLevel)); - emit this->keyLevelChanged(); - } -} - -QVariant ScStwSetting::getValue() { - return this->valueCache; -} -void ScStwSetting::setValue(QVariant value) { - if(value != this->valueCache && this->scStwSettings != nullptr) { - this->scStwSettings->writeSetting(this->key, value, this->keyLevel); - } -} - -void ScStwSetting::handleSettingChange(int key, int keyLevel, QVariant value) { - if(keyLevel == this->keyLevel && key == this->key) { - this->valueCache = value; - emit this->valueChanged(); - } -} diff --git a/ScStwLibraries/sources/scstwlibraries.cpp b/ScStwLibraries/sources/scstwlibraries.cpp index 701367c..2c1c0fb 100644 --- a/ScStwLibraries/sources/scstwlibraries.cpp +++ b/ScStwLibraries/sources/scstwlibraries.cpp @@ -35,7 +35,7 @@ void ScStwLibraries::init() { #ifdef ScStwLibraries_ClientLibs qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwClient"); qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwSettings"); - qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwSetting"); + qmlRegisterUncreatableType("de.itsblue.ScStw", 2, 0, "ScStwSetting", "The ScStwSetting is manage by a ScStwSettings instance and therefore not creatable"); #endif #endif } diff --git a/ScStwLibraries/sources/scstwsetting.cpp b/ScStwLibraries/sources/scstwsetting.cpp new file mode 100644 index 0000000..006ff38 --- /dev/null +++ b/ScStwLibraries/sources/scstwsetting.cpp @@ -0,0 +1,31 @@ +#include "scstwsetting.h" +#include + +ScStwSetting::ScStwSetting(QObject *parent) : QObject(parent) +{ +} + +QVariant ScStwSetting::getValue() { + if(this->hasToReload) { + this->valueCache = this->readSetting(this->key, this->keyLevel); + this->hasToReload = false; + } + return this->valueCache; +} +void ScStwSetting::setValue(QVariant value) { + if(value != this->valueCache) { + this->writeSetting(this->key, this->keyLevel, value); + } +} + +void ScStwSetting::handleSettingChange(int key, int keyLevel, QVariant value) { + if(keyLevel == this->keyLevel && key == this->key) { + this->valueCache = value; + qDebug() << "value changed!!! key: " << key << " new value " << value; + emit this->valueChanged(); + } + else if(key == -1 && this->key != -1 && this->keyLevel != -1 && this->keyLevel == keyLevel) { + this->hasToReload = true; + emit this->valueChanged(); + } +} diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index 909ac0c..2a7cb7e 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -16,32 +16,41 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path + "/settings.json")); connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile); + + this->registerKeyLevelHandler(ScStwSettings::KeyLevel, &ScStwSettings::keyToString); } QVariant ScStwSettings::readSetting(BaseStationSetting key) { - return this->readSetting(QMetaEnum::fromType().valueToKey(int(key)), int(key), 0); + return this->readSetting(key, 0); } QVariant ScStwSettings::readSetting(int key, int keyLevel) { - if(keyLevel == ScStwSettings::KeyLevel) - return this->readSetting(ScStwSettings::BaseStationSetting(key)); + if(this->keyToStringConverters.contains(keyLevel)) + return this->readSetting(this->keyToStringConverters[keyLevel](key), key, keyLevel); else return QVariant(); } bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) { - return this->writeSetting(QMetaEnum::fromType().valueToKey(int(key)), value, int(key), 0); + return this->writeSetting(key, 0, value); } -bool ScStwSettings::writeSetting(int key, QVariant value, int keyLevel) { - if(keyLevel == ScStwSettings::KeyLevel) - return this->writeSetting(ScStwSettings::BaseStationSetting(key), value); +bool ScStwSettings::writeSetting(int key, int keyLevel, QVariant value) { + if(this->keyToStringConverters.contains(keyLevel)) + return this->writeSetting(this->keyToStringConverters[keyLevel](key), value, key, keyLevel); else return false; } -void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) { - this->setDefaultSetting(QMetaEnum::fromType().valueToKey(int(key)), defaultVariant, int(key), 0); +bool ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultValue) { + return this->setDefaultSetting(key, 0, defaultValue); +} + +bool ScStwSettings::setDefaultSetting(int key, int keyLevel, QVariant defaultValue) { + if(this->keyToStringConverters.contains(keyLevel)) + return this->setDefaultSetting(this->keyToStringConverters[keyLevel](key), defaultValue, key, keyLevel); + else + return false; } QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) { @@ -63,9 +72,28 @@ bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int ke return true; } -void ScStwSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) { +bool ScStwSettings::setDefaultSetting(QString key, QVariant defaultValue, int keyInt, int keyLevel) { if(!this->settingsCache.contains(key)) - this->writeSetting(key, defaultVariant, keyInt, keyLevel); + return this->writeSetting(key, defaultValue, keyInt, keyLevel); + return true; +} + +ScStwSetting * ScStwSettings::getSetting(int key, int keyLevel) { + if(!this->internalSettingHandlers.contains(keyLevel)) + this->internalSettingHandlers.insert(keyLevel, {}); + + if(!this->internalSettingHandlers[keyLevel].contains(key)) + this->internalSettingHandlers[keyLevel].insert(key, new ScStwSettingInternal(key, keyLevel, this, this)); + + return this->internalSettingHandlers[keyLevel][key]; +} + +bool ScStwSettings::registerKeyLevelHandler(int keyLevel, keyToStringConverter converterFunction) { + if(this->keyToStringConverters.contains(keyLevel)) + return false; + + this->keyToStringConverters.insert(keyLevel, converterFunction); + return true; } /*