-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocument.cpp
234 lines (200 loc) · 6.91 KB
/
document.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
#include "document.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#ifdef Q_OS_ANDROID
#include "androidhelper.h"
#else
#include <quazip5/quazip.h>
#include <quazip5/quazipfile.h>
#endif
Document::Document(QUndoStack *undoStack, QObject *parent)
: QObject(parent), mUndoStack(undoStack) {}
void Document::newDocument() {
setMeasurements();
setMapImageUrl(QUrl(":/A4_120dpi.png"));
setNeedsSaving(false);
}
bool Document::save(QUrl fileUrl) {
#ifndef Q_OS_ANDROID
QuaZip zip(fileUrl.toLocalFile());
if (!zip.open(QuaZip::Mode::mdCreate)) {
return false;
}
QJsonObject root;
QJsonArray data;
for (auto position : mMeasurements->positions()) {
QJsonObject point;
point["x"] = position.pos.x();
point["y"] = position.pos.y();
QJsonArray scanInfo;
QJsonArray iperfRxInfo;
QJsonArray iperfTxInfo;
QJsonArray iperfRetransmitsInfo;
for (auto s : mMeasurements->measurementsAt(position)) {
QJsonObject scanItem;
scanItem["bssid"] = s.bss.bssid;
scanItem["ssid"] = s.bss.ssid;
scanItem["freq"] = s.bss.freq;
scanItem["channel"] = s.bss.channel;
switch (s.measurementType) {
case WiFiSignal:
scanItem["signal"] = s.value;
scanInfo.append(scanItem);
break;
case IperfRx:
scanItem["value"] = s.value;
iperfRxInfo.append(scanItem);
break;
case IperfTx:
scanItem["value"] = s.value;
iperfTxInfo.append(scanItem);
break;
case IperfRetransmits:
scanItem["value"] = s.value;
iperfRetransmitsInfo.append(scanItem);
break;
}
}
point["scanInfo"] = scanInfo;
point["iperfRx"] = iperfRxInfo;
point["iperfTxInfo"] = iperfTxInfo;
point["iperfRetransmitsInfo"] = iperfRetransmitsInfo;
data.append(point);
}
root["data"] = data;
QJsonDocument jdoc(root);
QuaZipFile dataFile(&zip);
dataFile.open(QIODevice::WriteOnly, QuaZipNewInfo("data.json"));
dataFile.write(jdoc.toJson(QJsonDocument::Compact));
dataFile.close();
QuaZipFile mapfile(&zip);
mapfile.open(QIODevice::WriteOnly, QuaZipNewInfo("mapimage.png"));
mMapImage.save(&mapfile, "png");
mapfile.close();
zip.close();
setNeedsSaving(false);
#endif
return true;
}
void Document::load(QUrl fileUrl) {
#ifndef Q_OS_ANDROID
QuaZip zip(fileUrl.toLocalFile());
if (!zip.open(QuaZip::Mode::mdUnzip)) {
return;
}
setMeasurements();
QuaZipFile file(&zip);
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
file.open(QIODevice::ReadOnly);
if (file.getActualFileName() == "data.json") {
read(file.readAll());
} else if (file.getActualFileName() == "mapimage.png") {
QImage image;
image.load(&file, "png");
setMapImage(image);
}
file.close();
}
zip.close();
#endif
setNeedsSaving(false);
}
Measurements *Document::measurements() const { return mMeasurements.get(); }
void helper(const QJsonObject &point, QString name,
MeasurementType measurementType, QVector<MeasurementEntry> &scans) {
QJsonArray iperfRxInfo = point[name].toArray();
for (int j = 0; j < iperfRxInfo.size(); ++j) {
QJsonObject scanItem = iperfRxInfo[j].toObject();
if (scanItem.contains("bssid") && scanItem["bssid"].isString() &&
scanItem.contains("ssid") && scanItem["ssid"].isString() &&
scanItem.contains("value") && scanItem["value"].isDouble() &&
scanItem.contains("freq") && scanItem["freq"].isDouble() &&
scanItem.contains("channel") && scanItem["channel"].isDouble()) {
Bss bss{scanItem["bssid"].toString(), scanItem["ssid"].toString(),
scanItem["freq"].toInt(), scanItem["channel"].toInt()};
scans.push_back({bss, measurementType, scanItem["value"].toDouble()});
}
}
}
void Document::read(QByteArray data) {
QJsonDocument jdoc(QJsonDocument::fromJson(data));
QJsonObject root = jdoc.object();
if (root.contains("data") && root["data"].isArray()) {
QJsonArray data = root["data"].toArray();
for (int i = 0; i < data.size(); ++i) {
QJsonObject point = data[i].toObject();
if (point.contains("x") && point["x"].isDouble() && point.contains("y") &&
point["y"].isDouble() && point.contains("scanInfo") &&
point["scanInfo"].isArray()) {
QVector<MeasurementEntry> scans;
helper(point, "iperfRx", IperfRx, scans);
helper(point, "iperfTxInfo", IperfTx, scans);
helper(point, "iperfRetransmitsInfo", IperfRetransmits, scans);
QJsonArray scanInfo = point["scanInfo"].toArray();
for (int j = 0; j < scanInfo.size(); ++j) {
QJsonObject scanItem = scanInfo[j].toObject();
if (scanItem.contains("bssid") && scanItem["bssid"].isString() &&
scanItem.contains("ssid") && scanItem["ssid"].isString() &&
scanItem.contains("signal") && scanItem["signal"].isDouble() &&
scanItem.contains("freq") && scanItem["freq"].isDouble() &&
scanItem.contains("channel") && scanItem["channel"].isDouble()) {
Bss bss{scanItem["bssid"].toString(), scanItem["ssid"].toString(),
scanItem["freq"].toInt(), scanItem["channel"].toInt()};
scans.push_back({bss, WiFiSignal, scanItem["signal"].toDouble()});
}
}
auto position =
Position{QPoint(point["x"].toInt(), point["y"].toInt())};
mMeasurements->addPosition(position);
mMeasurements->newMeasurementsAtPosition(position, scans);
}
}
}
}
QImage Document::mapImage() const { return mMapImage; }
void Document::setMapImage(const QImage &mapImage) {
mMapImage = mapImage;
emit mapImageChanged();
}
void Document::setMeasurements() {
mMeasurements.reset(new Measurements());
mUndoStack->clear();
// FIXME:
if (mMeasurements) {
connect(mMeasurements.get(), &Measurements::postMeasurementAppended, this,
[=]() { setNeedsSaving(true); });
connect(mMeasurements.get(), &Measurements::postMeasurementRemoved, this,
[=]() {
if (mMeasurements->positions().size() == 0)
setNeedsSaving(false);
else
setNeedsSaving(true);
});
connect(mMeasurements.get(), &Measurements::positionChanged, this,
[=]() { setNeedsSaving(true); });
}
measurementsChanged(mMeasurements.get());
}
void Document::setNeedsSaving(bool value) {
if (mNeedsSaving != value) {
mNeedsSaving = value;
emit needsSavingChanged(mNeedsSaving);
}
}
void Document::setMapImageUrl(const QUrl &mapImageUrl) {
#ifdef Q_OS_ANDROID
if (mapImageUrl.scheme() == "content") {
if (!checkPermission("READ_EXTERNAL_STORAGE")) {
return;
}
setMapImage(imageFromContentUrl(mapImageUrl));
} else {
setMapImage(QImage(mapImageUrl.path()));
}
#else
setMapImage(QImage(mapImageUrl.path()));
#endif
}
bool Document::needsSaving() const { return mNeedsSaving; }