mirror-chatterino2/settings/setting.h

78 lines
1.4 KiB
C
Raw Normal View History

2017-01-18 21:30:23 +01:00
#ifndef SETTING_H
#define SETTING_H
2017-01-20 06:10:28 +01:00
#include <QSettings>
2017-01-18 21:30:23 +01:00
#include <QString>
#include <boost/signals2.hpp>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace settings {
class BaseSetting
2017-01-18 21:30:23 +01:00
{
public:
virtual void save(QSettings &settings) = 0;
virtual void load(const QSettings &settings) = 0;
};
template <typename T>
class Setting : public BaseSetting
{
2017-01-18 21:30:23 +01:00
public:
Setting(const QString &_name, const T &defaultValue)
: name(_name)
, value(defaultValue)
2017-01-18 21:30:23 +01:00
{
}
const T &
get() const
{
return this->value;
}
void
set(const T &newValue)
{
if (this->value != newValue) {
this->value = newValue;
this->valueChanged(newValue);
}
}
virtual void
save(QSettings &settings) final
{
settings.setValue(this->getName(), QVariant::fromValue(this->value));
}
virtual void
load(const QSettings &settings) final
{
QVariant newValue = settings.value(this->getName(), QVariant());
if (newValue.isValid()) {
assert(newValue.canConvert<T>());
this->value = newValue.value<T>();
}
}
boost::signals2::signal<void(const T &newValue)> valueChanged;
2017-01-20 06:10:28 +01:00
protected:
2017-01-18 21:30:23 +01:00
const QString &
getName() const
{
return name;
}
private:
QString name;
T value;
2017-01-18 21:30:23 +01:00
};
} // namespace settings
} // namespace chatterino
2017-01-18 21:30:23 +01:00
#endif // SETTING_H