From 1a2fbcc1702b5c9e8378d1b86e8645c063835c83 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Mon, 6 Jul 2020 15:04:02 +0200 Subject: [PATCH] started to migrate to new settings --- ScStwApp.pro | 6 +-- headers/appsettings.h | 28 ----------- headers/scstwappbackend.h | 13 +++-- headers/scstwappsettings.h | 39 +++++++++++++++ shared-libraries | 2 +- sources/appsettings.cpp | 69 -------------------------- sources/main.cpp | 4 +- sources/scstwappbackend.cpp | 93 ++++++++++++++++++++---------------- sources/scstwappsettings.cpp | 48 +++++++++++++++++++ 9 files changed, 151 insertions(+), 151 deletions(-) delete mode 100644 headers/appsettings.h create mode 100644 headers/scstwappsettings.h delete mode 100644 sources/appsettings.cpp create mode 100644 sources/scstwappsettings.cpp diff --git a/ScStwApp.pro b/ScStwApp.pro index c05eabc..11a1f76 100644 --- a/ScStwApp.pro +++ b/ScStwApp.pro @@ -27,11 +27,11 @@ include($$PWD/shared-libraries/ScStwLibraries/ScStwLibraries.pri) SOURCES += \ sources/scstwappbackend.cpp \ sources/main.cpp \ - sources/appsettings.cpp + sources/scstwappsettings.cpp HEADERS += \ - headers/appsettings.h \ - headers/scstwappbackend.h + headers/scstwappbackend.h \ + headers/scstwappsettings.h RESOURCES += \ resources/shared/shared.qrc \ diff --git a/headers/appsettings.h b/headers/appsettings.h deleted file mode 100644 index 1cb6de4..0000000 --- a/headers/appsettings.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef APPSETTINGS_H -#define APPSETTINGS_H - -#include -#include -#include - -class AppSettings : public QObject -{ - Q_OBJECT -public: - explicit AppSettings(QObject *parent = nullptr); - ~AppSettings(); - - Q_INVOKABLE QString loadSetting(QString key); - Q_INVOKABLE void writeSetting(QString key, QVariant variant); - Q_INVOKABLE void setDefaultSetting(QString key, QVariant defaultVariant); - - QSettings *settingsManager; - -signals: - -public slots: - -}; -extern AppSettings * pGlobalAppSettings; - -#endif // APPSETTINGS_H diff --git a/headers/scstwappbackend.h b/headers/scstwappbackend.h index 9a5a5cf..20d2e51 100644 --- a/headers/scstwappbackend.h +++ b/headers/scstwappbackend.h @@ -8,7 +8,7 @@ #include #include -#include "headers/appsettings.h" +#include "headers/scstwappsettings.h" class ScStwAppBackend : public QObject @@ -18,6 +18,7 @@ class ScStwAppBackend : public QObject Q_PROPERTY(int mode READ getMode NOTIFY modeChanged) Q_PROPERTY(ScStwRace* race READ getRace NOTIFY raceChanged) Q_PROPERTY(ScStwClient *scStwClient READ getScStwClient NOTIFY scStwClientChanged) + Q_PROPERTY(ScStwAppSettings *settings READ getSettings NOTIFY settingsChanged) public: explicit ScStwAppBackend(QObject *parent = nullptr); @@ -27,7 +28,7 @@ public: private: RaceMode mode; - AppSettings * appSettings; + ScStwAppSettings * appSettings; ScStwClient * scStwClient; QTimer * timerTextRefreshTimer; @@ -39,13 +40,9 @@ public slots: // functions for qml Q_INVOKABLE ScStwRace *getRace(); Q_INVOKABLE ScStwClient *getScStwClient(); + Q_INVOKABLE ScStwAppSettings *getSettings(); Q_INVOKABLE int getMode(); - Q_INVOKABLE void writeSetting(QString key, QVariant value); - Q_INVOKABLE void writeSetting(ScStw::BaseStationSetting key, QVariant value); - Q_INVOKABLE QString readSetting(QString key); - Q_INVOKABLE QString readSetting(ScStw::BaseStationSetting key); - // athlete management Q_INVOKABLE QVariant getAthletes(); Q_INVOKABLE bool createAthlete( QString userName, QString fullName ); @@ -58,11 +55,13 @@ private slots: void refreshMode(); void reloadRaceSettings(); void reloadBaseStationIpAdress(); + void handleSettingChange(int keyInt, int keyLevel); signals: void modeChanged(); void raceChanged(); void scStwClientChanged(); + void settingsChanged(); void baseStationStateChanged(); void baseStationConnectionsChanged(); diff --git a/headers/scstwappsettings.h b/headers/scstwappsettings.h new file mode 100644 index 0000000..11e309f --- /dev/null +++ b/headers/scstwappsettings.h @@ -0,0 +1,39 @@ +#ifndef APPSETTINGS_H +#define APPSETTINGS_H + +#include +#include +#include +#include "scstwsettings.h" + +class ScStwAppSettings : public ScStwSettings +{ + Q_OBJECT +public: + explicit ScStwAppSettings(QObject *parent = nullptr); + ~ScStwAppSettings(); + + enum AppInternalSetting { + InvalidSetting = -1, + AppThemeSetting, + BaseStationIpSetting + }; + Q_ENUM(AppInternalSetting) + + using ScStwSettings::readSetting; + QVariant readSetting(AppInternalSetting key); + using ScStwSettings::writeSetting; + bool writeSetting(AppInternalSetting key, QVariant value); + using ScStwSettings::setDefaultSetting; + void setDefaultSetting(AppInternalSetting key, QVariant defaultVariant); + + QSettings *settingsManager; + +signals: + +public slots: + +}; +extern ScStwAppSettings * pGlobalAppSettings; + +#endif // APPSETTINGS_H diff --git a/shared-libraries b/shared-libraries index ae9e639..3d18b72 160000 --- a/shared-libraries +++ b/shared-libraries @@ -1 +1 @@ -Subproject commit ae9e6398ac233217ac89ef009fd84d8ce19f4159 +Subproject commit 3d18b72eaf7fc8993ff60f1e087fa0c30516f4b5 diff --git a/sources/appsettings.cpp b/sources/appsettings.cpp deleted file mode 100644 index 1ea1ffb..0000000 --- a/sources/appsettings.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - Speed Climbing Stopwatch - Simple Stopwatch for Climbers - Copyright (C) 2018 Itsblue Development - Dorian Zeder - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, version 3 of the License. - - 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 Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -#include "headers/appsettings.h" - -AppSettings * pGlobalAppSettings = nullptr; - -AppSettings::AppSettings(QObject* parent) - :QObject(parent) -{ - QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - - this->settingsManager = new QSettings(path+"/settings.ini", QSettings::IniFormat, this); - - this->setDefaultSetting("ready_en", "false"); - this->setDefaultSetting("ready_delay", 0); - this->setDefaultSetting("at_marks_en", "false"); - this->setDefaultSetting("at_marks_delay", 0); - - this->setDefaultSetting("theme", "Light"); - - this->setDefaultSetting("baseStationIpAdress", "192.168.4.1"); - - pGlobalAppSettings = this; -} - -QString AppSettings::loadSetting(QString key) -{ - this->settingsManager->beginGroup("AppSettings"); - QString value = this->settingsManager->value(key , false).toString(); - this->settingsManager->endGroup(); - return(value); -} - -void AppSettings::writeSetting(QString key, QVariant variant) -{ - this->settingsManager->beginGroup("AppSettings"); - this->settingsManager->setValue(key , variant); - this->settingsManager->endGroup(); -} - -void AppSettings::setDefaultSetting(QString key, QVariant defaultVariant) -{ - QString value = this->loadSetting(key); - if(value == "false"){ - this->writeSetting(key, defaultVariant); - } - -} - -AppSettings::~AppSettings() -{ - delete settingsManager; -} - diff --git a/sources/main.cpp b/sources/main.cpp index 860585d..5378cd7 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -50,7 +50,7 @@ #include #endif -#include "headers/appsettings.h" +#include "headers/scstwappsettings.h" //#include "headers/speedtimer.h" //#include "headers/climbingrace.h" #include "headers/scstwappbackend.h" @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) }); #endif - AppSettings * pAppSettings = new AppSettings(); + ScStwAppSettings * pAppSettings = new ScStwAppSettings(); // setup speed backend qmlRegisterType("de.itsblue.ScStwApp", 2, 0, "SpeedBackend"); diff --git a/sources/scstwappbackend.cpp b/sources/scstwappbackend.cpp index 78a4483..6af90e4 100644 --- a/sources/scstwappbackend.cpp +++ b/sources/scstwappbackend.cpp @@ -2,13 +2,23 @@ ScStwAppBackend::ScStwAppBackend(QObject *parent) : QObject(parent) { - this->appSettings = new AppSettings(this); + this->appSettings = new ScStwAppSettings(this); + + this->appSettings->setDefaultSetting(ScStwSettings::ReadySoundEnableSetting, false); + this->appSettings->setDefaultSetting(ScStwSettings::ReadySoundDelaySetting, 0); + this->appSettings->setDefaultSetting(ScStwSettings::AtYourMarksSoundEnableSetting, false); + this->appSettings->setDefaultSetting(ScStwSettings::AtYourMarksSoundDelaySetting, 0); + + this->appSettings->setDefaultSetting(ScStwAppSettings::AppThemeSetting, "Light"); + + this->appSettings->setDefaultSetting(ScStwAppSettings::BaseStationIpSetting, "192.168.4.1"); + this->scStwClient = new ScStwClient(); this->localRace = new ScStwRace(this); this->remoteRace = new ScStwRemoteMonitorRace(this->scStwClient, this); this->mode = LOCAL; - this->scStwClient->setIP(pGlobalAppSettings->loadSetting("baseStationIpAdress")); + this->scStwClient->setIP(this->appSettings->readSetting(ScStwAppSettings::BaseStationIpSetting).toString()); connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwAppBackend::baseStationStateChanged); connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwAppBackend::refreshMode); connect(this, &ScStwAppBackend::baseStationStateChanged, this, &ScStwAppBackend::baseStationPropertiesChanged); @@ -164,6 +174,10 @@ ScStwRace* ScStwAppBackend::getRace() { return nullptr; } +ScStwAppSettings * ScStwAppBackend::getSettings() { + return this->appSettings; +} + ScStwClient* ScStwAppBackend::getScStwClient() { return this->scStwClient; } @@ -172,51 +186,17 @@ int ScStwAppBackend::getMode() { return this->mode; } -void ScStwAppBackend::writeSetting(QString key, QVariant value) { - if(this->mode == REMOTE && ScStw::baseStationSettingFromString(key) != ScStw::InvalidSetting ){ - this->scStwClient->writeRemoteSetting(ScStw::baseStationSettingFromString(key), value.toString()); - } - else { - this->appSettings->writeSetting(key, value); - } - - this->reloadRaceSettings(); - this->reloadBaseStationIpAdress(); -} - -void ScStwAppBackend::writeSetting(ScStw::BaseStationSetting key, QVariant value) { - if(ScStw::baseStationSettingToString(key) != "Invalid" ){ - this->writeSetting(ScStw::baseStationSettingToString(key), value); - } -} - -QString ScStwAppBackend::readSetting(QString key) { - if(this->mode == REMOTE && ScStw::baseStationSettingFromString(key) != ScStw::InvalidSetting){ - return this->scStwClient->readRemoteSetting(ScStw::baseStationSettingFromString(key)); - } - else { - return this->appSettings->loadSetting(key); - } -} - -QString ScStwAppBackend::readSetting(ScStw::BaseStationSetting key) { - if(ScStw::baseStationSettingToString(key) != "Invalid") { - return this->readSetting(ScStw::baseStationSettingToString(key)); - } - return "false"; -} - void ScStwAppBackend::reloadRaceSettings() { this->getRace()->writeStartActionSetting( ScStwRace::AtYourMarks, - this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::AtYourMarksSoundEnableSetting)) == "true", - this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::AtYourMarksSoundDelaySetting)).toDouble() + this->appSettings->readSetting(ScStwSettings::AtYourMarksSoundEnableSetting).toBool(), + this->appSettings->readSetting(ScStwSettings::AtYourMarksSoundDelaySetting).toDouble() ); this->getRace()->writeStartActionSetting( ScStwRace::Ready, - this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::ReadySoundEnableSetting)) == "true", - this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::ReadySoundDelaySetting)).toDouble() + this->appSettings->readSetting(ScStwSettings::ReadySoundEnableSetting).toBool(), + this->appSettings->readSetting(ScStwSettings::ReadySoundDelaySetting).toDouble() ); this->getRace()->setSoundVolume(1); @@ -224,5 +204,36 @@ void ScStwAppBackend::reloadRaceSettings() { } void ScStwAppBackend::reloadBaseStationIpAdress() { - this->scStwClient->setIP(pGlobalAppSettings->loadSetting("baseStationIpAdress")); + this->scStwClient->setIP(this->appSettings->readSetting(ScStwAppSettings::BaseStationIpSetting).toString()); +} + +void ScStwAppBackend::handleSettingChange(int keyInt, int keyLevel) { + switch (keyLevel) { + case 0: { + // BaseStationSetting!! + ScStwSettings::BaseStationSetting key = static_cast(keyInt); + switch (key) { + case ScStwSettings::InvalidSetting: + return; + default: + this->reloadRaceSettings(); + return; + } + break; + } + case 1: { + // BaseStationInternalSetting!! + ScStwAppSettings::AppInternalSetting key = static_cast(keyInt); + switch (key) { + case ScStwAppSettings::InvalidSetting: + return; + case ScStwAppSettings::BaseStationIpSetting: + this->reloadBaseStationIpAdress(); + break; + default: + break; + } + break; + } + } } diff --git a/sources/scstwappsettings.cpp b/sources/scstwappsettings.cpp new file mode 100644 index 0000000..1616e01 --- /dev/null +++ b/sources/scstwappsettings.cpp @@ -0,0 +1,48 @@ +/* + Speed Climbing Stopwatch - Simple Stopwatch for Climbers + Copyright (C) 2018 Itsblue Development - Dorian Zeder + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, version 3 of the License. + + 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +#include "headers/scstwappsettings.h" + +ScStwAppSettings * pGlobalAppSettings = nullptr; + +ScStwAppSettings::ScStwAppSettings(QObject* parent) + :ScStwSettings(parent) +{ + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + + this->settingsManager = new QSettings(path+"/settings.ini", QSettings::IniFormat, this); + + pGlobalAppSettings = this; +} + +QVariant ScStwAppSettings::readSetting(AppInternalSetting key) { + return ScStwSettings::readSetting(QMetaEnum::fromType().valueToKey(int(key))); +} + +bool ScStwAppSettings::writeSetting(AppInternalSetting key, QVariant value) { + return ScStwSettings::writeSetting(QMetaEnum::fromType().valueToKey(int(key)), value, int(key), 1); +} + +void ScStwAppSettings::setDefaultSetting(AppInternalSetting key, QVariant defaultVariant) { + ScStwSettings::setDefaultSetting(QMetaEnum::fromType().valueToKey(int(key)), defaultVariant, int(key), 1); +} + +ScStwAppSettings::~ScStwAppSettings() +{ + delete settingsManager; +} +