Use event_target to filter out mouse events

Fixes #2262
This commit is contained in:
Florian Bruhin 2017-02-01 13:08:11 +01:00
parent e5176e18bd
commit ba2f4fb1b9
4 changed files with 8 additions and 23 deletions

View File

@ -486,10 +486,6 @@ class AbstractTab(QWidget):
We use this to unify QWebView and QWebEngineView.
Class attributes:
WIDGET_CLASS: The class of the main widget recieving events.
Needs to be overridden by subclasses.
Attributes:
history: The AbstractHistory for the current tab.
registry: The ObjectRegistry associated with this tab.
@ -523,8 +519,6 @@ class AbstractTab(QWidget):
contents_size_changed = pyqtSignal(QSizeF)
add_history_item = pyqtSignal(QUrl, QUrl, str) # url, requested url, title
WIDGET_CLASS = None
def __init__(self, win_id, mode_manager, parent=None):
self.win_id = win_id
self.tab_id = next(tab_id_gen)
@ -553,7 +547,7 @@ class AbstractTab(QWidget):
self._mode_manager = mode_manager
self._load_status = usertypes.LoadStatus.none
self._mouse_event_filter = mouse.MouseEventFilter(
self, widget_class=self.WIDGET_CLASS, parent=self)
self, parent=self)
self.backend = None
# FIXME:qtwebengine Should this be public api via self.hints?
@ -588,7 +582,7 @@ class AbstractTab(QWidget):
self._load_status = val
self.load_status_changed.emit(val.name)
def _event_target(self):
def event_target(self):
"""Return the widget events should be sent to."""
raise NotImplementedError
@ -603,7 +597,7 @@ class AbstractTab(QWidget):
if getattr(evt, 'posted', False):
raise AssertionError("Can't re-use an event which was already "
"posted!")
recipient = self._event_target()
recipient = self.event_target()
evt.posted = True
QApplication.postEvent(recipient, evt)

View File

@ -64,8 +64,6 @@ class MouseEventFilter(QObject):
"""Handle mouse events on a tab.
Attributes:
_widget_class: The class of the main widget getting the events.
All other events are ignored.
_tab: The browsertab object this filter is installed on.
_handlers: A dict of handler functions for the handled events.
_ignore_wheel_event: Whether to ignore the next wheelEvent.
@ -73,9 +71,8 @@ class MouseEventFilter(QObject):
done when the mouse is released.
"""
def __init__(self, tab, *, widget_class, parent=None):
def __init__(self, tab, *, parent=None):
super().__init__(parent)
self._widget_class = widget_class
self._tab = tab
self._handlers = {
QEvent.MouseButtonPress: self._handle_mouse_press,
@ -207,9 +204,7 @@ class MouseEventFilter(QObject):
evtype = event.type()
if evtype not in self._handlers:
return False
if not isinstance(obj, self._widget_class):
log.mouse.debug("Ignoring {} to {} which is not an instance of "
"{}".format(event.__class__.__name__, obj,
self._widget_class))
if obj is not self._tab.event_target():
log.mouse.debug("Ignoring {} to {}".format(event.__class__.__name__, obj))
return False
return self._handlers[evtype](event)

View File

@ -452,8 +452,6 @@ class WebEngineTab(browsertab.AbstractTab):
"""A QtWebEngine tab in the browser."""
WIDGET_CLASS = QOpenGLWidget
def __init__(self, win_id, mode_manager, parent=None):
super().__init__(win_id=win_id, mode_manager=mode_manager,
parent=parent)
@ -654,5 +652,5 @@ class WebEngineTab(browsertab.AbstractTab):
except AttributeError:
log.stub('contentsSizeChanged, on Qt < 5.7')
def _event_target(self):
def event_target(self):
return self._widget.focusProxy()

View File

@ -590,8 +590,6 @@ class WebKitTab(browsertab.AbstractTab):
"""A QtWebKit tab in the browser."""
WIDGET_CLASS = webview.WebView
def __init__(self, win_id, mode_manager, parent=None):
super().__init__(win_id=win_id, mode_manager=mode_manager,
parent=parent)
@ -717,5 +715,5 @@ class WebKitTab(browsertab.AbstractTab):
frame.contentsSizeChanged.connect(self._on_contents_size_changed)
frame.initialLayoutCompleted.connect(self._on_history_trigger)
def _event_target(self):
def event_target(self):
return self._widget