Merge pull request #456 from kiwix/open-external-link-from-menu

add "open link in web browser" action in context menu
This commit is contained in:
Matthieu Gautier 2020-07-10 10:51:25 +02:00 committed by GitHub
commit 13339b4772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View File

@ -110,5 +110,6 @@
"details":"Full article",
"yes":"yes",
"no":"no",
"no-filter":"no filter"
"no-filter":"no filter",
"open-link-in-web-browser":"Open link in web browser"
}

View File

@ -1,6 +1,7 @@
#include "webview.h"
#include <QDesktopServices>
#include <QMenu>
#include <QAction>
#include <iostream>
#include "kiwixapp.h"
@ -71,6 +72,29 @@ void WebView::wheelEvent(QWheelEvent *event) {
}
}
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
auto menu = this->page()->createStandardContextMenu();
pageAction(QWebEnginePage::OpenLinkInNewWindow)->setVisible(false);
if (!m_linkHovered.isEmpty()) {
if (!m_linkHovered.startsWith("zim://")) {
pageAction(QWebEnginePage::OpenLinkInNewTab)->setVisible(false);
auto openLinkInWebBrowserAction = new QAction(gt("open-link-in-web-browser"));
menu->insertAction(pageAction(QWebEnginePage::DownloadLinkToDisk) , openLinkInWebBrowserAction);
connect(menu, &QObject::destroyed, openLinkInWebBrowserAction, &QObject::deleteLater);
connect(openLinkInWebBrowserAction, &QAction::triggered, this, [=](bool checked) {
Q_UNUSED(checked);
QDesktopServices::openUrl(m_linkHovered);
});
} else {
pageAction(QWebEnginePage::OpenLinkInNewTab)->setVisible(true);
}
}
menu->exec(event->globalPos());
}
bool WebView::eventFilter(QObject *src, QEvent *e)
{
Q_UNUSED(src)

View File

@ -34,6 +34,7 @@ protected:
void wheelEvent(QWheelEvent *event);
bool event(QEvent *event);
bool eventFilter(QObject *src, QEvent *e);
void contextMenuEvent(QContextMenuEvent *event);
QString m_currentZimId;
QIcon m_icon;