From ad3f8318c709653863058fc234469bd0ff006042 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sat, 11 Jul 2020 11:51:42 +0200 Subject: [PATCH] created class "ScStwSetting" which can be used as a handler for settings --- ScStwLibraries/ScStwLibraries.pri | 8 +-- .../headers/client/scstwqmlsetting.h | 16 ------ ScStwLibraries/headers/client/scstwsetting.h | 47 ++++++++++++++++ ScStwLibraries/headers/scstwlibraries.h | 1 + ScStwLibraries/headers/scstwsettings.h | 2 + .../sources/client/scstwqmlsetting.cpp | 6 -- .../sources/client/scstwsetting.cpp | 55 +++++++++++++++++++ ScStwLibraries/sources/scstwlibraries.cpp | 1 + ScStwLibraries/sources/scstwsettings.cpp | 14 +++++ 9 files changed, 124 insertions(+), 26 deletions(-) delete mode 100644 ScStwLibraries/headers/client/scstwqmlsetting.h create mode 100644 ScStwLibraries/headers/client/scstwsetting.h delete mode 100644 ScStwLibraries/sources/client/scstwqmlsetting.cpp create mode 100644 ScStwLibraries/sources/client/scstwsetting.cpp diff --git a/ScStwLibraries/ScStwLibraries.pri b/ScStwLibraries/ScStwLibraries.pri index 0d1a693..781de65 100644 --- a/ScStwLibraries/ScStwLibraries.pri +++ b/ScStwLibraries/ScStwLibraries.pri @@ -36,16 +36,16 @@ HEADERS += \ ScStwLibraries_ClientLibs { SOURCES += \ - $$PWD/sources/client/scstwqmlsetting.cpp \ $$PWD/sources/client/scstwclient.cpp \ $$PWD/sources/client/scstwremotesettings.cpp \ - $$PWD/sources/client/scstwremotemonitorrace.cpp + $$PWD/sources/client/scstwremotemonitorrace.cpp \ + $$PWD/sources/client/scstwsetting.cpp HEADERS += \ - $$PWD/headers/client/scstwqmlsetting.h \ $$PWD/headers/client/scstwclient.h \ $$PWD/headers/client/scstwremotesettings.h \ - $$PWD/headers/client/scstwremotemonitorrace.h + $$PWD/headers/client/scstwremotemonitorrace.h \ + $$PWD/headers/client/scstwsetting.h INCLUDEPATH += $$PWD/headers/client } diff --git a/ScStwLibraries/headers/client/scstwqmlsetting.h b/ScStwLibraries/headers/client/scstwqmlsetting.h deleted file mode 100644 index 85e0809..0000000 --- a/ScStwLibraries/headers/client/scstwqmlsetting.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef SCSTWQMLSETTING_H -#define SCSTWQMLSETTING_H - -#include - -class ScStwQmlSetting : public QObject -{ - Q_OBJECT -public: - explicit ScStwQmlSetting(QObject *parent = nullptr); - -signals: - -}; - -#endif // SCSTWQMLSETTING_H diff --git a/ScStwLibraries/headers/client/scstwsetting.h b/ScStwLibraries/headers/client/scstwsetting.h new file mode 100644 index 0000000..42f857c --- /dev/null +++ b/ScStwLibraries/headers/client/scstwsetting.h @@ -0,0 +1,47 @@ +#ifndef SCSTWQMLSETTING_H +#define SCSTWQMLSETTING_H + +#include +#include + +class ScStwSetting : public QObject +{ + Q_OBJECT + Q_PROPERTY(ScStwSettings* scStwSettings READ getScStwSettings WRITE setScStwSettings NOTIFY scStwSettingsChanged) + Q_PROPERTY(int key READ getKey WRITE setKey NOTIFY keyChanged) + Q_PROPERTY(int keyLevel READ getKeyLevel WRITE setKeyLevel NOTIFY keyLevelChanged) + Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged) +public: + explicit ScStwSetting(QObject *parent = nullptr); + +private: + ScStwSettings* scStwSettings; + int key; + int keyLevel; + QVariant valueCache; + +public slots: + ScStwSettings* getScStwSettings(); + void setScStwSettings(ScStwSettings* scStwSettings); + + int getKey(); + void setKey(int key); + + int getKeyLevel(); + void setKeyLevel(int keyLevel); + + QVariant getValue(); + void setValue(QVariant value); + +private slots: + void handleSettingChange(int key, int keyLevel); + +signals: + void scStwSettingsChanged(); + void keyChanged(); + void keyLevelChanged(); + void valueChanged(); + +}; + +#endif // SCSTWQMLSETTING_H diff --git a/ScStwLibraries/headers/scstwlibraries.h b/ScStwLibraries/headers/scstwlibraries.h index 846f314..76ffb39 100644 --- a/ScStwLibraries/headers/scstwlibraries.h +++ b/ScStwLibraries/headers/scstwlibraries.h @@ -32,6 +32,7 @@ #include "scstwtimer.h" #include "scstwrace.h" #include "scstwsettings.h" +#include "scstwsetting.h" #ifdef ScStwLibraries_ClientLibs #include "scstwremotemonitorrace.h" #include "scstwclient.h" diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h index 7fca825..eb859e8 100644 --- a/ScStwLibraries/headers/scstwsettings.h +++ b/ScStwLibraries/headers/scstwsettings.h @@ -41,7 +41,9 @@ public: Q_ENUM(KeyLevelEnum) Q_INVOKABLE QVariant readSetting(BaseStationSetting key); + Q_INVOKABLE virtual QVariant readSetting(int key, int level); Q_INVOKABLE bool writeSetting(BaseStationSetting key, QVariant value); + Q_INVOKABLE virtual bool writeSetting(int key, QVariant value, int level); Q_INVOKABLE void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant); static BaseStationSetting keyFromInt(int i) { diff --git a/ScStwLibraries/sources/client/scstwqmlsetting.cpp b/ScStwLibraries/sources/client/scstwqmlsetting.cpp deleted file mode 100644 index 86321c3..0000000 --- a/ScStwLibraries/sources/client/scstwqmlsetting.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "../../headers/client/scstwqmlsetting.h" - -ScStwQmlSetting::ScStwQmlSetting(QObject *parent) : QObject(parent) -{ - -} diff --git a/ScStwLibraries/sources/client/scstwsetting.cpp b/ScStwLibraries/sources/client/scstwsetting.cpp new file mode 100644 index 0000000..df0f845 --- /dev/null +++ b/ScStwLibraries/sources/client/scstwsetting.cpp @@ -0,0 +1,55 @@ +#include "scstwsetting.h" + +ScStwSetting::ScStwSetting(QObject *parent) : QObject(parent) +{ + +} + +ScStwSettings* ScStwSetting::getScStwSettings(){ + return this->scStwSettings; +} +void ScStwSetting::setScStwSettings(ScStwSettings* scStwSettings) { + if(this->scStwSettings != scStwSettings && scStwSettings != nullptr) { + this->scStwSettings = scStwSettings; + connect(this->scStwSettings, &ScStwSettings::settingChanged, this, &ScStwSetting::handleSettingChange); + emit this->scStwSettingsChanged(); + } +} + +int ScStwSetting::getKey() { + return this->key; +} +void ScStwSetting::setKey(int key) { + if(this->key != key) { + this->key = key; + this->handleSettingChange(this->key, this->keyLevel); + emit this->keyChanged(); + } +} + +int ScStwSetting::getKeyLevel() { + return this->keyLevel; +} +void ScStwSetting::setKeyLevel(int keyLevel) { + if(this->keyLevel != keyLevel) { + this->keyLevel = keyLevel; + this->handleSettingChange(this->key, this->keyLevel); + emit this->keyLevelChanged(); + } +} + +QVariant ScStwSetting::getValue() { + return this->valueCache; +} +void ScStwSetting::setValue(QVariant value) { + if(value != this->valueCache && this->scStwSettings != nullptr) { + this->scStwSettings->writeSetting(this->key, value, this->keyLevel); + } +} + +void ScStwSetting::handleSettingChange(int key, int keyLevel) { + if(keyLevel == this->keyLevel && key == this->key) { + this->valueCache = this->scStwSettings->readSetting(key, keyLevel); + emit this->valueChanged(); + } +} diff --git a/ScStwLibraries/sources/scstwlibraries.cpp b/ScStwLibraries/sources/scstwlibraries.cpp index 4470c31..701367c 100644 --- a/ScStwLibraries/sources/scstwlibraries.cpp +++ b/ScStwLibraries/sources/scstwlibraries.cpp @@ -35,6 +35,7 @@ void ScStwLibraries::init() { #ifdef ScStwLibraries_ClientLibs qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwClient"); qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwSettings"); + qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwSetting"); #endif #endif } diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index 3be7226..7da1ac8 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -22,10 +22,24 @@ QVariant ScStwSettings::readSetting(BaseStationSetting key) { return this->readSetting(QMetaEnum::fromType().valueToKey(int(key)), int(key), 0); } +QVariant ScStwSettings::readSetting(int key, int keyLevel) { + if(keyLevel == ScStwSettings::KeyLevel) + return this->readSetting(ScStwSettings::BaseStationSetting(key)); + else + return QVariant(); +} + bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) { return this->writeSetting(QMetaEnum::fromType().valueToKey(int(key)), value, int(key), 0); } +bool ScStwSettings::writeSetting(int key, QVariant value, int keyLevel) { + if(keyLevel == ScStwSettings::KeyLevel) + return this->writeSetting(ScStwSettings::BaseStationSetting(key), value); + else + return false; +} + void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVariant) { this->setDefaultSetting(QMetaEnum::fromType().valueToKey(int(key)), defaultVariant, int(key), 0); }