-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_paged.cpp
More file actions
359 lines (302 loc) · 10 KB
/
Copy pathviewer_paged.cpp
File metadata and controls
359 lines (302 loc) · 10 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
/*
* 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"
/* ------------------------------------------------------------------------ */
// 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); }
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)
: ViewerScrollArea(parent)
{
content = new PagedContent(this);
setWidget(content);
}
/*
* 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);
if (updatesEnabled()) {
QPoint where = scrollBarPosition();
refresh();
setScrollBarPosition(where);
}
}
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();
}
/* ------------------------------------------------------------------------ */
PagedContent::PagedContent(QScrollArea *parent)
: QWidget(parent)
{
renderer = nullptr;
viewport = parent->viewport();
zoomFactor = 100;
purgeInvisible = true; // purge invisible pages to save memory?
isMoving = false;
moveTimer = new QTimer(this);
moveTimer->setSingleShot(true);
connect(moveTimer, &QTimer::timeout, this, &PagedContent::stoppedMoving);
// 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();
if (replacement != nullptr
&& replacement->mode() == Renderer::PagedContent) {
renderer = (PagedContentRenderer*)replacement;
// Prepare the cache
int numPages = renderer->numPages();
pages.reserve(numPages);
for (int i = 0; i < numPages; i++)
pages.append(new Page);
connect(this, &PagedContent::imageRequested,
renderer, &PagedContentRenderer::renderPage);
connect(renderer, &PagedContentRenderer::renderedPage,
this, &PagedContent::setPageImage);
} else
renderer = nullptr;
}
void PagedContent::setZoomFactor(int percent)
{
zoomFactor = percent;
if (renderer != nullptr) {
renderer->setZoomFactor(percent);
// 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 to occupy the same relative area as it would
// on a standard-DPI display (i.e., based on inches, not pixels)
dpiX *= dpRatio, dpiY *= dpRatio;
renderer->setPixelDensity(dpiX, dpiY);
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();
}
}
fitToContent();
setPagePositions();
}
void PagedContent::clear()
{
visiblePages.clear();
update();
}
void PagedContent::display()
{
setZoomFactor(zoomFactor);
refresh();
}
/*
* Render and paint visible pages.
*/
void PagedContent::refresh()
{
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)) {
if (page->pixmap.isNull()
&& (!(isMoving || page->isRendering))) {
// Request an image from the renderer
// setPageImage() will paint it when it comes back
page->isRendering = true;
emit imageRequested(i);
}
visiblePages.append(pages.at(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
}
update();
}
void PagedContent::moveEvent(QMoveEvent *event)
{
if (!updatesEnabled())
return;
// Refresh once immediately so the user can see the new content, but
// delay further renders until we've stopped moving.
// This avoids rendering pages that aren't visible for any meaningful
// amount of time when rapidly scrolling.
refresh();
isMoving = true;
moveTimer->start(50);
}
void PagedContent::paintEvent(QPaintEvent *event)
{
if (!updatesEnabled())
return;
QPainter painter(this);
for (int i = 0; i < visiblePages.count(); i++) {
const Page *page = 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
painter.fillRect(pageRect, Qt::white);
else
painter.drawPixmap(pageRect, page->pixmap);
}
}
}
void PagedContent::resizeEvent(QResizeEvent *event)
{
if (!updatesEnabled())
return;
setPagePositions();
}
/*
* 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();
bool wereUpdatesEnabled = updatesEnabled();
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 setPagePositions().
// 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.
setUpdatesEnabled(false);
setMinimumSize(w, h);
resize(std::max(w, visibleArea.width()), h);
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
}
/*
* Recalculate page positions when the widget is resized.
*/
void PagedContent::setPagePositions()
{
QRect visibleArea = visibleRect();
for (int i = 0, y = 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;
}
}
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());
}
}
void PagedContent::stoppedMoving()
{
isMoving = false;
refresh();
}