Commit 368a718d authored by Eike Ziller's avatar Eike Ziller

Add QtWebEngine backed help viewer backend

Supports most features. Defined priority for help viewers:
"qtwebkit" > "qtwebengine" > "textbrowser".

Task-number: QTCREATORBUG-13910
Change-Id: I59b4231ef93fdf9df46436850ed3743b5916f9cd
Reviewed-by: default avatarNikita Baryshnikov <nib952051@gmail.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent e7fe1ea4
......@@ -63,6 +63,13 @@ FORMS += docsettingspage.ui \
generalsettingspage.ui \
remotehelpfilter.ui
!isEmpty(QT.webenginewidgets.name):minQtVersion(5, 6, 0) {
QT += webenginewidgets
HEADERS += webenginehelpviewer.h
SOURCES += webenginehelpviewer.cpp
DEFINES += QTC_WEBENGINE_HELPVIEWER
}
osx {
DEFINES += QTC_MAC_NATIVE_HELPVIEWER
QT += macextras
......
......@@ -47,6 +47,9 @@
#ifdef QTC_MAC_NATIVE_HELPVIEWER
#include "macwebkithelpviewer.h"
#endif
#ifdef QTC_WEBENGINE_HELPVIEWER
#include "webenginehelpviewer.h"
#endif
#include <bookmarkmanager.h>
#include <contentwindow.h>
......@@ -69,6 +72,7 @@
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/find/findplugin.h>
#include <texteditor/texteditorconstants.h>
#include <utils/algorithm.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <utils/styledbar.h>
......@@ -362,48 +366,43 @@ HelpViewer *HelpPlugin::createHelpViewer(qreal zoom)
{
// check for backends
typedef std::function<HelpViewer *()> ViewerFactory;
QHash<QString, ViewerFactory> factories; // id -> factory
typedef QPair<QByteArray, ViewerFactory> ViewerFactoryItem; // id -> factory
QVector<ViewerFactoryItem> factories;
#ifndef QT_NO_WEBKIT
factories.append(qMakePair(QByteArray("qtwebkit"), []() { return new QtWebKitHelpViewer(); }));
#endif
#ifdef QTC_WEBENGINE_HELPVIEWER
factories.append(qMakePair(QByteArray("qtwebengine"), []() { return new WebEngineHelpViewer(); }));
#endif
factories.append(qMakePair(QByteArray("textbrowser"), []() { return new TextBrowserHelpViewer(); }));
#ifdef QTC_MAC_NATIVE_HELPVIEWER
factories.insert(QLatin1String("native"), []() { return new MacWebKitHelpViewer(); });
// default setting
#ifdef QTC_MAC_NATIVE_HELPVIEWER_DEFAULT
factories.prepend(qMakePair(QByteArray("native"), []() { return new MacWebKitHelpViewer(); }));
#else
factories.append(qMakePair(QByteArray("native"), []() { return new MacWebKitHelpViewer(); }));
#endif
#ifndef QT_NO_WEBKIT
factories.insert(QLatin1String("qtwebkit"), []() { return new QtWebKitHelpViewer(); });
#endif
factories.insert(QLatin1String("textbrowser"), []() { return new TextBrowserHelpViewer(); });
ViewerFactory factory;
// TODO: Visual Studio < 2013 has a bug in std::function's operator bool, which in this case
// leads to succeeding boolean checks on factory which should not succeed.
// So we may not check against "if (!factory)"
bool factoryFound = false;
HelpViewer *viewer = nullptr;
// check requested backend
const QString backend = QLatin1String(qgetenv("QTC_HELPVIEWER_BACKEND"));
const QByteArray backend = qgetenv("QTC_HELPVIEWER_BACKEND");
if (!backend.isEmpty()) {
if (!factories.contains(backend)) {
qWarning("Help viewer backend \"%s\" not found, using default.", qPrintable(backend));
const int pos = Utils::indexOf(factories, [backend](const ViewerFactoryItem &item) {
return backend == item.first;
});
if (pos == -1) {
qWarning("Help viewer backend \"%s\" not found, using default.", backend.constData());
} else {
factory = factories.value(backend);
factoryFound = true;
viewer = factories.at(pos).second();
}
}
// default setting
#ifdef QTC_MAC_NATIVE_HELPVIEWER_DEFAULT
if (!factoryFound && factories.contains(QLatin1String("native"))) {
factory = factories.value(QLatin1String("native"));
factoryFound = true;
}
#endif
if (!factoryFound && factories.contains(QLatin1String("qtwebkit"))) {
factory = factories.value(QLatin1String("qtwebkit"));
factoryFound = true;
}
if (!factoryFound && factories.contains(QLatin1String("textbrowser"))) {
factory = factories.value(QLatin1String("textbrowser"));
factoryFound = true;
}
QTC_ASSERT(factoryFound, return 0);
HelpViewer *viewer = factory();
if (!viewer)
viewer = factories.first().second();
QTC_ASSERT(viewer, return nullptr);
// initialize font
viewer->setViewerFont(LocalHelpManager::fallbackFont());
......
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "webenginehelpviewer.h"
#include "localhelpmanager.h"
#include "openpagesmanager.h"
#include <utils/qtcassert.h>
#include <QBuffer>
#include <QContextMenuEvent>
#include <QCoreApplication>
#include <QVBoxLayout>
#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;
}
WebEngineHelpViewer::WebEngineHelpViewer(QWidget *parent) :
HelpViewer(parent),
m_widget(new WebView(this))
{
m_widget->setPage(new HelpPage(this));
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);
connect(m_widget, &QWebEngineView::loadStarted, this, &WebEngineHelpViewer::slotLoadStarted);
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);
action->setText(QCoreApplication::translate("HelpViewer",
"Open Link as New Page"));
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());
}
QFont WebEngineHelpViewer::viewerFont() const
{
QWebEngineSettings *webSettings = m_widget->settings();
return QFont(webSettings->fontFamily(QWebEngineSettings::StandardFont),
webSettings->fontSize(QWebEngineSettings::DefaultFontSize));
}
void WebEngineHelpViewer::setViewerFont(const QFont &font)
{
QWebEngineSettings *webSettings = m_widget->settings();
webSettings->setFontFamily(QWebEngineSettings::StandardFont, font.family());
webSettings->setFontSize(QWebEngineSettings::DefaultFontSize, font.pointSize());
}
qreal WebEngineHelpViewer::scale() const
{
return m_widget->zoomFactor();
}
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);
}
}
}
void WebEngineHelpViewer::setOpenInNewPageActionVisible(bool visible)
{
m_widget->setOpenInNewPageActionVisible(visible);
}
bool WebEngineHelpViewer::findText(const QString &text, Core::FindFlags flags, bool incremental,
bool fromSearch, bool *wrapped)
{
Q_UNUSED(incremental)
Q_UNUSED(fromSearch)
if (wrapped)
*wrapped = false; // missing feature in QWebEngine
QWebEnginePage::FindFlags webEngineFlags = 0;
if (flags & Core::FindBackward)
webEngineFlags |= QWebEnginePage::FindBackward;
if (flags & Core::FindCaseSensitively)
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;
}
HelpPage *WebEngineHelpViewer::page() const
{
return static_cast<HelpPage *>(m_widget->page());
}
void WebEngineHelpViewer::scaleUp()
{
m_widget->setZoomFactor(m_widget->zoomFactor() + 0.1);
}
void WebEngineHelpViewer::scaleDown()
{
m_widget->setZoomFactor(qMax(qreal(0.1), m_widget->zoomFactor() - qreal(0.1)));
}
void WebEngineHelpViewer::resetScale()
{
m_widget->setZoomFactor(1.0);
}
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)
}
HelpPage::HelpPage(QObject *parent)
: QWebEnginePage(parent)
{
}
QWebEnginePage *HelpPage::createWindow(QWebEnginePage::WebWindowType)
{
auto viewer = static_cast<WebEngineHelpViewer *>(OpenPagesManager::instance().createPage());
return viewer->page();
}
WebView::WebView(QWidget *parent)
: QWebEngineView(parent)
{
}
void WebView::setOpenInNewPageActionVisible(bool visible)
{
m_openInNewPageActionVisible = visible;
}
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = page()->createStandardContextMenu();
if (m_openInNewPageActionVisible) {
// insert Open In New Tab 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);
menu->insertAction(before, page()->action(QWebEnginePage::OpenLinkInNewTab));
}
}
connect(menu, &QMenu::aboutToHide, menu, &QObject::deleteLater);
menu->popup(event->globalPos());
}
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "helpviewer.h"
#include <QWebEngineUrlSchemeHandler>
#include <QWebEngineView>
namespace Help {
namespace Internal {
class HelpUrlSchemeHandler : public QWebEngineUrlSchemeHandler
{
public:
explicit HelpUrlSchemeHandler(QObject *parent = 0);
void requestStarted(QWebEngineUrlRequestJob *job) override;
};
class HelpPage : public QWebEnginePage
{
public:
explicit HelpPage(QObject *parent = 0);
QWebEnginePage *createWindow(QWebEnginePage::WebWindowType) override;
};
class WebView : public QWebEngineView
{
public:
explicit WebView(QWidget *parent = 0);
void setOpenInNewPageActionVisible(bool visible);
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
private:
bool m_openInNewPageActionVisible = true;
};
class WebEngineHelpViewer : public HelpViewer
{
Q_OBJECT
public:
explicit WebEngineHelpViewer(QWidget *parent = 0);
QFont viewerFont() const override;
void setViewerFont(const QFont &font) override;
qreal scale() const override;
void setScale(qreal scale) override;
QString title() const override;
QUrl source() const override;
void setSource(const QUrl &url) override;
void setHtml(const QString &html) override;
QString selectedText() const override;
bool isForwardAvailable() const override;
bool isBackwardAvailable() const override;
void addBackHistoryItems(QMenu *backMenu) override;
void addForwardHistoryItems(QMenu *forwardMenu) override;
void setOpenInNewPageActionVisible(bool visible) override;
bool findText(const QString &text, Core::FindFlags flags, bool incremental, bool fromSearch, bool *wrapped) override;
HelpPage *page() const;
public slots:
void scaleUp() override;
void scaleDown() override;
void resetScale() override;
void copy() override;
void stop() override;
void forward() override;
void backward() override;
void print(QPrinter *printer) override;
private:
WebView *m_widget;
};
} // namespace Internal
} // namespace Help
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment