Start implementing eventFilter in ModeManager
This commit is contained in:
parent
a33c9827d6
commit
8f44b56923
1
THANKS
1
THANKS
@ -54,6 +54,7 @@ channels:
|
||||
- scummos
|
||||
- svuorela
|
||||
- kpj
|
||||
- hyde
|
||||
|
||||
Thanks to these projects which were essential while developing qutebrowser:
|
||||
- Python
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user