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
|
//===========================================
// Lumina Desktop source code
// Copyright (c) 2017, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
// Simple subclass of QPrintPreviewWidget to provide
// notification when a context menu is requested
//===========================================
#ifndef _PRINT_GRAPHICS_H
#define _PRINT_GRAPHICS_H
#include <QMouseEvent>
#include <QDebug>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QBoxLayout>
#include <QScrollBar>
#include <QStyleOptionGraphicsItem>
#include <QtMath>
#include <QPageLayout>
#include <QTextDocument>
#include <QApplication>
#include "Renderer.h"
#include "TextData.h"
class InkItem: public QGraphicsItem {
public:
InkItem(QGraphicsItem *parent, Annotation *_annot) : QGraphicsItem(parent), pointData(_annot->getInkList()), inkColor(_annot->getColor()), annot(_annot) {
setCacheMode(DeviceCoordinateCache);
bbox = annot->getLoc();
}
QRectF boundingRect() const Q_DECL_OVERRIDE { return bbox; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
{
Q_UNUSED(widget);
painter->setClipRect(option->exposedRect);
QPen inkPen = QPen(inkColor);
painter->setPen(inkPen);
foreach(QVector<QPointF> pointList, pointData) {
painter->drawLines(pointList);
}
}
private:
QRectF bbox;
QVector<QVector<QPointF>> pointData;
QColor inkColor;
Annotation *annot;
};
class PopupItem: public QGraphicsItem {
public:
PopupItem(QGraphicsItem *parent, Annotation *_annot) : QGraphicsItem(parent), author(_annot->getAuthor()), text(_annot->getText()) {
QRectF loc = _annot->getLoc();
setCacheMode(DeviceCoordinateCache);
QString allText = "Author: " + author + "\n\n" + text;
QTextDocument document;
document.setDefaultFont(QFont("Helvitica", 10, QFont::Normal));
document.setPageSize(QSize(120, 120));
document.setHtml(allText);
loc.moveTopLeft(QPointF(loc.center().x(), loc.center().y()+loc.height()/2));
loc.setSize(document.size()+QSize(10, 10));
bbox = loc;
}
QRectF boundingRect() const Q_DECL_OVERRIDE { return bbox; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
{
Q_UNUSED(widget);
QString allText = "Author: " + author + "\n\n" + text;
painter->setClipRect(option->exposedRect);
painter->setFont(QFont("Helvitica", 10, QFont::Normal));
painter->setBrush(QBrush(QColor(255, 255, 177, 255)));
painter->drawRect(bbox);
painter->setPen(QPen(QColor("Black")));
painter->drawText(bbox, Qt::AlignLeft | Qt::TextWordWrap, allText);
}
private:
QRectF bbox;
QString author;
QString text;
};
class AnnotZone: public QGraphicsItem {
public:
AnnotZone(QGraphicsItem *parent, Annotation *_annot, PopupItem *_annotItem) : QGraphicsItem(parent), bbox(_annot->getLoc()), annot(_annotItem) {
_hasText = !_annot->getText().isEmpty();
_hasAuthor = !_annot->getAuthor().isEmpty();
}
QRectF boundingRect() const Q_DECL_OVERRIDE { return bbox; }
PopupItem* annotation() const { return annot; }
bool hasText() const { return _hasText; }
bool hasAuthor() const { return _hasAuthor; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE { Q_UNUSED(widget); Q_UNUSED(painter); Q_UNUSED(option); }
private:
QRectF bbox;
PopupItem *annot;
bool _hasText;
bool _hasAuthor;
};
class LinkItem: public QGraphicsItem {
public:
LinkItem(QGraphicsItem *parent, TextData *_data) : QGraphicsItem(parent), bbox(_data->loc()), data(_data) {
setCacheMode(DeviceCoordinateCache);
setAcceptHoverEvents(true);
}
QRectF boundingRect() const Q_DECL_OVERRIDE
{ return bbox; }
inline TextData* getData() const
{ return data; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
{
Q_UNUSED(widget);
painter->setClipRect(option->exposedRect);
painter->setBrush(QBrush(QColor(255, 255, 177, 100)));
painter->setPen(QPen(QColor(255, 255, 100, 125)));
painter->drawRect(bbox);
}
private:
QRectF bbox;
TextData *data;
};
class PageItem : public QGraphicsItem {
public:
PageItem(int _pageNum, QImage _pagePicture, QSize _paperSize, Renderer *_backend)
: pageNum(_pageNum), pagePicture(_pagePicture), paperSize(_paperSize), BACKEND(_backend)
{
brect = QRectF(QPointF(-25, -25),
QSizeF(paperSize)+QSizeF(50, 50));
setCacheMode(DeviceCoordinateCache);
setAcceptHoverEvents(true);
}
QRectF boundingRect() const Q_DECL_OVERRIDE
{ return brect; }
inline int pageNumber() const
{ return pageNum; }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
{
Q_UNUSED(widget);
//Ensure all the antialiasing/smoothing options are turned on
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
QRectF paperRect(0,0, paperSize.width(), paperSize.height());
// Draw shadow
painter->setClipRect(option->exposedRect);
qreal shWidth = paperRect.width()/100;
QRectF rshadow(paperRect.topRight() + QPointF(0, shWidth),
paperRect.bottomRight() + QPointF(shWidth, 0));
QLinearGradient rgrad(rshadow.topLeft(), rshadow.topRight());
rgrad.setColorAt(0.0, QColor(0,0,0,255));
rgrad.setColorAt(1.0, QColor(0,0,0,0));
painter->fillRect(rshadow, QBrush(rgrad));
QRectF bshadow(paperRect.bottomLeft() + QPointF(shWidth, 0),
paperRect.bottomRight() + QPointF(0, shWidth));
QLinearGradient bgrad(bshadow.topLeft(), bshadow.bottomLeft());
bgrad.setColorAt(0.0, QColor(0,0,0,255));
bgrad.setColorAt(1.0, QColor(0,0,0,0));
painter->fillRect(bshadow, QBrush(bgrad));
QRectF cshadow(paperRect.bottomRight(),
paperRect.bottomRight() + QPointF(shWidth, shWidth));
QRadialGradient cgrad(cshadow.topLeft(), shWidth, cshadow.topLeft());
cgrad.setColorAt(0.0, QColor(0,0,0,255));
cgrad.setColorAt(1.0, QColor(0,0,0,0));
painter->fillRect(cshadow, QBrush(cgrad));
painter->setClipRect(paperRect & option->exposedRect);
painter->fillRect(paperRect, Qt::white);
painter->drawImage(QPoint(0,0), pagePicture);
for(int k = 0; k < BACKEND->annotSize(pageNum-1); k++) {
Annotation *annot = BACKEND->annotList(pageNum-1, k);
painter->drawImage(annot->getLoc(), annot->renderImage());
}
}
private:
int pageNum;
QImage pagePicture;
QSize paperSize;
QRectF brect;
Renderer *BACKEND;
};
class PrintWidget : public QGraphicsView
{
Q_OBJECT
public:
enum ViewMode {
SinglePageView,
FacingPagesView,
AllPagesView
};
enum ZoomMode {
CustomZoom,
FitToWidth,
FitInView
};
private:
void generatePreview();
void layoutPages();
void populateScene();
void setViewMode(ViewMode);
void setZoomMode(ZoomMode);
QGraphicsScene *scene;
int curPage, publicPageNum;
ViewMode viewMode;
ZoomMode zoomMode;
QPageLayout::Orientation orientation;
double zoomFactor;
bool initialized, fitting;
QList<QGraphicsItem*> pages;
QHash<int, QList<QGraphicsItem*>> links;
QHash<int, QList<QGraphicsItem*>> annots;
int degrees;
Renderer *BACKEND;
public:
PrintWidget(Renderer *backend, QWidget *parent = 0);
~PrintWidget();
double getZoomFactor() const { return this->zoomFactor; };
ZoomMode getZoomMode() const { return this->zoomMode; };
int currentPage() const { return publicPageNum; };
signals:
void resized();
void customContextMenuRequested(const QPoint&);
void currentPageChanged();
public slots:
void zoomIn(double factor=1.2);
void zoomOut(double factor=1.2);
void setCurrentPage(int);
void setVisible(bool) Q_DECL_OVERRIDE;
void highlightText(TextData*);
void goToPosition(int, float, float);
void updatePreview();
void fitView();
void fitToWidth();
void setAllPagesViewMode();
void setSinglePageViewMode();
void setFacingPagesViewMode();
private slots:
void updateCurrentPage();
int calcCurrentPage();
void fit(bool doFitting=false);
protected:
void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE {
/*{
const QSignalBlocker blocker(verticalScrollBar()); // Don't change page, QTBUG-14517
QGraphicsView::resizeEvent(e);
}*/
QGraphicsView::resizeEvent(e);
emit resized();
}
void clearItems(QList<QGraphicsItem*> itemList, QGraphicsItem *item) {
foreach(QGraphicsItem *graphicsItem, itemList) {
if(item == graphicsItem)
continue;
if(graphicsItem == dynamic_cast<LinkItem*>(graphicsItem))
graphicsItem->setOpacity(0.1);
if(graphicsItem == dynamic_cast<PopupItem*>(graphicsItem))
graphicsItem->setVisible(false);
}
}
void mouseMoveEvent(QMouseEvent *e) Q_DECL_OVERRIDE {
QGraphicsView::mouseMoveEvent(e);
static bool cursorSet = false;
if(QGraphicsItem *item = scene->itemAt(mapToScene(e->pos()), transform())) {
QList<QGraphicsItem*> linkList;
if(item == dynamic_cast<PopupItem*>(item))
item = item->parentItem();
if(PageItem *page = dynamic_cast<PageItem*>(item))
linkList = page->childItems();
else
linkList = item->parentItem()->childItems();
if(LinkItem *link = dynamic_cast<LinkItem*>(item)){
item->setOpacity(1);
if(!cursorSet) {
QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor));
cursorSet = true;
}
}else if(cursorSet){
QApplication::restoreOverrideCursor();
cursorSet = false;
}
if(AnnotZone *annotZone = dynamic_cast<AnnotZone*>(item)){
if(annotZone->hasText() or annotZone->hasAuthor())
annotZone->annotation()->setVisible(true);
item = annotZone->annotation();
}
clearItems(linkList, item);
}
}
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE {
QGraphicsView::mouseReleaseEvent(e);
QPointF scenePoint = mapToScene(e->pos());
QGraphicsItem *item = scene->itemAt(scenePoint, transform());
if(LinkItem *link = dynamic_cast<LinkItem*>(item)) {
BACKEND->handleLink(this, link->getData()->text());
link->setOpacity(0.1);
}
}
void showEvent(QShowEvent* e) Q_DECL_OVERRIDE {
QGraphicsView::showEvent(e);
emit resized();
}
};
#endif
|