Combine hint signals into a single one

This commit is contained in:
Florian Bruhin 2016-08-17 19:20:14 +02:00
parent 39fd6a6062
commit eabac17c58
2 changed files with 23 additions and 50 deletions

View File

@ -21,7 +21,7 @@
import itertools import itertools
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QObject, QSizeF from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QObject, QSizeF, QTimer
from PyQt5.QtGui import QIcon from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget from PyQt5.QtWidgets import QWidget
@ -60,7 +60,7 @@ class WebTabError(Exception):
"""Base class for various errors.""" """Base class for various errors."""
class TabData(QObject): class TabData:
"""A simple namespace with a fixed set of attributes. """A simple namespace with a fixed set of attributes.
@ -73,8 +73,7 @@ class TabData(QObject):
hint_target: Override for open_target for hints. hint_target: Override for open_target for hints.
""" """
def __init__(self, parent=None): def __init__(self):
super().__init__(parent)
self.keep_icon = False self.keep_icon = False
self.viewing_source = False self.viewing_source = False
self.inspector = None self.inspector = None
@ -87,21 +86,6 @@ class TabData(QObject):
else: else:
return self.open_target return self.open_target
@pyqtSlot(usertypes.ClickTarget)
def _on_start_hinting(self, hint_target):
"""Emitted before a hinting-click takes place.
Args:
hint_target: A ClickTarget member to set self.hint_target to.
"""
log.webview.debug("Setting force target to {}".format(hint_target))
self.hint_target = hint_target
@pyqtSlot()
def _on_stop_hinting(self):
log.webview.debug("Finishing hinting.")
self.hint_target = None
class AbstractPrinting: class AbstractPrinting:
@ -489,7 +473,7 @@ class AbstractTab(QWidget):
# self.search = AbstractSearch(parent=self) # self.search = AbstractSearch(parent=self)
# self.printing = AbstractPrinting() # self.printing = AbstractPrinting()
self.data = TabData(parent=self) self.data = TabData()
self._layout = miscwidgets.WrapperLayout(self) self._layout = miscwidgets.WrapperLayout(self)
self._widget = None self._widget = None
self._progress = 0 self._progress = 0
@ -501,11 +485,7 @@ class AbstractTab(QWidget):
# FIXME:qtwebengine Should this be public api via self.hints? # FIXME:qtwebengine Should this be public api via self.hints?
# Also, should we get it out of objreg? # Also, should we get it out of objreg?
hintmanager = hints.HintManager(win_id, self.tab_id, parent=self) hintmanager = hints.HintManager(win_id, self.tab_id, parent=self)
hintmanager.mouse_event.connect(self._on_hint_mouse_event) hintmanager.hint_events.connect(self._on_hint_events)
# pylint: disable=protected-access
hintmanager.start_hinting.connect(self.data._on_start_hinting)
hintmanager.stop_hinting.connect(self.data._on_stop_hinting)
# pylint: enable=protected-access
objreg.register('hintmanager', hintmanager, scope='tab', objreg.register('hintmanager', hintmanager, scope='tab',
window=self.win_id, tab=self.tab_id) window=self.win_id, tab=self.tab_id)
@ -536,16 +516,21 @@ class AbstractTab(QWidget):
"""Send the given event to the underlying widget.""" """Send the given event to the underlying widget."""
raise NotImplementedError raise NotImplementedError
@pyqtSlot('QMouseEvent') @pyqtSlot(usertypes.ClickTarget, list)
def _on_hint_mouse_event(self, evt): def _on_hint_events(self, target, events):
"""Post a new mouse event from a hintmanager.""" """Post a new mouse event from a hintmanager."""
# FIXME:qtwebengine Will this implementation work for QtWebEngine? log.modes.debug("Sending hint events to {!r} with target {}".format(
# We probably need to send the event to the self, target))
# focusProxy()?
log.modes.debug("Hint triggered, focusing {!r}".format(self))
self._widget.setFocus() self._widget.setFocus()
self.data.hint_target = target
for evt in events:
self.post_event(evt) self.post_event(evt)
def reset_target():
self.data.hint_target = None
QTimer.singleShot(0, reset_target)
@pyqtSlot(QUrl) @pyqtSlot(QUrl)
def _on_link_clicked(self, url): def _on_link_clicked(self, url):
log.webview.debug("link clicked: url {}, hint target {}, " log.webview.debug("link clicked: url {}, hint target {}, "

View File

@ -185,16 +185,10 @@ class HintActions(QObject):
"""Actions which can be done after selecting a hint. """Actions which can be done after selecting a hint.
Signals: Signals:
mouse_event: Mouse event to be posted in the web view. hint_events: Emitted with a ClickTarget and a list of hint event.s
arg: A QMouseEvent
start_hinting: Emitted when hinting starts, before a link is clicked.
arg: The ClickTarget to use.
stop_hinting: Emitted after a link was clicked.
""" """
mouse_event = pyqtSignal('QMouseEvent') hint_events = pyqtSignal(usertypes.ClickTarget, list) # QMouseEvent list
start_hinting = pyqtSignal(usertypes.ClickTarget)
stop_hinting = pyqtSignal()
def __init__(self, win_id, parent=None): def __init__(self, win_id, parent=None):
super().__init__(parent) super().__init__(parent)
@ -235,7 +229,6 @@ class HintActions(QObject):
log.hints.debug("{} on '{}' at position {}".format( log.hints.debug("{} on '{}' at position {}".format(
action, elem.debug_text(), pos)) action, elem.debug_text(), pos))
self.start_hinting.emit(target_mapping[context.target])
if context.target in [Target.tab, Target.tab_fg, Target.tab_bg, if context.target in [Target.tab, Target.tab_fg, Target.tab_bg,
Target.window]: Target.window]:
modifiers = Qt.ControlModifier modifiers = Qt.ControlModifier
@ -261,11 +254,10 @@ class HintActions(QObject):
if context.target == Target.current: if context.target == Target.current:
elem.remove_blank_target() elem.remove_blank_target()
for evt in events:
self.mouse_event.emit(evt) self.hint_events.emit(target_mapping[context.target], events)
if elem.is_text_input() and elem.is_editable(): if elem.is_text_input() and elem.is_editable():
QTimer.singleShot(0, context.tab.caret.move_to_end_of_document) QTimer.singleShot(0, context.tab.caret.move_to_end_of_document)
QTimer.singleShot(0, self.stop_hinting.emit)
def yank(self, url, context): def yank(self, url, context):
"""Yank an element to the clipboard or primary selection. """Yank an element to the clipboard or primary selection.
@ -408,9 +400,7 @@ class HintManager(QObject):
Target.spawn: "Spawn command via hint", Target.spawn: "Spawn command via hint",
} }
mouse_event = pyqtSignal('QMouseEvent') hint_events = pyqtSignal(usertypes.ClickTarget, list) # QMouseEvent list
start_hinting = pyqtSignal(usertypes.ClickTarget)
stop_hinting = pyqtSignal()
def __init__(self, win_id, tab_id, parent=None): def __init__(self, win_id, tab_id, parent=None):
"""Constructor.""" """Constructor."""
@ -421,9 +411,7 @@ class HintManager(QObject):
self._word_hinter = WordHinter() self._word_hinter = WordHinter()
self._actions = HintActions(win_id) self._actions = HintActions(win_id)
self._actions.start_hinting.connect(self.start_hinting) self._actions.hint_events.connect(self.hint_events)
self._actions.stop_hinting.connect(self.stop_hinting)
self._actions.mouse_event.connect(self.mouse_event)
mode_manager = objreg.get('mode-manager', scope='window', mode_manager = objreg.get('mode-manager', scope='window',
window=win_id) window=win_id)