-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
52 lines (43 loc) · 1.37 KB
/
settings.cpp
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
#include "settings.h"
Settings::Settings() { this->loadDefaults(); }
void Settings::loadDefaults() {
for (const auto &gm : DEFAULTS)
for (const std::pair<const QString, QVariant> &k : gm.second)
if (data[gm.first].find(k.first) == data[gm.first].end())
data[gm.first][k.first] = k.second;
}
void Settings::load(const QString &filename, bool loadNonDefaults) {
QSettings s(filename, QSettings::IniFormat);
#if QT_VERSION < 0x060000
s.setIniCodec("UTF-8");
#endif
data.clear();
const auto& groups = s.childGroups();
for (const auto &g : groups) {
if ((!loadNonDefaults) && (DEFAULTS.find(g) == DEFAULTS.end()))
continue;
s.beginGroup(g);
const auto& keys = s.childKeys();
for (const auto &k : keys) {
if ((!loadNonDefaults) && (DEFAULTS.at(g).find(k) == DEFAULTS.at(g).end()))
continue;
data[g][k] = s.value(k, "");
}
s.endGroup();
}
this->loadDefaults();
}
void Settings::save(const QString &filename) {
QSettings s(filename, QSettings::IniFormat);
#if QT_VERSION < 0x060000
s.setIniCodec("UTF-8");
#endif
for (const auto &gm : data) {
s.beginGroup(gm.first);
for (const std::pair<const QString, QVariant> &k : gm.second)
s.setValue(k.first, k.second);
s.endGroup();
}
}
std::map<QString, QVariant> &Settings::at(const QString &g) { return data[g]; }
std::map<QString, QVariant> &Settings::operator[](const QString &g) { return at(g); }