2020-07-11 21:03:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
** ScStw Libraries
|
|
|
|
** Copyright (C) 2020 Itsblue development
|
|
|
|
**
|
|
|
|
** This program is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-06 14:21:39 +02:00
|
|
|
#ifndef SCSTWSETTINGS_H
|
|
|
|
#define SCSTWSETTINGS_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QMetaEnum>
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <ScStw.hpp>
|
2020-07-10 15:20:13 +02:00
|
|
|
#include <QDir>
|
2020-07-11 20:30:04 +02:00
|
|
|
#include <scstwsetting.h>
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
class ScStwSettings : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit ScStwSettings(QObject *parent = nullptr);
|
|
|
|
|
2020-07-11 20:30:04 +02:00
|
|
|
typedef QString(*keyToStringConverter)(int);
|
2020-07-24 13:07:27 +02:00
|
|
|
typedef QVariant::Type(*keyToTypeConverter)(int);
|
2020-07-11 20:30:04 +02:00
|
|
|
|
2020-07-06 14:21:39 +02:00
|
|
|
/*!
|
|
|
|
* \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,
|
2020-08-03 20:02:42 +02:00
|
|
|
SoundVolumeSetting,
|
|
|
|
CompetitionModeSetting
|
2020-07-06 14:21:39 +02:00
|
|
|
};
|
|
|
|
Q_ENUM(BaseStationSetting)
|
|
|
|
|
2020-07-10 15:20:13 +02:00
|
|
|
enum KeyLevelEnum {
|
|
|
|
KeyLevel = 0
|
|
|
|
};
|
|
|
|
Q_ENUM(KeyLevelEnum)
|
|
|
|
|
2020-07-11 20:30:04 +02:00
|
|
|
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);
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
static BaseStationSetting keyFromInt(int i) {
|
|
|
|
QMetaEnum enumeration = QMetaEnum::fromType<BaseStationSetting>();
|
|
|
|
return static_cast<BaseStationSetting>(enumeration.keyToValue(enumeration.valueToKey(i)));
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:30:04 +02:00
|
|
|
static QString keyToString(int key) {
|
|
|
|
return QMetaEnum::fromType<BaseStationSetting>().valueToKey(key);
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:07:27 +02:00
|
|
|
static QVariant::Type keyToType(int key) {
|
|
|
|
QMap<BaseStationSetting, QVariant::Type> types = {
|
|
|
|
{ReadySoundEnableSetting, QVariant::Bool},
|
|
|
|
{ReadySoundDelaySetting, QVariant::Double},
|
|
|
|
{AtYourMarksSoundEnableSetting, QVariant::Bool},
|
|
|
|
{AtYourMarksSoundDelaySetting, QVariant::Double},
|
2020-08-03 20:02:42 +02:00
|
|
|
{SoundVolumeSetting, QVariant::Double},
|
|
|
|
{CompetitionModeSetting, QVariant::Bool}
|
2020-07-24 13:07:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(types.contains(BaseStationSetting(key)))
|
|
|
|
return types[BaseStationSetting(key)];
|
|
|
|
|
|
|
|
return QVariant::Invalid;
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:21:39 +02:00
|
|
|
protected:
|
2020-07-10 15:20:13 +02:00
|
|
|
virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1);
|
|
|
|
virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1);
|
2020-07-11 20:30:04 +02:00
|
|
|
virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1);
|
2020-07-24 13:07:27 +02:00
|
|
|
bool registerKeyLevelConverters(int keyLevel, keyToStringConverter, keyToTypeConverter);
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
QFile * settingsFile;
|
|
|
|
QVariantMap settingsCache;
|
|
|
|
|
|
|
|
bool loadSettingsFromFile();
|
|
|
|
|
2020-07-11 20:30:04 +02:00
|
|
|
QMap<int, keyToStringConverter> keyToStringConverters;
|
2020-07-24 13:07:27 +02:00
|
|
|
QMap<int, keyToTypeConverter> keyToTypeConverters;
|
2020-10-02 19:56:08 +02:00
|
|
|
QMap<int, QMap<int, ScStwSetting*>> internalSettingHandlers;
|
2020-07-11 20:30:04 +02:00
|
|
|
|
2020-07-06 14:21:39 +02:00
|
|
|
private slots:
|
|
|
|
bool writeSettingsToFile();
|
|
|
|
|
|
|
|
signals:
|
2020-07-11 14:00:31 +02:00
|
|
|
void settingChanged(int key, int keyLevel, QVariant value);
|
2020-07-06 14:21:39 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SCSTWSETTINGS_H
|