2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-03-04 20:29:38 +03:00
|
|
|
|
|
|
|
|
#include "webenginehelpviewer.h"
|
|
|
|
|
|
2016-09-09 15:37:01 +02:00
|
|
|
#include "helpconstants.h"
|
2022-07-14 16:25:16 +02:00
|
|
|
#include "helptr.h"
|
2016-03-04 20:29:38 +03:00
|
|
|
#include "localhelpmanager.h"
|
|
|
|
|
#include "openpagesmanager.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
|
#include <QContextMenuEvent>
|
|
|
|
|
#include <QCoreApplication>
|
2019-09-13 15:18:02 +02:00
|
|
|
#include <QDesktopServices>
|
2018-01-29 10:22:47 +01:00
|
|
|
#include <QTimer>
|
2016-03-04 20:29:38 +03:00
|
|
|
#include <QVBoxLayout>
|
2021-08-11 15:37:13 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
|
#include <QWebEngineContextMenuRequest>
|
|
|
|
|
#else
|
2016-09-09 15:37:01 +02:00
|
|
|
#include <QWebEngineContextMenuData>
|
2021-08-11 15:37:13 +02:00
|
|
|
#endif
|
2016-03-04 20:29:38 +03:00
|
|
|
#include <QWebEngineHistory>
|
|
|
|
|
#include <QWebEngineProfile>
|
|
|
|
|
#include <QWebEngineSettings>
|
|
|
|
|
#include <QWebEngineUrlRequestJob>
|
|
|
|
|
|
|
|
|
|
using namespace Help;
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
|
|
|
|
HelpUrlSchemeHandler::HelpUrlSchemeHandler(QObject *parent) :
|
|
|
|
|
QWebEngineUrlSchemeHandler(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
|
|
|
|
|
{
|
|
|
|
|
const QUrl url = job->requestUrl();
|
|
|
|
|
if (!HelpViewer::isLocalUrl(url)) {
|
|
|
|
|
job->fail(QWebEngineUrlRequestJob::RequestDenied);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LocalHelpManager::HelpData data = LocalHelpManager::helpData(url);
|
|
|
|
|
|
|
|
|
|
auto buffer = new QBuffer(job);
|
|
|
|
|
buffer->setData(data.data);
|
|
|
|
|
job->reply(data.mimeType.toUtf8(), buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HelpUrlSchemeHandler *helpUrlSchemeHandler()
|
|
|
|
|
{
|
|
|
|
|
static HelpUrlSchemeHandler *schemeHandler = nullptr;
|
|
|
|
|
if (!schemeHandler)
|
|
|
|
|
schemeHandler = new HelpUrlSchemeHandler(LocalHelpManager::instance());
|
|
|
|
|
return schemeHandler;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 15:18:02 +02:00
|
|
|
HelpUrlRequestInterceptor::HelpUrlRequestInterceptor(QObject *parent)
|
|
|
|
|
: QWebEngineUrlRequestInterceptor(parent)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void HelpUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
|
|
|
|
|
{
|
|
|
|
|
if (!HelpViewer::isLocalUrl(info.requestUrl())
|
|
|
|
|
&& info.navigationType() != QWebEngineUrlRequestInfo::NavigationTypeLink) {
|
|
|
|
|
info.block(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static HelpUrlRequestInterceptor *helpurlRequestInterceptor()
|
|
|
|
|
{
|
|
|
|
|
static HelpUrlRequestInterceptor *interceptor = nullptr;
|
|
|
|
|
if (!interceptor)
|
|
|
|
|
interceptor = new HelpUrlRequestInterceptor(LocalHelpManager::instance());
|
|
|
|
|
return interceptor;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 20:29:38 +03:00
|
|
|
WebEngineHelpViewer::WebEngineHelpViewer(QWidget *parent) :
|
|
|
|
|
HelpViewer(parent),
|
|
|
|
|
m_widget(new WebView(this))
|
|
|
|
|
{
|
2019-09-13 15:18:02 +02:00
|
|
|
// some of these should already be that way by default, but better be sure
|
|
|
|
|
QWebEngineSettings *settings = m_widget->settings();
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::XSSAuditingEnabled, true);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::PluginsEnabled, false);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, false);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::AllowGeolocationOnInsecureOrigins, false);
|
|
|
|
|
settings->setAttribute(QWebEngineSettings::AllowWindowActivationFromJavaScript, false);
|
|
|
|
|
|
2016-03-31 11:49:00 +02:00
|
|
|
m_widget->setPage(new WebEngineHelpPage(this));
|
2016-03-04 20:29:38 +03:00
|
|
|
auto layout = new QVBoxLayout;
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
layout->addWidget(m_widget, 10);
|
|
|
|
|
|
|
|
|
|
QPalette p = palette();
|
|
|
|
|
p.setColor(QPalette::Base, Qt::white);
|
|
|
|
|
p.setColor(QPalette::Text, Qt::black);
|
|
|
|
|
setPalette(p);
|
|
|
|
|
|
|
|
|
|
connect(m_widget, &QWebEngineView::urlChanged, this, &WebEngineHelpViewer::sourceChanged);
|
2018-01-29 10:22:47 +01:00
|
|
|
connect(m_widget, &QWebEngineView::loadStarted, this, [this] {
|
|
|
|
|
slotLoadStarted();
|
|
|
|
|
// Work around QTBUG-65223: if only anchor changed, we never get a loadFinished signal
|
|
|
|
|
// If a link is clicked in a page, it can happen that the new URL has not yet been set,
|
|
|
|
|
// so we need to delay a bit...
|
|
|
|
|
QTimer::singleShot(/*magic timeout=*/150, this, [this] {
|
|
|
|
|
QUrl urlWithoutFragment = source();
|
|
|
|
|
urlWithoutFragment.setFragment(QString());
|
|
|
|
|
if (urlWithoutFragment == m_previousUrlWithoutFragment)
|
|
|
|
|
slotLoadFinished();
|
|
|
|
|
m_previousUrlWithoutFragment = urlWithoutFragment;
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-03-04 20:29:38 +03:00
|
|
|
connect(m_widget, &QWebEngineView::loadFinished, this, &WebEngineHelpViewer::slotLoadFinished);
|
|
|
|
|
connect(m_widget, &QWebEngineView::titleChanged, this, &WebEngineHelpViewer::titleChanged);
|
|
|
|
|
connect(m_widget->page(), &QWebEnginePage::linkHovered, this, &WebEngineHelpViewer::setToolTip);
|
|
|
|
|
connect(m_widget->pageAction(QWebEnginePage::Back), &QAction::changed, this, [this]() {
|
|
|
|
|
emit backwardAvailable(isBackwardAvailable());
|
|
|
|
|
});
|
|
|
|
|
connect(m_widget->pageAction(QWebEnginePage::Forward), &QAction::changed, this, [this]() {
|
|
|
|
|
emit forwardAvailable(isForwardAvailable());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
QAction* action = m_widget->pageAction(QWebEnginePage::OpenLinkInNewTab);
|
2022-07-11 13:30:24 +02:00
|
|
|
action->setText(Tr::tr(Constants::TR_OPEN_LINK_AS_NEW_PAGE));
|
2016-03-04 20:29:38 +03:00
|
|
|
|
|
|
|
|
QWebEnginePage *viewPage = m_widget->page();
|
|
|
|
|
QTC_ASSERT(viewPage, return);
|
|
|
|
|
QWebEngineProfile *viewProfile = viewPage->profile();
|
|
|
|
|
QTC_ASSERT(viewProfile, return);
|
|
|
|
|
if (!viewProfile->urlSchemeHandler("qthelp"))
|
|
|
|
|
viewProfile->installUrlSchemeHandler("qthelp", helpUrlSchemeHandler());
|
2019-09-13 15:18:02 +02:00
|
|
|
viewProfile->setUrlRequestInterceptor(helpurlRequestInterceptor());
|
2016-03-04 20:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::setViewerFont(const QFont &font)
|
|
|
|
|
{
|
|
|
|
|
QWebEngineSettings *webSettings = m_widget->settings();
|
|
|
|
|
webSettings->setFontFamily(QWebEngineSettings::StandardFont, font.family());
|
|
|
|
|
webSettings->setFontSize(QWebEngineSettings::DefaultFontSize, font.pointSize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::setScale(qreal scale)
|
|
|
|
|
{
|
|
|
|
|
m_widget->setZoomFactor(scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString WebEngineHelpViewer::title() const
|
|
|
|
|
{
|
|
|
|
|
return m_widget->title();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QUrl WebEngineHelpViewer::source() const
|
|
|
|
|
{
|
|
|
|
|
return m_widget->url();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::setSource(const QUrl &url)
|
|
|
|
|
{
|
|
|
|
|
m_widget->setUrl(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::setHtml(const QString &html)
|
|
|
|
|
{
|
|
|
|
|
m_widget->setHtml(html);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString WebEngineHelpViewer::selectedText() const
|
|
|
|
|
{
|
|
|
|
|
return m_widget->selectedText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebEngineHelpViewer::isForwardAvailable() const
|
|
|
|
|
{
|
|
|
|
|
// m_view->history()->canGoForward()
|
|
|
|
|
return m_widget->pageAction(QWebEnginePage::Forward)->isEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebEngineHelpViewer::isBackwardAvailable() const
|
|
|
|
|
{
|
|
|
|
|
return m_widget->pageAction(QWebEnginePage::Back)->isEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::addBackHistoryItems(QMenu *backMenu)
|
|
|
|
|
{
|
|
|
|
|
if (QWebEngineHistory *history = m_widget->history()) {
|
|
|
|
|
QList<QWebEngineHistoryItem> items = history->backItems(history->count());
|
|
|
|
|
for (int i = items.count() - 1; i >= 0; --i) {
|
|
|
|
|
QWebEngineHistoryItem item = items.at(i);
|
|
|
|
|
auto action = new QAction(backMenu);
|
|
|
|
|
action->setText(item.title());
|
|
|
|
|
connect(action, &QAction::triggered, this, [this,item]() {
|
|
|
|
|
if (QWebEngineHistory *history = m_widget->history())
|
|
|
|
|
history->goToItem(item);
|
|
|
|
|
});
|
|
|
|
|
backMenu->addAction(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::addForwardHistoryItems(QMenu *forwardMenu)
|
|
|
|
|
{
|
|
|
|
|
if (QWebEngineHistory *history = m_widget->history()) {
|
|
|
|
|
QList<QWebEngineHistoryItem> items = history->forwardItems(history->count());
|
|
|
|
|
for (int i = 0; i < items.count(); ++i) {
|
|
|
|
|
QWebEngineHistoryItem item = items.at(i);
|
|
|
|
|
auto action = new QAction(forwardMenu);
|
|
|
|
|
action->setText(item.title());
|
|
|
|
|
connect(action, &QAction::triggered, this, [this,item]() {
|
|
|
|
|
if (QWebEngineHistory *history = m_widget->history())
|
|
|
|
|
history->goToItem(item);
|
|
|
|
|
});
|
|
|
|
|
forwardMenu->addAction(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 10:21:49 +02:00
|
|
|
bool WebEngineHelpViewer::findText(const QString &text, Utils::FindFlags flags, bool incremental,
|
2016-03-04 20:29:38 +03:00
|
|
|
bool fromSearch, bool *wrapped)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(incremental)
|
|
|
|
|
Q_UNUSED(fromSearch)
|
|
|
|
|
if (wrapped)
|
|
|
|
|
*wrapped = false; // missing feature in QWebEngine
|
2020-01-21 10:06:04 +01:00
|
|
|
QWebEnginePage::FindFlags webEngineFlags;
|
2023-06-06 10:21:49 +02:00
|
|
|
if (flags & Utils::FindBackward)
|
2016-03-04 20:29:38 +03:00
|
|
|
webEngineFlags |= QWebEnginePage::FindBackward;
|
2023-06-06 10:21:49 +02:00
|
|
|
if (flags & Utils::FindCaseSensitively)
|
2016-03-04 20:29:38 +03:00
|
|
|
webEngineFlags |= QWebEnginePage::FindCaseSensitively;
|
|
|
|
|
// QWebEngineView's findText is asynchronous, and the variant taking a callback runs the
|
|
|
|
|
// callback on the main thread, so blocking here becomes ugly too
|
|
|
|
|
// So we just claim that the search succeeded
|
|
|
|
|
m_widget->findText(text, webEngineFlags);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 11:49:00 +02:00
|
|
|
WebEngineHelpPage *WebEngineHelpViewer::page() const
|
2016-03-04 20:29:38 +03:00
|
|
|
{
|
2016-03-31 11:49:00 +02:00
|
|
|
return static_cast<WebEngineHelpPage *>(m_widget->page());
|
2016-03-04 20:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::copy()
|
|
|
|
|
{
|
|
|
|
|
m_widget->triggerPageAction(QWebEnginePage::Copy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::stop()
|
|
|
|
|
{
|
|
|
|
|
m_widget->triggerPageAction(QWebEnginePage::Stop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::forward()
|
|
|
|
|
{
|
|
|
|
|
m_widget->triggerPageAction(QWebEnginePage::Forward);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::backward()
|
|
|
|
|
{
|
|
|
|
|
m_widget->triggerPageAction(QWebEnginePage::Back);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebEngineHelpViewer::print(QPrinter *printer)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(printer)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 11:49:00 +02:00
|
|
|
WebEngineHelpPage::WebEngineHelpPage(QObject *parent)
|
2016-03-04 20:29:38 +03:00
|
|
|
: QWebEnginePage(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 15:18:02 +02:00
|
|
|
bool WebEngineHelpPage::acceptNavigationRequest(const QUrl &url,
|
|
|
|
|
QWebEnginePage::NavigationType type,
|
|
|
|
|
bool isMainFrame)
|
2016-03-04 20:29:38 +03:00
|
|
|
{
|
2019-09-13 15:18:02 +02:00
|
|
|
Q_UNUSED(type)
|
|
|
|
|
Q_UNUSED(isMainFrame)
|
|
|
|
|
if (HelpViewer::isLocalUrl(url))
|
|
|
|
|
return true;
|
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
|
return false;
|
2016-03-04 20:29:38 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-13 15:18:02 +02:00
|
|
|
WebView::WebView(WebEngineHelpViewer *viewer)
|
|
|
|
|
: QWebEngineView(viewer)
|
|
|
|
|
, m_viewer(viewer)
|
|
|
|
|
{}
|
|
|
|
|
|
2019-02-15 15:30:52 +01:00
|
|
|
bool WebView::event(QEvent *ev)
|
|
|
|
|
{
|
|
|
|
|
// work around QTBUG-43602
|
|
|
|
|
if (ev->type() == QEvent::ChildAdded) {
|
|
|
|
|
auto ce = static_cast<QChildEvent *>(ev);
|
|
|
|
|
ce->child()->installEventFilter(this);
|
|
|
|
|
} else if (ev->type() == QEvent::ChildRemoved) {
|
|
|
|
|
auto ce = static_cast<QChildEvent *>(ev);
|
|
|
|
|
ce->child()->removeEventFilter(this);
|
|
|
|
|
}
|
|
|
|
|
return QWebEngineView::event(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebView::eventFilter(QObject *src, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(src)
|
|
|
|
|
// work around QTBUG-43602
|
|
|
|
|
if (m_viewer->isScrollWheelZoomingEnabled() && e->type() == QEvent::Wheel) {
|
|
|
|
|
auto we = static_cast<QWheelEvent *>(e);
|
|
|
|
|
if (we->modifiers() == Qt::ControlModifier)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 20:29:38 +03:00
|
|
|
void WebView::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
|
{
|
2021-08-11 15:37:13 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
|
QMenu *menu = createStandardContextMenu();
|
|
|
|
|
#else
|
2016-03-04 20:29:38 +03:00
|
|
|
QMenu *menu = page()->createStandardContextMenu();
|
2021-08-11 15:37:13 +02:00
|
|
|
#endif
|
2016-09-09 15:37:01 +02:00
|
|
|
// insert Open as New Page etc if OpenLinkInThisWindow is also there
|
|
|
|
|
const QList<QAction*> actions = menu->actions();
|
|
|
|
|
auto it = std::find(actions.cbegin(), actions.cend(),
|
|
|
|
|
page()->action(QWebEnginePage::OpenLinkInThisWindow));
|
|
|
|
|
if (it != actions.cend()) {
|
|
|
|
|
// insert after
|
|
|
|
|
++it;
|
|
|
|
|
QAction *before = (it == actions.cend() ? 0 : *it);
|
2021-08-11 15:37:13 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
|
QUrl url = lastContextMenuRequest()->linkUrl();
|
|
|
|
|
#else
|
2016-09-09 15:37:01 +02:00
|
|
|
QUrl url = page()->contextMenuData().linkUrl();
|
2021-08-11 15:37:13 +02:00
|
|
|
#endif
|
2016-09-09 15:37:01 +02:00
|
|
|
if (m_viewer->isActionVisible(HelpViewer::Action::NewPage)) {
|
2022-07-11 13:30:24 +02:00
|
|
|
auto openLink = new QAction(Tr::tr(Constants::TR_OPEN_LINK_AS_NEW_PAGE), menu);
|
2016-09-09 15:37:01 +02:00
|
|
|
connect(openLink, &QAction::triggered, m_viewer, [this, url] {
|
|
|
|
|
m_viewer->newPageRequested(url);
|
|
|
|
|
});
|
|
|
|
|
menu->insertAction(before, openLink);
|
|
|
|
|
}
|
|
|
|
|
if (m_viewer->isActionVisible(HelpViewer::Action::ExternalWindow)) {
|
2022-07-11 13:30:24 +02:00
|
|
|
auto openLink = new QAction(Tr::tr(Constants::TR_OPEN_LINK_IN_WINDOW), menu);
|
2016-09-09 15:37:01 +02:00
|
|
|
connect(openLink, &QAction::triggered, m_viewer, [this, url] {
|
|
|
|
|
m_viewer->externalPageRequested(url);
|
|
|
|
|
});
|
|
|
|
|
menu->insertAction(before, openLink);
|
2016-03-04 20:29:38 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connect(menu, &QMenu::aboutToHide, menu, &QObject::deleteLater);
|
|
|
|
|
menu->popup(event->globalPos());
|
|
|
|
|
}
|