2017-06-07 10:09:24 +02:00
|
|
|
#pragma once
|
2017-01-18 21:30:23 +01:00
|
|
|
|
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 {
|
|
|
|
|
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:
|
2017-01-23 09:48:32 +01:00
|
|
|
BaseSetting(const QString &_name)
|
2017-09-21 12:15:01 +02:00
|
|
|
: name(_name)
|
2017-01-23 09:48:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:51:57 +01:00
|
|
|
virtual QVariant getVariant() = 0;
|
|
|
|
virtual void setVariant(QVariant value) = 0;
|
2017-01-23 09:48:32 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
const QString &getName() const
|
2017-01-23 09:48:32 +01:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->name;
|
2017-01-23 09:48:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-21 12:15:01 +02:00
|
|
|
QString name;
|
2017-01-22 23:00:35 +01:00
|
|
|
};
|
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-04-12 17:46:44 +02:00
|
|
|
Setting(std::vector<std::reference_wrapper<BaseSetting>> &settingItems, const QString &_name,
|
|
|
|
const T &defaultValue)
|
2017-01-23 09:48:32 +01:00
|
|
|
: BaseSetting(_name)
|
2017-09-21 12:15:01 +02:00
|
|
|
, value(defaultValue)
|
2017-01-18 21:30:23 +01:00
|
|
|
{
|
2017-01-23 09:48:32 +01:00
|
|
|
settingItems.push_back(*this);
|
2017-01-18 21:30:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
const T &get() const
|
2017-01-22 23:00:35 +01:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->value;
|
2017-01-22 23:00:35 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 00:57:42 +02:00
|
|
|
T &getnonConst()
|
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return this->value;
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void set(const T &newValue)
|
2017-01-22 23:00:35 +01:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
if (this->value != newValue) {
|
|
|
|
this->value = newValue;
|
2017-01-22 23:00:35 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
valueChanged(newValue);
|
2017-01-22 23:00:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
virtual QVariant getVariant() final
|
2017-01-22 23:00:35 +01:00
|
|
|
{
|
2017-09-21 12:15:01 +02:00
|
|
|
return QVariant::fromValue(this->value);
|
2017-01-22 23:00:35 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:13:42 +01:00
|
|
|
virtual void setVariant(QVariant newValue) final
|
2017-01-22 23:00:35 +01:00
|
|
|
{
|
2017-12-22 15:13:42 +01:00
|
|
|
if (newValue.isValid()) {
|
|
|
|
assert(newValue.canConvert<T>());
|
|
|
|
set(newValue.value<T>());
|
2017-01-22 23:00:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 00:57:42 +02:00
|
|
|
void insertMap(QString id, bool sound, bool task)
|
|
|
|
{
|
|
|
|
QPair<bool, bool> pair(sound, task);
|
2017-09-21 12:15:01 +02:00
|
|
|
this->value.insert(id, pair);
|
2017-07-31 00:37:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-22 23:00:35 +01:00
|
|
|
boost::signals2::signal<void(const T &newValue)> valueChanged;
|
2017-01-20 06:10:28 +01:00
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
private:
|
2017-09-21 12:15:01 +02:00
|
|
|
T value;
|
2017-01-18 21:30:23 +01:00
|
|
|
};
|
2017-01-22 23:00:35 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|