-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscribble_document.h
More file actions
242 lines (196 loc) · 7.1 KB
/
scribble_document.h
File metadata and controls
242 lines (196 loc) · 7.1 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
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
/*
* 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/>.
*
*/
#ifndef SCRIBBLE_DOCUMENT_H
#define SCRIBBLE_DOCUMENT_H
#include <QObject>
#include <QSizeF>
#include <QPoint>
#include <QColor>
#include <QHash>
#include <QPen>
#include <QPolygonF>
#include <QFile>
#include <QMouseEvent>
#include <QtXml/QXmlDefaultHandler>
class ScribbleStroke
{
public:
ScribbleStroke() {}
ScribbleStroke(const QPen &pen, const QPolygonF &points) : pen(pen), points(points) { updateBoundingRect(); }
const QPolygonF &getPoints() const { return points; }
const QPen &getPen() const { return pen; }
void setPen(const QPen &pen) { this->pen = pen; updateBoundingRect(); }
const QRectF &getBoundingRect() const { return boundingRect; }
bool segmentIntersects(int i, const ScribbleStroke &o) const;
bool boundingRectIntersects(const ScribbleStroke &o) const { return boundingRectIntersects(o.boundingRect); }
bool boundingRectIntersects(const QRectF &r) const { return boundingRect.intersects(r); }
void appendPoint(const QPointF &p) { points.append(p); updateBoundingRect(); }
void appendPoints(const QVector<QPointF> &p) { points += p; updateBoundingRect(); }
private:
void updateBoundingRect();
QPen pen;
QPolygonF points;
QRectF boundingRect;
};
class ScribbleLayer
{
public:
QList<ScribbleStroke> items;
};
class ScribbleXournalBackground
{
public:
/* if these are null (QString::isNull), the
* respective attributes are not present */
QString type;
QString color;
QString style;
QString domain;
QString filename;
QString pageno;
};
class ScribblePage
{
public:
ScribblePage() : size(QSizeF(612, 792)) {} /* TODO use reasonable values */
QList<ScribbleLayer> layers;
QSizeF size;
ScribbleXournalBackground background;
void invalidate() const {
/* TODO could be used to invalide cached XML representation */
}
QByteArray getXmlRepresentation() const;
};
class EraserContext
{
public:
EraserContext() {}
bool erase(const ScribbleStroke *stroke, QList<ScribbleStroke> *removedStrokes, QList<ScribbleStroke> *newStrokes,
const QPointF &point, qreal width);
private:
void appendFromPreviousChangeIndexUpTo(int endIndex, QList<ScribbleStroke> *list);
void eraseEnded();
bool previousPointRemoved;
int previousChangeIndex;
const ScribbleStroke *stroke;
QList<ScribbleStroke> *removedStrokes;
QList<ScribbleStroke> *newStrokes;
};
class XournalXMLHandler : public QXmlDefaultHandler {
public:
XournalXMLHandler();
bool startDocument();
bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts);
bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
bool characters(const QString &ch);
bool ignorableWhitespace(const QString &ch);
bool endDocument();
bool fatalError(const QXmlParseException &exception);
/* convenience functions for reading and writing */
static QString encodeString(const QString &str) {
QString text = str;
return text.replace("&", "&").replace("\"",""")
.replace("'", "'").replace("<", "<")
.replace(">", ">");
}
/* returns the null string if the attribute does not exist */
static QString getAttribute(const QXmlAttributes &attrs, const QString &attr) {
return attrs.index(attr) < 0 ? QString() : attrs.value(attr);
}
QString getTitle() const { return title; }
QList<ScribblePage> getPages() const { return pages; }
bool isValid() const { return valid; }
QString errorString() const { return errorStr; }
private:
void parseError(const QString &errorStr) {
valid = false;
if (this->errorStr.isEmpty())
this->errorStr = errorStr;
}
bool valid;
QString errorStr;
QString title;
QList<ScribblePage> pages;
QString currentLocalName;
QByteArray currentStrokeString;
QHash<QString, QColor> xournal_colors;
};
class Stylus
{
public:
bool sketching;
enum Mode {
PEN, ERASER
} mode;
QPen pen;
};
class ScribbleDocument : public QObject
{
Q_OBJECT
public:
explicit ScribbleDocument(QObject *parent = 0);
bool loadXournalFile(QByteArray data);
QByteArray toXournalXMLFormat();
static QByteArray toXournalXMLFormat(const QList<ScribblePage> &pages);
/* TODO this will cause deep copies to occur upon the first
* change (i.e. first mouse move) */
QList<ScribblePage> getPagesCopy() const { return pages; }
int getNumPages() const { return pages.length(); }
const ScribblePage &getCurrentPage() const { return pages[currentPage]; }
int getCurrentLayer() const { return currentLayer; }
bool hasChangedSinceLastSave() const { return changedSinceLastSave; }
void setSaved() { changedSinceLastSave = false; }
signals:
void pageOrLayerNumberChanged(int currentPage, int maxPages, int currentLayer, int maxLayers);
/* only if changed completely */
void pageOrLayerChanged(const ScribblePage &page, int currentLayer);
/* Only the last point was added. The stroke is the newest one. */
void strokePointAdded(const ScribbleStroke &);
/* Is emitted after all points have been added. */
void strokeCompleted(const ScribbleStroke &);
void strokesChanged(const ScribblePage &page, int layer, const QList<ScribbleStroke> &removedStrokes);
public slots:
void usePen() { endCurrentStroke(); stylus.mode = stylus.PEN; stylus.pen.setWidth(2); }
void useEraser() { endCurrentStroke(); stylus.mode = stylus.ERASER; stylus.pen.setWidth(10); }
bool setCurrentPage(int index);
/* will create new page if at end of document */
void nextPage();
void previousPage();
/* will create a new layer if at end of number of layers */
void layerUp();
void layerDown();
void touchEventDataReceived(const QPoint &pos, int pressure);
void setViewSize(const QSize &size) { currentViewSize = size; }
private:
void initAfterLoad();
void endCurrentStroke();
void eraseAt(const QPointF &point);
QString title;
QList<ScribblePage> pages;
int currentPage;
int currentLayer;
/* used when a new page is created */
QSize currentViewSize;
Stylus stylus;
/* shortcut to last item of current layer (if it exists) */
ScribbleStroke *currentStroke;
bool changedSinceLastSave;
};
#endif // SCRIBBLE_DOCUMENT_H