ui: Implement custom context menu for the web view (fixes #635)

This commit is contained in:
Oleg Shparber
2018-01-07 00:47:06 +02:00
parent 5cec65591b
commit b468d31aab
2 changed files with 64 additions and 0 deletions
+63
View File
@@ -29,6 +29,8 @@
#include <core/application.h>
#include <QApplication>
#include <QDesktopServices>
#include <QMenu>
#include <QWheelEvent>
using namespace Zeal::WidgetUi;
@@ -98,6 +100,67 @@ QWebView *WebView::createWindow(QWebPage::WebWindowType type)
return mainWindow->createTab()->m_webView;
}
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
const QWebHitTestResult hitTestResult = hitTestContent(event->pos());
// Return standard menu for input fields.
if (hitTestResult.isContentEditable()) {
QWebView::contextMenuEvent(event);
return;
}
event->accept();
if (m_contextMenu) {
m_contextMenu->deleteLater();
}
QMenu *m_contextMenu = new QMenu(this);
const QUrl linkUrl = hitTestResult.linkUrl();
const QString linkText = hitTestResult.linkText();
if (linkUrl.isValid()) {
m_contextMenu->addAction(tr("Open Link in New Tab"), this, [this]() {
triggerPageAction(QWebPage::WebAction::OpenLinkInNewWindow);
});
if (linkUrl.scheme() != QLatin1String("qrc")) {
m_contextMenu->addAction(tr("Open Link in Desktop Browser"), this, [linkUrl]() {
QDesktopServices::openUrl(linkUrl);
});
m_contextMenu->addAction(pageAction(QWebPage::CopyLinkToClipboard));
}
}
if (hitTestResult.isContentSelected()) {
if (!m_contextMenu->isEmpty()) {
m_contextMenu->addSeparator();
}
m_contextMenu->addAction(pageAction(QWebPage::Copy));
}
if (!linkUrl.isValid() && url().scheme() != QLatin1String("qrc")) {
if (!m_contextMenu->isEmpty()) {
m_contextMenu->addSeparator();
}
m_contextMenu->addAction(tr("Open Page in Desktop Browser"), this, [this]() {
QDesktopServices::openUrl(url());
});
}
if (m_contextMenu->isEmpty()) {
return;
}
m_contextMenu->popup(event->globalPos());
}
void WebView::mousePressEvent(QMouseEvent *event)
{
switch (event->button()) {