created class "ScStwSetting" which can be used as a handler for settings

This commit is contained in:
Dorian Zedler 2020-07-11 11:51:42 +02:00
parent ba8d786ca2
commit ad3f8318c7
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
9 changed files with 124 additions and 26 deletions

View file

@ -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
}

View file

@ -1,16 +0,0 @@
#ifndef SCSTWQMLSETTING_H
#define SCSTWQMLSETTING_H
#include <QObject>
class ScStwQmlSetting : public QObject
{
Q_OBJECT
public:
explicit ScStwQmlSetting(QObject *parent = nullptr);
signals:
};
#endif // SCSTWQMLSETTING_H

View file

@ -0,0 +1,47 @@
#ifndef SCSTWQMLSETTING_H
#define SCSTWQMLSETTING_H
#include <scstwsettings.h>
#include <QObject>
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

View file

@ -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"

View file

@ -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) {

View file

@ -1,6 +0,0 @@
#include "../../headers/client/scstwqmlsetting.h"
ScStwQmlSetting::ScStwQmlSetting(QObject *parent) : QObject(parent)
{
}

View file

@ -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();
}
}

View file

@ -35,6 +35,7 @@ void ScStwLibraries::init() {
#ifdef ScStwLibraries_ClientLibs
qmlRegisterType<ScStwClient>("de.itsblue.ScStw", 2, 0, "ScStwClient");
qmlRegisterType<ScStwSettings>("de.itsblue.ScStw", 2, 0, "ScStwSettings");
qmlRegisterType<ScStwSetting>("de.itsblue.ScStw", 2, 0, "ScStwSetting");
#endif
#endif
}

View file

@ -22,10 +22,24 @@ QVariant ScStwSettings::readSetting(BaseStationSetting key) {
return this->readSetting(QMetaEnum::fromType<BaseStationSetting>().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<BaseStationSetting>().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<BaseStationSetting>().valueToKey(int(key)), defaultVariant, int(key), 0);
}