10#include <QtCore/QFileInfo>
11#include <QtCore/QStringBuilder>
12#include <QtCore/QTemporaryFile>
13#include <QtCore/QXmlStreamReader>
14#include <QtCore/QXmlStreamWriter>
16#include <QtGui/QDesktopServices>
17#if QT_CONFIG(clipboard)
18#include <QtGui/QClipboard>
20#include <QtGui/QGuiApplication>
21#include <QtGui/QStyleHints>
22#include <QtGui/QWheelEvent>
24#include <QtWidgets/QScrollBar>
25#include <QtWidgets/QVBoxLayout>
27#include <QtHelp/QHelpEngineCore>
29#include <qlitehtmlwidget.h>
33using namespace Qt::StringLiterals;
41 {
".bmp",
"image/bmp" },
42 {
".css",
"text/css" },
43 {
".gif",
"image/gif" },
44 {
".html",
"text/html" },
45 {
".htm",
"text/html" },
46 {
".ico",
"image/x-icon" },
47 {
".jpeg",
"image/jpeg" },
48 {
".jpg",
"image/jpeg" },
49 {
".js",
"application/x-javascript" },
50 {
".mng",
"video/x-mng" },
51 {
".pbm",
"image/x-portable-bitmap" },
52 {
".pgm",
"image/x-portable-graymap" },
54 {
".png",
"image/png" },
55 {
".ppm",
"image/x-portable-pixmap" },
56 {
".rss",
"application/rss+xml" },
57 {
".svg",
"image/svg+xml" },
58 {
".svgz",
"image/svg+xml" },
59 {
".text",
"text/plain" },
60 {
".tif",
"image/tiff" },
61 {
".tiff",
"image/tiff" },
62 {
".txt",
"text/plain" },
63 {
".xbm",
"image/x-xbitmap" },
64 {
".xml",
"text/xml" },
65 {
".xpm",
"image/x-xpm" },
66 {
".xsl",
"text/xsl" },
67 {
".xhtml",
"application/xhtml+xml" },
68 {
".wml",
"text/vnd.wap.wml" },
69 {
".wmlc",
"application/vnd.wap.wmlc" },
70 {
"about:blank",
nullptr },
77 QPalette p = widget->palette();
78 p.setColor(QPalette::Inactive, QPalette::Highlight,
79 p.color(QPalette::Active, QPalette::Highlight));
80 p.setColor(QPalette::Inactive, QPalette::HighlightedText,
81 p.color(QPalette::Active, QPalette::HighlightedText));
82 p.setColor(QPalette::Base, Qt::white);
83 p.setColor(QPalette::Text, Qt::black);
84 widget->setPalette(p);
91 return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark
92 || QGuiApplication::palette().color(QPalette::Base).lightnessF()
93 < QGuiApplication::palette().color(QPalette::Text).lightnessF();
98 QPalette appPalette = QGuiApplication::palette();
105 appPalette.setColor(QPalette::Base, appPalette.color(QPalette::Base));
106 widget->setPalette(appPalette);
112 QXmlStreamReader reader(data);
115 if (reader.readNextStartElement()) {
116 if (reader.name() ==
"svg") {
119 qint64 rest = reader.characterOffset();
124 QXmlStreamWriter writer(&newData);
126 writer.writeStartDocument();
127 writer.writeStartElement(
"svg");
128 writer.writeAttributes(reader.attributes());
129 writer.writeAttribute(
"class",
"dark");
132 writer.writeCharacters(
"");
136 return newData + data.mid(rest);
152 QUrl actualUrl = url;
153 QString path = url.path(QUrl::FullyEncoded);
154 static const char simpleCss[] =
"/offline-simple.css";
155 if (path.endsWith(simpleCss)) {
158 QString darkPath = path;
159 darkPath.replace(simpleCss,
"/offline-dark.css");
160 actualUrl.setPath(darkPath);
162 if (!data.isEmpty()) {
165 setPaletteFromApp(widget);
169 path.replace(simpleCss,
"/offline.css");
170 actualUrl.setPath(path);
173 if (actualUrl.isValid()) {
176 return addDarkThemeToSvg(data);
181 const bool isAbout = (actualUrl.toString() ==
"about:blank"_L1);
182 return isAbout ? HelpViewerImpl::AboutBlank.toUtf8()
183 : HelpViewerImpl::PageNotFoundMessage.arg(url.toString()).toUtf8();
209 return { m_viewer->url(), m_viewer->title(), m_viewer->verticalScrollBar()->value() };
214 QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
216 const bool isHelp = (url.toString() ==
"help"_L1);
217 const QUrl resolvedUrl = (isHelp ? HelpViewerImpl::LocalHelpFile
218 : HelpEngineWrapper::instance().findFile(url));
220 QUrl currentUrlWithoutFragment = m_viewer->url();
221 currentUrlWithoutFragment.setFragment({});
222 QUrl newUrlWithoutFragment = resolvedUrl;
223 newUrlWithoutFragment.setFragment({});
225 m_viewer->setUrl(resolvedUrl);
226 if (currentUrlWithoutFragment != newUrlWithoutFragment || reload) {
231 m_viewer->setHtml(QString::fromUtf8(getData(resolvedUrl, q)));
234 m_viewer->verticalScrollBar()->setValue(*vscroll);
236 m_viewer->scrollToAnchor(resolvedUrl.fragment(QUrl::FullyEncoded));
238 QGuiApplication::restoreOverrideCursor();
240 emit q->sourceChanged(q->source());
241 emit q->loadFinished();
242 emit q->titleChanged();
247 const int incrementPercentage = 10 * steps;
254 const int newZoom = qBound(10, percentage, 300);
258 m_viewer->setZoomFactor(newZoom / 100.0);
265 auto layout =
new QVBoxLayout;
267 d->m_viewer =
new QLiteHtmlWidget(
this);
268 d->m_viewer->setResourceHandler([
this](
const QUrl &url) {
return getData(url,
this); });
269 d->m_viewer->viewport()->installEventFilter(
this);
270 const int zoomPercentage = zoom == 0 ? 100 : zoom * 100;
272 connect(d->m_viewer, &QLiteHtmlWidget::linkClicked,
this, &HelpViewer::setSource);
273 connect(d->m_viewer, &QLiteHtmlWidget::linkHighlighted,
this, &HelpViewer::highlighted);
274#if QT_CONFIG(clipboard)
275 connect(d->m_viewer, &QLiteHtmlWidget::copyAvailable,
this, &HelpViewer::copyAvailable);
278 layout->setContentsMargins(0, 0, 0, 0);
279 layout->addWidget(d->m_viewer, 10);
283 QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged,
this,
285 int vscroll = d->m_viewer->verticalScrollBar()->value();
286 d->setSourceInternal(source(), &vscroll,
true);
289 Qt::QueuedConnection);
299 return d->m_viewer->defaultFont();
304 d->m_viewer->setDefaultFont(font);
324 return d->m_viewer->zoomFactor();
329 return d->m_viewer->title();
334 return d->m_viewer->url();
339 doSetSource(source(),
true);
344 doSetSource(url,
false);
347void HelpViewer::doSetSource(
const QUrl &url,
bool reload)
349 if (launchWithExternalApp(url))
352 d->m_forwardItems.clear();
353 emit forwardAvailable(
false);
354 if (d->m_viewer->url().isValid()) {
357 d->m_backItems.erase(d->m_backItems.begin());
358 emit backwardAvailable(
true);
361 d->setSourceInternal(url,
nullptr, reload);
364#if QT_CONFIG(printer)
365void HelpViewer::print(QPrinter *printer)
367 d->m_viewer->print(printer);
373 return d->m_viewer->selectedText();
378 return !d->m_forwardItems.empty();
383 return !d->m_backItems.empty();
388 QTextDocument::FindFlags textDocFlags;
389 if (flags & HelpViewer::FindBackward)
390 textDocFlags |= QTextDocument::FindBackward;
391 if (flags & HelpViewer::FindCaseSensitively)
392 textDocFlags |= QTextDocument::FindCaseSensitively;
398 Q_UNUSED(fromSearch);
399 return d->m_viewer->findText(text, textDocumentFlagsForFindFlags(flags), incremental);
402#if QT_CONFIG(clipboard)
403void HelpViewer::copy()
405 QGuiApplication::clipboard()->setText(selectedText());
411 setSource(HelpEngineWrapper::instance().homePage());
417 if (d->m_forwardItems.empty())
419 d->m_backItems.push_back(nextItem);
420 nextItem = d->m_forwardItems.front();
421 d->m_forwardItems.erase(d->m_forwardItems.begin());
425 d->setSourceInternal(nextItem.url, &nextItem
.vscroll);
431 if (d->m_backItems.empty())
433 d->m_forwardItems.insert(d->m_forwardItems.begin(), previousItem);
434 previousItem = d->m_backItems.back();
435 d->m_backItems.pop_back();
439 d->setSourceInternal(previousItem.url, &previousItem
.vscroll);
444 if (event->type() == QEvent::Wheel) {
445 auto we =
static_cast<QWheelEvent *>(event);
446 if (we->modifiers() == Qt::ControlModifier) {
448 const int deltaY = we->angleDelta().y();
454 return QWidget::eventFilter(src, event);
460 const QString &scheme = url.scheme();
461 return scheme.isEmpty()
462 || scheme ==
"file"_L1
463 || scheme ==
"qrc"_L1
464 || scheme ==
"data"_L1
465 || scheme ==
"qthelp"_L1
466 || scheme ==
"about"_L1;
472 return !mimeFromUrl(QUrl::fromLocalFile(path)).isEmpty();
478 const QString &path = url.path();
479 const int index = path.lastIndexOf(u'.');
480 const QByteArray &ext = path.mid(index).toUtf8().toLower();
488 return "application/octet-stream"_L1;
494 if (isLocalUrl(url)) {
496 const QUrl &resolvedUrl = helpEngine.findFile(url);
497 if (!resolvedUrl.isValid())
500 const QString& path = resolvedUrl.toLocalFile();
501 if (!canOpenPage(path)) {
502 QTemporaryFile tmpTmpFile;
503 if (!tmpTmpFile.open())
506 const QString &extension = QFileInfo(path).completeSuffix();
507 QFile actualTmpFile(tmpTmpFile.fileName() %
"."_L1 % extension);
508 if (!actualTmpFile.open(QIODevice::ReadWrite | QIODevice::Truncate))
511 actualTmpFile.write(helpEngine.fileData(resolvedUrl));
512 actualTmpFile.close();
513 return QDesktopServices::openUrl(QUrl::fromLocalFile(actualTmpFile.fileName()));
517 return QDesktopServices::openUrl(url);
static HelpEngineWrapper & instance()
HistoryItem currentHistoryItem() const
std::vector< HistoryItem > m_backItems
std::vector< HistoryItem > m_forwardItems
void applyZoom(int percentage)
QLiteHtmlWidget * m_viewer
void incrementZoom(int steps)
void setSourceInternal(const QUrl &url, int *vscroll=nullptr, bool reload=false)
void setViewerFont(const QFont &font)
void setSource(const QUrl &url)
QString selectedText() const
bool isForwardAvailable() const
bool findText(const QString &text, FindFlags flags, bool incremental, bool fromSearch)
bool isBackwardAvailable() const
bool eventFilter(QObject *src, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
static void setPaletteFromApp(QWidget *widget)
static QByteArray addDarkThemeToSvg(const QByteArray &data)
static bool isDarkTheme()
static void setLight(QWidget *widget)
static QTextDocument::FindFlags textDocumentFlagsForFindFlags(HelpViewer::FindFlags flags)
static QByteArray getData(const QUrl &url, QWidget *widget)
const int kMaxHistoryItems