From c674d96cfeac1ac50c0e240a1100a15eee3f5b64 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 24 Apr 2014 21:03:45 +0200 Subject: [PATCH] Reorganize input modules into keyinput subpackage --- qutebrowser/app.py | 27 ++++--- qutebrowser/browser/hints.py | 56 ++------------- .../keys.py => keyinput/commandmode.py} | 2 +- qutebrowser/keyinput/hintmode.py | 70 +++++++++++++++++++ qutebrowser/{utils => keyinput}/keyparser.py | 0 .../modemanager.py => keyinput/modes.py} | 0 qutebrowser/widgets/browsertab.py | 12 ++-- qutebrowser/widgets/statusbar.py | 16 ++--- 8 files changed, 102 insertions(+), 81 deletions(-) rename qutebrowser/{commands/keys.py => keyinput/commandmode.py} (98%) create mode 100644 qutebrowser/keyinput/hintmode.py rename qutebrowser/{utils => keyinput}/keyparser.py (100%) rename qutebrowser/{utils/modemanager.py => keyinput/modes.py} (100%) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index f78de1f4f..50324f030 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -52,11 +52,11 @@ import qutebrowser.commands.utils as cmdutils import qutebrowser.config.style as style import qutebrowser.config.config as config import qutebrowser.network.qutescheme as qutescheme +import qutebrowser.keyinput.modes as modes import qutebrowser.utils.message as message -import qutebrowser.utils.modemanager as modemanager from qutebrowser.widgets.mainwindow import MainWindow from qutebrowser.widgets.crash import CrashDialog -from qutebrowser.commands.keys import CommandKeyParser +from qutebrowser.keyinput.commandmode import CommandKeyParser from qutebrowser.commands.parsers import CommandParser, SearchParser from qutebrowser.browser.hints import HintKeyParser from qutebrowser.utils.appdirs import AppDirs @@ -129,17 +129,16 @@ class QuteBrowser(QApplication): } self._init_cmds() self.mainwindow = MainWindow() - modemanager.init(self) - modemanager.manager.register("normal", - self._keyparsers["normal"].handle) - modemanager.manager.register("hint", self._keyparsers["hint"].handle) - modemanager.manager.register("insert", None, passthrough=True) - modemanager.manager.register("command", None, passthrough=True) - self.installEventFilter(modemanager.manager) + modes.init(self) + modes.manager.register("normal", self._keyparsers["normal"].handle) + modes.manager.register("hint", self._keyparsers["hint"].handle) + modes.manager.register("insert", None, passthrough=True) + modes.manager.register("command", None, passthrough=True) + self.installEventFilter(modes.manager) self.setQuitOnLastWindowClosed(False) self._connect_signals() - modemanager.enter("normal") + modes.enter("normal") self.mainwindow.show() self._python_hacks() @@ -255,10 +254,10 @@ class QuteBrowser(QApplication): tabs.currentChanged.connect(self.mainwindow.update_inspector) # status bar - modemanager.manager.entered.connect(status.on_mode_entered) - modemanager.manager.left.connect(status.on_mode_left) + modes.manager.entered.connect(status.on_mode_entered) + modes.manager.left.connect(status.on_mode_left) # FIXME what to do here? - modemanager.manager.key_pressed.connect(status.on_key_pressed) + modes.manager.key_pressed.connect(status.on_key_pressed) for obj in [kp["normal"], tabs]: obj.set_cmd_text.connect(cmd.set_cmd_text) @@ -284,7 +283,7 @@ class QuteBrowser(QApplication): # config self.config.style_changed.connect(style.invalidate_caches) for obj in [tabs, completion, self.mainwindow, config.cmd_history, - websettings, kp["normal"], modemanager.manager]: + websettings, kp["normal"], modes.manager]: self.config.changed.connect(obj.on_config_changed) # statusbar diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index ea9044dd9..736a5b1d9 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -26,64 +26,16 @@ from PyQt5.QtGui import QMouseEvent, QClipboard from PyQt5.QtWidgets import QApplication import qutebrowser.config.config as config +import qutebrowser.keyinput.modes as modes import qutebrowser.utils.message as message import qutebrowser.utils.url as urlutils -import qutebrowser.utils.modemanager as modemanager import qutebrowser.utils.webelem as webelem -from qutebrowser.utils.keyparser import KeyChainParser +from qutebrowser.keyinput.hintmode import HintKeyParser ElemTuple = namedtuple('ElemTuple', 'elem, label') -class HintKeyParser(KeyChainParser): - - """KeyChainParser for hints. - - Class attributes: - supports_count: If the keyparser should support counts. - - Signals: - fire_hint: When a hint keybinding was completed. - Arg: the keystring/hint string pressed. - abort_hinting: Esc pressed, so abort hinting. - """ - - supports_count = False - fire_hint = pyqtSignal(str) - abort_hinting = pyqtSignal() - - def _handle_special_key(self, e): - """Handle the escape key. - - FIXME make this more generic - - Emit: - abort_hinting: Emitted if hinting was aborted. - """ - if e.key() == Qt.Key_Escape: - self._keystring = '' - self.abort_hinting.emit() - return True - return False - - def execute(self, cmdstr, count=None): - """Handle a completed keychain. - - Emit: - fire_hint: Always emitted. - """ - self.fire_hint.emit(cmdstr) - - def on_hint_strings_updated(self, strings): - """Handler for HintManager's hint_strings_updated. - - Args: - strings: A list of hint strings. - """ - self.bindings = {s: s for s in strings} - - class HintManager(QObject): """Manage drawing hints over links or other elements. @@ -364,7 +316,7 @@ class HintManager(QObject): self._elems[string] = ElemTuple(e, label) frame.contentsSizeChanged.connect(self.on_contents_size_changed) self.hint_strings_updated.emit(strings) - modemanager.enter("hint") + modes.enter("hint") def stop(self): """Stop hinting.""" @@ -375,7 +327,7 @@ class HintManager(QObject): self._elems = {} self._target = None self._frame = None - modemanager.leave("hint") + modes.leave("hint") message.clear() def handle_partial_key(self, keystr): diff --git a/qutebrowser/commands/keys.py b/qutebrowser/keyinput/commandmode.py similarity index 98% rename from qutebrowser/commands/keys.py rename to qutebrowser/keyinput/commandmode.py index cf11d21ed..bd4a040b4 100644 --- a/qutebrowser/commands/keys.py +++ b/qutebrowser/keyinput/commandmode.py @@ -25,7 +25,7 @@ import logging from PyQt5.QtCore import pyqtSignal -from qutebrowser.utils.keyparser import KeyChainParser +from qutebrowser.keyinput.keyparser import KeyChainParser from qutebrowser.commands.parsers import (CommandParser, ArgumentCountError, NoSuchCommandError) diff --git a/qutebrowser/keyinput/hintmode.py b/qutebrowser/keyinput/hintmode.py new file mode 100644 index 000000000..d689eeabf --- /dev/null +++ b/qutebrowser/keyinput/hintmode.py @@ -0,0 +1,70 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""KeyChainParser for "hint" mode.""" + +from PyQt5.QtCore import pyqtSignal, Qt + +from qutebrowser.keyinput.keyparser import KeyChainParser + + +class HintKeyParser(KeyChainParser): + + """KeyChainParser for hints. + + Class attributes: + supports_count: If the keyparser should support counts. + + Signals: + fire_hint: When a hint keybinding was completed. + Arg: the keystring/hint string pressed. + abort_hinting: Esc pressed, so abort hinting. + """ + + supports_count = False + fire_hint = pyqtSignal(str) + abort_hinting = pyqtSignal() + + def _handle_special_key(self, e): + """Handle the escape key. + + FIXME make this more generic + + Emit: + abort_hinting: Emitted if hinting was aborted. + """ + if e.key() == Qt.Key_Escape: + self._keystring = '' + self.abort_hinting.emit() + return True + return False + + def execute(self, cmdstr, count=None): + """Handle a completed keychain. + + Emit: + fire_hint: Always emitted. + """ + self.fire_hint.emit(cmdstr) + + def on_hint_strings_updated(self, strings): + """Handler for HintManager's hint_strings_updated. + + Args: + strings: A list of hint strings. + """ + self.bindings = {s: s for s in strings} diff --git a/qutebrowser/utils/keyparser.py b/qutebrowser/keyinput/keyparser.py similarity index 100% rename from qutebrowser/utils/keyparser.py rename to qutebrowser/keyinput/keyparser.py diff --git a/qutebrowser/utils/modemanager.py b/qutebrowser/keyinput/modes.py similarity index 100% rename from qutebrowser/utils/modemanager.py rename to qutebrowser/keyinput/modes.py diff --git a/qutebrowser/widgets/browsertab.py b/qutebrowser/widgets/browsertab.py index 4153e0cdf..b3993fdfe 100644 --- a/qutebrowser/widgets/browsertab.py +++ b/qutebrowser/widgets/browsertab.py @@ -27,8 +27,8 @@ from PyQt5.QtWebKitWidgets import QWebView, QWebPage import qutebrowser.utils.url as urlutils import qutebrowser.config.config as config +import qutebrowser.keyinput.modes as modes import qutebrowser.utils.message as message -import qutebrowser.utils.modemanager as modemanager import qutebrowser.utils.webelem as webelem from qutebrowser.browser.webpage import BrowserPage from qutebrowser.browser.hints import HintManager @@ -86,7 +86,7 @@ class BrowserTab(QWebView): self.page_.setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page_.linkHovered.connect(self.linkHovered) self.linkClicked.connect(self.on_link_clicked) - self.loadStarted.connect(lambda: modemanager.maybe_leave("insert")) + self.loadStarted.connect(lambda: modes.maybe_leave("insert")) self.loadFinished.connect(self.on_load_finished) # FIXME find some way to hide scrollbars without setScrollBarPolicy @@ -255,9 +255,9 @@ class BrowserTab(QWebView): webelem.SELECTORS['editable_focused']) logging.debug("focus element: {}".format(not elem.isNull())) if elem.isNull(): - modemanager.maybe_leave("insert") + modes.maybe_leave("insert") else: - modemanager.enter("insert") + modes.enter("insert") @pyqtSlot(str) def set_force_open_target(self, target): @@ -319,11 +319,11 @@ class BrowserTab(QWebView): hitresult = frame.hitTestContent(pos) if self._is_editable(hitresult): logging.debug("Clicked editable element!") - modemanager.enter("insert") + modes.enter("insert") else: logging.debug("Clicked non-editable element!") try: - modemanager.leave("insert") + modes.leave("insert") except ValueError: pass diff --git a/qutebrowser/widgets/statusbar.py b/qutebrowser/widgets/statusbar.py index 959aa0d63..03ac8d7d3 100644 --- a/qutebrowser/widgets/statusbar.py +++ b/qutebrowser/widgets/statusbar.py @@ -23,8 +23,8 @@ from PyQt5.QtWidgets import (QWidget, QLineEdit, QProgressBar, QLabel, QShortcut) from PyQt5.QtGui import QPainter, QKeySequence, QValidator -import qutebrowser.commands.keys as keys -import qutebrowser.utils.modemanager as modemanager +import qutebrowser.keyinput.modes as modes +from qutebrowser.keyinput.commandmode import STARTCHARS from qutebrowser.config.style import set_register_stylesheet, get_stylesheet from qutebrowser.utils.url import urlstring from qutebrowser.commands.parsers import split_cmdline @@ -173,13 +173,13 @@ class StatusBar(QWidget): @pyqtSlot(str) def on_mode_entered(self, mode): """Mark certain modes in the commandline.""" - if mode in modemanager.manager.passthrough: + if mode in modes.manager.passthrough: self.txt.normaltext = "-- {} MODE --".format(mode.upper()) @pyqtSlot(str) def on_mode_left(self, mode): """Clear marked mode.""" - if mode in modemanager.manager.passthrough: + if mode in modes.manager.passthrough: self.txt.normaltext = "" def resizeEvent(self, e): @@ -344,7 +344,7 @@ class _Command(QLineEdit): """ # FIXME we should consider the cursor position. text = self.text() - if text[0] in keys.STARTCHARS: + if text[0] in STARTCHARS: prefix = text[0] text = text[1:] else: @@ -357,7 +357,7 @@ class _Command(QLineEdit): def focusInEvent(self, e): """Extend focusInEvent to enter command mode.""" - modemanager.enter("command") + modes.enter("command") super().focusInEvent(e) def focusOutEvent(self, e): @@ -375,7 +375,7 @@ class _Command(QLineEdit): clear_completion_selection: Always emitted. hide_completion: Always emitted so the completion is hidden. """ - modemanager.leave("command") + modes.leave("command") if e.reason() in [Qt.MouseFocusReason, Qt.TabFocusReason, Qt.BacktabFocusReason, Qt.OtherFocusReason]: self.setText('') @@ -400,7 +400,7 @@ class _CommandValidator(QValidator): Return: A tuple (status, string, pos) as a QValidator should. """ - if any(string.startswith(c) for c in keys.STARTCHARS): + if any(string.startswith(c) for c in STARTCHARS): return (QValidator.Acceptable, string, pos) else: return (QValidator.Invalid, string, pos)