From 954c1983608faa5123094c4569bc23f6746cd75d Mon Sep 17 00:00:00 2001 From: Fredrik Johansson Date: Sun, 27 Jan 2019 14:58:24 +0100 Subject: [PATCH] Add viewsource for QWebKit --- src/Mod/Web/Gui/BrowserView.cpp | 51 +++++++++++++++++++++++++++------ src/Mod/Web/Gui/BrowserView.h | 4 +-- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Mod/Web/Gui/BrowserView.cpp b/src/Mod/Web/Gui/BrowserView.cpp index 3ac1b30db5..ef938ab3e3 100644 --- a/src/Mod/Web/Gui/BrowserView.cpp +++ b/src/Mod/Web/Gui/BrowserView.cpp @@ -90,7 +90,12 @@ using namespace Gui; namespace WebGui { enum WebAction { - OpenLink = 0xff + OpenLink = 0xff, +#ifdef QTWEBENGINE + ViewSource = QWebEnginePage::ViewSource +#else + ViewSource = 200 // QWebView doesn't have a ViewSource option +#endif }; #ifdef QTWEBENGINE @@ -267,17 +272,17 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) } #if QT_VERSION >= 0x050800 && defined(QTWEBENGINE) else { // for view source - // QWebEngine caches standardContextMenu, guard so we only add signlmapper once + // QWebEngine caches standardContextMenu, guard so we only add signalmapper once static bool firstRun = true; if (firstRun) { firstRun = false; QMenu *menu = page()->createStandardContextMenu(); QList actions = menu->actions(); for(QAction *ac : actions) { - if (ac->data().toInt() == QWEBPAGE::ViewSource) { + if (ac->data().toInt() == WebAction::ViewSource) { QSignalMapper* signalMapper = new QSignalMapper (this); signalMapper->setProperty("url", QVariant(r.linkUrl())); - signalMapper->setMapping(ac, QWEBPAGE::ViewSource); + signalMapper->setMapping(ac, WebAction::ViewSource); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(triggerContextMenuAction(int))); connect (ac, SIGNAL(triggered()), signalMapper, SLOT(map())); @@ -285,6 +290,19 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) } } } +#else + else { + QMenu *menu = page()->createStandardContextMenu(); + QAction *ac = menu->addAction(tr("View source")); + ac->setData(WebAction::ViewSource); + QSignalMapper* signalMapper = new QSignalMapper (this); + signalMapper->setProperty("url", QVariant(r.linkUrl())); + signalMapper->setMapping(ac, WebAction::ViewSource); + connect(signalMapper, SIGNAL(mapped(int)), + this, SLOT(triggerContextMenuAction(int))); + connect (ac, SIGNAL(triggered()), signalMapper, SLOT(map())); + menu->exec(event->globalPos()); + } #endif QWEBVIEW::contextMenuEvent(event); } @@ -301,11 +319,9 @@ void WebView::triggerContextMenuAction(int id) case QWEBPAGE::OpenLinkInNewWindow: openLinkInNewWindow(url); break; -#ifdef QTWEBENGINE - case QWEBPAGE::ViewSource: + case WebAction::ViewSource: Q_EMIT viewSource(url); break; -#endif default: break; } @@ -372,10 +388,10 @@ BrowserView::BrowserView(QWidget* parent) this, SLOT(onDownloadRequested(QWebEngineDownloadItem*))); connect(view->page(), SIGNAL(iconChanged(const QIcon &)), this, SLOT(setWindowIcon(const QIcon &))); - connect(view, SIGNAL(viewSource(const QUrl&)), - this, SLOT(onViewSource(const QUrl&))); #endif + connect(view, SIGNAL(viewSource(const QUrl&)), + this, SLOT(onViewSource(const QUrl&))); connect(view, SIGNAL(loadStarted()), this, SLOT(onLoadStarted())); connect(view, SIGNAL(loadProgress(int)), @@ -522,6 +538,23 @@ void BrowserView::onUnsupportedContent(QNetworkReply* reply) // then fails to load. Thus, we reload the previous url. view->reload(); } + +void BrowserView::onViewSource(const QUrl &url) +{ + Q_UNUSED(url); + if (!view->page() || !view->page()->currentFrame()) + return; + QString pageSource = view->page()->currentFrame()->toHtml(); + QPlainTextEdit *editorWidget = new QPlainTextEdit {}; + App::TextDocument *txtDoc = new App::TextDocument; + TextDocumentEditorView *textDocView = new TextDocumentEditorView { + txtDoc, + editorWidget, getMainWindow() + }; + editorWidget->setReadOnly(true); + editorWidget->setPlainText(pageSource); + getMainWindow()->addWindow(textDocView); +} #endif void BrowserView::load(const char* URL) diff --git a/src/Mod/Web/Gui/BrowserView.h b/src/Mod/Web/Gui/BrowserView.h index 871c5c0743..95147f5e47 100644 --- a/src/Mod/Web/Gui/BrowserView.h +++ b/src/Mod/Web/Gui/BrowserView.h @@ -71,9 +71,7 @@ private Q_SLOTS: Q_SIGNALS: void openLinkInExternalBrowser(const QUrl&); void openLinkInNewWindow(const QUrl&); -#ifdef QTWEBENGINE void viewSource(const QUrl&); -#endif }; /** @@ -113,12 +111,12 @@ protected Q_SLOTS: void onDownloadRequested(QWebEngineDownloadItem *request); void setWindowIcon(const QIcon &icon); void urlFilter(const QUrl &url); - void onViewSource(const QUrl &url); #else void onDownloadRequested(const QNetworkRequest& request); void onUnsupportedContent(QNetworkReply* reply); void onLinkClicked (const QUrl& url); #endif + void onViewSource(const QUrl &url); void onOpenLinkInExternalBrowser(const QUrl& url); void onOpenLinkInNewWindow(const QUrl&);