Robert C. Helling e7977ee280 desktop: re-add WebEngine support
This reverts commit 643f4a5726 and finishes the task of adding WebEngine as an
alternate backend to be used in printing.

The ultimate goal is to be able to build without QtWebKit (as that is no longer
supported in Qt6). WebKit was used in two places: The user manual and printing.
This patch makes printing work with WebEngine.

The main obstacle is that WebEngine no longer allows accessing HTML elements
from C++ code and rendering the page to a QPainter.

The old version used this to figure out dimensions and page breaks for the
pages and then in the QPainter placed the profile images. With WebEninge, you
need to access the elements using JavaScript which is now used to place the
profile in the html proplerly as an <img> tag. To this end, both html and
profile images are written to a temporary directory on disk.

This image replacement by JavaScript is only necessary to make old templates
still work. It could be replaced by actually putting <img> tags in the
templates (but this would break user edited templates).

In my experiments, the page breaking was done great by html/css, so the
additional magic seems superflous.

What remains to be done:
* remove empty page at the end of printout
* make preview great again (in particlar needed for template editing)

Note: since QtWebEngine currently cannot be built with our toolchain on
Windows, this patch keeps QtWebKit support around by making the QtWebEngine
compile-time conditional via #ifdefs.

[Dirk Hohndel: merged a few commits to make this more logical - the resulting
commit is fairly big, but IMHO preferrable]

Signed-off-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2021-11-26 12:32:10 -08:00

198 lines
6.0 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include <QDesktopServices>
#include <QShortcut>
#include <QFile>
#include "desktop-widgets/usermanual.h"
#include "desktop-widgets/mainwindow.h"
#include "core/qthelper.h"
SearchBar::SearchBar(QWidget *parent): QWidget(parent)
{
ui.setupUi(this);
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
ui.findNext->setIcon(QIcon(":go-down-icon"));
ui.findPrev->setIcon(QIcon(":go-up-icon"));
ui.findClose->setIcon(QIcon(":window-close-icon"));
#endif
connect(ui.findNext, SIGNAL(pressed()), this, SIGNAL(searchNext()));
connect(ui.findPrev, SIGNAL(pressed()), this, SIGNAL(searchPrev()));
connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchTextChanged(QString)));
connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButtons(QString)));
connect(ui.findClose, SIGNAL(pressed()), this, SLOT(hide()));
}
void SearchBar::setVisible(bool visible)
{
QWidget::setVisible(visible);
ui.searchEdit->setFocus();
}
void SearchBar::enableButtons(const QString &s)
{
ui.findPrev->setEnabled(s.length());
ui.findNext->setEnabled(s.length());
}
#ifdef USE_WEBENGINE
MyQWebEnginePage::MyQWebEnginePage(QObject* parent) : QWebEnginePage(parent)
{
}
bool MyQWebEnginePage::acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
QDesktopServices::openUrl(url);
return false;
}
return true;
}
MyQWebEngineView::MyQWebEngineView(QWidget* parent)
{
}
#endif
UserManual::UserManual(QWidget *parent) : QDialog(parent)
{
QShortcut *closeKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
connect(closeKey, SIGNAL(activated()), this, SLOT(close()));
QShortcut *quitKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(quitKey, SIGNAL(activated()), qApp, SLOT(quit()));
QAction *actionShowSearch = new QAction(this);
actionShowSearch->setShortcut(Qt::CTRL + Qt::Key_F);
actionShowSearch->setShortcutContext(Qt::WindowShortcut);
addAction(actionShowSearch);
QAction *actionHideSearch = new QAction(this);
actionHideSearch->setShortcut(Qt::Key_Escape);
actionHideSearch->setShortcutContext(Qt::WindowShortcut);
addAction(actionHideSearch);
setWindowTitle(tr("User manual"));
setWindowIcon(QIcon(":subsurface-icon"));
#ifdef USE_WEBENGINE
userManual = new MyQWebEngineView(this);
MyQWebEnginePage *page = new MyQWebEnginePage();
userManual->setPage(page);
#else
userManual = new QWebView(this);
userManual->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
#endif
QString colorBack = palette().highlight().color().name(QColor::HexRgb);
QString colorText = palette().highlightedText().color().name(QColor::HexRgb);
userManual->setStyleSheet(QString("QWebView { selection-background-color: %1; selection-color: %2; }")
.arg(colorBack).arg(colorText));
QString searchPath = getSubsurfaceDataPath("Documentation");
if (searchPath.size()) {
// look for localized versions of the manual first
QString lang = getUiLanguage();
QString prefix = searchPath.append("/user-manual");
QFile manual(prefix + "_" + lang + ".html");
if (!manual.exists())
manual.setFileName(prefix + "_" + lang.left(2) + ".html");
if (!manual.exists())
manual.setFileName(prefix + ".html");
if (!manual.exists()) {
userManual->setHtml(tr("Cannot find the Subsurface manual"));
} else {
QString urlString = QString("file:///") + manual.fileName();
userManual->setUrl(QUrl(urlString, QUrl::TolerantMode));
}
} else {
userManual->setHtml(tr("Cannot find the Subsurface manual"));
}
searchBar = new SearchBar(this);
searchBar->hide();
connect(actionShowSearch, SIGNAL(triggered(bool)), searchBar, SLOT(show()));
connect(actionHideSearch, SIGNAL(triggered(bool)), searchBar, SLOT(hide()));
#ifndef USE_WEBENGINE
connect(userManual, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
#endif
connect(searchBar, SIGNAL(searchTextChanged(QString)), this, SLOT(searchTextChanged(QString)));
connect(searchBar, SIGNAL(searchNext()), this, SLOT(searchNext()));
connect(searchBar, SIGNAL(searchPrev()), this, SLOT(searchPrev()));
QVBoxLayout *vboxLayout = new QVBoxLayout();
userManual->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
vboxLayout->addWidget(userManual);
vboxLayout->addWidget(searchBar);
setLayout(vboxLayout);
}
#ifdef USE_WEBENGINE
void UserManual::search(QString text, QWebEnginePage::FindFlags flags = QFlag(0))
{
userManual->findText(text, flags,
[this, text](bool found) {searchBar->setStyleSheet(found || text.length() == 0 ? "" : "QLineEdit{background: red;}");});
}
#else
void UserManual::search(QString text, QWebPage::FindFlags flags = QFlag(0))
{
if (userManual->findText(text, QWebPage::FindWrapsAroundDocument | flags) || text.length() == 0) {
searchBar->setStyleSheet("");
} else {
searchBar->setStyleSheet("QLineEdit{background: red;}");
}
}
#endif
void UserManual::searchTextChanged(const QString& text)
{
mLastText = text;
search(text);
}
void UserManual::searchNext()
{
search(mLastText);
}
void UserManual::searchPrev()
{
#ifdef USE_WEBENGINE
search(mLastText, QWebEnginePage::FindBackward);
#else
search(mLastText, QWebPage::FindBackward);
#endif
}
#ifndef USE_WEBENGINE
void UserManual::linkClickedSlot(const QUrl& url)
{
QDesktopServices::openUrl(url);
}
#endif
#ifdef Q_OS_MAC
void UserManual::showEvent(QShowEvent *e)
{
MainWindow *m = MainWindow::instance();
filterAction = m->findChild<QAction *>(QLatin1String("actionFilterTags"), Qt::FindDirectChildrenOnly);
if (filterAction != nullptr)
filterAction->setShortcut(QKeySequence());
closeAction = m->findChild<QAction *>(QLatin1String("actionClose"), Qt::FindDirectChildrenOnly);
if (closeAction != nullptr)
closeAction->setShortcut(QKeySequence());
}
void UserManual::hideEvent(QHideEvent *e)
{
if (closeAction != NULL)
closeAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
if (filterAction != NULL)
filterAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));
closeAction = filterAction = NULL;
}
#endif