Implement more hinting modes

This commit is contained in:
Florian Bruhin 2014-04-21 19:29:11 +02:00
parent 0741326e5d
commit a11b14fd2d
5 changed files with 39 additions and 26 deletions

21
TODO
View File

@ -184,30 +184,9 @@ Lock tab [n]. Locking a tab will lock this tab to the current domain, its not
[n]xu
Lock tab [n]. Locking a tab will lock this tab to the current uri, its not possible to navigate to another uri until unlocked. lock_uri, aliases: ulock).
f
Show hints (command hints, aliases: hints, hi).
F
Show hints, open link in a new foreground tab. (command hints_tab, aliases: tabhints, thi).
;b
Show hints, open link in a new background tab. (command hints_background, aliases: backhints, bhi).
wf
Show hints, open link in a new tab. (command hints_win, aliases: winhints, whi).
;i
Follow image (command hints_images, aliases: ihints, ihi).
;I
Follow image in a new tab (command hints_images_tab, aliases: itabhints, ithi).
.i
Follow image in a background tab (command hints_images_background, aliases: ibackhints).
;e
Focus editable elements via hints (command hints_editable, aliases: ehints, ehi).
;o
Set hints url in commandline (command hints_url, aliases: uhints, uhi).

View File

@ -165,15 +165,16 @@ class CurCommandDispatcher(QObject):
self._tabs.currentWidget().forward()
@cmdutils.register(instance='mainwindow.tabs.cur')
def hint(self, mode="all"):
def hint(self, mode="all", target="normal"):
"""Start hinting.
Command handler for :hint.
Args:
mode: The hinting mode to use.
target: Where to open the links.
"""
self._tabs.currentWidget().hintmanager.start(mode)
self._tabs.currentWidget().hintmanager.start(mode, target)
@pyqtSlot(str)
def handle_hint_key(self, keystr):

View File

@ -80,6 +80,7 @@ class HintManager(QObject):
Attributes:
_frame: The QWebFrame to use.
_elems: A mapping from keystrings to (elem, label) namedtuples.
_target: What to do with the opened links.
Signals:
hint_strings_updated: Emitted when the possible hint strings changed.
@ -91,6 +92,7 @@ class HintManager(QObject):
openurl: Open a new url
arg 0: URL to open as a string.
arg 1: true if it should be opened in a new tab, else false.
set_open_target: Set a new target to open the links in.
"""
SELECTORS = {
@ -120,6 +122,7 @@ class HintManager(QObject):
hint_strings_updated = pyqtSignal(list)
set_mode = pyqtSignal(str)
mouse_event = pyqtSignal('QMouseEvent')
set_open_target = pyqtSignal(str)
def __init__(self, frame):
"""Constructor.
@ -130,6 +133,7 @@ class HintManager(QObject):
super().__init__(frame)
self._frame = frame
self._elems = {}
self._target = None
def _hint_strings(self, elems):
"""Calculate the hint strings for elems.
@ -238,13 +242,16 @@ class HintManager(QObject):
css, string))
return doc.lastChild()
def start(self, mode="all"):
def start(self, mode="all", target="normal"):
"""Start hinting.
Args:
mode: The mode to be used.
target: What to do with the link.
"normal"/"tab"/"bgtab": Get passed to BrowserTab.
"""
selector = HintManager.SELECTORS[mode]
self._target = target
elems = self._frame.findAllElements(selector)
visible_elems = []
for e in elems:
@ -270,6 +277,7 @@ class HintManager(QObject):
for elem in self._elems.values():
elem.label.removeFromDocument()
self._elems = {}
self._target = None
self.set_mode.emit("normal")
def handle_partial_key(self, keystr):
@ -290,6 +298,7 @@ class HintManager(QObject):
def fire(self, keystr):
"""Fire a completed hint."""
elem = self._elems[keystr].elem
self.set_open_target.emit(self._target)
self.stop()
point = elem.geometry().topLeft()
scrollpos = self._frame.scrollPosition()

View File

@ -346,6 +346,12 @@ DATA = OrderedDict([
('H', 'back'),
('L', 'forward'),
('f', 'hint'),
('F', 'hint all tab'),
(';b', 'hint all bgtab'),
(';i', 'hint images'),
(';I', 'hint images tab'),
('.i', 'hint images bgtab'),
(';e', 'hint editable'),
('h', 'scroll -50 0'),
('j', 'scroll 0 50'),
('k', 'scroll 0 -50'),

View File

@ -48,6 +48,7 @@ class BrowserTab(QWebView):
_scroll_pos: The old scroll position.
_shutdown_callback: Callback to be called after shutdown.
_open_target: Where to open the next tab ("normal", "tab", "bgtab")
_force_open_target: Override for _open_target.
_shutdown_callback: The callback to call after shutting down.
_destroyed: Dict of all items to be destroyed on shtudown.
@ -73,6 +74,7 @@ class BrowserTab(QWebView):
self._scroll_pos = (-1, -1)
self._shutdown_callback = None
self._open_target = "normal"
self._force_open_target = None
self._destroyed = {}
self._zoom = None
self._init_neighborlist()
@ -80,6 +82,7 @@ class BrowserTab(QWebView):
self.setPage(self.page_)
self.hintmanager = HintManager(self.page_.mainFrame())
self.hintmanager.mouse_event.connect(self.on_mouse_event)
self.hintmanager.set_open_target.connect(self.set_force_open_target)
self.signal_cache = SignalCache(uncached=['linkHovered'])
self.page_.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page_.linkHovered.connect(self.linkHovered)
@ -196,6 +199,15 @@ class BrowserTab(QWebView):
self.setFocus()
QApplication.postEvent(self, evt)
@pyqtSlot(str)
def set_force_open_target(self, target):
"""Change the forced link target. Setter for _force_open_target.
Args:
target: A string to set self._force_open_target to.
"""
self._force_open_target = target
def _on_destroyed(self, sender):
"""Called when a subsystem has been destroyed during shutdown.
@ -258,12 +270,18 @@ class BrowserTab(QWebView):
The superclass event return value.
"""
if e.type() in [QEvent.MouseButtonPress, QEvent.MouseButtonDblClick]:
if (e.button() == Qt.MidButton or
e.modifiers() & Qt.ControlModifier):
if self._force_open_target is not None:
self._open_target = self._force_open_target
self._force_open_target = None
logging.debug("Setting force target: {}".format(
self._open_target))
elif (e.button() == Qt.MidButton or
e.modifiers() & Qt.ControlModifier):
if config.get('general', 'background_tabs'):
self._open_target = "bgtab"
else:
self._open_target = "tab"
logging.debug("Setting target: {}".format(self._open_target))
else:
self._open_target = "normal"
return super().event(e)