Utilize templates for Settings class

Use boost2::signals for the valueChanged signal (a LOT less messy to
        deal with than qt signals)
Remove unused settings classes (BoolSetting, FloatSetting, IntSetting,
        StringSetting)

What's left: Implement the remaining signals
This commit is contained in:
Rasmus Karlsson
2017-01-22 23:00:35 +01:00
parent 84be4c9046
commit 5429e62e73
13 changed files with 189 additions and 512 deletions
+50 -9
View File
@@ -3,22 +3,61 @@
#include <QSettings>
#include <QString>
#include <boost/signals2.hpp>
namespace chatterino {
namespace settings {
class Setting : public QObject
class BaseSetting
{
Q_OBJECT
public:
explicit Setting(const QString &name)
: name(name)
virtual void save(QSettings &settings) = 0;
virtual void load(const QSettings &settings) = 0;
};
template <typename T>
class Setting : public BaseSetting
{
public:
Setting(const QString &_name, const T &defaultValue)
: name(_name)
, value(defaultValue)
{
}
virtual void save(const QSettings &settings) = 0;
virtual void load(const QSettings &settings) = 0;
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;
protected:
const QString &
@@ -29,8 +68,10 @@ protected:
private:
QString name;
T value;
};
}
}
} // namespace settings
} // namespace chatterino
#endif // SETTING_H