added settings listner

This commit is contained in:
Andrei Yankovich 2021-11-19 21:54:45 +03:00
parent c27ca21445
commit e56482ab50
3 changed files with 94 additions and 0 deletions

View File

@ -58,6 +58,10 @@ void ISettings::sync() {
void ISettings::setValue(const QString key, const QVariant &value) {
if (_cache.contains(key) && _cache.value(key) == value) {
return;
}
_cache[key] = value;
emit valueChanged(key, value);

32
settingslistner.cpp Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "isettings.h"
#include "settingslistner.h"
namespace QuasarAppUtils {
SettingsListner::SettingsListner() {
auto settings = ISettings::instance();
if (settings) {
auto listner = [this](const QString& key, const QVariant& val){
this->handleSettingsChanged(key, val);
};
_listnerConnection = QObject::connect(settings,
&ISettings::valueChanged,
listner);
}
}
SettingsListner::~SettingsListner() {
QObject::disconnect(_listnerConnection);
}
}

58
settingslistner.h Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef SETTINGSLISTNER_H
#define SETTINGSLISTNER_H
#include <QString>
#include <QVariant>
namespace QuasarAppUtils {
/**
* @brief The SettingsListner class is listner of the ISettings global object.
* The SettingsListner class is abstrct class and contains only one method for hendling settings changes.
*
*
* ### Example of use :
*
* @code{cpp}
* class MyClass : protected QuasarAppUtils::SettingsListner {
* protected:
* void handleSettingsChanged(const QString& key, const QVariant& value) override {
*
* if (key == "shareName") {
setSessinon(static_cast<long long >(rand()) * rand());
}
* }
*
* };
* @endcode
* @see ISettings
*/
class SettingsListner
{
public:
SettingsListner();
virtual ~SettingsListner();
protected:
/**
* @brief handleSettingsChanged This method will be invoked when settings of application has bean changed.
* @param key This is key of a changed settings.
* @param value This is a new value of a changed settings.
*/
virtual void handleSettingsChanged(const QString& key,
const QVariant& value) = 0;
private:
QMetaObject::Connection _listnerConnection;
};
}
#endif // SETTINGSLISTNER_H