Ctrl + Click and middle click on external link don't open a new empty tab

Connect the &QWebEnginePage::linkHovered signal to set the m_linkHovered
member.
The Ctrl + Click and middle click events are intercepted to directly open the m_linkHovered
variable if it's an external link
This commit is contained in:
luddens 2020-03-13 00:54:31 +01:00 committed by Matthieu Gautier
parent df9db1317a
commit 285417a2d8
2 changed files with 14 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include "webview.h"
#include <QDesktopServices>
#include <QAction>
#include <iostream>
#include "kiwixapp.h"
@ -12,6 +13,9 @@ WebView::WebView(QWidget *parent)
{
setPage(new WebPage(this));
QObject::connect(this, &QWebEngineView::urlChanged, this, &WebView::onUrlChanged);
connect(this->page(), &QWebEnginePage::linkHovered, this, [=] (const QString& url) {
m_linkHovered = url;
});
}
WebView::~WebView()
@ -75,6 +79,15 @@ bool WebView::eventFilter(QObject *src, QEvent *e)
if (we->modifiers() == Qt::ControlModifier)
return true;
}
if (e->type() == QEvent::MouseButtonRelease) {
auto me = static_cast<QMouseEvent *>(e);
if (!m_linkHovered.startsWith("zim://")
&& (me->modifiers() == Qt::ControlModifier || me->button() == Qt::MiddleButton))
{
QDesktopServices::openUrl(m_linkHovered);
return true;
}
}
return false;
}

View File

@ -36,6 +36,7 @@ protected:
QString m_currentZimId;
QIcon m_icon;
QString m_linkHovered;
};
#endif // WEBVIEW_H