1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

[WebView] Added option to force context menu to be opened on mouse release

This is needed for mouse gestures to be able to use right mouse button.
See #1280
This commit is contained in:
nowrep 2014-04-05 13:20:21 +02:00
parent 297090692f
commit 41ff89c3dd
2 changed files with 40 additions and 9 deletions

View File

@ -50,6 +50,8 @@
#include <QTouchEvent>
#include <QPrintPreviewDialog>
bool WebView::s_forceContextMenuOnMouseRelease = false;
WebView::WebView(QWidget* parent)
: QWebView(parent)
, m_isLoading(false)
@ -73,9 +75,7 @@ WebView::WebView(QWidget* parent)
m_zoomLevels = zoomLevels();
m_currentZoomLevel = m_zoomLevels.indexOf(100);
#if QTWEBKIT_TO_2_3
installEventFilter(this);
#endif
#ifdef Q_OS_MAC
new MacWebViewScroller(this);
@ -310,6 +310,18 @@ QList<int> WebView::zoomLevels()
<< 220 << 233 << 250 << 270 << 285 << 300;
}
// static
bool WebView::forceContextMenuOnMouseRelease()
{
return s_forceContextMenuOnMouseRelease;
}
// static
void WebView::setForceContextMenuOnMouseRelease(bool force)
{
s_forceContextMenuOnMouseRelease = force;
}
void WebView::addNotification(QWidget* notif)
{
emit showNotification(notif);
@ -1320,10 +1332,16 @@ void WebView::mouseReleaseEvent(QMouseEvent* event)
return;
}
}
break;
}
case Qt::RightButton:
if (s_forceContextMenuOnMouseRelease) {
QContextMenuEvent ev(QContextMenuEvent::Mouse, event->pos(), event->globalPos(), event->modifiers());
QApplication::sendEvent(this, &ev);
}
break;
default:
break;
}
@ -1488,15 +1506,21 @@ void WebView::resizeEvent(QResizeEvent* event)
emit viewportResized(page()->viewportSize());
}
///
bool WebView::eventFilter(QObject* obj, QEvent* event)
{
if (s_forceContextMenuOnMouseRelease && obj == this && event->type() == QEvent::ContextMenu) {
QContextMenuEvent* ev = static_cast<QContextMenuEvent*>(event);
if (ev->reason() == QContextMenuEvent::Mouse && ev->spontaneous()) {
ev->accept();
return true;
}
}
// This hack is no longer needed with QtWebKit 2.3 (bundled in Qt 5)
#if QTWEBKIT_TO_2_3
// This function was taken and modified from QTestBrowser to fix bug #33 with flightradar24.com
// You can find original source and copyright here:
// http://gitorious.org/+qtwebkit-developers/webkit/qtwebkit/blobs/qtwebkit-2.2/Tools/QtTestBrowser/launcherwindow.cpp
///
bool WebView::eventFilter(QObject* obj, QEvent* event)
{
// This hack is no longer needed with QtWebKit 2.3 (bundled in Qt 5)
#if QTWEBKIT_TO_2_3
if (obj != this || m_disableTouchMocking) {
return false;
}

View File

@ -69,6 +69,11 @@ public:
static QUrl guessUrlFromString(const QString &string);
static QList<int> zoomLevels();
// Force context menu event to be sent on mouse release
// This allows to override right mouse button events (eg. for mouse gestures)
static bool forceContextMenuOnMouseRelease();
static void setForceContextMenuOnMouseRelease(bool force);
signals:
void viewportResized(QSize);
void showNotification(QWidget*);
@ -202,6 +207,8 @@ private:
bool m_hasRss;
bool m_rssChecked;
static bool s_forceContextMenuOnMouseRelease;
};
#endif // WEBVIEW_H