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
|
//===========================================
// 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 "Renderer.h"
#include "TextData.h"
#include "graphicsitems.h"
#include <QApplication>
#include <QBoxLayout>
#include <QDebug>
#include <QGraphicsItem>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QPageLayout>
#include <QScrollBar>
#include <QStyleOptionGraphicsItem>
#include <QTextDocument>
#include <QtMath>
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 curPage;
//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 if (item != dynamic_cast<QGraphicsRectItem *>(item))
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
|