-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cpp
More file actions
202 lines (178 loc) · 4.63 KB
/
Settings.cpp
File metadata and controls
202 lines (178 loc) · 4.63 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "./Settings.h"
#include <QApplication>
#include <QStandardPaths>
QT_UTILS_NAMESPACE_BEGIN
// statics
Settings * Settings::s_Instance = nullptr;
const QSettings::Format Settings::CustomFormat = Settings::RegisterFormat();
//!
//! Default constructor. This will initialize the instance to store settings in
//! a `Settings.bin` file located in the directory returned by
//! `QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)`
//!
Settings::Settings(void)
: Settings(QString("%1/Settings.bin")
.arg(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)))
{
}
//!
//! Initialize the settings so that the settings file will be stored in @p path file
//!
Settings::Settings(const QString & path)
: Settings(path, CustomFormat)
{
}
//!
//! Construct the settings with a given file @p path and @p format. This uses
//! QSettings::QSettings(const QString &, QSettings::Format)
//!
Settings::Settings(const QString & path, QSettings::Format format)
: QSettings(path, format)
{
Q_ASSERT(s_Instance == nullptr);
Q_ASSERT(this->isWritable() == true);
s_Instance = this;
}
//!
//! Get a setting value
//!
QVariant Settings::get(const QString & key, QVariant defaultValue) const
{
return QSettings::value(key, defaultValue);
}
//!
//! Set a setting value
//!
void Settings::set(const QString & key, QVariant value, bool sync)
{
QVariant oldValue = this->value(key);
if (oldValue != value)
{
QSettings::setValue(key, value);
if (sync == true)
{
this->sync();
}
emit settingChanged(key, oldValue, value);
}
}
//!
//! Init a setting. This will set it if it doesn't already exist only.
//! @returns true if the setting was set, false if it already existed.
//!
bool Settings::init(const QString & key, QVariant value, bool sync)
{
if (this->contains(key) == false)
{
QSettings::setValue(key, value);
if (sync == true)
{
this->sync();
}
emit settingChanged(key, QVariant(), value);
return true;
}
return false;
}
//!
//! Returns whether the settings contain the given @p key
//!
bool Settings::contains(const QString & key) const
{
return QSettings::contains(key);
}
//!
//! Remove a setting.
//!
void Settings::remove(const QString & key)
{
QSettings::remove(key);
}
//!
//! Sync the settings.
//!
void Settings::sync(void)
{
QSettings::sync();
}
//!
//! Register our custom format
//!
QSettings::Format Settings::RegisterFormat(void)
{
auto format = QSettings::registerFormat("bin", Settings::Read, Settings::Write);
Q_ASSERT(format != QSettings::InvalidFormat);
return format;
}
//!
//! Read settings file.
//!
bool Settings::Read(QIODevice & device, QSettings::SettingsMap & map)
{
for (int i = 0, iend = Read(device, 0); i < iend; ++i)
{
QVariant key = Read(device);
if (key.isNull() == true || key.isValid() == false)
{
return false;
}
QVariant value = Read(device);
if (value.isNull() == true || value.isValid() == false)
{
return false;
}
map.insert(key.toString(), value);
}
return true;
}
//!
//! Reads an element
//!
QVariant Settings::Read(QIODevice & device)
{
static_assert(sizeof(Type) == sizeof(int));
// get the type
Type type = Read(device, Type::Invalid);
if (type == Type::Invalid)
{
return QVariant();
}
// get the value
switch (type)
{
case Type::Bool: return Read(device, static_cast< char >(0)) != 0;
case Type::Int32: return Read(device, static_cast< int >(0));
case Type::Int64: return Read(device, static_cast< long long >(0));
case Type::Float32: return Read(device, 0.0f);
case Type::Float64: return Read(device, 0.0);
case Type::String: return Read(device, QString(""));
case Type::Point: return Read(device, QPoint{});
case Type::PointF: return Read(device, QPointF{});
case Type::Size: return Read(device, QSize{});
case Type::SizeF: return Read(device, QSizeF{});
case Type::Rect: return Read(device, QRect{});
case Type::RectF: return Read(device, QRectF{});
case Type::Color: return QColor(Read(device, 0.0f), Read(device, 0.0f), Read(device, 0.0f), Read(device, 0.0f));
default: return QVariant();
}
}
//!
//! Write settings file
//!
bool Settings::Write(QIODevice & device, const QSettings::SettingsMap & map)
{
Write(device, map.size());
for (auto setting = map.constBegin(), end = map.constEnd(); setting != end; ++setting)
{
if (Write(device, Type::String) == false || Write(device, setting.key()) == false)
{
return false;
}
if (Write(device, setting.value()) == false)
{
return false;
}
}
return true;
}
QT_UTILS_NAMESPACE_END