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
|
#include "PrintWidget.h"
PrintWidget::PrintWidget(Renderer *backend, QWidget *parent) :
QGraphicsView(parent), scene(0), curPage(1), viewMode(SinglePageView),
zoomMode(FitInView), zoomFactor(1), initialized(false), fitting(true), BACKEND(backend) {
this->setMouseTracking(true);
QList<QWidget*> children = this->findChildren<QWidget*>("",Qt::FindChildrenRecursively);
for(int i=0; i<children.length(); i++){
children[i]->setContextMenuPolicy(Qt::CustomContextMenu);
connect(children[i], SIGNAL(customContextMenuRequested(const QPoint&)), this, SIGNAL(customContextMenuRequested(const QPoint&)) );
}
this->setInteractive(false);
this->setDragMode(QGraphicsView::ScrollHandDrag);
this->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
this->setFocusPolicy(Qt::NoFocus);
QObject::connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(updateCurrentPage()));
QObject::connect(this, SIGNAL(resized()), this, SLOT(fit()));
scene = new QGraphicsScene(this);
scene->setBackgroundBrush(Qt::gray);
this->setScene(scene);
/*QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(this);*/
}
PrintWidget::~PrintWidget() {
for (int i = 0; i < pages.size(); i++){
scene->removeItem(pages.at(i));
}
qDeleteAll(pages);
pages.clear();
scene->deleteLater();
}
//Public Slots
void PrintWidget::fitView() {
setZoomMode(FitInView);
setCurrentPage(publicPageNum); //Make sure we stay on the same page
}
void PrintWidget::fitToWidth() {
setZoomMode(FitToWidth);
setCurrentPage(publicPageNum); //Make sure we stay on the same page
}
void PrintWidget::setZoomMode(ZoomMode mode) {
zoomMode = mode;
fitting = true;
fit(true);
}
void PrintWidget::setAllPagesViewMode() {
setViewMode(AllPagesView);
}
void PrintWidget::setSinglePageViewMode() {
setViewMode(SinglePageView);
}
void PrintWidget::setFacingPagesViewMode() {
setViewMode(FacingPagesView);
}
void PrintWidget::setViewMode(ViewMode mode) {
viewMode = mode;
layoutPages();
if (viewMode == AllPagesView) {
this->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
fitting = false;
zoomMode = CustomZoom;
zoomFactor = this->transform().m11() * (150.0 / logicalDpiY());
} else {
fitting = true;
fit();
}
}
void PrintWidget::zoomIn(double factor) {
fitting = false;
zoomMode = CustomZoom;
zoomFactor *= factor;
this->scale(factor, factor);
}
void PrintWidget::zoomOut(double factor) {
fitting = false;
zoomMode = CustomZoom;
zoomFactor *= factor;
this->scale(1/factor, 1/factor);
}
void PrintWidget::updatePreview() {
initialized = true;
generatePreview();
this->updateGeometry();
}
void PrintWidget::setVisible(bool visible) {
if(visible and !initialized)
updatePreview();
QGraphicsView::setVisible(visible);
}
void PrintWidget::setCurrentPage(int pageNumber) {
if(pageNumber < 0 || pageNumber > (pages.count()+1) ){ return; }
publicPageNum = pageNumber; //publicly requested page number (+/- 1 from actual page range)
emit currentPageChanged();
if(pageNumber < 1 || pageNumber > pages.count())
return;
int lastPage = curPage;
curPage = pageNumber;
if (lastPage != curPage && lastPage > 0 && lastPage <= pages.count()) {
if (zoomMode != FitInView) {
QScrollBar *hsc = this->horizontalScrollBar();
QScrollBar *vsc = this->verticalScrollBar();
QPointF pt = this->transform().map(pages.at(curPage-1)->pos());
vsc->setValue(int(pt.y()) - 10);
hsc->setValue(int(pt.x()) - 10);
} else {
this->centerOn(pages.at(curPage-1));
}
}
}
void PrintWidget::highlightText(TextData *text) {
//Creates a rectangle around the text if the text has not already been highlighted
if(!text->highlighted() && !text->loc().isNull()) {
int degrees = BACKEND->rotatedDegrees();
//Shows the text's location on a non-rotated page
QRectF rect = text->loc();
//Rotates the rectangle by the page's center and gets the right calculation for text's new location
if(degrees != 0) {
QSize center = BACKEND->imageHash(text->page()-1).size()/2;
if(degrees == 90 or degrees == 270)
center.transpose();
double cx = center.width(), cy = center.height();
rect.adjust(-cx, -cy, -cx, -cy);
QMatrix matrix;
matrix.rotate(BACKEND->rotatedDegrees());
rect = matrix.mapRect(rect);
if(BACKEND->rotatedDegrees() == 180)
rect.adjust(cx, cy, cx, cy);
else
rect.adjust(cy, cx, cy, cx);
}
//Moves the rectangle onto the right page
double pageHeight = 0;
for(int i = 0; i < text->page() - 1; i++)
pageHeight += pages.at(i)->boundingRect().height();
rect.moveTop(rect.y() + pageHeight);
//Transparent yellow for the highlight box
QBrush highlightFill(QColor(255, 255, 177, 100));
QPen highlightOutline(QColor(255, 255, 100, 125));
scene->addRect(rect, highlightOutline, highlightFill);
text->highlighted(true);
}
goToPosition(text->page(), text->loc().x(), text->loc().y());
}
//Private functions
void PrintWidget::generatePreview() {
populateScene(); // i.e. setPreviewPrintedPictures() e.l.
layoutPages();
curPage = qBound(1, curPage, pages.count());
publicPageNum = curPage;
emit currentPageChanged();
if (fitting){ fit(); }
}
void PrintWidget::layoutPages() {
int numPages = pages.count();
if (numPages < 1)
return;
int numPagePlaces = numPages;
int cols = 1; // singleMode and default
QSize pageSize = BACKEND->imageHash(curPage-1).size();
if (viewMode == AllPagesView) {
cols = pageSize.width() > pageSize.height() ? qFloor(qSqrt(numPages)) : qCeil(qSqrt(numPages));
cols += cols % 2; // Nicer with an even number of cols
} else if (viewMode == FacingPagesView) {
cols = 2;
numPagePlaces += 1;
}
int rows = qCeil(double(numPagePlaces) / cols);
int pageNum = 0;
QList<double> rowMaxList;
for (int i = 0; i < rows && pageNum < numPages; i++) {
double rowMax = 0;
for (int j = 0; j < cols && pageNum < numPages; j++) {
double itemWidth = 0, itemHeight = 0;
double pageHeight = pages.at(pageNum)->boundingRect().height();
for(int k = cols * (pageNum / cols); k < pageNum; k++)
itemWidth += pages.at(k)->boundingRect().width();
foreach(double size, rowMaxList)
itemHeight += size;
pages.at(pageNum)->setPos(QPointF(itemWidth, itemHeight));
pageNum++;
rowMax = qMax(rowMax, pageHeight);
}
rowMaxList.push_back(rowMax);
}
scene->setSceneRect(scene->itemsBoundingRect());
//qDebug() << "Finished Page Layout";
}
void PrintWidget::populateScene()
{
for (int i = 0; i < pages.size(); i++){
scene->removeItem(pages.at(i));
}
for(int i = 0; i < links.size(); i++) {
if(links[i].size() > 0) {
qDeleteAll(links[i]);
links[i].clear();
}
}
links.clear();
qDeleteAll(pages);
pages.clear();
int numPages = BACKEND->numPages();
if(BACKEND->hashSize() < numPages){ return; } //nothing to show yet
for (int i = 0; i < numPages; i++) {
QImage pagePicture = BACKEND->imageHash(i);
QSize paperSize = pagePicture.size();
QList<QGraphicsItem*> linkLocations;
if(pagePicture.isNull()) {
qDebug() << "NULL IMAGE ON PAGE " << i;
continue;
}
PageItem* item = new PageItem(i+1, pagePicture, paperSize);
scene->addItem(item);
pages.append(item);
if(BACKEND->supportsExtraFeatures()) {
for(int k = 0; k < BACKEND->linkSize(i); k++) {
LinkItem *lItem = new LinkItem(item, BACKEND->linkList(i, k));
lItem->setOpacity(0.1);
linkLocations.append(lItem);
}
links.insert(i, linkLocations);
}
}
}
//Private Slots
void PrintWidget::updateCurrentPage() {
if (viewMode == AllPagesView)
return;
int newPage = calcCurrentPage();
if (newPage != curPage) {
curPage = newPage;
publicPageNum = curPage;
emit currentPageChanged();
}
}
int PrintWidget::calcCurrentPage() {
int maxArea = 0;
int newPage = curPage;
QRect viewRect = this->viewport()->rect();
QList<QGraphicsItem*> items = this->items(viewRect);
for (int i=0; i<items.size(); ++i) {
PageItem* pg = static_cast<PageItem*>(items.at(i));
QRect overlap = this->mapFromScene(pg->sceneBoundingRect()).boundingRect() & viewRect;
int area = overlap.width() * overlap.height();
if (area > maxArea) {
maxArea = area;
newPage = pg->pageNumber();
} else if (area == maxArea && pg->pageNumber() < newPage) {
newPage = pg->pageNumber();
}
}
return newPage;
}
void PrintWidget::fit(bool doFitting) {
if (curPage < 1 || curPage > pages.count())
return;
if (!doFitting && !fitting)
return;
if (doFitting && fitting) {
QRect viewRect = this->viewport()->rect();
if (zoomMode == FitInView) {
QList<QGraphicsItem*> containedItems = this->items(viewRect, Qt::ContainsItemBoundingRect);
foreach(QGraphicsItem* item, containedItems) {
PageItem* pg = static_cast<PageItem*>(item);
if (pg->pageNumber() == curPage)
return;
}
}
int newPage = calcCurrentPage();
if (newPage != curPage)
curPage = newPage;
}
QRectF target = pages.at(curPage-1)->sceneBoundingRect();
if (viewMode == FacingPagesView) {
if (curPage % 2)
target.setLeft(target.left() - target.width());
else
target.setRight(target.right() + target.width());
} else if (viewMode == AllPagesView) {
target = scene->itemsBoundingRect();
}
if (zoomMode == FitToWidth) {
QTransform t;
qreal scale = this->viewport()->width() / target.width();
t.scale(scale, scale);
this->setTransform(t);
if (doFitting && fitting) {
QRectF viewSceneRect = this->viewportTransform().mapRect(this->viewport()->rect());
viewSceneRect.moveTop(target.top());
this->ensureVisible(viewSceneRect); // Nah...
}
} else {
this->fitInView(target, Qt::KeepAspectRatio);
if (zoomMode == FitInView) {
int step = qRound(this->matrix().mapRect(target).height());
this->verticalScrollBar()->setSingleStep(step);
this->verticalScrollBar()->setPageStep(step);
}
}
zoomFactor = this->transform().m11() * (150.0 / this->logicalDpiY());
}
void PrintWidget::goToPosition(int pagenum, float x, float y) {
setCurrentPage(pagenum);
QScrollBar *hsc = this->horizontalScrollBar();
QScrollBar *vsc = this->verticalScrollBar();
QPointF pt = this->transform().map(pages.at(pagenum-1)->pos());
int secondPagenum = pagenum < pages.size() ? pagenum : pagenum-2;
QPointF pt2 = this->transform().map(pages.at(secondPagenum)->pos());
double realHeight = pages.at(pagenum-1)->boundingRect().height();
double virtualHeight = qAbs(pt2.y() - pt.y());
int yConv = int(pt.y() + y*(virtualHeight/realHeight)) - 30;
int xConv = int(pt.x() + x*(virtualHeight/realHeight)) - 30;
if(yConv > vsc->maximum())
vsc->triggerAction(QAbstractSlider::SliderToMaximum);
else if(y != 0)
vsc->setValue(yConv);
if(x != 0)
hsc->setValue(xConv);
}
|