From 529a4d499b8dad35de6cd43e3004017ccfd288aa Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Tue, 9 Aug 2016 00:12:12 +0200 Subject: Start transition from QWebKit to QWebEngine This removes all references to WebKit if cmake option USE_WEBKIT is enabled. For the user manual it changes it to WebEngine (seems to work for me). Similar for the Facebook connection (minus a reference to a cookie jar). This I could not test at the moment, as I wrote this on a train. Printing does not work, it is a null operation at the moment. Currently, large parts of of the printing code are commented out as there is no direct way to access page elements in WebEngine. It seems this needs to be done via Javascript (with a callback invoked). There is new functionality in WebEngine to render a view to a PDF file but this needs more work (and probably some thoughts towards page breaks). Signed-off-by: Robert C. Helling --- CMakeLists.txt | 1 + cmake/Modules/HandleUserManual.cmake | 11 +++++- .../plugins/facebook/facebookconnectwidget.cpp | 17 +++++++- .../plugins/facebook/facebookconnectwidget.h | 8 ++++ desktop-widgets/printer.cpp | 40 ++++++++++++++++++- desktop-widgets/printer.h | 8 ++++ desktop-widgets/usermanual.cpp | 45 ++++++++++++++++++++++ desktop-widgets/usermanual.h | 36 ++++++++++++++++- 8 files changed, 159 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bbe5516c0..991eabaf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ option(NO_USERMANUAL "don't include a viewer for the user manual" OFF) option(FBSUPPORT "allow posting to Facebook" ON) option(BTSUPPORT "enable support for QtBluetooth (requires Qt5.4 or newer)" ON) option(FTDISUPPORT "enable support for libftdi based serial" OFF) +option(USE_WEBENGINE "Use QWebEngine instead of QWebKit" OFF) # Options regarding What should we build on subsurface option(MAKE_TESTS "Make the tests" ON) diff --git a/cmake/Modules/HandleUserManual.cmake b/cmake/Modules/HandleUserManual.cmake index 6e4874c60..a9354be2b 100644 --- a/cmake/Modules/HandleUserManual.cmake +++ b/cmake/Modules/HandleUserManual.cmake @@ -2,6 +2,13 @@ if(NO_USERMANUAL) message(STATUS "building without usermanual") add_definitions(-DNO_USERMANUAL) else() - list(APPEND QT_EXTRA_COMPONENTS WebKitWidgets) - list(APPEND QT_EXTRA_LIBRARIES Qt5::WebKitWidgets) + if(USE_WEBENGINE) + message(STATUS "building with QWebEngine") + list(APPEND QT_EXTRA_COMPONENTS WebEngineWidgets) + list(APPEND QT_EXTRA_LIBRARIES Qt5::WebEngineWidgets) + add_definitions(-DUSE_WEBENGINE) + else() + list(APPEND QT_EXTRA_COMPONENTS WebKitWidgets) + list(APPEND QT_EXTRA_LIBRARIES Qt5::WebKitWidgets) + endif() endif() diff --git a/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp b/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp index e0d2aab67..40d598229 100644 --- a/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp +++ b/desktop-widgets/plugins/facebook/facebookconnectwidget.cpp @@ -16,8 +16,11 @@ #include #include #include +#ifdef USE_WEBENGINE +#include +#else #include - +#endif #include "mainwindow.h" #include "profile-widget/profilewidget2.h" @@ -224,14 +227,22 @@ void FacebookManager::sendDive() FacebookConnectWidget::FacebookConnectWidget(QWidget *parent) : QDialog(parent), ui(new Ui::FacebookConnectWidget) { ui->setupUi(this); FacebookManager *fb = FacebookManager::instance(); +#ifdef USE_WEBENGINE + facebookWebView = new QWebEngineView(this); +#else facebookWebView = new QWebView(this); +#endif ui->fbWebviewContainer->layout()->addWidget(facebookWebView); if (fb->loggedIn()) { facebookLoggedIn(); } else { facebookDisconnect(); } +#ifdef USE_WEBENGINE + connect(facebookWebView, &QWebEngineView::urlChanged, fb, &FacebookManager::tryLogin); +#else connect(facebookWebView, &QWebView::urlChanged, fb, &FacebookManager::tryLogin); +#endif connect(fb, &FacebookManager::justLoggedIn, this, &FacebookConnectWidget::facebookLoggedIn); } @@ -250,7 +261,11 @@ void FacebookConnectWidget::facebookDisconnect() ui->fbWebviewContainer->setEnabled(true); ui->FBLabel->setText(tr("To connect to Facebook, please log in. This enables Subsurface to publish dives to your timeline")); if (facebookWebView) { +#ifdef USE_WEBENGINE + //FIX ME +#else facebookWebView->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar()); +#endif facebookWebView->setUrl(FacebookManager::instance()->connectUrl()); } } diff --git a/desktop-widgets/plugins/facebook/facebookconnectwidget.h b/desktop-widgets/plugins/facebook/facebookconnectwidget.h index e97097806..8fbf249e2 100644 --- a/desktop-widgets/plugins/facebook/facebookconnectwidget.h +++ b/desktop-widgets/plugins/facebook/facebookconnectwidget.h @@ -2,7 +2,11 @@ #define FACEBOOKCONNECTWIDGET_H #include +#ifdef USE_WEBENGINE +class QWebEngineView; +#else class QWebView; +#endif namespace Ui { class FacebookConnectWidget; class SocialnetworksDialog; @@ -41,7 +45,11 @@ public: void facebookDisconnect(); private: Ui::FacebookConnectWidget *ui; +#ifdef USE_WEBENGINE + QWebEngineView *facebookWebView; +#else QWebView *facebookWebView; +#endif }; class SocialNetworkDialog : public QDialog { diff --git a/desktop-widgets/printer.cpp b/desktop-widgets/printer.cpp index eea304347..bad1c2760 100644 --- a/desktop-widgets/printer.cpp +++ b/desktop-widgets/printer.cpp @@ -4,10 +4,14 @@ #include "core/helpers.h" #include -#include #include +#ifdef USE_WEBENGINE +#include +#else +#include #include #include +#endif #include "profile-widget/profilewidget2.h" Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, template_options *templateOptions, PrintMode printMode) @@ -18,7 +22,11 @@ Printer::Printer(QPaintDevice *paintDevice, print_options *printOptions, templat this->printMode = printMode; dpi = 0; done = 0; +#ifdef USE_WEBENGINE + webView = new QWebEngineView(); +#else webView = new QWebView(); +#endif } Printer::~Printer() @@ -61,6 +69,7 @@ void Printer::putProfileImage(QRect profilePlaceholder, QRect viewPort, QPainter void Printer::flowRender() { // add extra padding at the bottom to pages with height not divisible by view port +#ifndef USE_WEBENGINE int paddingBottom = pageSize.height() - (webView->page()->mainFrame()->contentsSize().height() % pageSize.height()); QString styleString = QString::fromUtf8("padding-bottom: ") + QString::number(paddingBottom) + "px;"; webView->page()->mainFrame()->findFirstElement("body").setAttribute("style", styleString); @@ -115,6 +124,9 @@ void Printer::flowRender() webView->page()->mainFrame()->render(&painter, QWebFrame::ContentsLayer, reigon); painter.end(); +#else + // FIX ME +#endif } void Printer::render(int Pages = 0) @@ -140,6 +152,9 @@ void Printer::render(int Pages = 0) painter.setRenderHint(QPainter::SmoothPixmapTransform); // get all refereces to diveprofile class in the Html template +#ifdef USE_WEBENGINE + //FIX ME +#else QWebElementCollection collection = webView->page()->mainFrame()->findAllElements(".diveprofile"); QSize originalSize = profile->size(); @@ -173,13 +188,18 @@ void Printer::render(int Pages = 0) static_cast(paintDevice)->newPage(); } painter.end(); +#endif // return profle settings profile->setFrameStyle(profileFrameStyle); profile->setPrintMode(false); profile->setFontPrintScale(fontScale); profile->setToolTipVisibile(true); +#ifdef USE_WEBENGINE + //FIXME +#else profile->resize(originalSize); +#endif prefs.animation_speed = animationOriginal; //replot the dive after returning the settings @@ -210,8 +230,12 @@ void Printer::print() //rendering resolution = selected paper size in inchs * printer dpi pageSize.setHeight(qCeil(printerPtr->pageRect(QPrinter::Inch).height() * dpi)); pageSize.setWidth(qCeil(printerPtr->pageRect(QPrinter::Inch).width() * dpi)); +#ifdef USE_WEBENGINE + //FIXME +#else webView->page()->setViewportSize(pageSize); webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); +#endif // export border width with at least 1 pixel templateOptions->border_width = std::max(1, pageSize.width() / 1000); if (printOptions->type == print_options::DIVELIST) { @@ -229,11 +253,15 @@ void Printer::print() // get number of dives per page from data-numberofdives attribute in the body of the selected template bool ok; +#ifdef USE_WEBENGINE + // FIX ME +#else divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); if (!ok) { divesPerPage = 1; // print each dive in a single page if the attribute is missing or malformed //TODO: show warning } +#endif int Pages; if (divesPerPage == 0) { flowRender(); @@ -250,7 +278,11 @@ void Printer::previewOnePage() pageSize.setHeight(paintDevice->height()); pageSize.setWidth(paintDevice->width()); +#ifdef USE_WEBENGINE + //FIXME +#else webView->page()->setViewportSize(pageSize); +#endif // initialize the border settings templateOptions->border_width = std::max(1, pageSize.width() / 1000); if (printOptions->type == print_options::DIVELIST) { @@ -258,7 +290,10 @@ void Printer::previewOnePage() } else if (printOptions->type == print_options::STATISTICS ) { webView->setHtml(t.generateStatistics()); } - +#ifdef USE_WEBENGINE + // FIX ME + render(1); +#else bool ok; int divesPerPage = webView->page()->mainFrame()->findFirstElement("body").attribute("data-numberofdives").toInt(&ok); if (!ok) { @@ -270,5 +305,6 @@ void Printer::previewOnePage() } else { render(1); } +#endif } } diff --git a/desktop-widgets/printer.h b/desktop-widgets/printer.h index e5f16d77d..fc867bb3c 100644 --- a/desktop-widgets/printer.h +++ b/desktop-widgets/printer.h @@ -2,7 +2,11 @@ #define PRINTER_H #include +#ifdef USE_WEBENGINE +#include +#else #include +#endif #include #include @@ -20,7 +24,11 @@ public: private: QPaintDevice *paintDevice; +#ifdef USE_WEBENGINE + QWebEngineView *webView; +#else QWebView *webView; +#endif print_options *printOptions; template_options *templateOptions; QSize pageSize; diff --git a/desktop-widgets/usermanual.cpp b/desktop-widgets/usermanual.cpp index 690307961..8e75b432f 100644 --- a/desktop-widgets/usermanual.cpp +++ b/desktop-widgets/usermanual.cpp @@ -34,6 +34,27 @@ void SearchBar::enableButtons(const QString &s) 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) : QWidget(parent) { QShortcut *closeKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this); @@ -54,12 +75,20 @@ UserManual::UserManual(QWidget *parent) : QWidget(parent) 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); +#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)); +#ifndef USE_WEBENGINE userManual->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks); +#endif QString searchPath = getSubsurfaceDataPath("Documentation"); if (searchPath.size()) { // look for localized versions of the manual first @@ -84,7 +113,9 @@ UserManual::UserManual(QWidget *parent) : QWidget(parent) 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())); @@ -96,6 +127,13 @@ UserManual::UserManual(QWidget *parent) : QWidget(parent) setLayout(vboxLayout); } +#ifdef USE_WEBENGINE +void UserManual::search(QString text, QWebEnginePage::FindFlags flags = 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 = 0) { if (userManual->findText(text, QWebPage::FindWrapsAroundDocument | flags) || text.length() == 0) { @@ -104,6 +142,7 @@ void UserManual::search(QString text, QWebPage::FindFlags flags = 0) searchBar->setStyleSheet("QLineEdit{background: red;}"); } } +#endif void UserManual::searchTextChanged(const QString& text) { @@ -118,13 +157,19 @@ void UserManual::searchNext() 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) { diff --git a/desktop-widgets/usermanual.h b/desktop-widgets/usermanual.h index 5101a3c3b..04ae15605 100644 --- a/desktop-widgets/usermanual.h +++ b/desktop-widgets/usermanual.h @@ -1,8 +1,12 @@ #ifndef USERMANUAL_H #define USERMANUAL_H +#ifdef USE_WEBENGINE +#include +#include +#else #include - +#endif #include "ui_searchbar.h" class SearchBar : public QWidget{ @@ -21,6 +25,27 @@ private: Ui::SearchBar ui; }; +#ifdef USE_WEBENGINE +class MyQWebEnginePage : public QWebEnginePage +{ + Q_OBJECT + +public: + MyQWebEnginePage(QObject* parent = 0); + bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool); +}; + +class MyQWebEngineView : public QWebEngineView +{ + Q_OBJECT + +public: + MyQWebEngineView(QWidget* parent = 0); + MyQWebEnginePage* page() const; +}; +#endif + + class UserManual : public QWidget { Q_OBJECT @@ -40,11 +65,18 @@ slots: void searchTextChanged(const QString& s); void searchNext(); void searchPrev(); +#ifndef USE_WEBENGINE void linkClickedSlot(const QUrl& url); +#endif private: - QWebView *userManual; SearchBar *searchBar; QString mLastText; +#ifdef USE_WEBENGINE + QWebEngineView *userManual; + void search(QString, QWebEnginePage::FindFlags); +#else + QWebView *userManual; void search(QString, QWebPage::FindFlags); +#endif }; #endif // USERMANUAL_H -- cgit v1.2.3-70-g09d2