-
Notifications
You must be signed in to change notification settings - Fork 23
/
aogsettings.cpp
248 lines (201 loc) · 6.31 KB
/
aogsettings.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "aogsettings.h"
#include <QString>
#include <QStringList>
#include <QColor>
//#include "common.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDataStream>
#include <QFile>
#include "qmlsettings.h"
extern QMLSettings qml_settings;
QVariant unset("UNSET"); //sentinal used to identify missing values in settings
AOGSettings::AOGSettings(QObject *parent) : QSettings(parent)
{
}
QVariant AOGSettings::value(const QString &key, const QVariant &defaultvalue)
{
QVariant val;
val = QSettings::value(key,unset);
if (val == unset && defaultvalue != unset) {
qDebug() << "type for " << key.toUtf8() << "is " << val.typeId();
QSettings::setValue(key,defaultvalue);
return defaultvalue;
}
return val;
}
QVector<int> AOGSettings::value(const QString &key, const QVector<int> &defaultvalue)
{
QVariant val;
val = QSettings::value(key,unset);
if (val == unset) {
QSettings::setValue(key,toVariant(defaultvalue));
return defaultvalue;
}
return toVector<int>(val);
}
void AOGSettings::setValue(const QString &key, const QVector<int> &value_list)
{
QSettings::setValue(key,toVariant(value_list));
qml_settings.updateSetting(key);
//emit updateFromSettings();
}
void AOGSettings::setValue(const QString &key, const QVariant &value)
{
QSettings::setValue(key,value);
qml_settings.updateSetting(key);
//emit updateFromSettings();
}
void AOGSettings::setValue_noqml(const QString &key, const QVariant &value)
{
QSettings::setValue(key,value);
//emit updateFromSettings();
}
QJsonObject AOGSettings::toJson()
{
//b = QVariant(QColor::fromRgbF(1,0.5,0.2));
QVariant b;
QStringList keys = allKeys();
QString type;
QString json_value;
QJsonObject blah; // = QJsonObject::fromVariantMap(keysValuesPairs);
for (const auto &key : keys)
{
b = value(key,unset);
if (b == unset) continue;
type = b.typeName();
if (type == "QStringList" ||
type == "QVariantList" ||
type == "QJSValue")
{
QVector<int> list = value(key,QVector<int>());
json_value="@List:";
for(int i=0; i < list.length(); i++){
json_value += QString("%1").arg(list[i]);
if (i < list.length() -1)
json_value +=",";
}
blah[key] = json_value;
} else if (type == "QPoint") {
QByteArray raw_value;
QDataStream ds(&raw_value,QIODevice::WriteOnly);
ds << b;
json_value = QLatin1String("@Variant(");
json_value += QString::fromLatin1(raw_value.constData(), raw_value.size());
json_value += ")";
blah[key] = json_value;
} else {
blah[key] = QJsonValue::fromVariant(b);
}
//qDebug() << key <<", " << type << ", " << json_value;
/*
} else {
QByteArray raw_value;
QDataStream ds(&raw_value,QIODevice::WriteOnly);
ds << b;
json_value = QLatin1String("@Variant(");
json_value += QString::fromLatin1(raw_value.constData(), raw_value.size());
json_value += ")";
}
*/
}
/*
QMap<QString, QVariant> keysValuesPairs;
keysValuesPairs.insert("testing",QVariant(QColor::fromRgbF(1,0.5,0.25)));
*/
return blah;
}
bool AOGSettings::loadJson(QString filename)
{
QFile loadfile(filename);
if (!loadfile.open(QIODevice::ReadOnly))
{
qWarning() << "Could not load json settings file " << filename;
return false;
}
QByteArray loadedjson = loadfile.readAll();
QJsonDocument loaded(QJsonDocument::fromJson(loadedjson));
QJsonObject j = loaded.object();
QString new_value;
QVariant v;
for (const auto &key : j.keys())
{
new_value = j[key].toString();
if (new_value.startsWith("@Variant("))
{
QByteArray raw_data;
QDataStream ds(&raw_data,QIODevice::ReadOnly);
raw_data = new_value.toLatin1().mid(9);
ds >> v;
QSettings::setValue(key, v);
} else if(new_value.startsWith("@List:")) {
new_value = new_value.mid(6);
QStringList parts = new_value.split(",");
QVector<int> list;
for(QString part: parts) {
list.append(part.toInt());
}
setValue(key,list);
} else {
//if (key == "display/isMetric")
// qDebug() << "isMetric is a problem child.";
v = j[key].toVariant();
QSettings::setValue(key, v);
}
qml_settings.updateSetting(key);
}
return true;
}
bool AOGSettings::saveJson(QString filename)
{
QFile savefile(filename);
if (!savefile.open(QIODevice::WriteOnly))
{
qWarning() << "Could not save json settings file " << filename;
return false;
}
savefile.write(QJsonDocument(toJson()).toJson());
savefile.close();
return true;
}
//TODO: why are these functions here and not in glutils.cpp?
QColor parseColor(QString setcolor)
{
//qDebug() << setcolor;
QStringList c = setcolor.split(",");
if (c.size() == 3) {
return QColor::fromRgb(c.at(0).toInt(),
c.at(1).toInt(),
c.at(2).toInt());
} else if (c.size() == 4) {
return QColor::fromRgb(c.at(0).toInt(),
c.at(1).toInt(),
c.at(2).toInt(),
c.at(3).toInt());
} else {
return QColor::fromRgb(255,0,255); //return magenta so we can see it
}
}
int colorSettingStringToInt(QString colorSettingString)
{
QStringList parts = colorSettingString.split(',');
int color;
if (parts.length() > 3)
color = parts[3].toInt() << 24; //Alpha
else
color = 0xff000000;
color |= parts[0].toInt() << 16; // red
color |= parts[1].toInt() << 8; // green
color |= parts[2].toInt(); //blue
return color;
}
QVector3D parseColorVector(QString setcolor)
{
//qDebug() << setcolor;
QStringList c = setcolor.split(",");
return QVector3D(c.at(0).toInt(),
c.at(1).toInt(),
c.at(2).toInt());
}