This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
shared-libraries/ScStwLibraries/headers/scstwsettings.h

127 lines
4.3 KiB
C++

/****************************************************************************
** 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/>.
****************************************************************************/
#ifndef SCSTWSETTINGS_H
#define SCSTWSETTINGS_H
#include <QObject>
#include <QVariant>
#include <QMetaEnum>
#include <QtDebug>
#include <QFile>
#include <QStandardPaths>
#include <QJsonDocument>
#include <ScStw.hpp>
#include <QDir>
#include <scstwsetting.h>
class ScStwSettings : public QObject
{
Q_OBJECT
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
*
* \see ScStw::baseStationSettingFromInt()
* \see ScStw::baseStationSettingToString()
* \see ScStw::baseStationSettingFromString()
* \see ScStw::baseStationSettings
*/
enum BaseStationSetting {
InvalidSetting = -1,
ReadySoundEnableSetting,
ReadySoundDelaySetting,
AtYourMarksSoundEnableSetting,
AtYourMarksSoundDelaySetting,
SoundVolumeSetting
};
Q_ENUM(BaseStationSetting)
enum KeyLevelEnum {
KeyLevel = 0
};
Q_ENUM(KeyLevelEnum)
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<BaseStationSetting>();
return static_cast<BaseStationSetting>(enumeration.keyToValue(enumeration.valueToKey(i)));
}
static QString keyToString(int key) {
return QMetaEnum::fromType<BaseStationSetting>().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 bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1);
bool registerKeyLevelHandler(int keyLevel, keyToStringConverter);
private:
QFile * settingsFile;
QVariantMap settingsCache;
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<int, keyToStringConverter> keyToStringConverters;
QMap<int, QMap<int, ScStwSettingInternal*>> internalSettingHandlers;
private slots:
bool writeSettingsToFile();
signals:
void settingChanged(int key, int keyLevel, QVariant value);
};
#endif // SCSTWSETTINGS_H