-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscribble_document.cpp
592 lines (523 loc) · 18.7 KB
/
scribble_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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* scribble: Scribbling Application for Onyx Boox M92
*
* Copyright (C) 2012 peter-x
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "scribble_document.h"
#include <QtXml/QtXml>
#include <QHash>
#include <QColor>
#include "fileio.h"
bool ScribbleStroke::segmentIntersects(int i, const ScribbleStroke &o) const
{
if (i < 0 || i + 1 >= points.size()) return false;
QPointF p1(points[i]);
QPointF p2(points[i + 1]);
/* TODO
* first check if the bounding box of this segment (plus width!) intersects the stroke
* then check for each segment of the stroke if they intersect */
return true;
}
void ScribbleStroke::updateBoundingRect()
{
boundingRect = points.boundingRect();
qreal a = pen.widthF() / 2.0;
a = qMin(a, qreal(0.6));
/* bounding rect with zero width or height produces not the intended result */
boundingRect.adjust(-a, -a, a, a);
}
QByteArray ScribblePage::getXmlRepresentation() const
{
/* TODO Can we cache this? During autosave, it is computed in the
* other thread. Getting the information back would save
* quite some CPU. */
QByteArray output;
output += QString().sprintf("<page width=\"%.2f\" height=\"%.2f\">\n",
size.width(),
size.height()).toUtf8();
ScribbleXournalBackground back = background;
if (back.type.isNull()) {
/* default background style */
back.type = "solid";
back.style = "plain";
back.color = "white";
}
output += "<background type=\"" + XournalXMLHandler::encodeString(back.type).toUtf8() + "\" ";
if (!back.color.isNull())
output += "color=\"" + XournalXMLHandler::encodeString(back.color).toUtf8() + "\" ";
if (!back.style.isNull())
output += "style=\"" + XournalXMLHandler::encodeString(back.style).toUtf8() + "\" ";
if (!back.domain.isNull())
output += "domain=\"" + XournalXMLHandler::encodeString(back.domain).toUtf8() + "\" ";
if (!back.filename.isNull())
output += "filename=\"" + XournalXMLHandler::encodeString(back.filename).toUtf8() + "\" ";
if (!back.pageno.isNull())
output += "pageno=\"" + XournalXMLHandler::encodeString(back.pageno).toUtf8() + "\" ";
output += "/>\n";
foreach (const ScribbleLayer &layer, layers) {
output += "<layer>\n";
foreach (const ScribbleStroke &stroke, layer.items) {
QColor color = stroke.getPen().color();
quint32 colorVal = (((((color.red() << 8) | color.green()) << 8) | color.blue()) << 8) | color.alpha();
output += QString().sprintf("<stroke tool=\"pen\" color=\"#%08x\" width=\"%.2f\">",
colorVal, stroke.getPen().widthF()).toUtf8();
int pos = output.size();
QPolygonF points(stroke.getPoints());
if (points.size() == 1) {
/* add a second point */
points.append(points[0]);
}
output.resize(output.size() + points.size() * 14);
foreach (const QPointF &point, points) {
int written;
while (1) {
written = snprintf(output.data() + pos, output.size() - pos,
"%.2f %.2f ", point.x(), point.y());
if (written >= output.size() - pos) {
output.resize(output.size() + 1024);
} else {
break;
}
}
pos += written;
}
output.resize(pos);
output += "\n</stroke>\n";
/* TODO error for text items */
}
output += "</layer>\n";
}
output += "</page>\n";
return output;
}
/* ---------------------------------------------------------------- */
XournalXMLHandler::XournalXMLHandler()
{
parseError("Document is empty.");
valid = false;
xournal_colors["black"] = QColor("#000000");
xournal_colors["blue"] = QColor("#3333cc");
xournal_colors["red"] = QColor("#ff0000");
xournal_colors["green"] = QColor("#008000");
xournal_colors["gray"] = QColor("#808080");
xournal_colors["lightblue"] = QColor("#00c0ff");
xournal_colors["lightgreen"] = QColor("#00ff00");
xournal_colors["magenta"] = QColor("#ff00ff");
xournal_colors["orange"] = QColor("#ff8000");
xournal_colors["yellow"] = QColor("#ffff00");
xournal_colors["white"] = QColor("#ffffff");
}
bool XournalXMLHandler::startDocument()
{
valid = true;
errorStr.clear();
title.clear();
pages.clear();
currentLocalName.clear();
return true;
}
bool XournalXMLHandler::startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
{
Q_UNUSED(namespaceURI);
Q_UNUSED(qName);
if (localName == "stroke") {
if (atts.value("tool") != "pen") {
parseError("Document uses tools that are not pens.");
return false;
}
QPen pen;
ScribbleStroke stroke;
QString color = atts.value("color");
bool ok;
if (xournal_colors.contains(color)) {
pen.setColor(xournal_colors[color]);
} else if (color[0] == '#') {
quint32 col = color.mid(1).toUInt(&ok, 16);
if (!ok) {
parseError("Document uses invalid color format.");
return false;
}
pen.setColor(QColor((col >> 24) & 0xff, (col >> 16) & 0xff,
(col >> 8) & 0xff, col & 0xff));
} else {
parseError("Document uses invalid color specification.");
return false;
}
pen.setWidthF(atts.value("width").toFloat(&ok));
if (!ok) {
parseError("Document uses invalid pen width.");
return false;
}
/* XXX variable width (multiple float values separated by whitespace) */
stroke.setPen(pen);
pages.last().layers.last().items.append(stroke);
} else if (localName == "page") {
ScribblePage p;
bool ok1, ok2;
p.size = QSizeF(atts.value("width").toFloat(&ok1), atts.value("height").toFloat(&ok2));
if (!ok1 || !ok2) {
parseError("Document uses invalid page size.");
return false;
}
pages.append(p);
} else if (localName == "background") {
ScribblePage &p(pages.last());
p.background.type = getAttribute(atts, "type");
p.background.color = getAttribute(atts, "color");
p.background.style = getAttribute(atts, "style");
p.background.domain = getAttribute(atts, "domain");
p.background.filename = getAttribute(atts, "filename");
p.background.pageno = getAttribute(atts, "pageno");
} else if (localName == "layer") {
ScribbleLayer l;
pages.last().layers.append(l);
} else if (localName == "title") {
title.clear();
} else if (localName == "xournal") {
} else {
parseError("Document contains invalid elements.");
return false;
}
currentLocalName = localName;
return true;
}
bool XournalXMLHandler::endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
{
Q_UNUSED(namespaceURI);
Q_UNUSED(qName);
if (localName == "stroke") {
ScribbleStroke &s = pages.last().layers.last().items.last();
QByteArray chunk;
QVector<QPointF> points;
float x;
bool xValid = false;
for (int i = 0; i < currentStrokeString.length(); i ++) {
char c = currentStrokeString[i];
if (('0' <= c && c <= '9') || c == '.' || c == '+' || c == '-' || c == 'e' || c == 'E') {
chunk += c;
} else if (c == ' ' || c == '\n' || c == '\r') {
if (!chunk.isEmpty()) {
float v = chunk.toFloat();
chunk.clear();
if (xValid) {
points.append(QPointF(x, v));
} else {
x = v;
}
xValid = !xValid;
}
} else {
parseError("Document contains invalid character in stroke description.");
return false;
}
}
if (!chunk.isEmpty() && xValid) {
points.append(QPointF(x, chunk.toFloat()));
}
s.appendPoints(points);
currentStrokeString.clear();
}
currentLocalName.clear();
return true;
}
bool XournalXMLHandler::characters(const QString &ch)
{
if (currentLocalName == "stroke") {
currentStrokeString += ch.toAscii();
} else if (currentLocalName == "title") {
title += ch;
}
return true;
}
bool XournalXMLHandler::ignorableWhitespace(const QString &ch)
{
if (currentLocalName == "stroke") {
currentStrokeString += ch.toAscii();
} else if (currentLocalName == "title") {
title += ch;
}
return true;
}
bool XournalXMLHandler::endDocument()
{
if (pages.isEmpty()) {
pages.append(ScribblePage());
}
for (int i = 0; i < pages.length(); i ++) {
if (pages[i].layers.isEmpty()) {
pages[i].layers.append(ScribbleLayer());
}
}
return true;
}
bool XournalXMLHandler::fatalError(const QXmlParseException & exception)
{
qWarning() << "Fatal error on line" << exception.lineNumber()
<< ", column" << exception.columnNumber() << ":"
<< exception.message();
return false;
}
/* --------------------------------------------------------------- */
bool EraserContext::erase(const ScribbleStroke *stroke, QList<ScribbleStroke> *removedStrokes, QList<ScribbleStroke> *newStrokes,
const QPointF &point, qreal width)
{
this->stroke = stroke;
this->removedStrokes = removedStrokes;
this->newStrokes = newStrokes;
previousPointRemoved = false;
previousChangeIndex = 0;
qreal halfWidthSq = width * width / 4.0;
/* TODO avoid creating strokes with only one point */
/* we only check intersections with points, not
* line segments */
for (int i = 0; i < stroke->getPoints().size(); i ++) {
QPointF p = stroke->getPoints()[i] - point;
qreal d = p.x() * p.x() + p.y() * p.y();
if (d <= halfWidthSq) {
/* remove point */
if (!previousPointRemoved) {
appendFromPreviousChangeIndexUpTo(i, newStrokes);
}
previousPointRemoved = true;
} else {
/* retain point */
if (previousPointRemoved) {
appendFromPreviousChangeIndexUpTo(i, removedStrokes);
}
previousPointRemoved = false;
}
}
eraseEnded();
return previousPointRemoved || previousChangeIndex > 0;
}
void EraserContext::eraseEnded()
{
int endIndex = stroke->getPoints().size() - 1;
if (previousPointRemoved) {
appendFromPreviousChangeIndexUpTo(endIndex, removedStrokes);
} else {
if (previousChangeIndex == 0) {
/* nothing happened */
return;
} else {
appendFromPreviousChangeIndexUpTo(endIndex, newStrokes);
}
}
}
void EraserContext::appendFromPreviousChangeIndexUpTo(int endIndex, QList<ScribbleStroke> *list)
{
if (endIndex > previousChangeIndex) {
list->append(ScribbleStroke(stroke->getPen(),
stroke->getPoints().mid(previousChangeIndex,
endIndex - previousChangeIndex + 1)));
}
previousChangeIndex = endIndex;
}
/* --------------------------------------------------------------- */
ScribbleDocument::ScribbleDocument(QObject *parent) :
QObject(parent), title("")
{
initAfterLoad();
}
bool ScribbleDocument::loadXournalFile(QByteArray data)
{
QBuffer dataBuffer(&data);
QXmlInputSource source(&dataBuffer);
QXmlSimpleReader reader;
XournalXMLHandler handler;
reader.setContentHandler(&handler);
reader.setErrorHandler(&handler);
if (!reader.parse(&source, false)) {
/* TODO message box */
qDebug() << "Parsing error:" << handler.errorString();
return false;
}
title = handler.getTitle();
pages = handler.getPages();
initAfterLoad();
/* TODO only save to this file again if we were able
* to read all content correctly */
emit pageOrLayerNumberChanged(currentPage, pages.length(), currentLayer, getCurrentPage().layers.length());
emit pageOrLayerChanged(getCurrentPage(), currentLayer);
return true;
}
QByteArray ScribbleDocument::toXournalXMLFormat()
{
return toXournalXMLFormat(pages);
}
QByteArray ScribbleDocument::toXournalXMLFormat(const QList<ScribblePage> &pages)
{
setlocale(LC_NUMERIC, "C");
QByteArray output = "<?xml version=\"1.0\" standalone=\"no\"?>\n"
"<xournal version=\"0.4.5\">\n"
"<title>Scribble document - see https://github.com/peter-x/scribble</title>\n";
for (int i = 0; i < pages.length(); i ++) {
output += pages[i].getXmlRepresentation();
}
output += "</xournal>\n";
setlocale(LC_NUMERIC, "");
return output;
}
void ScribbleDocument::initAfterLoad()
{
if (pages.length() == 0) {
pages.append(ScribblePage());
pages[0].layers.append(ScribbleLayer());
pages[0].invalidate();
}
currentPage = 0;
currentLayer = getCurrentPage().layers.length() - 1;
stylus.sketching = false;
stylus.mode = stylus.PEN;
currentStroke = 0;
stylus.pen.setColor(QColor(0, 0, 0));
stylus.pen.setWidth(2);
changedSinceLastSave = false;
emit pageOrLayerNumberChanged(currentPage, pages.length(), currentLayer, getCurrentPage().layers.length());
emit pageOrLayerChanged(getCurrentPage(), currentLayer);
}
void ScribbleDocument::endCurrentStroke()
{
if (!stylus.sketching) return;
if (stylus.mode == stylus.PEN) {
if (currentStroke != 0 && currentStroke->getPoints().size() == 1) {
currentStroke->appendPoint(currentStroke->getPoints()[0]);
}
emit strokeCompleted(*currentStroke);
currentStroke = 0;
}
stylus.sketching = false;
}
bool ScribbleDocument::setCurrentPage(int index)
{
if (index < 0 || index >= pages.length())
return false;
endCurrentStroke();
if (currentLayer == getCurrentPage().layers.length() - 1) {
currentPage = index;
currentLayer = getCurrentPage().layers.length() - 1;
} else {
currentPage = index;
currentLayer = qMin(currentLayer, getCurrentPage().layers.length() - 1);
}
emit pageOrLayerNumberChanged(currentPage, pages.length(), currentLayer, getCurrentPage().layers.length());
emit pageOrLayerChanged(getCurrentPage(), currentLayer);
return true;
}
void ScribbleDocument::nextPage()
{
if (currentPage + 1 >= pages.length()) {
ScribblePage p;
if (!currentViewSize.isEmpty())
p.size = currentViewSize;
p.layers.append(ScribbleLayer());
pages.append(p);
}
setCurrentPage(currentPage + 1);
}
void ScribbleDocument::previousPage()
{
setCurrentPage(currentPage - 1);
}
void ScribbleDocument::layerUp()
{
endCurrentStroke();
ScribblePage &p = pages[currentPage];
if (currentLayer + 1 >= p.layers.length()) {
p.layers.append(ScribbleLayer());
p.invalidate();
changedSinceLastSave = true;
}
currentLayer += 1;
emit pageOrLayerNumberChanged(currentPage, pages.length(), currentLayer, getCurrentPage().layers.length());
emit pageOrLayerChanged(getCurrentPage(), currentLayer);
}
void ScribbleDocument::layerDown()
{
if (currentLayer == 0) return;
endCurrentStroke();
currentLayer -= 1;
emit pageOrLayerNumberChanged(currentPage, pages.length(), currentLayer, getCurrentPage().layers.length());
emit pageOrLayerChanged(getCurrentPage(), currentLayer);
}
void ScribbleDocument::touchEventDataReceived(const QPoint &pos, int pressure)
{
if (!QRect(QPoint(0, 0), currentViewSize).contains(pos)) {
endCurrentStroke();
return;
}
if (stylus.mode == stylus.ERASER) {
if (pressure > 0) {
stylus.sketching = true;
eraseAt(pos);
} else {
stylus.sketching = false;
}
} else if (stylus.mode == stylus.PEN){
if (pressure > 0) {
if (!stylus.sketching) {
stylus.sketching = true;
ScribbleLayer &l = pages[currentPage].layers[currentLayer];
l.items.append(ScribbleStroke(stylus.pen, QPolygonF()));
currentStroke = &l.items.last();
}
currentStroke->appendPoint(pos);
pages[currentPage].invalidate();
changedSinceLastSave = true;
emit strokePointAdded(*currentStroke);
} else if (stylus.sketching) {
endCurrentStroke();
}
}
}
void ScribbleDocument::eraseAt(const QPointF &point)
{
ScribbleLayer &layer = pages[currentPage].layers[currentLayer];
qreal width = stylus.pen.widthF();
QRectF eraserBox;
eraserBox.setSize(QSizeF(width, width));
eraserBox.moveCenter(point);
QList<ScribbleStroke> removedStrokes;
QList<ScribbleStroke> newStrokes;
EraserContext eraserContext;
for (int i = 0; i < layer.items.length(); i ++) {
ScribbleStroke &s = layer.items[i];
if (!s.boundingRectIntersects(eraserBox))
continue;
if (!eraserContext.erase(&s, &removedStrokes, &newStrokes, point, width)) {
/* nothing removed */
continue;
}
if (newStrokes.isEmpty()) {
/* stroke removed completely */
layer.items.removeAt(i);
i -= 1;
} else {
layer.items[i] = newStrokes[0];
for (int j = 1; j < newStrokes.length(); j ++) {
layer.items.insert(i + j, newStrokes[j]);
}
i += newStrokes.length() - 1;
newStrokes.clear();
}
}
if (!removedStrokes.isEmpty()) {
pages[currentPage].invalidate();
changedSinceLastSave = true;
emit strokesChanged(getCurrentPage(), currentLayer, removedStrokes);
}
}