forked from segfault87/stitchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumentio.cpp
350 lines (267 loc) · 7.18 KB
/
documentio.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <QFile>
#include <QMap>
#include <QTextStream>
#include <qjson/parser.h>
#include <qjson/serializer.h>
#include "cell.h"
#include "colormanager.h"
#include "document.h"
#include "globalstate.h"
#include "kdtree.h"
#include "sparsemap.h"
#include "documentio.h"
/* DocumentIo */
DocumentIo::DocumentIo(Document *doc)
: document_(doc)
{
}
DocumentIo::~DocumentIo()
{
}
QVariant DocumentIo::save(QString &error)
{
VariantMap output;
output["version"] = version();
output["data"] = serialize(error);
if (output["data"].isNull())
return QVariant();
return QVariant(output);
}
bool DocumentIo::load(const QVariant &data, QString &error)
{
if (data.type() != QVariant::Map) {
error = QObject::tr("Invalid JSON format.");
return false;
}
return deserialize(data.toMap(), error);
}
/* DocumentIoV1 */
DocumentIoV1::DocumentIoV1(Document *doc)
: DocumentIo(doc)
{
}
DocumentIoV1::~DocumentIoV1()
{
}
QVariant DocumentIoV1::serialize(QString &error) const
{
VariantMap root;
const QSize &dim = document_->size();
root["rows"] = dim.height();
root["columns"] = dim.width();
root["title"] = document_->title();
root["author"] = document_->author();
root["colors"] = serializeColors(error);
root["stitches"] = serializeStitches(error);
if (root["colors"].isNull() || root["stitches"].isNull())
return QVariant();
return QVariant(root);
}
bool DocumentIoV1::deserialize(const VariantMap &data, QString &error)
{
QSize size(data["columns"].toInt(), data["rows"].toInt());
if (size == QSize()) {
error = QObject::tr("Invalid document size.");
return false;
}
document_->setSize(size);
document_->setTitle(data["title"].toString());
document_->setAuthor(data["author"].toString());
QVariant sv = data["stitches"];
if (sv.isNull() || sv.type() != QVariant::List) {
error = QObject::tr("No stitch data!");
return false;
}
VariantList sl = sv.toList();
if (!deserializeStitches(sl, error))
return false;
return true;
}
VariantList DocumentIoV1::serializeColors(QString &error) const
{
Q_UNUSED(error);
VariantList list;
const ColorUsageTracker *colorTracker = document_->colorTracker();
const QVector<const Color *> colors = colorTracker->colorList();
foreach (const Color *c, colors) {
VariantMap item;
if (c->parent())
item["category"] = c->parent()->id();
item["id"] = c->id();
item["name"] = c->name();
item["color"] = c->color().name();
list.append(QVariant(item));
}
return list;
}
VariantList DocumentIoV1::serializeStitches(QString &error) const
{
Q_UNUSED(error);
VariantList list;
const CellMap &cells = document_->map()->cells();
for (CellMap::ConstIterator it = cells.begin(); it != cells.end(); ++it) {
const QPoint &pos = it.key();
const Cell *cell = it.value();
VariantMap map;
map["x"] = pos.x();
map["y"] = pos.y();
VariantList features;
for (int i = 0; i < CELL_COUNT; ++i) {
if (cell->contains(i)) {
VariantList cellItem;
const Color *c = cell->color(i);
if (c->parent())
cellItem << c->parent()->id();
else
cellItem << QVariant();
cellItem << c->id();
cellItem << i;
features.append(QVariant(cellItem));
}
}
if (features.length() == 0)
continue;
map["features"] = features;
list.append(QVariant(map));
}
return list;
}
bool DocumentIoV1::deserializeStitches(const VariantList &list, QString &error)
{
SparseMap *map = document_->map();
MetaColorManager *cm = GlobalState::self()->colorManager();
int cnt = 0;
foreach (const QVariant &v, list) {
if (v.type() != QVariant::Map)
continue;
VariantMap m = v.toMap();
int x = m["x"].toInt();
int y = m["y"].toInt();
if (x == 0 || y == 0)
continue;
VariantList features = m["features"].toList();
if (features.length() == 0)
continue;
Cell *c = map->cellAt(QPoint(x, y));
foreach (const QVariant &f, features) {
const VariantList &fi = f.toList();
const Color *color = cm->get(fi[0].toString(), fi[1].toString());
c->addFeature(fi[2].toInt(), color);
}
c->createGraphicsItems();
++cnt;
}
if (!cnt) {
error = QObject::tr("No stitch item!");
return false;
}
return true;
}
/* DocumentFactory */
DocumentIo* DocumentFactory::defaultSerializer(Document *doc)
{
return new DocumentIoV1(doc);
}
Document* DocumentFactory::load(const QString &path, QString &error)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
error = QObject::tr("Coule not open file %1.").arg(path);
return NULL;
}
QTextStream stream(&file);
QString jsonText = stream.readAll();
file.close();
QJson::Parser parser;
bool ok;
QVariant body = parser.parse(jsonText.toUtf8(), &ok);
if (!ok) {
error = QObject::tr("This file is corrupted.");
return NULL;
}
VariantMap map = body.toMap();
if (map.isEmpty()) {
error = QObject::tr("Invalid document format.");
return NULL;
}
Document *doc = new Document();
DocumentIo *io = NULL;
int version = map["version"].toInt();
switch (map["version"].toInt()) {
case 1:
io = new DocumentIoV1(doc);
break;
default:
error = QObject::tr("Unsupport version %1").arg(version);
delete doc;
return NULL;
}
const QVariant &subdata = map["data"];
if (!io->load(subdata, error)) {
delete doc;
doc = NULL;
}
delete io;
return doc;
}
Document* DocumentFactory::load(const QImage &image, ColorManager *manager,
int width, const QColor *transparentColor)
{
if (image.isNull())
return NULL;
int height = image.height() / (image.width() / (float) width);
QImage scaled = image.scaled(width + 1, height + 1, Qt::KeepAspectRatio);
Document *doc = new Document(QSize(width, height));
SparseMap *map = doc->map();
Color *transparent = NULL;
if (transparentColor)
transparent = new Color("Transparent color", "transparent", *transparentColor);
KdTree kdtree(manager, transparent);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
QRgb pix = scaled.pixel(x, y);
QColor c;
if (qAlpha(pix) < 64)
continue;
else
c = QColor(pix);
const Color *stitch = kdtree.nearest(c);
if (stitch == transparent || !stitch)
continue;
Cell *cell = map->cellAt(QPoint(x, y));
cell->addFullStitch(stitch);
cell->createGraphicsItems();
}
}
if (transparent)
delete transparent;
return doc;
}
bool DocumentFactory::save(Document *doc, const QString &path, QString &error)
{
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
error = QObject::tr("Coule not open file %1.").arg(path);
return false;
}
DocumentIo *io = defaultSerializer(doc);
QVariant out = io->save(error);
if (out.isNull()) {
delete io;
return false;
}
QJson::Serializer sr;
//sr.setIndentMode(QJson::IndentMedium);
QByteArray output = sr.serialize(out);
if (output.isNull()) {
delete io;
error = QObject::tr("Serialization error.");
file.close();
return false;
}
QTextStream outStream(&file);
outStream << output;
file.close();
delete io;
return true;
}