-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.cpp
More file actions
277 lines (234 loc) · 7.59 KB
/
Copy pathviewer.cpp
File metadata and controls
277 lines (234 loc) · 7.59 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
/*
* A widget to display file previews.
* 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::clamp()
#include <QtCore>
#include <QtWidgets>
#include "viewer.h"
#include "viewer_text.h"
#include "viewer_paged.h"
#include "renderer.h"
/* ------------------------------------------------------------------------ */
#define ZOOM_MIN 10
#define ZOOM_MAX 800
// 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
/* ------------------------------------------------------------------------ */
Viewer::Viewer(QWidget *parent)
: QStackedWidget(parent)
{
textContentViewer = new TextContentViewer(this);
addWidget(textContentViewer);
pagedContentScrollArea = new ViewerScrollArea(this);
addWidget(pagedContentScrollArea);
pagedContent = new PagedContent(pagedContentScrollArea);
pagedContentScrollArea->setWidget(pagedContent);
renderer = nullptr;
renderThread = new QThread(this);
renderThread->start();
zoomFactor = 100;
connect(textContentViewer, &TextContentViewer::wheelZoomed,
this, &Viewer::zoomIn);
connect(pagedContentScrollArea, &ViewerScrollArea::wheelZoomed,
this, &Viewer::zoomIn);
}
Viewer::~Viewer()
{
unloadRenderer();
if (renderThread != nullptr) {
renderThread->quit();
renderThread->wait();
}
}
/*
* Load and display the specified file.
*/
void Viewer::display(const QString &path)
{
clear();
load(path);
refresh();
}
/*
* Create and connect a renderer for the specified file,
* but do not immediately display it.
*/
void Viewer::load(const QString &path)
{
QString loadError;
unloadRenderer();
path_ = path;
renderer = Renderer::create(path_, &loadError);
if (renderer == nullptr) {
// An error occurred while loading the file
displayError(loadError);
return;
}
renderer->moveToThread(renderThread);
connect(renderer, &Renderer::errorEncountered,
this, &Viewer::displayError);
// These will reject one another's Renderers, so no need to overthink this
textContentViewer->setRenderer(renderer);
pagedContent->setRenderer(renderer);
}
/*
* Disconnect and delete the current renderer.
*/
void Viewer::unloadRenderer()
{
path_.clear();
textContentViewer->setRenderer(nullptr);
pagedContent->setRenderer(nullptr);
if (renderer != nullptr) {
// Don't respond to any more signals from this Renderer
disconnect(renderer, nullptr, nullptr, nullptr);
// Qt gets upset and segfaults if we delete this directly
renderer->deleteLater();
renderer = nullptr;
}
}
void Viewer::setFocusPolicy(Qt::FocusPolicy policy)
{
textContentViewer->setFocusPolicy(policy);
pagedContentScrollArea->setFocusPolicy(policy);
}
/*
* Clear displayed content.
*/
void Viewer::clear()
{
// Do NOT unload the renderer here; we may want to reuse it
textContentViewer->clear();
pagedContent->clear();
// Scroll back to the top-left corner
pagedContentScrollArea->setScrollBarPosition(0, 0);
}
/*
* Update the display.
*/
void Viewer::refresh()
{
if (renderer == nullptr)
return;
// Do not clear() here! The entire _point_ is that we do not clear() here.
// (We don't want its side effects like changing the scrollbar position)
switch (renderer->mode()) {
case Renderer::TextContent:
setCurrentWidget(textContentViewer);
textContentViewer->display();
break;
case Renderer::PagedContent:
setCurrentWidget(pagedContentScrollArea);
pagedContent->display();
break;
}
}
void Viewer::setZoom(int percent)
{
zoomFactor = std::clamp(percent, ZOOM_MIN, ZOOM_MAX);
textContentViewer->setZoomFactor(zoomFactor);
pagedContent->setZoomFactor(zoomFactor);
if (currentWidget() == pagedContentScrollArea) {
QPoint where = pagedContentScrollArea->scrollBarPosition();
pagedContent->refresh();
pagedContentScrollArea->setScrollBarPosition(where);
}
}
void Viewer::displayError(const QString &details)
{
QString message;
QTextStream textStream(&message);
QString path = path_; // save this before unloadRenderer() clears it
// Stop rendering immediately; we'll do other cleanup later
unloadRenderer();
// Tell the user what happened
textStream << "An error occurred while attempting to display this file:"
<< Qt::endl
<< path;
// Append details if we have them
if (!details.isEmpty())
textStream << Qt::endl
<< Qt::endl
<< details;
clear();
setCurrentWidget(textContentViewer);
textContentViewer->setPlainText(message);
}
/*
* Default to the paged content viewer's preferred size.
*/
QSize Viewer::sizeHint() const
{
return pagedContentScrollArea->sizeHint();
}
/* ------------------------------------------------------------------------ */
ViewerScrollArea::ViewerScrollArea(QWidget *parent)
: QScrollArea(parent)
{
setBackgroundRole(QPalette::Dark);
}
QPoint ViewerScrollArea::scrollBarPosition() const {
return QPoint(
horizontalScrollBar()->sliderPosition(),
verticalScrollBar()->sliderPosition());
}
void ViewerScrollArea::setScrollBarPosition(int x, int y)
{
horizontalScrollBar()->setSliderPosition(x);
verticalScrollBar()->setSliderPosition(y);
}
/*
* 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 ViewerScrollArea::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);
}
/*
* Resize the inner frame when the widget's size changes.
*/
void ViewerScrollArea::resizeEvent(QResizeEvent *event)
{
widget()->resize(
std::max(viewport()->width(), widget()->minimumWidth()),
std::max(viewport()->height(), widget()->minimumHeight()));
}
void ViewerScrollArea::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);
}