From 8f44b569239ef80bc40b53cff25c25ce4feed92d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 23 Apr 2014 22:22:58 +0200 Subject: [PATCH] Start implementing eventFilter in ModeManager --- THANKS | 1 + qutebrowser/app.py | 6 ++++-- qutebrowser/utils/modemanager.py | 28 +++++++++++++++------------- qutebrowser/widgets/browsertab.py | 3 ++- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/THANKS b/THANKS index d5230912a..225b09651 100644 --- a/THANKS +++ b/THANKS @@ -54,6 +54,7 @@ channels: - scummos - svuorela - kpj + - hyde Thanks to these projects which were essential while developing qutebrowser: - Python diff --git a/qutebrowser/app.py b/qutebrowser/app.py index f90e07a06..861040200 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -45,7 +45,7 @@ import qutebrowser.utils.harfbuzz as harfbuzz harfbuzz.fix() from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox -from PyQt5.QtCore import pyqtSlot, QTimer, QEventLoop +from PyQt5.QtCore import pyqtSignal, pyqtSlot, QTimer, QEventLoop, QEvent import qutebrowser import qutebrowser.commands.utils as cmdutils @@ -129,7 +129,8 @@ class QuteBrowser(QApplication): } self._init_cmds() self.mainwindow = MainWindow() - modemanager.init(self.mainwindow.tabs.keypress, self._keyparsers, self) + modemanager.init(self._keyparsers, self) + self.installEventFilter(modemanager.manager) self.setQuitOnLastWindowClosed(False) self._connect_signals() @@ -249,6 +250,7 @@ class QuteBrowser(QApplication): tabs.currentChanged.connect(self.mainwindow.update_inspector) # status bar + # FIXME what to do here? tabs.keypress.connect(status.keypress) for obj in [kp["normal"], tabs]: obj.set_cmd_text.connect(cmd.set_cmd_text) diff --git a/qutebrowser/utils/modemanager.py b/qutebrowser/utils/modemanager.py index aaa0855bd..523c7adf2 100644 --- a/qutebrowser/utils/modemanager.py +++ b/qutebrowser/utils/modemanager.py @@ -23,25 +23,24 @@ Module attributes: import logging -from PyQt5.QtCore import pyqtSignal, QObject +from PyQt5.QtCore import pyqtSignal, QObject, QEvent manager = None -def init(source, parsers=None, parent=None): +def init(parsers=None, parent=None): """Initialize the global ModeManager. This needs to be done by hand because the import time is before Qt is ready for everything. Args: - source: The keypress source signal. parsers: A dict of KeyParsers to register. parent: Parent to use for ModeManager. """ global manager - manager = ModeManager(source, parsers, parent) + manager = ModeManager(parsers, parent) def enter(mode): @@ -64,7 +63,6 @@ class ModeManager(QObject): """Manager for keyboard modes. Attributes: - _source: The keypress source signal. _handlers: A dictionary of modes and their handlers. mode: The current mode. @@ -78,15 +76,13 @@ class ModeManager(QObject): entered = pyqtSignal(str) leaved = pyqtSignal(str) - def __init__(self, sourcesig, parsers=None, parent=None): + def __init__(self, parsers=None, parent=None): """Constructor. Args: - sourcesig: The signal which gets emitted on a keypress. parsers: A list of parsers to register. """ super().__init__(parent) - self._source = sourcesig self._handlers = {} self.mode = None if parsers is not None: @@ -112,11 +108,17 @@ class ModeManager(QObject): oldmode = self.mode logging.debug("Switching mode: {} -> {}".format(oldmode, mode)) if oldmode is not None: - try: - self._source.disconnect(self._handlers[oldmode]) - except TypeError: - logging.debug("Could not disconnect mode {}".format(oldmode)) self.leaved.emit(oldmode) - self._source.connect(self._handlers[mode]) self.mode = mode self.entered.emit(mode) + + def eventFilter(self, obj, evt): + if evt.type() not in [QEvent.KeyPress, QEvent.KeyRelease]: + return False + elif self.mode == "insert": + return False + elif evt.type() == QEvent.KeyPress: + self._handlers[self.mode](evt) + return True + else: + return True diff --git a/qutebrowser/widgets/browsertab.py b/qutebrowser/widgets/browsertab.py index 0d748bf9f..dd9d3e995 100644 --- a/qutebrowser/widgets/browsertab.py +++ b/qutebrowser/widgets/browsertab.py @@ -28,6 +28,7 @@ from PyQt5.QtWebKitWidgets import QWebView, QWebPage import qutebrowser.utils.url as urlutils import qutebrowser.config.config as config import qutebrowser.utils.message as message +import qutebrowser.utils.modemanager as modemanager from qutebrowser.browser.webpage import BrowserPage from qutebrowser.browser.hints import HintManager from qutebrowser.utils.signals import SignalCache @@ -269,7 +270,7 @@ class BrowserTab(QWebView): hitresult = frame.hitTestContent(pos) if hitresult.isContentEditable(): logging.debug("Clicked editable element!") - self.setFocus() + modemanager.enter("insert") if self._force_open_target is not None: self._open_target = self._force_open_target