From b0dc543a29dbf005a38f218293700b15fbe4b58d Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Mon, 6 Jul 2020 14:21:39 +0200 Subject: [PATCH] added ScStwSettings class (addressed issue #11 ) --- ScStwLibraries/ScStwLibraries.pri | 2 + ScStwLibraries/headers/ScStw.hpp | 62 +------------------- ScStwLibraries/headers/scstwlibraries.h | 1 + ScStwLibraries/headers/scstwsettings.h | 66 +++++++++++++++++++++ ScStwLibraries/sources/ScStw.cpp | 32 ----------- ScStwLibraries/sources/scstwsettings.cpp | 73 ++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 91 deletions(-) create mode 100644 ScStwLibraries/headers/scstwsettings.h create mode 100644 ScStwLibraries/sources/scstwsettings.cpp diff --git a/ScStwLibraries/ScStwLibraries.pri b/ScStwLibraries/ScStwLibraries.pri index e34d9f9..8a72ff1 100644 --- a/ScStwLibraries/ScStwLibraries.pri +++ b/ScStwLibraries/ScStwLibraries.pri @@ -14,6 +14,7 @@ INCLUDEPATH += $$PWD/headers $$PWD SOURCES += \ $$PWD/sources/ScStw.cpp \ + $$PWD/sources/scstwsettings.cpp \ $$PWD/sources/scstwsoundplayer.cpp \ $$PWD/sources/scstwlibraries.cpp \ $$PWD/sources/scstwrace.cpp \ @@ -24,6 +25,7 @@ HEADERS += \ $$PWD/headers/ScStwLibraries_global.h \ $$PWD/headers/scstwlibraries.h \ $$PWD/headers/scstwrace.h \ + $$PWD/headers/scstwsettings.h \ $$PWD/headers/scstwsoundplayer.h \ $$PWD/headers/scstwtimer.h diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index 263c951..cb2229a 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -79,24 +79,6 @@ public: }; Q_ENUM(SignalKey) - /*! - * \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client - * - * \see ScStw::baseStationSettingFromInt() - * \see ScStw::baseStationSettingToString() - * \see ScStw::baseStationSettingFromString() - * \see ScStw::baseStationSettings - */ - enum BaseStationSetting { - InvalidSetting = -1, - ReadySoundEnableSetting, - ReadySoundDelaySetting, - AtYourMarksSoundEnableSetting, - AtYourMarksSoundDelaySetting, - SoundVolumeSetting - }; - Q_ENUM(BaseStationSetting) - /*! * \brief The SocketCommand enum contains all commands the base station can handle * @@ -107,9 +89,9 @@ public: InitializeSessionCommand = 1, - StartTimersCommand = 1000, - StopTimersCommand = 1001, - ResetTimersCommand = 1002, + StartRaceCommand = 1000, + StopRaceCommand = 1001, + ResetRaceCommand = 1002, GetRaceStateCommand = 2000, GetNextStartActionCommand = 2005, @@ -171,44 +153,6 @@ public: */ static const char* SOCKET_MESSAGE_END_KEY; - /*! - * \brief baseStationSettings contains a string with reference to all BaseStationSetting values - * - * \see ScStw::BaseStationSetting - * \see ScStw::baseStationSettingToString() - * \see ScStw::baseStationSettingFromString() - */ - static const QMap baseStationSettings; - - /*! - * \brief Function to convert an int to a BaseStationSetting - * \param i the int to convert - * \return a BaseStationSetting - * - * \see ScStw::BaseStationSetting - */ - static BaseStationSetting baseStationSettingfromInt(int i); - - /*! - * \brief Function to convert a QString to a BaseStationSetting - * \param s the string to convert - * \return a BaseStationSetting - * - * \see ScStw::BaseStationSetting - * \see ScStw::baseStationSettingToString() - */ - static BaseStationSetting baseStationSettingFromString(QString s); - - /*! - * \brief Function to convert BaseStationSetting to a QString - * \param s the BaseStationSetting to convert - * \return a QString - * - * \see ScStw::BaseStationSetting - * \see ScStw::baseStationSettingFromString() - */ - Q_INVOKABLE static QString baseStationSettingToString(BaseStationSetting s); - /*! * \brief Function to convert an int to a SignalKey * \param i the int to convert diff --git a/ScStwLibraries/headers/scstwlibraries.h b/ScStwLibraries/headers/scstwlibraries.h index f6b1857..1321860 100644 --- a/ScStwLibraries/headers/scstwlibraries.h +++ b/ScStwLibraries/headers/scstwlibraries.h @@ -25,6 +25,7 @@ #endif #include "scstwtimer.h" #include "scstwrace.h" +#include "scstwsettings.h" #ifdef ScStwLibraries_ClientLibs #include "scstwremotemonitorrace.h" #include "scstwclient.h" diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h new file mode 100644 index 0000000..8c1c655 --- /dev/null +++ b/ScStwLibraries/headers/scstwsettings.h @@ -0,0 +1,66 @@ +#ifndef SCSTWSETTINGS_H +#define SCSTWSETTINGS_H + +#include +#include +#include +#include +#include +#include +#include +#include + +class ScStwSettings : public QObject +{ + Q_OBJECT +public: + explicit ScStwSettings(QObject *parent = nullptr); + + /*! + * \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client + * + * \see ScStw::baseStationSettingFromInt() + * \see ScStw::baseStationSettingToString() + * \see ScStw::baseStationSettingFromString() + * \see ScStw::baseStationSettings + */ + enum BaseStationSetting { + InvalidSetting = -1, + ReadySoundEnableSetting, + ReadySoundDelaySetting, + AtYourMarksSoundEnableSetting, + AtYourMarksSoundDelaySetting, + SoundVolumeSetting + }; + + Q_ENUM(BaseStationSetting) + + QVariant readSetting(BaseStationSetting key); + bool writeSetting(BaseStationSetting key, QVariant value); + void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant); + + static BaseStationSetting keyFromInt(int i) { + QMetaEnum enumeration = QMetaEnum::fromType(); + return static_cast(enumeration.keyToValue(enumeration.valueToKey(i))); + } + +protected: + QVariant readSetting(QString key); + bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1); + void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); + +private: + QFile * settingsFile; + QVariantMap settingsCache; + + bool loadSettingsFromFile(); + +private slots: + bool writeSettingsToFile(); + +signals: + void settingChanged(int key, int keyLevel); + +}; + +#endif // SCSTWSETTINGS_H diff --git a/ScStwLibraries/sources/ScStw.cpp b/ScStwLibraries/sources/ScStw.cpp index f01abb2..2cc19f0 100644 --- a/ScStwLibraries/sources/ScStw.cpp +++ b/ScStwLibraries/sources/ScStw.cpp @@ -21,38 +21,6 @@ const char *ScStw::SOCKET_MESSAGE_START_KEY = ""; const char *ScStw::SOCKET_MESSAGE_END_KEY = ""; -const QMap ScStw::baseStationSettings = { - {"ReadySoundEnable", ScStw::ReadySoundEnableSetting}, - {"ReadySoundDelay", ScStw::ReadySoundDelaySetting}, - {"AtYourMarksSoundEnable", ScStw::AtYourMarksSoundEnableSetting}, - {"AtYourMarksSoundDelay", ScStw::AtYourMarksSoundDelaySetting}, - {"SoundVolume", ScStw::SoundVolumeSetting} -}; - -ScStw::BaseStationSetting ScStw::baseStationSettingFromString(QString s) { - if(!ScStw::baseStationSettings.contains(s)) - return ScStw::InvalidSetting; - - return ScStw::baseStationSettings[s]; -} - -QString ScStw::baseStationSettingToString(ScStw::BaseStationSetting s) { - for(QString key: ScStw::baseStationSettings.keys()) { - if(ScStw::baseStationSettings[key] == s) - return key; - } - return "Invalid"; -} - -ScStw::BaseStationSetting ScStw::baseStationSettingfromInt(int i) { - bool ok; - BaseStationSetting s = ScStw::toEnumValue(i, &ok); - if(!ok) - return InvalidSetting; - else - return s; -} - ScStw::SignalKey ScStw::signalKeyFromInt(int i) { bool ok; ScStw::SignalKey k = ScStw::toEnumValue(i, &ok); diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp new file mode 100644 index 0000000..5e33941 --- /dev/null +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -0,0 +1,73 @@ +#include "../headers/scstwsettings.h" + +ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) +{ + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/settings.json"; + this->settingsFile = new QFile(path); + if(!this->settingsFile->open(QFile::ReadWrite)) + qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path)); + + if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0) + qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path)); + + connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile); +} + +QVariant ScStwSettings::readSetting(BaseStationSetting key) { + return this->readSetting(QMetaEnum::fromType().valueToKey(int(key))); +} + +bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) { + return this->writeSetting(QMetaEnum::fromType().valueToKey(int(key)), value, int(key), 0); +} + +void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) { + this->setDefaultSetting(QMetaEnum::fromType().valueToKey(int(key)), defaultVariant, int(key), 0); +} + +QVariant ScStwSettings::readSetting(QString key) { + return this->settingsCache[key]; +} + +bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) { + if(!this->settingsCache.contains(key)) + this->settingsCache.insert(key, value); + else if (this->settingsCache[key] == value) + return true; + else + this->settingsCache[key] = value; + + emit this->settingChanged(keyInt, keyLevel); + + return true; +} + +void ScStwSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) { + if(!this->settingsCache.contains(key)) + this->writeSetting(key, defaultVariant, keyInt, keyLevel); +} + +/* + * File handling + */ + +bool ScStwSettings::writeSettingsToFile() { + QJsonDocument doc = QJsonDocument::fromVariant(this->settingsCache); + + // overwrite file + this->settingsFile->reset(); + this->settingsFile->write(doc.toJson(QJsonDocument::Indented)); + this->settingsFile->flush(); + + return true; +} + +bool ScStwSettings::loadSettingsFromFile() { + QJsonDocument doc = QJsonDocument::fromJson(this->settingsFile->readAll()); + + if(doc.toVariant().type() != QVariant::Map) + return false; + + this->settingsCache = doc.toVariant().toMap(); + return true; +}