added ScStwSettings class (addressed issue #11 )
This commit is contained in:
parent
a46e916999
commit
b0dc543a29
6 changed files with 145 additions and 91 deletions
|
@ -14,6 +14,7 @@ INCLUDEPATH += $$PWD/headers $$PWD
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
$$PWD/sources/ScStw.cpp \
|
$$PWD/sources/ScStw.cpp \
|
||||||
|
$$PWD/sources/scstwsettings.cpp \
|
||||||
$$PWD/sources/scstwsoundplayer.cpp \
|
$$PWD/sources/scstwsoundplayer.cpp \
|
||||||
$$PWD/sources/scstwlibraries.cpp \
|
$$PWD/sources/scstwlibraries.cpp \
|
||||||
$$PWD/sources/scstwrace.cpp \
|
$$PWD/sources/scstwrace.cpp \
|
||||||
|
@ -24,6 +25,7 @@ HEADERS += \
|
||||||
$$PWD/headers/ScStwLibraries_global.h \
|
$$PWD/headers/ScStwLibraries_global.h \
|
||||||
$$PWD/headers/scstwlibraries.h \
|
$$PWD/headers/scstwlibraries.h \
|
||||||
$$PWD/headers/scstwrace.h \
|
$$PWD/headers/scstwrace.h \
|
||||||
|
$$PWD/headers/scstwsettings.h \
|
||||||
$$PWD/headers/scstwsoundplayer.h \
|
$$PWD/headers/scstwsoundplayer.h \
|
||||||
$$PWD/headers/scstwtimer.h
|
$$PWD/headers/scstwtimer.h
|
||||||
|
|
||||||
|
|
|
@ -79,24 +79,6 @@ public:
|
||||||
};
|
};
|
||||||
Q_ENUM(SignalKey)
|
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
|
* \brief The SocketCommand enum contains all commands the base station can handle
|
||||||
*
|
*
|
||||||
|
@ -107,9 +89,9 @@ public:
|
||||||
|
|
||||||
InitializeSessionCommand = 1,
|
InitializeSessionCommand = 1,
|
||||||
|
|
||||||
StartTimersCommand = 1000,
|
StartRaceCommand = 1000,
|
||||||
StopTimersCommand = 1001,
|
StopRaceCommand = 1001,
|
||||||
ResetTimersCommand = 1002,
|
ResetRaceCommand = 1002,
|
||||||
|
|
||||||
GetRaceStateCommand = 2000,
|
GetRaceStateCommand = 2000,
|
||||||
GetNextStartActionCommand = 2005,
|
GetNextStartActionCommand = 2005,
|
||||||
|
@ -171,44 +153,6 @@ public:
|
||||||
*/
|
*/
|
||||||
static const char* SOCKET_MESSAGE_END_KEY;
|
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<QString, ScStw::BaseStationSetting> 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
|
* \brief Function to convert an int to a SignalKey
|
||||||
* \param i the int to convert
|
* \param i the int to convert
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#endif
|
#endif
|
||||||
#include "scstwtimer.h"
|
#include "scstwtimer.h"
|
||||||
#include "scstwrace.h"
|
#include "scstwrace.h"
|
||||||
|
#include "scstwsettings.h"
|
||||||
#ifdef ScStwLibraries_ClientLibs
|
#ifdef ScStwLibraries_ClientLibs
|
||||||
#include "scstwremotemonitorrace.h"
|
#include "scstwremotemonitorrace.h"
|
||||||
#include "scstwclient.h"
|
#include "scstwclient.h"
|
||||||
|
|
66
ScStwLibraries/headers/scstwsettings.h
Normal file
66
ScStwLibraries/headers/scstwsettings.h
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
#ifndef SCSTWSETTINGS_H
|
||||||
|
#define SCSTWSETTINGS_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QVariant>
|
||||||
|
#include <QMetaEnum>
|
||||||
|
#include <QtDebug>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <ScStw.hpp>
|
||||||
|
|
||||||
|
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<BaseStationSetting>();
|
||||||
|
return static_cast<BaseStationSetting>(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
|
|
@ -21,38 +21,6 @@
|
||||||
const char *ScStw::SOCKET_MESSAGE_START_KEY = "<message>";
|
const char *ScStw::SOCKET_MESSAGE_START_KEY = "<message>";
|
||||||
const char *ScStw::SOCKET_MESSAGE_END_KEY = "</message>";
|
const char *ScStw::SOCKET_MESSAGE_END_KEY = "</message>";
|
||||||
|
|
||||||
const QMap<QString, ScStw::BaseStationSetting> 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<ScStw::BaseStationSetting>(i, &ok);
|
|
||||||
if(!ok)
|
|
||||||
return InvalidSetting;
|
|
||||||
else
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
ScStw::SignalKey ScStw::signalKeyFromInt(int i) {
|
ScStw::SignalKey ScStw::signalKeyFromInt(int i) {
|
||||||
bool ok;
|
bool ok;
|
||||||
ScStw::SignalKey k = ScStw::toEnumValue<ScStw::SignalKey>(i, &ok);
|
ScStw::SignalKey k = ScStw::toEnumValue<ScStw::SignalKey>(i, &ok);
|
||||||
|
|
73
ScStwLibraries/sources/scstwsettings.cpp
Normal file
73
ScStwLibraries/sources/scstwsettings.cpp
Normal file
|
@ -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<BaseStationSetting>().valueToKey(int(key)));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) {
|
||||||
|
return this->writeSetting(QMetaEnum::fromType<BaseStationSetting>().valueToKey(int(key)), value, int(key), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) {
|
||||||
|
this->setDefaultSetting(QMetaEnum::fromType<BaseStationSetting>().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;
|
||||||
|
}
|
Reference in a new issue