-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.cpp
More file actions
194 lines (165 loc) · 4.9 KB
/
Copy pathviewer.cpp
File metadata and controls
194 lines (165 loc) · 4.9 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
/*
* 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
Viewer::Viewer(QWidget *parent)
: QStackedWidget(parent)
{
textContentViewer = new TextContentViewer(this);
addWidget(textContentViewer);
pagedContentViewer = new PagedContentViewer(this);
addWidget(pagedContentViewer);
renderer = nullptr;
renderThread = new QThread(this);
renderThread->start();
zoomFactor = 100;
connect(textContentViewer, &TextContentViewer::wheelZoomed,
this, &Viewer::zoomIn);
connect(pagedContentViewer, &PagedContentViewer::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);
pagedContentViewer->setRenderer(renderer);
}
/*
* Disconnect and delete the current renderer.
*/
void Viewer::unloadRenderer()
{
path_.clear();
textContentViewer->setRenderer(nullptr);
pagedContentViewer->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);
pagedContentViewer->setFocusPolicy(policy);
}
/*
* Clear displayed content.
*/
void Viewer::clear()
{
// Do NOT unload the renderer here; we may want to reuse it
textContentViewer->clear();
pagedContentViewer->clear();
}
/*
* 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(pagedContentViewer);
pagedContentViewer->display();
break;
}
}
void Viewer::setZoom(int percent)
{
zoomFactor = std::clamp(percent, ZOOM_MIN, ZOOM_MAX);
textContentViewer->setZoomFactor(zoomFactor);
pagedContentViewer->setZoomFactor(zoomFactor);
}
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 pagedContentViewer->sizeHint();
}