-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.h
58 lines (54 loc) · 2.71 KB
/
settings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <QSettings>
#include <QColor>
#include <QSize>
#include <QApplication>
#include <memory>
#include "globals.h"
class BasicSetting
{
public:
BasicSetting(const QString &applicationName,const QString &category,const QString &name,const QVariant &defaultValue=QVariant()) : name(QString("%1/%2").arg(category,name)), defaultValue(defaultValue), source(std::make_shared<QSettings>(Platform::Windows() ? QSettings::IniFormat : QSettings::NativeFormat,QSettings::UserScope,qApp->organizationName(),applicationName))
{
// if the setting is the word "true" or "false", convert the value to an actual boolean
// this is because everything is read in as a QString (https://stackoverflow.com/questions/32654233/qsettings-with-different-types)
// this is a problem because I'm relying on type information to differentiate between whether a value is false or nonexistent
// this hack will have unintended side-effects that may become a problem later
QString text=Value().toString();
if (text == "true") Set(true);
if (text == "false") Set(false);
}
const QString Name() const { return name; }
void Save() { source->sync(); }
const QVariant Value() const { return source->value(name,defaultValue); }
void Set(const QVariant &value) { source->setValue(name,value); }
void Unset() { source->remove(name); }
operator bool() const
{
const QVariant candidate=Value();
if (!candidate.isValid()) return false; // setting does not appear in file, no default value
if (Value().userType() == QMetaType::Bool) return candidate.toBool(); // setting appears in file and it a boolean
return true; // setting appears in file
}
operator QString() const { return Value().toString(); }
operator unsigned int() const { return Value().toUInt(); }
operator int() const { return Value().toInt(); }
operator quint16() const { return Value().toUInt(); }
operator qint64() const { return Value().toLongLong(); }
operator qreal() const { return Value().toReal(); }
operator std::chrono::milliseconds() const { return std::chrono::milliseconds(Value().toUInt()); }
operator std::chrono::seconds() const { return std::chrono::seconds(Value().toUInt()); }
operator QColor() const { return source->value(name,defaultValue).value<QColor>(); }
operator QSize() const { return source->value(name,defaultValue).toSize(); }
operator QByteArray() const { return Value().toString().toLocal8Bit(); }
operator QUrl() const { return Value().toUrl(); }
protected:
QString name;
QVariant defaultValue;
std::shared_ptr<QSettings> source;
};
class ApplicationSetting : public BasicSetting
{
public:
ApplicationSetting(const QString &category,const QString &name,const QVariant &value=QVariant()) : BasicSetting(qApp->applicationName(),category,name,value) { }
};