From a11b14fd2d754fd9ecee47d81cd8829194fb023e Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 21 Apr 2014 19:29:11 +0200 Subject: [PATCH] Implement more hinting modes --- TODO | 21 --------------------- qutebrowser/browser/curcommand.py | 5 +++-- qutebrowser/browser/hints.py | 11 ++++++++++- qutebrowser/config/configdata.py | 6 ++++++ qutebrowser/widgets/browsertab.py | 22 ++++++++++++++++++++-- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/TODO b/TODO index 23707f5b2..9712bd935 100644 --- a/TODO +++ b/TODO @@ -184,30 +184,9 @@ Lock tab [n]. Locking a tab will lock this tab to the current domain, it’s not [n]xu Lock tab [n]. Locking a tab will lock this tab to the current uri, it’s 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 hint’s url in commandline (command hints_url, aliases: uhints, uhi). diff --git a/qutebrowser/browser/curcommand.py b/qutebrowser/browser/curcommand.py index ec0c94254..383764368 100644 --- a/qutebrowser/browser/curcommand.py +++ b/qutebrowser/browser/curcommand.py @@ -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): diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index 1e6879845..a1f82e359 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -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() diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 79531ff8a..84b3f3d9d 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -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'), diff --git a/qutebrowser/widgets/browsertab.py b/qutebrowser/widgets/browsertab.py index 104b9ddc3..cf676d977 100644 --- a/qutebrowser/widgets/browsertab.py +++ b/qutebrowser/widgets/browsertab.py @@ -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)