forked from unruhschuh/MrWriter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget.h
462 lines (366 loc) · 12 KB
/
widget.h
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
#ifndef WIDGET_H
#define WIDGET_H
#define MIN_ZOOM 0.1
#define MAX_ZOOM 10.0
#include <QWidget>
#include <QOpenGLWidget>
#include <QVector>
#include <QPainter>
#include <QTabletEvent>
#include <QUndoStack>
#include <QScrollArea>
#include <QSizeGrip>
#include <QGridLayout>
#include <QSet>
#include <QThread>
#include <QtConcurrent>
#include <QMutex>
#include <QMutexLocker>
#include <memory>
#include <algorithm>
#include <unordered_map>
#include <math.h>
#include <chrono>
#include <QTime>
#include <QTimer>
#include <poppler-link.h>
#include "tabletapplication.h"
#include "mrdoc.h"
#include "document.h"
#include "textbox.h"
#include "markdownbox.h"
#include "page.h"
#include "markdownselection.h"
/**
* These structs are basically for @ref basePixmapMap
*/
struct BasicPageSize;
struct BasicPageSizeHash;
struct BasicPageSizeEqual;
class UpdateWorker;
class Widget : public QWidget
// class Widget : public QOpenGLWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
enum class tool
{
NONE,
PEN,
HIGHLIGHTER,
RULER,
CIRCLE,
ERASER,
SELECT,
HAND,
TEXT,
MARKDOWN
};
enum class state
{
IDLE,
DRAWING,
RULING,
CIRCLING,
TEXTTYPING,
MARKDOWNTYPING,
SELECTING,
SELECTED,
MOVING_SELECTION,
RESIZING_SELECTION,
ROTATING_SELECTION,
MARKDOWN_SELECTED,
MARKDOWN_MOVING
};
enum class view
{
HORIZONTAL,
VERTICAL
};
static constexpr qreal veryFinePenWidth = 0.42;
static constexpr qreal finePenWidth = 0.85;
static constexpr qreal mediumPenWidth = 1.41;
static constexpr qreal thickPenWidth = 2.26;
static constexpr qreal veryThickPenWidth = 5.67;
void setCurrentTool(tool toolID);
tool getCurrentTool()
{
return currentTool;
}
void setCurrentPenWidth(qreal penWidth);
qreal getCurrentPenWidth()
{
return currentPenWidth;
}
void mouseAndTabletEvent(QPointF mousePos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers keyboardModifiers,
QTabletEvent::PointerType pointerType, QEvent::Type eventType, qreal pressure, bool tabletEvent);
/**
* @brief updateAllPageBuffers updates the page buffers in @ref pageBufferPtr.
* @details If zoom level changes or a new document was loaded the page buffer
* is cleared. After that only the visible pages (and a few more pages around)
* are painted and loaded to @ref pageBufferPtr. For the other pages only a pointer to
* a blank placeholder (stored in @ref basePixmapMap) is loaded into the buffer.
*/
void updateAllPageBuffers();
/**
* @brief updateAllPageBuffersDirtyZoom updates the page buffers in @ref pageBufferPtr with new zoom level.
* @details It zooms by scaling the existing buffer pixmaps by zoom factor. It does not a rerender.
*/
void updateAllPageBuffersDirtyZoom();
/**
* @brief updateNecessaryPagesBuffer updates the page buffer only for currentpage plus/minus 6 pages.
* @details A page gets repainted if its current buffer pixmap is a placeholder.
*/
void updateNecessaryPagesBuffer();
/**
* @brief updateBuffer updates the page buffer for a single page.
* @param i page index of the page to repaint and load into buffer
* @see updateBufferWithPlaceholder
*/
void updateBuffer(int i);
/**
* @brief updateBufferWithPlaceholder loads a pointer to a blank placeholder into @ref pageBufferPtr
* @param buffNum page index
*/
void updateBufferWithPlaceholder(int buffNum);
/**
* @brief updateBufferDirtyZoom loads a scaled pixmap into @ref pageBufferPtr.
* @param buffNum page index
*/
void updateBufferDirtyZoom(int buffNum);
void updateBufferRegion(int buffNum, QRectF const &clipRect);
void drawOnBuffer(bool last = false);
int getPageFromMousePos(QPointF mousePos);
QPointF getPagePosFromMousePos(QPointF mousePos, int pageNum);
QPointF getAbsolutePagePosFromMousePos(QPointF mousePos);
/**
* @brief getWidgetGeometry calculates current/needed widget geometry based on page sizes and gaps between pages.
* @return
*/
QRect getWidgetGeometry();
int getCurrentPage();
/**
* @brief getVisiblePages
* @return the indices of the visible pages
*/
QSet<int> getVisiblePages();
void setCurrentState(state newState);
state getCurrentState();
void setCurrentView(view newView);
view getCurrentView();
void setCurrentColor(QColor newColor);
QColor getCurrentColor();
void setCurrentFont(QFont newFont);
QFont getCurrentFont();
void setDocument(const MrDoc::Document &newDocument);
void letGoSelection();
void letGoMarkdownSelection();
void newFile();
// void openFile();
void zoomIn();
void zoomOut();
void zoomTo(qreal newZoom);
void zoomFitWidth();
void zoomFitHeight();
void rotateSelection(qreal angle);
/**
* @brief closeTextBox closes the text insertion box if one is open
*/
void closeTextBox();
/**
* @brief searchAllPdf searchs in the loaded pdf for @param text and highlights the results.
*/
void searchAllPdf(const QString &text);
MrDoc::Document currentDocument;
QVector<std::shared_ptr<std::shared_ptr<QPixmap>>> pageBufferPtr; /**< buffer for page pixmaps */
QMutex pageBufferPtrMutex; /**< Mutex for @ref pageBufferPtr It locks only single buffer operations like replace, but not clear.*/
QMutex overallBufferMutex; /**< Mutex for @ref pageBufferPtr. */
QColor currentColor;
qreal currentPenWidth;
qreal m_currentAngle;
void setCurrentPattern(QVector<qreal> newPattern);
QVector<qreal> getCurrentPattern();
MrDoc::Selection currentSelection;
MrDoc::Selection &clipboard = static_cast<TabletApplication *>(qApp)->clipboard;
MrDoc::MarkdownSelection currentMarkdownSelection;
int selectingOnPage;
QScrollArea *scrollArea;
QUndoStack undoStack;
qreal zoom;
qreal prevZoom = 0;
bool ctrlZoom = false; /**< true, if ctrl+wheel was/is used for zooming. Becomes false, when ctrl is released */ //maybe use getter/setter?
bool dismissedCleanZoom = false; /**< true, if a clean zoom was dismissed, because ctrlZoom was true. */
private:
struct BasicPageSize{
int pageWidth;
int pageHeight;
};
struct BasicPageSizeHash{
std::size_t operator ()(const BasicPageSize& s) const{
std::hash<int> f;
return f(s.pageHeight) ^ f(s.pageWidth);
}
};
struct BasicPageSizeEqual{
bool operator ()(const BasicPageSize& a, const BasicPageSize& b) const{
return a.pageWidth == b.pageWidth && a.pageHeight == b.pageHeight;
}
};
std::unordered_map<BasicPageSize, std::shared_ptr<std::shared_ptr<QPixmap>>, BasicPageSizeHash, BasicPageSizeEqual> basePixmapMap; /**< Stores the pointers to the blank placeholders for certain page sizes */
QMutex basePixmapMutex;
int previousVerticalValueRendered = 0; /**< Stores the vertical slider value where the last call of updateAllPageBuffers happened */
int previousVerticalValueMaybeRendered = 0; /**< Stores the vertical slider value where the last @ref scrollTimer start happened */
int previousHorizontalValueRendered = 0;
int previousHorizontalValueMaybeRendered = 0;
QTimer* scrollTimer;
QThread* updateThread = new QThread();
bool dirtyZoom = false;
QTimer* updateAllPageBuffersTimer;
QTime timer;
QTimer *updateTimer;
QTimer *updateDirtyTimer;
qreal count;
QList<int> pageHistory; /**< Stores the page numbers where the user were when he clicked on goto links in the pdf */
int pageHistoryPosition = -1; /**< Stores the current position in @ref pageHistory */
void appendToPageHistory(int pageNum); /**< Appends a new page to @ref pageHistory and truncates unnecessary page numbers */
void scrollDocumentToPageNum(int pageNum);
QVector<qreal> currentPattern = MrDoc::solidLinePattern;
QCursor penCursor;
QCursor circleCursor;
QCursor rulerCursor;
QCursor eraserCursor;
MrDoc::Stroke currentStroke;
QRect currentUpdateRect;
state currentState;
bool penDown = false;
int drawingOnPage;
tool currentTool;
tool previousTool;
bool realEraser;
view currentView;
QFont currentFont;
qreal currentDashOffset;
qreal minWidthMultiplier = 0.0;
qreal maxWidthMultiplier = 1.25;
QPointF currentCOSPos;
QPointF firstMousePos;
QPointF previousMousePos;
QPointF previousPagePos;
MrDoc::Selection::GrabZone m_grabZone = MrDoc::Selection::GrabZone::None;
int previousMarkdownPageNum;
QPointF previousMarkdownPagePos; /**< previous position of markdown selection */
QPointF markdownDelta;
TextBox* textBox;
bool textBoxOpen = false;
bool textChanged = false;
MarkdownBox* markdownBox;
bool markdownBoxOpen = false;
bool markdownChanged = false;
QString previousSearchText;
int previousSearchPageIndex;
QVector<int> searchPageNums; /**< Stores the page numbers where a search result is */
void startDrawing(QPointF mousePos, qreal pressure);
void continueDrawing(QPointF mousePos, qreal pressure);
void stopDrawing(QPointF mousePos, qreal pressure);
void startRuling(QPointF mousePos);
void continueRuling(QPointF mousePos);
void stopRuling(QPointF mousePos);
void startCircling(QPointF mousePos);
void continueCircling(QPointF mousePos);
void stopCircling(QPointF mousePos);
void startSelecting(QPointF mousePos);
void continueSelecting(QPointF mousePos);
void stopSelecting(QPointF mousePos);
void startMovingSelection(QPointF mousePos);
void continueMovingSelection(QPointF mousePos);
void startRotatingSelection(QPointF mousePos);
void continueRotatingSelection(QPointF mousePos);
void stopRotatingSelection(QPointF mousePos);
void startResizingSelection(QPointF mousePos, MrDoc::Selection::GrabZone grabZone);
void continueResizingSelection(QPointF mousePos);
void stopResizingSelection(QPointF mousePos);
void startMovingMarkdownSelection(QPointF mousePos);
void continueMovingMarkdownSelection(QPointF mousePos);
void setPreviousTool();
void erase(QPointF mousePos, bool invertEraser = false);
private slots:
/**
* @brief updatePage starts @ref scrollTimer when the user scrolled more than one (average) page height/width
* @param value is current scrollbar value
*/
void updatePageAfterScrolling(int value);
void updateAllDirtyBuffers();
/**
* @brief updatePageAfterScrollTimer updates (necessary) pages when @ref scrollTimer stopped.
*/
void updatePageAfterScrollTimer();
void updatePageAfterZoomTimer();
void undo();
void redo();
void selectAll();
void copy();
void paste();
void cut();
void pageFirst();
void pageLast();
void pageUp();
void pageDown();
void pageAddBefore();
void pageAddAfter();
void pageAddBeginning();
void pageAddEnd();
void pageRemove();
void veryFine();
void fine();
void medium();
void thick();
void veryThick();
void solidPattern();
void dashPattern();
void dashDotPattern();
void dotPattern();
void updateWhileDrawing();
signals:
void pen();
void ruler();
void circle();
void eraser();
void select();
void hand();
void setSimpleText();
void setMarkdownText();
void updateGUI();
void modified();
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseDoubleClickEvent(QMouseEvent* event) override;
void tabletEvent(QTabletEvent *event) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent* event) override;
void wheelEvent(QWheelEvent* event) override;
signals:
public slots:
void searchPdfNext(const QString& text);
void searchPdfPrev(const QString& text);
void clearPdfSearch();
void pageHistoryForward();
void pageHistoryBackward();
};
/**
* @brief The UpdateWorker class calls updateNecessaryPages. It is necessary to work with QThread instead of QtConcurrent.
*/
class UpdateWorker : public QObject {
Q_OBJECT
public:
UpdateWorker(Widget* widget);
public slots:
void process();
signals:
void finished();
private:
Widget* widgetPtr;
};
#endif // WIDGET_H