2014-04-23 16:57:12 +02:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Mode manager singleton which handles the current keyboard mode.
|
|
|
|
|
|
|
|
Module attributes:
|
|
|
|
manager: The ModeManager instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2014-04-25 06:22:01 +02:00
|
|
|
from PyQt5.QtGui import QWindow
|
2014-04-24 18:31:01 +02:00
|
|
|
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QEvent
|
2014-05-05 17:56:14 +02:00
|
|
|
from PyQt5.QtWidgets import QApplication
|
2014-04-23 16:57:12 +02:00
|
|
|
|
2014-04-24 16:53:16 +02:00
|
|
|
import qutebrowser.config.config as config
|
2014-04-24 23:09:12 +02:00
|
|
|
import qutebrowser.commands.utils as cmdutils
|
2014-04-25 00:10:07 +02:00
|
|
|
import qutebrowser.utils.debug as debug
|
2014-04-24 16:53:16 +02:00
|
|
|
|
2014-04-23 16:57:12 +02:00
|
|
|
|
2014-05-05 17:56:14 +02:00
|
|
|
def instance():
|
|
|
|
"""Get the global modeman instance."""
|
2014-05-05 20:12:20 +02:00
|
|
|
return QApplication.instance().modeman
|
2014-04-23 16:57:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def enter(mode):
|
|
|
|
"""Enter the mode 'mode'."""
|
2014-05-05 17:56:14 +02:00
|
|
|
instance().enter(mode)
|
2014-04-23 16:57:12 +02:00
|
|
|
|
|
|
|
|
2014-04-24 06:44:58 +02:00
|
|
|
def leave(mode):
|
|
|
|
"""Leave the mode 'mode'."""
|
2014-05-05 17:56:14 +02:00
|
|
|
instance().leave(mode)
|
2014-04-24 06:44:58 +02:00
|
|
|
|
|
|
|
|
2014-04-24 16:03:16 +02:00
|
|
|
def maybe_leave(mode):
|
|
|
|
"""Convenience method to leave 'mode' without exceptions."""
|
|
|
|
try:
|
2014-05-05 17:56:14 +02:00
|
|
|
instance().leave(mode)
|
2014-04-24 16:03:16 +02:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-04-23 16:57:12 +02:00
|
|
|
class ModeManager(QObject):
|
|
|
|
|
|
|
|
"""Manager for keyboard modes.
|
|
|
|
|
|
|
|
Attributes:
|
2014-04-24 06:59:39 +02:00
|
|
|
mode: The current mode (readonly property).
|
|
|
|
passthrough: A list of modes in which to pass through events.
|
2014-04-23 16:57:12 +02:00
|
|
|
_handlers: A dictionary of modes and their handlers.
|
2014-04-24 06:44:58 +02:00
|
|
|
_mode_stack: A list of the modes we're currently in, with the active
|
|
|
|
one on the right.
|
2014-04-25 08:40:46 +02:00
|
|
|
_forward_unbound_keys: If we should forward unbound keys.
|
2014-04-25 09:20:19 +02:00
|
|
|
_releaseevents_to_pass: A list of keys where the keyPressEvent was
|
|
|
|
passed through, so the release event should as
|
|
|
|
well.
|
2014-04-23 17:56:36 +02:00
|
|
|
|
|
|
|
Signals:
|
|
|
|
entered: Emitted when a mode is entered.
|
|
|
|
arg: Name of the entered mode.
|
2014-04-24 07:01:27 +02:00
|
|
|
left: Emitted when a mode is left.
|
|
|
|
arg: Name of the left mode.
|
2014-04-24 18:31:01 +02:00
|
|
|
key_pressed: A key was pressed.
|
2014-04-23 16:57:12 +02:00
|
|
|
"""
|
|
|
|
|
2014-04-23 17:56:36 +02:00
|
|
|
entered = pyqtSignal(str)
|
2014-04-24 07:01:27 +02:00
|
|
|
left = pyqtSignal(str)
|
2014-04-24 15:47:38 +02:00
|
|
|
key_pressed = pyqtSignal('QKeyEvent')
|
2014-04-23 17:56:36 +02:00
|
|
|
|
2014-04-23 23:23:04 +02:00
|
|
|
def __init__(self, parent=None):
|
2014-04-23 16:57:12 +02:00
|
|
|
super().__init__(parent)
|
|
|
|
self._handlers = {}
|
2014-04-24 06:59:39 +02:00
|
|
|
self.passthrough = []
|
2014-04-24 06:44:58 +02:00
|
|
|
self._mode_stack = []
|
2014-04-25 09:20:19 +02:00
|
|
|
self._releaseevents_to_pass = []
|
2014-04-27 21:21:14 +02:00
|
|
|
self._forward_unbound_keys = config.get('input',
|
|
|
|
'forward-unbound-keys')
|
2014-04-24 06:44:58 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mode(self):
|
|
|
|
"""Read-only property for the current mode."""
|
|
|
|
if not self._mode_stack:
|
|
|
|
return None
|
|
|
|
return self._mode_stack[-1]
|
2014-04-23 16:57:12 +02:00
|
|
|
|
2014-04-25 11:21:00 +02:00
|
|
|
def _eventFilter_keypress(self, event):
|
|
|
|
"""Handle filtering of KeyPress events.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event: The KeyPress to examine.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if event should be filtered, False otherwise.
|
|
|
|
"""
|
|
|
|
handler = self._handlers[self.mode]
|
|
|
|
logging.debug("KeyPress, calling handler {}".format(handler))
|
|
|
|
self.key_pressed.emit(event)
|
|
|
|
handled = handler(event) if handler is not None else False
|
|
|
|
|
|
|
|
if handled:
|
|
|
|
filter_this = True
|
|
|
|
elif self.mode in self.passthrough or self._forward_unbound_keys:
|
|
|
|
filter_this = False
|
|
|
|
else:
|
|
|
|
filter_this = True
|
|
|
|
|
|
|
|
if not filter_this:
|
|
|
|
self._releaseevents_to_pass.append(event)
|
|
|
|
|
2014-04-27 21:21:14 +02:00
|
|
|
logging.debug("handled: {}, forward-unbound-keys: {}, passthrough: {} "
|
2014-04-25 11:21:00 +02:00
|
|
|
"--> filter: {}".format(handled,
|
|
|
|
self._forward_unbound_keys,
|
|
|
|
self.mode in self.passthrough,
|
|
|
|
filter_this))
|
|
|
|
return filter_this
|
|
|
|
|
|
|
|
def _eventFilter_keyrelease(self, event):
|
|
|
|
"""Handle filtering of KeyRelease events.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event: The KeyPress to examine.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if event should be filtered, False otherwise.
|
|
|
|
"""
|
|
|
|
# handle like matching KeyPress
|
|
|
|
if event in self._releaseevents_to_pass:
|
|
|
|
# remove all occurences
|
|
|
|
self._releaseevents_to_pass = [
|
|
|
|
e for e in self._releaseevents_to_pass if e != event]
|
|
|
|
filter_this = False
|
|
|
|
else:
|
|
|
|
filter_this = True
|
|
|
|
logging.debug("KeyRelease --> filter: {}".format(filter_this))
|
|
|
|
return filter_this
|
|
|
|
|
2014-04-23 23:22:34 +02:00
|
|
|
def register(self, mode, handler, passthrough=False):
|
2014-04-23 16:57:12 +02:00
|
|
|
"""Register a new mode.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
mode: The name of the mode.
|
|
|
|
handler: Handler for keyPressEvents.
|
2014-04-23 23:22:34 +02:00
|
|
|
passthrough: Whether to pass keybindings in this mode through to
|
|
|
|
the widgets.
|
2014-04-23 16:57:12 +02:00
|
|
|
"""
|
|
|
|
self._handlers[mode] = handler
|
2014-04-23 23:22:34 +02:00
|
|
|
if passthrough:
|
2014-04-24 06:59:39 +02:00
|
|
|
self.passthrough.append(mode)
|
2014-04-23 16:57:12 +02:00
|
|
|
|
2014-04-25 07:13:36 +02:00
|
|
|
@cmdutils.register(instance='modeman', name='enter_mode', hide=True)
|
2014-04-23 16:57:12 +02:00
|
|
|
def enter(self, mode):
|
2014-04-23 17:56:36 +02:00
|
|
|
"""Enter a new mode.
|
|
|
|
|
2014-04-24 06:44:58 +02:00
|
|
|
Args:
|
|
|
|
mode; The name of the mode to enter.
|
|
|
|
|
2014-04-23 17:56:36 +02:00
|
|
|
Emit:
|
|
|
|
entered: With the new mode name.
|
|
|
|
"""
|
2014-04-24 06:44:58 +02:00
|
|
|
logging.debug("Switching mode to {}".format(mode))
|
2014-04-23 23:23:30 +02:00
|
|
|
if mode not in self._handlers:
|
|
|
|
raise ValueError("No handler for mode {}".format(mode))
|
2014-04-24 07:44:54 +02:00
|
|
|
if self._mode_stack and self._mode_stack[-1] == mode:
|
|
|
|
logging.debug("Already at end of stack, doing nothing")
|
|
|
|
return
|
2014-04-24 06:44:58 +02:00
|
|
|
self._mode_stack.append(mode)
|
2014-04-24 07:44:47 +02:00
|
|
|
logging.debug("New mode stack: {}".format(self._mode_stack))
|
2014-04-23 17:56:36 +02:00
|
|
|
self.entered.emit(mode)
|
2014-04-23 22:22:58 +02:00
|
|
|
|
2014-04-24 06:44:58 +02:00
|
|
|
def leave(self, mode):
|
|
|
|
"""Leave a mode.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
mode; The name of the mode to leave.
|
|
|
|
|
|
|
|
Emit:
|
2014-04-24 07:01:27 +02:00
|
|
|
left: With the old mode name.
|
2014-04-24 06:44:58 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._mode_stack.remove(mode)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Mode {} not on mode stack!".format(mode))
|
2014-04-24 07:44:47 +02:00
|
|
|
logging.debug("Leaving mode {}".format(mode))
|
|
|
|
logging.debug("New mode stack: {}".format(self._mode_stack))
|
2014-04-24 07:01:27 +02:00
|
|
|
self.left.emit(mode)
|
2014-04-24 06:44:58 +02:00
|
|
|
|
2014-04-25 10:10:58 +02:00
|
|
|
@cmdutils.register(instance='modeman', name='leave_mode',
|
|
|
|
not_modes=['normal'], hide=True)
|
2014-04-24 23:09:12 +02:00
|
|
|
def leave_current_mode(self):
|
2014-04-25 11:21:00 +02:00
|
|
|
"""Leave the mode we're currently in."""
|
2014-04-25 16:53:23 +02:00
|
|
|
if self.mode == 'normal':
|
2014-04-24 23:09:12 +02:00
|
|
|
raise ValueError("Can't leave normal mode!")
|
|
|
|
self.leave(self.mode)
|
|
|
|
|
2014-04-25 08:40:46 +02:00
|
|
|
@pyqtSlot(str, str)
|
|
|
|
def on_config_changed(self, section, option):
|
|
|
|
"""Update local setting when config changed."""
|
2014-04-27 21:21:14 +02:00
|
|
|
if (section, option) == ('input', 'forward-unbound-keys'):
|
|
|
|
self._forward_unbound_keys = config.get('input',
|
|
|
|
'forward-unbound-keys')
|
2014-04-25 08:40:46 +02:00
|
|
|
|
2014-04-25 11:21:00 +02:00
|
|
|
def eventFilter(self, obj, event):
|
2014-04-23 23:24:46 +02:00
|
|
|
"""Filter all events based on the currently set mode.
|
|
|
|
|
|
|
|
Also calls the real keypress handler.
|
2014-04-24 15:47:38 +02:00
|
|
|
|
2014-04-25 11:21:00 +02:00
|
|
|
Args:
|
|
|
|
event: The KeyPress to examine.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
True if event should be filtered, False otherwise.
|
|
|
|
|
2014-04-24 15:47:38 +02:00
|
|
|
Emit:
|
|
|
|
key_pressed: When a key was actually pressed.
|
2014-04-23 23:24:46 +02:00
|
|
|
"""
|
2014-05-06 23:17:05 +02:00
|
|
|
if not hasattr(self, 'mode'):
|
|
|
|
# FIXME I have no idea how this could possibly happen, but on
|
|
|
|
# Ubuntu it does on startup.
|
|
|
|
# self.mode is a simple @property and we certainly don't delete it,
|
|
|
|
# so this all makes no sense at all.
|
|
|
|
logging.warn("eventFilter called but self.mode doesn't exist!")
|
|
|
|
return False
|
|
|
|
elif self.mode is None:
|
2014-04-24 21:29:28 +02:00
|
|
|
# We got events before mode is set, so just pass them through.
|
|
|
|
return False
|
2014-04-25 11:21:00 +02:00
|
|
|
typ = event.type()
|
2014-04-23 23:24:46 +02:00
|
|
|
if typ not in [QEvent.KeyPress, QEvent.KeyRelease]:
|
|
|
|
# We're not interested in non-key-events so we pass them through.
|
2014-04-23 22:22:58 +02:00
|
|
|
return False
|
2014-04-25 06:22:01 +02:00
|
|
|
if not isinstance(obj, QWindow):
|
|
|
|
# We already handled this same event at some point earlier, so
|
|
|
|
# we're not interested in it anymore.
|
2014-04-25 09:22:01 +02:00
|
|
|
logging.debug("Ignoring event {} for {}".format(
|
2014-04-25 06:39:17 +02:00
|
|
|
debug.EVENTS[typ], obj.__class__.__name__))
|
2014-04-25 06:22:01 +02:00
|
|
|
return False
|
|
|
|
|
2014-04-25 09:22:01 +02:00
|
|
|
logging.debug("Got event {} for {} in mode {}".format(
|
|
|
|
debug.EVENTS[typ], obj.__class__.__name__, self.mode))
|
2014-04-25 06:39:17 +02:00
|
|
|
|
2014-04-25 09:20:19 +02:00
|
|
|
if typ == QEvent.KeyPress:
|
2014-04-25 11:21:00 +02:00
|
|
|
return self._eventFilter_keypress(event)
|
2014-04-25 06:39:17 +02:00
|
|
|
else:
|
2014-04-25 11:21:00 +02:00
|
|
|
return self._eventFilter_keyrelease(event)
|