-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer_paged.cpp
More file actions
292 lines (246 loc) · 8.05 KB
/
Copy pathviewer_paged.cpp
File metadata and controls
292 lines (246 loc) · 8.05 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
/*
* 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
/* ------------------------------------------------------------------------ */
struct Page {
Page();
inline QRect rect() const { return QRect(x, y, width, height); }
QImage image;
int x;
int y;
int width;
int height;
bool isRendering;
};
Page::Page()
{
x = y = -1;
width = height = 0;
isRendering = false;
}
/* ------------------------------------------------------------------------ */
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);
// Render at the correct physical size on high-DPI screens
renderer->setPixelDensity(logicalDpiX(), logicalDpiY());
qreal dpRatio = devicePixelRatio();
for (int i = 0; i < pages.count(); i++) {
Page *page = pages[i];
// The renderer does not understand Qt's high-DPI handling
// (something it and I have in common), so we need to manually
// scale this back to the correct logical size
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->image = QImage();
}
}
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->image.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->image = QImage(); // 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()) {
event->accept();
// 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);
} else
event->ignore();
}
void PagedContent::paintEvent(QPaintEvent *event)
{
if (updatesEnabled()) {
event->accept();
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->image.isNull())
// Paint a placeholder to reduce flicker
painter.fillRect(pageRect, Qt::white);
else
painter.drawImage(pageRect, page->image);
}
}
} else
event->ignore();
}
void PagedContent::resizeEvent(QResizeEvent *event)
{
if (updatesEnabled()) {
event->accept();
setPagePositions();
} else
event->ignore();
}
/*
* 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->image = image;
page->isRendering = false;
// Paint at the correct physical size on high-DPI screens
page->image.setDevicePixelRatio(devicePixelRatio());
// We only need to repaint this page; the others are fine
update(page->rect());
}
}
void PagedContent::stoppedMoving()
{
isMoving = false;
refresh();
}