Initial str() attempt

This commit is contained in:
Florian Bruhin 2017-12-29 14:22:20 +01:00
parent d9c768ed86
commit 7b17ab4b3f
2 changed files with 17 additions and 17 deletions

View File

@ -28,6 +28,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from qutebrowser.config import configdata, configexc from qutebrowser.config import configdata, configexc
from qutebrowser.utils import utils, log, jinja from qutebrowser.utils import utils, log, jinja
from qutebrowser.misc import objects from qutebrowser.misc import objects
from qutebrowser.keyinput import keyutils
# An easy way to access the config from other code via config.val.foo # An easy way to access the config from other code via config.val.foo
val = None val = None
@ -136,11 +137,7 @@ class KeyConfig:
"""Make sure the given mode exists and normalize the key.""" """Make sure the given mode exists and normalize the key."""
if mode not in configdata.DATA['bindings.default'].default: if mode not in configdata.DATA['bindings.default'].default:
raise configexc.KeybindingError("Invalid mode {}!".format(mode)) raise configexc.KeybindingError("Invalid mode {}!".format(mode))
# FIXME needed? return str(keyutils.KeySequence.parse(key))
# if utils.is_special_key(key):
# # <Ctrl-t>, <ctrl-T>, and <ctrl-t> should be considered equivalent
# return utils.normalize_keystr(key)
return key
def get_bindings_for(self, mode): def get_bindings_for(self, mode):
"""Get the combined bindings for the given mode.""" """Get the combined bindings for the given mode."""

View File

@ -127,10 +127,12 @@ def key_to_string(key):
def keyevent_to_string(e): def keyevent_to_string(e):
"""Convert a QKeyEvent to a meaningful name. """Convert a QKeyEvent to a meaningful name."""
return key_with_modifiers_to_string(int(e.key()) | int(e.modifiers()))
Args:
e: A QKeyEvent. def key_with_modifiers_to_string(key):
"""Convert a Qt.Key with modifiers to a meaningful name.
Return: Return:
A name of the key (combination) as a string or A name of the key (combination) as a string or
@ -154,27 +156,27 @@ def keyevent_to_string(e):
(Qt.MetaModifier, 'Meta'), (Qt.MetaModifier, 'Meta'),
(Qt.ShiftModifier, 'Shift'), (Qt.ShiftModifier, 'Shift'),
]) ])
modifiers = (Qt.Key_Control, Qt.Key_Alt, Qt.Key_Shift, Qt.Key_Meta, modifiers = (Qt.Key_Control | Qt.Key_Alt | Qt.Key_Shift | Qt.Key_Meta |
Qt.Key_AltGr, Qt.Key_Super_L, Qt.Key_Super_R, Qt.Key_Hyper_L, Qt.Key_AltGr | Qt.Key_Super_L | Qt.Key_Super_R |
Qt.Key_Hyper_R, Qt.Key_Direction_L, Qt.Key_Direction_R) Qt.Key_Hyper_L | Qt.Key_Hyper_R | Qt.Key_Direction_L |
if e.key() in modifiers: Qt.Key_Direction_R)
if not (key & ~modifiers):
# Only modifier pressed # Only modifier pressed
return None return None
mod = e.modifiers()
parts = [] parts = []
for (mask, s) in modmask2str.items(): for (mask, s) in modmask2str.items():
if mod & mask and s not in parts: if key & mask and s not in parts:
parts.append(s) parts.append(s)
key_string = key_to_string(e.key()) key_string = key_to_string(key & ~modifiers)
if len(key_string) == 1: if len(key_string) == 1:
category = unicodedata.category(key_string) category = unicodedata.category(key_string)
is_control_char = (category == 'Cc') is_control_char = (category == 'Cc')
else: else:
is_control_char = False is_control_char = False
if e.modifiers() == Qt.ShiftModifier and not is_control_char: if key & ~modifiers == Qt.ShiftModifier and not is_control_char:
parts = [] parts = []
parts.append(key_string) parts.append(key_string)
@ -235,7 +237,8 @@ class KeySequence:
# FIXME handle more than 4 keys # FIXME handle more than 4 keys
def __str__(self): def __str__(self):
return self._sequence.toString() return ''.join(key_with_modifiers_to_string(key)
for key in self._sequence)
def __repr__(self): def __repr__(self):
return utils.get_repr(self, keys=str(self)) return utils.get_repr(self, keys=str(self))