diff --git a/src/lib/tools/scripts.cpp b/src/lib/tools/scripts.cpp index 95852d7b4..c26ef3b85 100644 --- a/src/lib/tools/scripts.cpp +++ b/src/lib/tools/scripts.cpp @@ -279,3 +279,32 @@ QString Scripts::getFormData(const QPoint &pos) return source.arg(QString::number(pos.x()), QString::number(pos.y())); } + +QString Scripts::toggleMediaPause(const QPoint &pos) +{ + QString source = QL1S("(function() {" + "var e = document.elementFromPoint(%1, %2);" + "if (!e)" + " return;" + "if (e.paused)" + " e.play();" + "else" + " e.pause();" + "})()"); + + return source.arg(QString::number(pos.x()), QString::number(pos.y())); + +} + +QString Scripts::toggleMediaMute(const QPoint &pos) +{ + QString source = QL1S("(function() {" + "var e = document.elementFromPoint(%1, %2);" + "if (!e)" + " return;" + "e.muted = !e.muted;" + "})()"); + + return source.arg(QString::number(pos.x()), QString::number(pos.y())); + +} diff --git a/src/lib/tools/scripts.h b/src/lib/tools/scripts.h index aa438ed6f..9f46ae2ef 100644 --- a/src/lib/tools/scripts.h +++ b/src/lib/tools/scripts.h @@ -38,6 +38,8 @@ public: static QString getAllImages(); static QString getAllMetaAttributes(); static QString getFormData(const QPoint &pos); + static QString toggleMediaPause(const QPoint &pos); + static QString toggleMediaMute(const QPoint &pos); }; #endif // SCRIPTS_H diff --git a/src/lib/webengine/webhittestresult.cpp b/src/lib/webengine/webhittestresult.cpp index 50ea5c9e2..ca8fbb6a5 100644 --- a/src/lib/webengine/webhittestresult.cpp +++ b/src/lib/webengine/webhittestresult.cpp @@ -22,6 +22,8 @@ WebHitTestResult::WebHitTestResult(const WebPage *page, const QPoint &pos) : m_isNull(true) , m_isContentEditable(false) , m_isContentSelected(false) + , m_mediaPaused(false) + , m_mediaMuted(false) , m_pos(pos) { QString source = QL1S("(function() {" @@ -57,17 +59,17 @@ WebHitTestResult::WebHitTestResult(const WebPage *page, const QPoint &pos) " res.linkTitle = e.text;" " res.linkUrl = e.getAttribute('href');" "}" - "if (isMediaElement(e))" - " res.mediaUrl = e.getAttribute('src');" - "var pe = e.parentElement;" - "while (pe) {" - " if (res.linkTitle == '' && pe.tagName == 'A')" - " res.linkTitle = pe.text;" - " if (res.linkUrl == '' && pe.tagName == 'A')" - " res.linkUrl = pe.getAttribute('href');" - " if (res.mediaUrl == '' && isMediaElement(pe))" - " res.mediaUrl = pe.getAttribute('src');" - " pe = pe.parentElement;" + "while (e) {" + " if (res.linkTitle == '' && e.tagName == 'A')" + " res.linkTitle = e.text;" + " if (res.linkUrl == '' && e.tagName == 'A')" + " res.linkUrl = e.getAttribute('href');" + " if (res.mediaUrl == '' && isMediaElement(e)) {" + " res.mediaUrl = e.currentSrc;" + " res.mediaPaused = e.paused;" + " res.mediaMuted = e.muted;" + " }" + " e = e.parentElement;" "}" "return res;" "})()"); @@ -122,6 +124,16 @@ QUrl WebHitTestResult::mediaUrl() const return m_mediaUrl; } +bool WebHitTestResult::mediaPaused() const +{ + return m_mediaPaused; +} + +bool WebHitTestResult::mediaMuted() const +{ + return m_mediaMuted; +} + QPoint WebHitTestResult::pos() const { return m_pos; @@ -144,6 +156,8 @@ void WebHitTestResult::init(const QUrl &url, const QVariantMap &map) m_linkTitle = map.value(QSL("linkTitle")).toString(); m_linkUrl = map.value(QSL("linkUrl")).toUrl(); m_mediaUrl = map.value(QSL("mediaUrl")).toUrl(); + m_mediaPaused = map.value(QSL("mediaPaused")).toBool(); + m_mediaMuted = map.value(QSL("mediaMuted")).toBool(); m_tagName = map.value(QSL("tagName")).toString(); const QVariantList &rect = map.value(QSL("boundingRect")).toList(); diff --git a/src/lib/webengine/webhittestresult.h b/src/lib/webengine/webhittestresult.h index cfc1273bf..7da8c4ed2 100644 --- a/src/lib/webengine/webhittestresult.h +++ b/src/lib/webengine/webhittestresult.h @@ -39,6 +39,8 @@ public: QString linkTitle() const; QUrl linkUrl() const; QUrl mediaUrl() const; + bool mediaPaused() const; + bool mediaMuted() const; QPoint pos() const; QString tagName() const; @@ -54,6 +56,8 @@ private: QString m_linkTitle; QUrl m_linkUrl; QUrl m_mediaUrl; + bool m_mediaPaused; + bool m_mediaMuted; QPoint m_pos; QString m_tagName; }; diff --git a/src/lib/webengine/webview.cpp b/src/lib/webengine/webview.cpp index be7e98fcd..5c585c065 100644 --- a/src/lib/webengine/webview.cpp +++ b/src/lib/webengine/webview.cpp @@ -669,11 +669,9 @@ void WebView::createContextMenu(QMenu *menu, const WebHitTestResult &hitTest) createImageContextMenu(menu, hitTest); } -#if QTWEBENGINE_DISABLED - if (isMediaElement(hitTest.element())) { + if (!hitTest.mediaUrl().isEmpty()) { createMediaContextMenu(menu, hitTest); } -#endif if (hitTest.isContentEditable()) { // This only checks if the menu is empty (only spellchecker actions added) @@ -878,6 +876,23 @@ void WebView::createSelectedTextContextMenu(QMenu* menu, const WebHitTestResult menu->addMenu(swMenu); } +void WebView::createMediaContextMenu(QMenu *menu, const WebHitTestResult &hitTest) +{ + m_clickedPos = hitTest.pos(); + bool paused = hitTest.mediaPaused(); + bool muted = hitTest.mediaMuted(); + + menu->addSeparator(); + menu->addAction(paused ? tr("&Play") : tr("&Pause"), this, SLOT(toggleMediaPause()))->setIcon(QIcon::fromTheme(paused ? "media-playback-start" : "media-playback-pause")); + menu->addAction(muted ? tr("Un&mute") : tr("&Mute"), this, SLOT(toggleMediaMute()))->setIcon(QIcon::fromTheme(muted ? "audio-volume-muted" : "audio-volume-high")); + menu->addSeparator(); + menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy Media Address"), this, SLOT(copyLinkToClipboard()))->setData(hitTest.mediaUrl()); + menu->addAction(QIcon::fromTheme("mail-message-new"), tr("&Send Media Address"), this, SLOT(sendLinkByMail()))->setData(hitTest.mediaUrl()); +#if QTWEBENGINE_DISABLED + menu->addAction(QIcon::fromTheme("document-save"), tr("Save Media To &Disk"), this, SLOT(downloadUrlToDisk()))->setData(hitTest.mediaUrl()); +#endif +} + void WebView::checkForForm(QAction *action, const QPoint &pos) { m_clickedPos = pos; @@ -907,29 +922,6 @@ void WebView::createSearchEngine() }); } -#if QTWEBENGINE_DISABLED -void WebView::createMediaContextMenu(QMenu* menu, const QWebHitTestResult &hitTest) -{ - m_clickedElement = hitTest.element(); - - if (m_clickedElement.isNull()) { - return; - } - - bool paused = m_clickedElement.evaluateJavaScript("this.paused").toBool(); - bool muted = m_clickedElement.evaluateJavaScript("this.muted").toBool(); - QUrl videoUrl = m_clickedElement.evaluateJavaScript("this.currentSrc").toUrl(); - - menu->addSeparator(); - menu->addAction(paused ? tr("&Play") : tr("&Pause"), this, SLOT(pauseMedia()))->setIcon(QIcon::fromTheme(paused ? "media-playback-start" : "media-playback-pause")); - menu->addAction(muted ? tr("Un&mute") : tr("&Mute"), this, SLOT(muteMedia()))->setIcon(QIcon::fromTheme(muted ? "audio-volume-muted" : "audio-volume-high")); - menu->addSeparator(); - menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy Media Address"), this, SLOT(copyLinkToClipboard()))->setData(videoUrl); - menu->addAction(QIcon::fromTheme("mail-message-new"), tr("&Send Media Address"), this, SLOT(sendLinkByMail()))->setData(videoUrl); - menu->addAction(QIcon::fromTheme("document-save"), tr("Save Media To &Disk"), this, SLOT(downloadUrlToDisk()))->setData(videoUrl); -} -#endif - void WebView::addSpeedDial() { page()->runJavaScript("addSpeedDial()"); @@ -945,6 +937,16 @@ void WebView::reloadAllSpeedDials() page()->runJavaScript("reloadAll()"); } +void WebView::toggleMediaPause() +{ + page()->runJavaScript(Scripts::toggleMediaPause(m_clickedPos)); +} + +void WebView::toggleMediaMute() +{ + page()->runJavaScript(Scripts::toggleMediaMute(m_clickedPos)); +} + void WebView::initializeActions() { QAction* undoAction = pageAction(QWebEnginePage::Undo); diff --git a/src/lib/webengine/webview.h b/src/lib/webengine/webview.h index 4556db047..d74afa49e 100644 --- a/src/lib/webengine/webview.h +++ b/src/lib/webengine/webview.h @@ -152,19 +152,20 @@ protected: void createLinkContextMenu(QMenu *menu, const WebHitTestResult &hitTest); void createImageContextMenu(QMenu *menu, const WebHitTestResult &hitTest); void createSelectedTextContextMenu(QMenu *menu, const WebHitTestResult &hitTest); + void createMediaContextMenu(QMenu *menu, const WebHitTestResult &hitTest); void checkForForm(QAction *action, const QPoint &pos); void createSearchEngine(); -#if QTWEBENGINE_DISABLED - void createMediaContextMenu(QMenu* menu, const QWebHitTestResult &hitTest); -#endif private slots: void addSpeedDial(); void configureSpeedDial(); void reloadAllSpeedDials(); + void toggleMediaPause(); + void toggleMediaMute(); + private: void initializeActions();