This repository has been archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.cpp
345 lines (296 loc) · 10.1 KB
/
viewer.cpp
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
/*
* A widget to display file previews.
* Copyright (c) 2021 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>
#include <QtCore>
#include <QtWidgets>
#include "viewer.h"
// 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
// Margin in pixels for graphical content
#define PAGE_MARGIN 1
// Margin in pixels for plain-text content
#define TEXT_MARGIN 8
/* ------------------------------------------------------------------------ */
Viewer::Viewer(QWidget *parent)
: QStackedWidget(parent)
{
textContentViewer = new TextContentViewer(this);
addWidget(textContentViewer);
pagedContentViewer = new PagedContentViewer(this);
addWidget(pagedContentViewer);
// QThread is responsible for this while it exists
renderer = nullptr;
renderThread = nullptr;
}
void Viewer::display(const QString &path)
{
clear();
renderThread = new QThread;
// Always use logical DPI for correctly-scaled output on high-DPI screens
renderer = new Renderer(path, logicalDpiX(), logicalDpiY());
renderer->moveToThread(renderThread);
// QThread signals
connect(renderThread, &QThread::started,
renderer, &Renderer::render);
connect(renderThread, &QThread::finished,
renderer, &QObject::deleteLater);
connect(renderThread, &QThread::finished,
renderThread, &QObject::deleteLater);
// Renderer signals
connect(renderer, &Renderer::renderedImage,
this, &Viewer::addImage);
connect(renderer, &Renderer::renderedPage,
this, &Viewer::addPage);
connect(renderer, &Renderer::renderedText,
this, &Viewer::addText);
connect(renderer, &Renderer::renderMode,
this, &Viewer::setRenderMode);
connect(renderer, &Renderer::renderProgress,
this, &Viewer::renderProgress);
renderThread->start();
}
void Viewer::setFocusPolicy(Qt::FocusPolicy policy)
{
textContentViewer->setFocusPolicy(policy);
pagedContentViewer->setFocusPolicy(policy);
}
void Viewer::clear()
{
// Cancel any active rendering operation
stopRender();
textContentViewer->clear();
pagedContentViewer->clear();
}
void Viewer::stopRender()
{
// renderer and renderThread are cleaned up by QObject::deleteLater()
if (renderer != nullptr) {
// Don't respond to any more signals from this Renderer
disconnect(renderer, nullptr, nullptr, nullptr);
renderer = nullptr;
}
if (renderThread != nullptr) {
if (renderThread->isRunning())
renderThread->requestInterruption();
// Trust, but verify
renderThread->quit();
renderThread->wait();
renderThread = nullptr;
}
}
void Viewer::addImage(const QImage &image)
{
pagedContentViewer->addImage(image);
}
void Viewer::addPage(const QImage &image)
{
pagedContentViewer->addPage(image);
}
void Viewer::addText(const QString &text)
{
if (currentWidget() == textContentViewer) {
// FIXME: This clobbers any existing content.
textContentViewer->setPlainText(text);
} else if (currentWidget() == pagedContentViewer)
pagedContentViewer->addText(text);
}
void Viewer::setRenderMode(int mode)
{
if (mode == Renderer::TextContent)
setCurrentWidget(textContentViewer);
else if (mode == Renderer::PagedContent)
setCurrentWidget(pagedContentViewer);
}
/*
* Default to PagedContentViewer's preferred size.
*/
QSize Viewer::sizeHint() const
{
return pagedContentViewer->sizeHint();
}
/* ------------------------------------------------------------------------ */
TextContentViewer::TextContentViewer(QWidget *parent)
: QPlainTextEdit(parent)
{
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
setReadOnly(true);
setTabChangesFocus(true);
setWordWrapMode(QTextOption::WordWrap);
}
/* ------------------------------------------------------------------------ */
PagedContentViewer::PagedContentViewer(QWidget *parent)
: QScrollArea(parent)
{
frame = new QFrame(this);
layout = new QVBoxLayout(frame);
setWidget(frame);
setBackgroundRole(QPalette::Dark);
// Total width and height of graphical elements in pageWidgets
// Note: Storing these is an optimization; since they contain fixed
// images, their dimensions don't change. Text widgets are not included
// since they are resized dynamically to fit the viewport.
totalPageWidth = 0;
totalPageHeight = 0;
}
PagedContentViewer::~PagedContentViewer()
{
clear();
}
/*
* Add an image.
*/
void PagedContentViewer::addImage(const QImage &image)
{
addPage_(image, false);
}
/*
* Add a page from a multi-page document.
*/
void PagedContentViewer::addPage(const QImage &image)
{
addPage_(image);
}
/*
* Add text content.
*
* This is mostly useful if you need to mix text and paged content,
* for example to display an error message if the renderer was unable
* to process a particular page.
*
* QLabel is inefficient for displaying large amounts of text content,
* which is why TextContentViewer exists for that.
*/
void PagedContentViewer::addText(const QString &text)
{
QLabel *textWidget = createContentWidget();
textWidget->setMargin(TEXT_MARGIN);
textWidget->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
textWidget->setTextFormat(Qt::PlainText);
textWidget->setText(text);
textWidget->setWordWrap(true);
layout->addWidget(textWidget);
textWidgets.append(textWidget);
resizeFrame();
}
void PagedContentViewer::clear()
{
for (int i = 0; i < pageWidgets.size(); ++i) {
layout->removeWidget(pageWidgets[i]);
delete pageWidgets[i];
}
for (int i = 0; i < textWidgets.size(); ++i) {
layout->removeWidget(textWidgets[i]);
delete textWidgets[i];
}
pageWidgets.clear();
textWidgets.clear();
totalPageWidth = 0;
totalPageHeight = 0;
frame->setMinimumSize(0, 0);
frame->resize(0, 0);
}
/*
* 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);
}
/*
* Create a widget to display content.
*/
QLabel *PagedContentViewer::createContentWidget(bool drawBorder)
{
QLabel *widget = new QLabel(frame);
if (drawBorder) {
widget->setBackgroundRole(QPalette::Base);
widget->setAutoFillBackground(true);
widget->setFrameStyle(QFrame::Box | QFrame::Plain);
widget->setLineWidth(1);
widget->setMargin(PAGE_MARGIN);
}
return widget;
}
/*
* Add a widget displaying graphical content.
*
* Used by addImage() and addPage(). The only difference between the two is
* pages are displayed with a border around their content, and images are not.
* This matches the format typically used by other document and image viewers,
* respectively.
*/
void PagedContentViewer::addPage_(const QImage &image, bool drawBorder)
{
QLabel *pageWidget = createContentWidget(drawBorder);
pageWidget->setPixmap(QPixmap::fromImage(image));
pageWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(pageWidget);
pageWidgets.append(pageWidget);
// Store the width of the widest page, and the total height of all pages
QSize pageSize = pageWidget->minimumSizeHint();
totalPageWidth = std::max(totalPageWidth,
pageSize.width() + 2 * PAGE_MARGIN);
totalPageHeight += pageSize.height() + 2 * PAGE_MARGIN;
resizeFrame();
}
/*
* Resize the inner frame when the widget's size changes.
*/
void PagedContentViewer::resizeEvent(QResizeEvent *event)
{
resizeFrame();
}
/*
* Resize the inner frame to fit displayed content.
*
* The frame's size is NOT updated automatically when we add content,
* so this is necessary to ensure that content is visible, and that the
* scrollable area is sized appropriately.
*/
void PagedContentViewer::resizeFrame()
{
int width, height;
// The frame should be as wide as the viewport or the widest page,
// whichever is larger. Note that text widgets are always frame width.
width = std::max(viewport()->width(), totalPageWidth);
// The frame should be tall enough to fit all of the displayed content.
// Images and paged content have fixed heights.
height = totalPageHeight;
// Text widgets' height must be dynamically calculated since it may
// change due to word wrapping.
// Note QLabel::heightForWidth() does not account for margins.
for (int i = 0; i < textWidgets.size(); ++i)
height += textWidgets[i]->heightForWidth(width) + 2 * TEXT_MARGIN;
frame->setMinimumSize(width, height);
frame->resize(width, height);
}