[rkward/work/qtwebengine] rkward: Compiles with QWebEngine and basics are working (no in-depth testing done, so far)
Thomas Friedrichsmeier
null at kde.org
Mon Mar 30 16:35:59 BST 2020
Git commit 654b8ceaa44d4abe528ae9b62247f1048b128f94 by Thomas Friedrichsmeier.
Committed on 30/03/2020 at 15:35.
Pushed by tfry into branch 'work/qtwebengine'.
Compiles with QWebEngine and basics are working (no in-depth testing done, so far)
M +6 -1 rkward/misc/rkfindbar.cpp
M +1 -0 rkward/misc/rkfindbar.h
M +161 -74 rkward/windows/rkhtmlwindow.cpp
M +6 -41 rkward/windows/rkhtmlwindow.h
https://commits.kde.org/rkward/654b8ceaa44d4abe528ae9b62247f1048b128f94
diff --git a/rkward/misc/rkfindbar.cpp b/rkward/misc/rkfindbar.cpp
index 190bd870..976c23d5 100644
--- a/rkward/misc/rkfindbar.cpp
+++ b/rkward/misc/rkfindbar.cpp
@@ -138,7 +138,12 @@ void RKFindBar::doSearch (bool backward) {
bool found = false;
QString term = term_edit->currentText ();
findRequest (term, backward, this, &found);
- if (!(found || term.isEmpty ())) term_edit->lineEdit ()->setPalette (nomatch_palette);
+ if (!(found || term.isEmpty ())) indicateSearchFail();
+}
+
+void RKFindBar::indicateSearchFail () {
+ RK_TRACE (APP);
+ term_edit->lineEdit ()->setPalette (nomatch_palette);
}
void RKFindBar::activate () {
diff --git a/rkward/misc/rkfindbar.h b/rkward/misc/rkfindbar.h
index 7348e297..70c41765 100644
--- a/rkward/misc/rkfindbar.h
+++ b/rkward/misc/rkfindbar.h
@@ -45,6 +45,7 @@ However, these can't be inserted into anything other than QToolBar or QMenu... *
QCheckBox* getOption (const FindOptions option);
bool isOptionSet (const FindOptions option) const;
+ void indicateSearchFail ();
public slots:
void activate ();
void activateWithFindAsYouType ();
diff --git a/rkward/windows/rkhtmlwindow.cpp b/rkward/windows/rkhtmlwindow.cpp
index 7709696d..d44c3bda 100644
--- a/rkward/windows/rkhtmlwindow.cpp
+++ b/rkward/windows/rkhtmlwindow.cpp
@@ -40,6 +40,7 @@
#include <QGuiApplication>
#include <QIcon>
#include <QMimeDatabase>
+#include <QCheckBox>
#include "../rkglobals.h"
#include "../rbackend/rkrinterface.h"
@@ -66,79 +67,147 @@
#ifdef NO_QT_WEBENGINE
# include <QWebFrame>
# include <QNetworkRequest>
+# include <kwebpage.h>
# include <kwebview.h>
+class RKWebPage : public KWebPage {
+#else
+# include <QWebEnginePage>
+# include <QWebEngineView>
+# include <QWebEngineSettings>
+# include <QWebEngineProfile>
+class RKWebPage : public QWebEnginePage {
+#endif
+ Q_OBJECT
+public:
+#ifdef NO_QT_WEBENGINE
// NOTE: According to an earlier note at this place, KIOIntegration used to be very buggy around KF5 5.9.0. It seem to just work,
// at 5.44.0, and the symptoms are probably not terrible for earlier versions, so we use it here (allows us to render help:/-pages
// inside the help window.
-RKWebPage::RKWebPage (RKHTMLWindow* window): KWebPage (window, KPartsIntegration | KIOIntegration) {
+ explicit RKWebPage (RKHTMLWindow* window): KWebPage (window, KPartsIntegration | KIOIntegration) {
#else
-RKWebPage::RKWebPage (RKHTMLWindow* window): QWebEnginePage (window) {
+ explicit RKWebPage (RKHTMLWindow* window): QWebEnginePage (window) {
#endif
- RK_TRACE (APP);
- RKWebPage::window = window;
- new_window = false;
- direct_load = false;
- settings ()->setFontFamily (QWebSettings::StandardFont, QFontDatabase::systemFont(QFontDatabase::GeneralFont).family ());
- settings ()->setFontFamily (QWebSettings::FixedFont, QFontDatabase::systemFont(QFontDatabase::FixedFont).family ());
-}
-
+ RK_TRACE (APP);
+ RKWebPage::window = window;
+ new_window = false;
+ direct_load = false;
#ifdef NO_QT_WEBENGINE
-bool RKWebPage::acceptNavigationRequest (QWebFrame* frame, const QNetworkRequest& request, QWebPage::NavigationType type) {
- QUrl navurl = request.url ();
- QUrl cururl (mainFrame ()->url ());
- bool is_main_frame = frame == mainFrame ();
+ settings ()->setFontFamily (QWebSettings::StandardFont, QFontDatabase::systemFont(QFontDatabase::GeneralFont).family ());
+ settings ()->setFontFamily (QWebSettings::FixedFont, QFontDatabase::systemFont(QFontDatabase::FixedFont).family ());
#else
-bool RKWebPage::acceptNavigationRequest (const QUrl &navurl, QWebEnginePage::NavigationType type, bool is_main_frame) override {
- QUrl cururl (url ());
+ settings ()->setFontFamily (QWebEngineSettings::StandardFont, QFontDatabase::systemFont(QFontDatabase::GeneralFont).family ());
+ settings ()->setFontFamily (QWebEngineSettings::FixedFont, QFontDatabase::systemFont(QFontDatabase::FixedFont).family ());
#endif
- Q_UNUSED (type);
+ }
- RK_TRACE (APP);
- RK_DEBUG (APP, DL_DEBUG, "Navigation request to %s", qPrintable (navurl.toString ()));
- if (direct_load && (is_main_frame)) {
- direct_load = false;
- return true;
+ void load (const QUrl& url) {
+ RK_TRACE (APP);
+ direct_load = true;
+#ifdef NO_QT_WEBENGINE
+ mainFrame ()->load (url);
+#else
+ QWebEnginePage::load (url);
+#endif
}
- if (new_window) {
- new_window = false;
- RKWorkplace::mainWorkplace ()->openAnyUrl (navurl);
+#ifdef NO_QT_WEBENGINE
+ QUrl url () {
+ return mainFrame ()->url ();
+ }
+ void setHTML (const QString &html) {
+ mainFrame ()->setHTML (html);
+ }
+ QPointF scroll_position () const {
+ return mainFrame ()->scrollPosition();
+ }
+#else
+ bool supportsContentType (const QString &name) {
+ if (name.startsWith("text")) return true;
+#warning TODO
return false;
}
-
- if (!is_main_frame) {
- if (request.url ().isLocalFile () && supportsContentType(QMimeDatabase ().mimeTypeForUrl (request.url ()).name ())) return true;
+ void downloadUrl (const QUrl& url) {
+ download (url);
}
-
- if (cururl.matches (navurl, QUrl::NormalizePathSegments | QUrl::StripTrailingSlash)) {
- RK_DEBUG (APP, DL_DEBUG, "Page internal navigation request from %s to %s", qPrintable (cururl.toString ()), qPrintable (navurl.toString ()));
- emit (pageInternalNavigation (navurl));
- return true;
+ void setScrollPosition (const QPoint &point) {
+ runJavaScript (QString ("window.scrollTo(%1, %2);").arg (point.x ()).arg(point.y ()));
}
+#endif
- window->openURL (navurl);
- return false;
-}
+signals:
+ void pageInternalNavigation (const QUrl& url);
+protected:
+#ifdef NO_QT_WEBENGINE
+/** reimplemented to always emit linkClicked() for pages that need special handling (importantly, rkward://-urls). */
+ bool acceptNavigationRequest (QWebFrame* frame, const QNetworkRequest& request, QWebPage::NavigationType type) override {
+ QUrl navurl = request.url ();
+ QUrl cururl (mainFrame ()->url ());
+ bool is_main_frame = frame == mainFrame ();
+#else
+ bool acceptNavigationRequest (const QUrl &navurl, QWebEnginePage::NavigationType type, bool is_main_frame) override {
+ QUrl cururl (url ());
+#endif
+ Q_UNUSED (type);
+
+ RK_TRACE (APP);
+ RK_DEBUG (APP, DL_DEBUG, "Navigation request to %s", qPrintable (navurl.toString ()));
+ if (direct_load && (is_main_frame)) {
+ direct_load = false;
+ return true;
+ }
+
+ if (new_window) {
+ new_window = false;
+ RKWorkplace::mainWorkplace ()->openAnyUrl (navurl);
+ return false;
+ }
+
+ if (!is_main_frame) {
+ if (navurl.isLocalFile () && supportsContentType(QMimeDatabase ().mimeTypeForUrl (navurl).name ())) return true;
+ }
+
+ if (cururl.matches (navurl, QUrl::NormalizePathSegments | QUrl::StripTrailingSlash)) {
+ RK_DEBUG (APP, DL_DEBUG, "Page internal navigation request from %s to %s", qPrintable (cururl.toString ()), qPrintable (navurl.toString ()));
+ emit (pageInternalNavigation (navurl));
+ return true;
+ }
+
+ window->openURL (navurl);
+ return false;
+ }
-void RKWebPage::load (const QUrl& url) {
- RK_TRACE (APP);
- direct_load = true;
#ifdef NO_QT_WEBENGINE
- mainFrame ()->load (url);
+/** reimplemented to schedule new window creation for the next page to load */
+ QWebPage* createWindow (QWebPage::WebWindowType) override {
#else
- load (url);
+ QWebEnginePage* createWindow (QWebEnginePage::WebWindowType) {
#endif
-}
+ RK_TRACE (APP);
+ new_window = true; // Don't actually create the window, until we know which URL we're talking about.
+ return (this);
+ }
+
+private:
+ RKHTMLWindow *window;
+ bool new_window;
+ bool direct_load;
+};
#ifdef NO_QT_WEBENGINE
-QWebPage* RKWebPage::createWindow (QWebPage::WebWindowType) {
+class RKWebView : public KWebView {
+public:
+ RKWebView (QWidget *parent) : KWebView (parent, false) {};
#else
-QWebEnginePage* RKWebPage::createWindow (QWebEnginePage::WebWindowType) {
+class RKWebView : public QWebEngineView {
+public:
+ RKWebView (QWidget *parent) : QWebEngineView (parent) {};
+ void print (QPrinter *printer) {
+ if (!page ()) return;
+ page ()->print (printer, [](bool){});
+ };
#endif
- RK_TRACE (APP);
- new_window = true; // Don't actually create the window, until we know which URL we're talking about.
- return (this);
-}
+};
+
RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (parent, RKMDIWindow::HelpWindow) {
RK_TRACE (APP);
@@ -147,12 +216,17 @@ RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (par
QVBoxLayout* layout = new QVBoxLayout (this);
layout->setContentsMargins (0, 0, 0, 0);
- view = new KWebView (this, false);
+ view = new RKWebView (this);
page = new RKWebPage (this);
view->setPage (page);
view->setContextMenuPolicy (Qt::CustomContextMenu);
layout->addWidget (view, 1);
+#ifdef NO_QT_WEBENGINE
findbar = new RKFindBar (this);
+#else
+ findbar = new RKFindBar (this, true);
+ findbar->setPrimaryOptions (QList<QWidget*>() << findbar->getOption (RKFindBar::FindAsYouType) << findbar->getOption (RKFindBar::MatchCase));
+#endif
layout->addWidget (findbar);
findbar->hide ();
connect (findbar, &RKFindBar::findRequest, this, &RKHTMLWindow::findRequest);
@@ -168,8 +242,12 @@ RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (par
// We have to connect this in order to allow browsing.
connect (page, &RKWebPage::pageInternalNavigation, this, &RKHTMLWindow::internalNavigation);
- connect (page, &QWebPage::downloadRequested, this, &RKHTMLWindow::saveRequested); --> webengine: override triggerAction virtual
- connect (page, &QWebPage::printRequested, this, &RKHTMLWindow::slotPrint);
+#ifdef NO_QT_WEBENGINE
+ connect (page, &QWebPage::downloadRequested, [page](const QNetworkRequest &request) { page->downloadUrl (request.url ()); });
+#else
+ connect (page->profile (), &QWebEngineProfile::downloadRequested, [this](QWebEngineDownloadItem* item) { page->downloadUrl (item->url ()); });
+#endif
+ connect (page, &RKWebPage::printRequested, this, &RKHTMLWindow::slotPrint);
connect (view, &QWidget::customContextMenuRequested, this, &RKHTMLWindow::makeContextMenu);
current_history_position = -1;
@@ -179,7 +257,7 @@ RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (par
useMode (mode);
// needed to enable / disable the run selection action
- connect (view, &KWebView::selectionChanged, this, &RKHTMLWindow::selectionChanged);
+ connect (view, &RKWebView::selectionChanged, this, &RKHTMLWindow::selectionChanged);
selectionChanged ();
}
@@ -229,6 +307,7 @@ void RKHTMLWindow::runSelection () {
void RKHTMLWindow::findRequest (const QString& text, bool backwards, const RKFindBar* findbar, bool* found) {
RK_TRACE (APP);
+#ifdef QT_NO_WEBENGINE
QWebPage::FindFlags flags = QWebPage::FindWrapsAroundDocument;
if (backwards) flags |= QWebPage::FindBackward;
bool highlight = findbar->isOptionSet (RKFindBar::HighlightAll);
@@ -240,8 +319,17 @@ void RKHTMLWindow::findRequest (const QString& text, bool backwards, const RKFin
*found = page->findText (text, flags);
have_highlight = found && highlight;
+#else
+ // QWebEngine does not offer highlight all
+ *found = true;
+ QWebEnginePage::FindFlags flags;
+ if (backwards) flags |= QWebEnginePage::FindBackward;
+ if (findbar->isOptionSet (RKFindBar::MatchCase)) flags |= QWebEnginePage::FindCaseSensitively;
+ page->findText (text, flags, [this](bool found) { if (!found) this->findbar->indicateSearchFail(); });
+#endif
}
+
void RKHTMLWindow::slotPrint () {
RK_TRACE (APP);
@@ -257,13 +345,7 @@ void RKHTMLWindow::slotPrint () {
void RKHTMLWindow::slotSave () {
RK_TRACE (APP);
- page->downloadUrl (page->mainFrame ()->url ());
-}
-
-void RKHTMLWindow::saveRequested (const QNetworkRequest& request) {
- RK_TRACE (APP);
-
- page->downloadUrl (request.url ());
+ page->downloadUrl (page->url ());
}
void RKHTMLWindow::openLocationFromHistory (VisitedLocation &loc) {
@@ -406,7 +488,7 @@ bool RKHTMLWindow::openURL (const QUrl &url) {
RK_DEBUG (APP, DL_WARNING, "Applying workaround for https://bugs.kde.org/show_bug.cgi?id=405386");
QFile f (url.toLocalFile ());
f.open (QIODevice::ReadOnly);
- page->mainFrame ()->setHtml (f.readAll());
+ page->setHtml (f.readAll());
f.close ();
} else {
page->load (url);
@@ -538,7 +620,11 @@ void RKHTMLWindow::scrollToBottom () {
RK_TRACE (APP);
RK_ASSERT (window_mode == HTMLOutputWindow);
- view->page ()->mainFrame ()->setScrollBarValue (Qt::Vertical, view->page ()->mainFrame ()->scrollBarMaximum (Qt::Vertical));
+#ifdef NO_QT_WEBENGINE
+ page->mainFrame ()->setScrollBarValue (Qt::Vertical, view->page ()->mainFrame ()->scrollBarMaximum (Qt::Vertical));
+#else
+ page->runJavaScript(QString("{ let se = (document.scrollingElement || document.body); se.scrollTop = se.scrollHeight; }"));
+#endif
}
void RKHTMLWindow::zoomIn () {
@@ -568,8 +654,8 @@ void RKHTMLWindow::useMode (WindowMode new_mode) {
setWindowIcon (RKStandardIcons::getIcon (RKStandardIcons::WindowOutput));
part->setOutputWindowSkin ();
setMetaInfo (i18n ("Output Window"), QUrl ("rkward://page/rkward_output"), RKSettings::PageOutput);
- connect (page, &QWebPage::loadFinished, this, &RKHTMLWindow::scrollToBottom);
- page->action (QWebPage::Reload)->setText (i18n ("&Refresh Output"));
+ connect (page, &RKWebPage::loadFinished, this, &RKHTMLWindow::scrollToBottom);
+ page->action (RKWebPage::Reload)->setText (i18n ("&Refresh Output"));
// TODO: This would be an interesting extension, but how to deal with concurrent edits?
// page->setContentEditable (true);
@@ -579,7 +665,7 @@ void RKHTMLWindow::useMode (WindowMode new_mode) {
type = RKMDIWindow::HelpWindow | RKMDIWindow::DocumentWindow;
setWindowIcon (RKStandardIcons::getIcon (RKStandardIcons::WindowHelp));
part->setHelpWindowSkin ();
- disconnect (page, &QWebPage::loadFinished, this, &RKHTMLWindow::scrollToBottom);
+ disconnect (page, &RKWebPage::loadFinished, this, &RKHTMLWindow::scrollToBottom);
}
updateCaption (current_url);
@@ -625,8 +711,8 @@ void RKHTMLWindow::flushOutput () {
void RKHTMLWindow::saveBrowserState (VisitedLocation* state) {
RK_TRACE (APP);
- if (view && view->page () && view->page ()->mainFrame ()) {
- state->scroll_position = view->page ()->mainFrame ()->scrollPosition ();
+ if (page) {
+ state->scroll_position = page->scrollPosition ();
} else {
state->scroll_position = QPoint ();
}
@@ -636,8 +722,8 @@ void RKHTMLWindow::restoreBrowserState (VisitedLocation* state) {
RK_TRACE (APP);
if (state->scroll_position.isNull ()) return;
- RK_ASSERT (view && view->page () && view->page ()->mainFrame ());
- view->page ()->mainFrame ()->setScrollPosition (state->scroll_position);
+ RK_ASSERT (page);
+ page->setScrollPosition (state->scroll_position.toPoint ());
}
RKHTMLWindowPart::RKHTMLWindowPart (RKHTMLWindow* window) : KParts::Part (window) {
@@ -651,18 +737,18 @@ void RKHTMLWindowPart::initActions () {
RK_TRACE (APP);
// We keep our own history.
- window->page->action (QWebPage::Back)->setVisible (false);
- window->page->action (QWebPage::Forward)->setVisible (false);
+ window->page->action (RKWebPage::Back)->setVisible (false);
+ window->page->action (RKWebPage::Forward)->setVisible (false);
// For now we won't bother with this one: Does not behave well, in particular (but not only) WRT to rkward://-links
- window->page->action (QWebPage::DownloadLinkToDisk)->setVisible (false);
+ window->page->action (RKWebPage::DownloadLinkToDisk)->setVisible (false);
// common actions
- actionCollection ()->addAction (KStandardAction::Copy, "copy", window->view->pageAction (QWebPage::Copy), SLOT (trigger()));
+ actionCollection ()->addAction (KStandardAction::Copy, "copy", window->view->pageAction (RKWebPage::Copy), SLOT (trigger()));
QAction* zoom_in = actionCollection ()->addAction ("zoom_in", new QAction (QIcon::fromTheme("zoom-in"), i18n ("Zoom In"), this));
connect (zoom_in, &QAction::triggered, window, &RKHTMLWindow::zoomIn);
QAction* zoom_out = actionCollection ()->addAction ("zoom_out", new QAction (QIcon::fromTheme("zoom-out"), i18n ("Zoom Out"), this));
connect (zoom_out, &QAction::triggered, window, &RKHTMLWindow::zoomOut);
- actionCollection ()->addAction (KStandardAction::SelectAll, "select_all", window->view->pageAction (QWebPage::SelectAll), SLOT (trigger()));
+ actionCollection ()->addAction (KStandardAction::SelectAll, "select_all", window->view->pageAction (RKWebPage::SelectAll), SLOT (trigger()));
// unfortunately, this will only affect the default encoding, not necessarily the "real" encoding
KCodecAction *encoding = new KCodecAction (QIcon::fromTheme("character-set"), i18n ("Default &Encoding"), this, true);
encoding->setStatusTip (i18n ("Set the encoding to assume in case no explicit encoding has been set in the page or in the HTTP headers."));
@@ -686,7 +772,7 @@ void RKHTMLWindowPart::initActions () {
outputFlush->setText (i18n ("&Flush Output"));
outputFlush->setIcon (QIcon::fromTheme("edit-delete"));
- outputRefresh = actionCollection ()->addAction ("output_refresh", window->page->action(QWebPage::Reload));
+ outputRefresh = actionCollection ()->addAction ("output_refresh", window->page->action (RKWebPage::Reload));
actionCollection ()->addAction (KStandardAction::Find, "find", window->findbar, SLOT (activate()));
QAction* findAhead = actionCollection ()->addAction ("find_ahead", new QAction (i18n ("Find as you type"), this));
@@ -1190,3 +1276,4 @@ void RKOutputWindowManager::windowDestroyed (QObject *window) {
}
}
+#include "rkhtmlwindow.moc"
diff --git a/rkward/windows/rkhtmlwindow.h b/rkward/windows/rkhtmlwindow.h
index de6d20a5..b701940c 100644
--- a/rkward/windows/rkhtmlwindow.h
+++ b/rkward/windows/rkhtmlwindow.h
@@ -2,7 +2,7 @@
rkhtmlwindow - description
-------------------
begin : Wed Oct 12 2005
- copyright : (C) 2005-2017 by Thomas Friedrichsmeier
+ copyright : (C) 2005-2020 by Thomas Friedrichsmeier
email : thomas.friedrichsmeier at kdemail.net
***************************************************************************/
@@ -23,6 +23,7 @@
#include <kio/jobclasses.h>
#include <QDomElement>
+#include <QNetworkRequest>
#include "../windows/rkmdiwindow.h"
@@ -35,39 +36,8 @@ class RKHTMLWindowPart;
class QTemporaryFile;
class RKHTMLWindow;
class RKFindBar;
-
-#ifdef NO_QT_WEBENGINE
-# include <kwebpage.h>
-class KWebView;
-class RKWebPage : public KWebPage {
-#else
-# include <QWebEnginePage>
-class QWebEngineView;
-class RKWebPage : public QWebEnginePage {
-#endif
- Q_OBJECT
-public:
- explicit RKWebPage (RKHTMLWindow* window);
- void load (const QUrl& url);
-signals:
- void pageInternalNavigation (const QUrl& url);
-protected:
-#ifdef NO_QT_WEBENGINE
-/** reimplemented to always emit linkClicked() for pages that need special handling (importantly, rkward://-urls). */
- bool acceptNavigationRequest (QWebFrame* frame, const QNetworkRequest& request, NavigationType type) override;
-/** reimplemented to schedule new window creation for the next page to load */
- QWebPage* createWindow (WebWindowType type) override;
-#else
-/** reimplemented to always emit linkClicked() for pages that need special handling (importantly, rkward://-urls). */
- bool acceptNavigationRequest (const QUrl &url, NavigationType type, bool isMainFrame) override;
-/** reimplemented to schedule new window creation for the next page to load */
- QWebEnginePage* createWindow (WebWindowType type) override;
-#endif
-private:
- RKHTMLWindow *window;
- bool new_window;
- bool direct_load;
-};
+class RKWebPage;
+class RKWebView;
/**
\brief Show html files.
@@ -111,7 +81,6 @@ public:
public slots:
void slotPrint ();
void slotSave ();
- void saveRequested (const QNetworkRequest& request);
void slotForward ();
void slotBack ();
void selectionChanged ();
@@ -133,11 +102,7 @@ private slots:
void findRequest (const QString& text, bool backwards, const RKFindBar *findbar, bool* found);
private:
friend class RKHTMLWindowPart;
-#ifdef NO_QT_WEBENGINE
- KWebView* view;
-#else
- QWebEngineView* view;
-#endif
+ RKWebView* view;
RKWebPage* page;
RKFindBar* findbar;
bool have_highlight;
@@ -150,7 +115,7 @@ friend class RKHTMLWindowPart;
struct VisitedLocation {
QUrl url;
- QPoint scroll_position;
+ QPointF scroll_position;
};
QList<VisitedLocation> url_history;
void openLocationFromHistory (VisitedLocation &loc);
More information about the rkward-tracker
mailing list