Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
helpviewerimpl_qtb.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
6
10#include "tracer.h"
11
12#include <QtCore/QStringBuilder>
13
14#include <QtGui/QContextMenuEvent>
15#include <QtWidgets/QMenu>
16#include <QtWidgets/QScrollBar>
17#if QT_CONFIG(clipboard)
18#include <QtGui/QClipboard>
19#endif
20#include <QtWidgets/QApplication>
21
23
24HelpViewerImpl::HelpViewerImpl(qreal zoom, QWidget *parent)
25 : QTextBrowser(parent)
26 , d(new HelpViewerImplPrivate(zoom))
27{
29 QPalette p = palette();
30 p.setColor(QPalette::Inactive, QPalette::Highlight,
31 p.color(QPalette::Active, QPalette::Highlight));
32 p.setColor(QPalette::Inactive, QPalette::HighlightedText,
33 p.color(QPalette::Active, QPalette::HighlightedText));
34 setPalette(p);
35
36 installEventFilter(this);
37 document()->setDocumentMargin(8);
38
39 QFont font = viewerFont();
40 font.setPointSize(int(font.pointSize() + zoom));
41 setViewerFont(font);
42
43 connect(this, &QTextBrowser::sourceChanged, this, &HelpViewerImpl::titleChanged);
44 connect(this, &HelpViewerImpl::loadFinished, this, &HelpViewerImpl::setLoadFinished);
45}
46
47QFont HelpViewerImpl::viewerFont() const
48{
50 if (HelpEngineWrapper::instance().usesBrowserFont())
51 return HelpEngineWrapper::instance().browserFont();
52 return qApp->font();
53}
54
55void HelpViewerImpl::setViewerFont(const QFont &newFont)
56{
58 if (font() != newFont) {
59 d->forceFont = true;
60 setFont(newFont);
61 d->forceFont = false;
62 }
63}
64
65void HelpViewerImpl::scaleUp()
66{
68 if (d->zoomCount < 10) {
69 d->zoomCount++;
70 d->forceFont = true;
71 zoomIn();
72 d->forceFont = false;
73 }
74}
75
76void HelpViewerImpl::scaleDown()
77{
79 if (d->zoomCount > -5) {
80 d->zoomCount--;
81 d->forceFont = true;
82 zoomOut();
83 d->forceFont = false;
84 }
85}
86
87void HelpViewerImpl::resetScale()
88{
90 if (d->zoomCount != 0) {
91 d->forceFont = true;
92 zoomOut(d->zoomCount);
93 d->forceFont = false;
94 }
95 d->zoomCount = 0;
96}
97
98qreal HelpViewerImpl::scale() const
99{
101 return d->zoomCount;
102}
103
104QString HelpViewerImpl::title() const
105{
107 return documentTitle();
108}
109
110QUrl HelpViewerImpl::source() const
111{
113 return QTextBrowser::source();
114}
115
116void HelpViewerImpl::doSetSource(const QUrl &url, QTextDocument::ResourceType type)
117{
119 Q_UNUSED(type);
120 if (HelpViewer::launchWithExternalApp(url))
121 return;
122
123 bool helpOrAbout = (url.toString() == "help"_L1);
124 const QUrl resolvedUrl = (helpOrAbout ? LocalHelpFile : HelpEngineWrapper::instance().findFile(url));
125
126 QTextBrowser::doSetSource(resolvedUrl, type);
127
128 if (!resolvedUrl.isValid()) {
129 helpOrAbout = (url.toString() == "about:blank"_L1);
130 setHtml(helpOrAbout ? AboutBlank : PageNotFoundMessage.arg(url.toString()));
131 }
132 emit loadFinished(true);
133}
134
135QString HelpViewerImpl::selectedText() const
136{
138 return textCursor().selectedText();
139}
140
141bool HelpViewerImpl::isForwardAvailable() const
142{
144 return QTextBrowser::isForwardAvailable();
145}
146
147bool HelpViewerImpl::isBackwardAvailable() const
148{
150 return QTextBrowser::isBackwardAvailable();
151}
152
153bool HelpViewerImpl::findText(const QString &text, HelpViewer::FindFlags flags, bool incremental,
154 bool fromSearch)
155{
157 QTextDocument *doc = document();
158 QTextCursor cursor = textCursor();
159 if (!doc || cursor.isNull())
160 return false;
161
162 const int position = cursor.selectionStart();
163 if (incremental)
164 cursor.setPosition(position);
165
166 QTextDocument::FindFlags textDocFlags;
167 if (flags & HelpViewer::FindBackward)
168 textDocFlags |= QTextDocument::FindBackward;
169 if (flags & HelpViewer::FindCaseSensitively)
170 textDocFlags |= QTextDocument::FindCaseSensitively;
171
172 QTextCursor found = doc->find(text, cursor, textDocFlags);
173 if (found.isNull()) {
174 if ((flags & HelpViewer::FindBackward) == 0)
175 cursor.movePosition(QTextCursor::Start);
176 else
177 cursor.movePosition(QTextCursor::End);
178 found = doc->find(text, cursor, textDocFlags);
179 }
180
181 if (fromSearch) {
182 cursor.beginEditBlock();
183 viewport()->setUpdatesEnabled(false);
184
185 QTextCharFormat marker;
186 marker.setForeground(Qt::red);
187 cursor.movePosition(QTextCursor::Start);
188 setTextCursor(cursor);
189
190 while (find(text)) {
191 QTextCursor hit = textCursor();
192 hit.mergeCharFormat(marker);
193 }
194
195 viewport()->setUpdatesEnabled(true);
196 cursor.endEditBlock();
197 }
198
199 bool cursorIsNull = found.isNull();
200 if (cursorIsNull) {
201 found = textCursor();
202 found.setPosition(position);
203 }
204 setTextCursor(found);
205 return !cursorIsNull;
206}
207
208// -- public slots
209
210#if QT_CONFIG(clipboard)
211void HelpViewerImpl::copy()
212{
213 TRACE_OBJ
214 QTextBrowser::copy();
215}
216#endif
217
218void HelpViewerImpl::forward()
219{
221 QTextBrowser::forward();
222}
223
224void HelpViewerImpl::backward()
225{
227 QTextBrowser::backward();
228}
229
230// -- protected
231
232void HelpViewerImpl::keyPressEvent(QKeyEvent *e)
233{
235 if ((e->key() == Qt::Key_Home && e->modifiers() != Qt::NoModifier)
236 || (e->key() == Qt::Key_End && e->modifiers() != Qt::NoModifier)) {
237 QKeyEvent* event = new QKeyEvent(e->type(), e->key(), Qt::NoModifier,
238 e->text(), e->isAutoRepeat(), e->count());
239 e = event;
240 }
241 QTextBrowser::keyPressEvent(e);
242}
243
244void HelpViewerImpl::wheelEvent(QWheelEvent *e)
245{
247 if (e->modifiers() == Qt::ControlModifier) {
248 e->accept();
249 e->angleDelta().y() > 0 ? scaleUp() : scaleDown();
250 } else {
251 QTextBrowser::wheelEvent(e);
252 }
253}
254
255void HelpViewerImpl::mousePressEvent(QMouseEvent *e)
256{
258#ifdef Q_OS_LINUX
259 if (handleForwardBackwardMouseButtons(e))
260 return;
261#endif
262
263 QTextBrowser::mousePressEvent(e);
264}
265
266void HelpViewerImpl::mouseReleaseEvent(QMouseEvent *e)
267{
269#ifndef Q_OS_LINUX
270 if (handleForwardBackwardMouseButtons(e))
271 return;
272#endif
273
274 bool controlPressed = e->modifiers() & Qt::ControlModifier;
275 if ((controlPressed && d->hasAnchorAt(this, e->pos())) ||
276 (e->button() == Qt::MiddleButton && d->hasAnchorAt(this, e->pos()))) {
277 d->openLinkInNewPage();
278 return;
279 }
280
281 QTextBrowser::mouseReleaseEvent(e);
282}
283
284
285void HelpViewerImpl::resizeEvent(QResizeEvent *e)
286{
287 const int topTextPosition = cursorForPosition({width() / 2, 0}).position();
288 QTextBrowser::resizeEvent(e);
289 scrollToTextPosition(topTextPosition);
290}
291
292// -- private slots
293
294void HelpViewerImpl::actionChanged()
295{
296 // stub
298}
299
300// -- private
301
302bool HelpViewerImpl::eventFilter(QObject *obj, QEvent *event)
303{
305 if (event->type() == QEvent::FontChange && !d->forceFont)
306 return true;
307 return QTextBrowser::eventFilter(obj, event);
308}
309
310void HelpViewerImpl::contextMenuEvent(QContextMenuEvent *event)
311{
313
314 QMenu menu(QString(), nullptr);
315 QUrl link;
316#if QT_CONFIG(clipboard)
317 QAction *copyAnchorAction = nullptr;
318#endif
319 if (d->hasAnchorAt(this, event->pos())) {
320 link = anchorAt(event->pos());
321 if (link.isRelative())
322 link = source().resolved(link);
323 menu.addAction(tr("Open Link"), d, &HelpViewerImplPrivate::openLink);
324 menu.addAction(tr("Open Link in New Tab\tCtrl+LMB"), d, &HelpViewerImplPrivate::openLinkInNewPage);
325
326#if QT_CONFIG(clipboard)
327 if (!link.isEmpty() && link.isValid())
328 copyAnchorAction = menu.addAction(tr("Copy &Link Location"));
329#endif
330 } else if (!selectedText().isEmpty()) {
331#if QT_CONFIG(clipboard)
332 menu.addAction(tr("Copy"), this, &HelpViewerImpl::copy);
333#endif
334 } else {
335 menu.addAction(tr("Reload"), this, &HelpViewerImpl::reload);
336 }
337
338#if QT_CONFIG(clipboard)
339 if (copyAnchorAction == menu.exec(event->globalPos()))
340 QApplication::clipboard()->setText(link.toString());
341#endif
342}
343
344QVariant HelpViewerImpl::loadResource(int type, const QUrl &name)
345{
347 QByteArray ba;
348 if (type < 4) {
349 const QUrl url = HelpEngineWrapper::instance().findFile(name);
350 ba = HelpEngineWrapper::instance().fileData(url);
351 if (url.toString().endsWith(".svg"_L1, Qt::CaseInsensitive)) {
352 QImage image;
353 image.loadFromData(ba, "svg");
354 if (!image.isNull())
355 return image;
356 }
357 }
358 return ba;
359}
360
361
362void HelpViewerImpl::scrollToTextPosition(int position)
363{
364 QTextCursor tc(document());
365 tc.setPosition(position);
366 const int dy = cursorRect(tc).top();
367 if (verticalScrollBar()) {
368 verticalScrollBar()->setValue(
369 std::min(verticalScrollBar()->value() + dy, verticalScrollBar()->maximum()));
370 }
371}
372
373QT_END_NAMESPACE
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34