From 1a110c3641afeff39b65496b5dadd9046e765c68 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 26 Dec 2016 17:23:52 +0100 Subject: [PATCH] WebHitTestResult: Use data from QWebEngineContextMenuData Fixes handling context menus in iframes. Closes #2126 --- src/lib/popupwindow/popupwebview.cpp | 3 +-- src/lib/webengine/webhittestresult.cpp | 29 ++++++++++++++++++++++++++ src/lib/webengine/webhittestresult.h | 4 ++++ src/lib/webengine/webview.cpp | 5 +++-- src/lib/webengine/webview.h | 2 +- src/lib/webtab/tabbedwebview.cpp | 3 +-- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/lib/popupwindow/popupwebview.cpp b/src/lib/popupwindow/popupwebview.cpp index 4efd8f02a..a87f5ad96 100644 --- a/src/lib/popupwindow/popupwebview.cpp +++ b/src/lib/popupwindow/popupwebview.cpp @@ -92,8 +92,7 @@ void PopupWebView::_contextMenuEvent(QContextMenuEvent *event) { m_menu->clear(); - const WebHitTestResult hitTest = page()->hitTestContent(event->pos()); - + WebHitTestResult hitTest = page()->hitTestContent(event->pos()); createContextMenu(m_menu, hitTest); if (WebInspector::isEnabled()) { diff --git a/src/lib/webengine/webhittestresult.cpp b/src/lib/webengine/webhittestresult.cpp index f3403016a..0dcba3e29 100644 --- a/src/lib/webengine/webhittestresult.cpp +++ b/src/lib/webengine/webhittestresult.cpp @@ -18,6 +18,8 @@ #include "webhittestresult.h" #include "webpage.h" +#include + WebHitTestResult::WebHitTestResult(const WebPage *page, const QPoint &pos) : m_isNull(true) , m_isContentEditable(false) @@ -87,6 +89,32 @@ WebHitTestResult::WebHitTestResult(const WebPage *page, const QPoint &pos) init(p->url(), p->execJavaScript(js, WebPage::SafeJsWorld).toMap()); } +void WebHitTestResult::updateWithContextMenuData(const QWebEngineContextMenuData &data) +{ + if (!data.isValid()) { + return; + } + + m_linkTitle = data.linkText(); + m_linkUrl = data.linkUrl(); + m_isContentEditable = data.isContentEditable(); + m_isContentSelected = !data.selectedText().isEmpty(); + + switch (data.mediaType()) { + case QWebEngineContextMenuData::MediaTypeImage: + m_imageUrl = data.mediaUrl(); + break; + + case QWebEngineContextMenuData::MediaTypeVideo: + case QWebEngineContextMenuData::MediaTypeAudio: + m_mediaUrl = data.mediaUrl(); + break; + + default: + break; + } +} + QUrl WebHitTestResult::baseUrl() const { return m_baseUrl; @@ -167,6 +195,7 @@ void WebHitTestResult::init(const QUrl &url, const QVariantMap &map) if (map.isEmpty()) return; + m_isNull = false; m_baseUrl = map.value(QSL("baseUrl")).toUrl(); m_alternateText = map.value(QSL("alternateText")).toString(); m_imageUrl = map.value(QSL("imageUrl")).toUrl(); diff --git a/src/lib/webengine/webhittestresult.h b/src/lib/webengine/webhittestresult.h index e167cacd9..6bfa6d797 100644 --- a/src/lib/webengine/webhittestresult.h +++ b/src/lib/webengine/webhittestresult.h @@ -25,6 +25,8 @@ #include "qzcommon.h" +class QWebEngineContextMenuData; + class WebPage; class QUPZILLA_EXPORT WebHitTestResult @@ -32,6 +34,8 @@ class QUPZILLA_EXPORT WebHitTestResult public: explicit WebHitTestResult(const WebPage *page, const QPoint &pos); + void updateWithContextMenuData(const QWebEngineContextMenuData &data); + QUrl baseUrl() const; QString alternateText() const; QRect boundingRect() const; diff --git a/src/lib/webengine/webview.cpp b/src/lib/webengine/webview.cpp index 99a87ceef..5fdf9546d 100644 --- a/src/lib/webengine/webview.cpp +++ b/src/lib/webengine/webview.cpp @@ -609,14 +609,15 @@ void WebView::userDefinedOpenUrlInBgTab(const QUrl &url) userDefinedOpenUrlInNewTab(actionUrl, true); } -void WebView::createContextMenu(QMenu *menu, const WebHitTestResult &hitTest) +void WebView::createContextMenu(QMenu *menu, WebHitTestResult &hitTest) { // cppcheck-suppress variableScope int spellCheckActionCount = 0; -#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) const QWebEngineContextMenuData &contextMenuData = page()->contextMenuData(); + hitTest.updateWithContextMenuData(contextMenuData); +#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) if (!contextMenuData.misspelledWord().isEmpty()) { QFont boldFont = menu->font(); boldFont.setBold(true); diff --git a/src/lib/webengine/webview.h b/src/lib/webengine/webview.h index efdd161f5..8a63be275 100644 --- a/src/lib/webengine/webview.h +++ b/src/lib/webengine/webview.h @@ -152,7 +152,7 @@ protected: void loadRequest(const LoadRequest &req); void applyZoom(); - void createContextMenu(QMenu *menu, const WebHitTestResult &hitTest); + void createContextMenu(QMenu *menu, WebHitTestResult &hitTest); void createPageContextMenu(QMenu *menu); void createLinkContextMenu(QMenu *menu, const WebHitTestResult &hitTest); void createImageContextMenu(QMenu *menu, const WebHitTestResult &hitTest); diff --git a/src/lib/webtab/tabbedwebview.cpp b/src/lib/webtab/tabbedwebview.cpp index 321003f09..ef2a10fd0 100644 --- a/src/lib/webtab/tabbedwebview.cpp +++ b/src/lib/webtab/tabbedwebview.cpp @@ -199,8 +199,7 @@ void TabbedWebView::_contextMenuEvent(QContextMenuEvent *event) { m_menu->clear(); - const WebHitTestResult hitTest = page()->hitTestContent(event->pos()); - + WebHitTestResult hitTest = page()->hitTestContent(event->pos()); createContextMenu(m_menu, hitTest); if (!hitTest.isContentEditable() && !hitTest.isContentSelected() && m_window) {