-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_paged.cpp
More file actions
449 lines (381 loc) · 12.7 KB
/
Copy pathviewer_paged.cpp
File metadata and controls
449 lines (381 loc) · 12.7 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
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
/*
* Widget for viewing paged content like a PDF document.
* Copyright (c) 2021-2026 Benjamin Johnson
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <algorithm> // for std::max()
#include <QtCore>
#include <QtWidgets>
#include "viewer_paged.h"
#include "renderer.h"
#include "render_image.h"
/* ------------------------------------------------------------------------ */
// Margin in pixels for graphical content
#define PAGE_MARGIN 2
// The initial viewer size is 8.5 x 5.5 in, or half of a US letter page.
// This fits a reasonable amount of content without making drastic assumptions
// about the size of the user's screen, and approximates the 16:9 or 16:10
// aspect ratio found on most modern displays.
#define INITIAL_WIDTH 85
#define INITIAL_HEIGHT 55
// Units above are multiplied by a factor of 10 to allow use of integer math.
#define INITIAL_FACTOR 10
/* ------------------------------------------------------------------------ */
struct Page {
Page();
inline QRect rect() const { return QRect(x, y, width, height); }
inline QSize size() const { return QSize(width, height); }
QPixmap pixmap; // more efficient for display than QImage
int x;
int y;
int width;
int height;
bool isRendering;
};
Page::Page()
{
x = y = -1;
width = height = 0;
isRendering = false;
}
/* ------------------------------------------------------------------------ */
PagedContentViewer::PagedContentViewer(QWidget *parent)
: QScrollArea(parent)
{
content = new PagedContent(this);
setWidget(content);
setBackgroundRole(QPalette::Dark);
}
/*
* Default to a size large enough to show a reasonable amount of content on
* most screens. The exact size is specified by INITIAL_{HEIGHT,WIDTH} above.
*/
QSize PagedContentViewer::sizeHint() const
{
int initialWidth, initialHeight;
initialWidth = INITIAL_WIDTH * logicalDpiX() / INITIAL_FACTOR;
initialHeight = INITIAL_HEIGHT * logicalDpiY();
// Compensate for the viewport margins and vertical scroll bar
QMargins margins = viewportMargins();
initialWidth += margins.left() + margins.right();
initialWidth += verticalScrollBar()->width();
return QSize(initialWidth, initialHeight);
}
void PagedContentViewer::setRenderer(Renderer *replacement)
{
content->setRenderer(replacement);
}
void PagedContentViewer::setZoomFactor(int percent)
{
content->setZoomFactor(percent);
}
QPoint PagedContentViewer::scrollBarPosition() const {
return QPoint(
horizontalScrollBar()->sliderPosition(),
verticalScrollBar()->sliderPosition());
}
void PagedContentViewer::setScrollBarPosition(int x, int y)
{
horizontalScrollBar()->setSliderPosition(x);
verticalScrollBar()->setSliderPosition(y);
}
void PagedContentViewer::clear()
{
content->clear();
// Scroll back to the top-left corner
setScrollBarPosition(0, 0);
}
void PagedContentViewer::display()
{
content->display();
}
void PagedContentViewer::refresh()
{
content->refresh();
}
/*
* Resize the inner frame when the widget's size changes.
*/
void PagedContentViewer::resizeEvent(QResizeEvent *event)
{
widget()->resize(
std::max(viewport()->width(), widget()->minimumWidth()),
std::max(viewport()->height(), widget()->minimumHeight()));
}
void PagedContentViewer::wheelEvent(QWheelEvent *event)
{
// Adapted from QPlainTextEdit::wheelEvent()
if (event->modifiers() & Qt::ControlModifier) {
float delta = event->angleDelta().y() / 120.f;
emit wheelZoomed(delta);
return;
}
QScrollArea::wheelEvent(event);
}
/* ------------------------------------------------------------------------ */
PagedContent::PagedContent(QScrollArea *parent)
: QWidget(parent)
{
renderer = nullptr;
movie = nullptr;
zoomFactor = 100;
purgeInvisible = true; // purge invisible pages to save memory?
renderTimer = new QTimer(this);
renderTimer->setSingleShot(true);
connect(renderTimer, &QTimer::timeout,
this, &PagedContent::renderVisiblePages);
// Never shrink smaller than the content
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
PagedContent::~PagedContent()
{
if (!pages.isEmpty())
purgeCache();
}
void PagedContent::setRenderer(Renderer *replacement)
{
if (!pages.isEmpty())
purgeCache();
renderer = qobject_cast<PagedContentRenderer*>(replacement);
if (renderer == nullptr)
return;
// Prepare the cache
int numPages = renderer->numPages();
pages.reserve(numPages);
for (int i = 0; i < numPages; i++)
pages.append(new Page);
ImageRenderer *imageRenderer = qobject_cast<ImageRenderer*>(renderer);
if (imageRenderer != nullptr && imageRenderer->supportsAnimation()) {
// Bypass normal rendering and let QMovie provide the images.
// Even though animations and paged content are logically distinct
// things, their viewers would share enough layout and painting
// logic anyway that it makes practical sense to combine them.
movie = new QMovie(imageRenderer->path());
connect(movie, &QMovie::updated,
this, &PagedContent::showNextFrame);
} else {
connect(this, &PagedContent::imageRequested,
renderer, &PagedContentRenderer::renderPage);
connect(renderer, &PagedContentRenderer::renderedPage,
this, &PagedContent::setPageImage);
}
}
void PagedContent::setZoomFactor(int percent)
{
zoomFactor = percent;
display();
}
void PagedContent::clear()
{
if (movie != nullptr)
movie->stop();
visiblePages.clear();
update();
}
void PagedContent::display()
{
calculatePageSizes();
fitToContent();
adjustPagePositions();
refresh();
}
void PagedContent::refresh()
{
if (movie != nullptr)
movie->stop();
checkVisiblePages();
update();
if (movie != nullptr)
movie->start();
}
void PagedContent::moveEvent(QMoveEvent *event)
{
if (!updatesEnabled())
return;
refresh();
}
void PagedContent::paintEvent(QPaintEvent *event)
{
if (!updatesEnabled())
return;
QPainter painter(this);
for (int i = 0; i < visiblePages.count(); i++) {
const Page *page = pages.at(visiblePages.at(i));
QRect pageRect = page->rect();
// The area to paint may be smaller than the total visible area
if (pageRect.intersects(event->rect())) {
if (page->pixmap.isNull()) {
// Paint a placeholder to reduce flicker
if (renderer->shouldPaintPlaceholders())
painter.fillRect(pageRect, Qt::white);
} else
painter.drawPixmap(pageRect, page->pixmap);
}
}
}
void PagedContent::resizeEvent(QResizeEvent *event)
{
if (!updatesEnabled())
return;
adjustPagePositions();
}
/*
* Recalculate page positions when the widget is resized.
*/
void PagedContent::adjustPagePositions()
{
QRect visibleArea = visibleRect();
int y = std::max(0, (visibleArea.bottom() - contentSize.height()) / 2);
for (int i = 0; i < pages.count(); i++) {
Page *page = pages[i];
// Center the page if the visible area is wider
page->x = std::max(0, (visibleArea.width() - page->width) / 2);
page->y = y;
y += page->height + PAGE_MARGIN;
}
}
/*
* Calculate page sizes at our current zoom level and screen DPI.
*/
void PagedContent::calculatePageSizes()
{
if (renderer == nullptr)
return;
renderer->setZoomFactor(zoomFactor);
// Images are always rendered at their real pixel size, but layout
// calculations use DPI-independent "logical" pixels. You are not
// expected to understand this -- just to trust that this produces
// correct results on high-DPI screens.
qreal dpRatio = devicePixelRatio();
int dpiX = logicalDpiX(), dpiY = logicalDpiY();
if (!renderer->isPixelExact())
// Scale the image based on inches, not pixels, so it fills
// the same relative area on screen regardless of DPI
dpiX *= dpRatio, dpiY *= dpRatio;
renderer->setPixelDensity(dpiX, dpiY);
// Now the actual page size calculations
for (int i = 0; i < pages.count(); i++) {
Page *page = pages[i];
QSize size = renderer->pageSize(i) / dpRatio;
page->width = size.width();
page->height = size.height();
// Purge the old image so we're forced to re-render
page->pixmap = QPixmap();
}
}
/*
* Determine which pages are currently visible, and render them if needed.
*/
void PagedContent::checkVisiblePages()
{
visiblePages.clear();
visiblePages.reserve(2); // this doesn't have to be exact
QRect visibleArea = visibleRect();
for (int i = 0; i < pages.count(); i++) {
Page *page = pages[i];
if (page->rect().intersects(visibleArea))
visiblePages.append(i);
else if (purgeInvisible)
page->pixmap = QPixmap(); // tantamount to deletion
else if (page->y > visibleArea.bottom())
break; // the remaining pages are outside our visible area
}
// We trigger rendering via a timer to combine multiple calls occurring
// in quick succession when rapidly scrolling. This prevents rendering
// pages that aren't visible for any meaningful amount of time.
renderTimer->start(10);
}
/*
* Adjust this widget's size so it can fit all its content.
*/
void PagedContent::fitToContent()
{
int w = 0, h = 0, pageCount = pages.count();
QRect visibleArea = visibleRect();
if (pageCount) {
h = (pageCount - 1) * PAGE_MARGIN;
for (int i = 0; i < pageCount; i++) {
Page *page = pages[i];
w = std::max(w, page->width);
h += page->height;
}
}
// Disable updates so the resize event doesn't call adjustPagePositions().
// It isn't reliably triggered here, so we call it manually after calling
// this method to ensure it always happens when we need it to.
bool wereUpdatesEnabled = updatesEnabled();
setUpdatesEnabled(false);
contentSize = QSize(w, h);
setMinimumSize(contentSize);
// Shrink the widget if its new size is smaller
resize(std::max(w, visibleArea.width()),
std::max(h, visibleArea.height()));
setUpdatesEnabled(wereUpdatesEnabled);
}
void PagedContent::purgeCache()
{
for (int i = 0; i < pages.count(); i++)
delete pages[i];
pages.clear();
pages.squeeze();
visiblePages.clear(); // this is small so we don't need to squeeze() it
if (movie != nullptr) {
movie->stop();
delete movie; // a hard delete is safe here, and needed to not
// block renaming the file on Windows
movie = nullptr;
}
}
void PagedContent::renderVisiblePages()
{
for (int i = 0; i < visiblePages.count(); i++) {
int num = visiblePages.at(i);
Page *page = pages[num];
if (page->pixmap.isNull() && !page->isRendering) {
// Request an image from the renderer
// setPageImage() will paint it when it comes back
page->isRendering = true;
emit imageRequested(num);
}
}
}
void PagedContent::setPageImage(int num, const QImage &image)
{
if (0 <= num && num < pages.count()) {
Page *page = pages[num];
page->pixmap = QPixmap::fromImage(image);
page->isRendering = false;
// Paint at the correct physical size on high-DPI screens
page->pixmap.setDevicePixelRatio(devicePixelRatio());
// We only need to repaint this page; the others are fine
update(page->rect());
}
}
/*
* Show the next frame of an animation.
*/
void PagedContent::showNextFrame(const QRect &rect)
{
if (pages.isEmpty() || movie == nullptr)
return;
// Animations are always effectively single-"page" documents
Page *page = pages[0];
// Since we bypassed the renderer we have to handle scaling ourselves
page->pixmap = movie->currentPixmap().scaled(
page->size() * devicePixelRatio(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
update(page->rect());
}