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>
|
2017-01-22 23:00:35 +01:00
|
|
|
#include <boost/signals2.hpp>
|
2017-01-18 21:30:23 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace settings {
|
|
|
|
|
2017-01-22 23:00:35 +01:00
|
|
|
class BaseSetting
|
2017-01-18 21:30:23 +01:00
|
|
|
{
|
2017-01-22 23:00:35 +01:00
|
|
|
public:
|
|
|
|
virtual void save(QSettings &settings) = 0;
|
|
|
|
virtual void load(const QSettings &settings) = 0;
|
|
|
|
};
|
2017-01-22 05:58:23 +01:00
|
|
|
|
2017-01-22 23:00:35 +01:00
|
|
|
template <typename T>
|
|
|
|
class Setting : public BaseSetting
|
|
|
|
{
|
2017-01-18 21:30:23 +01:00
|
|
|
public:
|
2017-01-22 23:00:35 +01:00
|
|
|
Setting(const QString &_name, const T &defaultValue)
|
|
|
|
: name(_name)
|
|
|
|
, value(defaultValue)
|
2017-01-18 21:30:23 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-22 23:00:35 +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;
|
2017-01-22 23:00:35 +01:00
|
|
|
T value;
|
2017-01-18 21:30:23 +01:00
|
|
|
};
|
2017-01-22 23:00:35 +01:00
|
|
|
|
|
|
|
} // namespace settings
|
|
|
|
} // namespace chatterino
|
2017-01-18 21:30:23 +01:00
|
|
|
|
|
|
|
#endif // SETTING_H
|