-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsettings.h
119 lines (84 loc) · 2.57 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QtCore>
#define SETTINGS_CONTAINS Settings::instance()->contains
#define SETTINGS_GET_BOOL Settings::instance()->value<bool>
#define SETTINGS_SET_BOOL Settings::instance()->setValue<bool>
#define SETTINGS_GET_INT Settings::instance()->value<int>
#define SETTINGS_SET_INT Settings::instance()->setValue<int>
#define SETTINGS_GET_STRING Settings::instance()->value<QString>
#define SETTINGS_SET_STRING Settings::instance()->setValue<QString>
#define SETTINGS_REMOVE Settings::instance()->remove
#define SETTING_LAST_DIRECTORY "last-directory"
#define SETTING_SKIP_TAGGED "skip-tagged"
#define SETTING_MOVE_UNTAGGED "move-untagged"
#define SETTING_TOOLTIPS "tooltips"
class SettingsPrivate;
/*
* System settings
*/
class Settings
{
public:
static Settings* instance();
~Settings();
enum SyncType { NoSync, Sync };
template <typename T>
T value(const QString &key);
template <typename T>
T value(const QString &key, const T &def);
template <typename T>
void setValue(const QString &key, const T &value, SyncType sync = Sync);
QVariant defaultValue(const QString &key) const;
/*
* Remove the specified key from the section "settings"
*/
void remove(const QString &key, SyncType sync = Sync);
void sync();
/*
* Available translations, hardcoded
*/
QMap<QString, QString> translations();
private:
Settings();
/*
* Install some default values
*/
void addDefaultValues();
void addDefaultValues(const QHash<QString, QVariant> &defaultValues);
void fillTranslations();
QHash<QString, QVariant> &defaultValues();
QSettings *settings() const;
private:
SettingsPrivate *d;
};
/**********************************/
template <typename T>
T Settings::value(const QString &key)
{
T def = T();
QHash<QString, QVariant>::iterator it = defaultValues().find(key);
if(it != defaultValues().end())
def = it.value().value<T>();
return value<T>(key, def);
}
template <typename T>
T Settings::value(const QString &key, const T &def)
{
QSettings *s = settings();
s->beginGroup("settings");
QVariant value = s->value(key, QVariant::fromValue(def));
s->endGroup();
return value.value<T>();
}
template <typename T>
void Settings::setValue(const QString &key, const T &value, Settings::SyncType sync)
{
QSettings *s = settings();
s->beginGroup("settings");
s->setValue(key, QVariant::fromValue(value));
s->endGroup();
if(sync == Sync)
s->sync();
}
#endif // SETTINGS_H