Add a qenum_key debug method

This commit is contained in:
Florian Bruhin 2014-06-15 11:11:08 +02:00
parent 59c8d6f9d1
commit b905b44b07
2 changed files with 27 additions and 6 deletions

View File

@ -250,14 +250,14 @@ class ModeManager(QObject):
# We already handled this same event at some point earlier, so
# we're not interested in it anymore.
logger.debug("Ignoring event {} for {}".format(
debug.EVENTS[typ], obj.__class__.__name__))
debug.qenum_key(QEvent, typ), obj.__class__.__name__))
return False
if QCoreApplication.instance().activeWindow() is not self.mainwindow:
# Some other window (print dialog, etc.) is focused so we pass
# the event through.
return False
logger.debug("Got event {} for {} in mode {}".format(
debug.EVENTS[typ], obj.__class__.__name__, self.mode))
debug.qenum_key(QEvent, typ), obj.__class__.__name__, self.mode))
if typ == QEvent.KeyPress:
return self._eventFilter_keypress(event)

View File

@ -33,9 +33,6 @@ except ImportError:
import qutebrowser.commands.utils as cmdutils
EVENTS = {n: x for x, n in vars(QEvent).items()
if isinstance(n, QEvent.Type)}
def log_events(klass):
"""Class decorator to log Qt events."""
@ -45,7 +42,7 @@ def log_events(klass):
def new_event(self, e, *args, **kwargs):
"""Wrapper for event() which logs events."""
logger.debug("Event in {}: {}".format(klass.__name__,
EVENTS[e.type()]))
qenum_key(QEvent, e.type())))
return old_event(self, e, *args, **kwargs)
klass.event = new_event
@ -87,3 +84,27 @@ def trace_lines(do_trace):
sys.settrace(trace)
else:
sys.settrace(None)
def qenum_key(base, value):
"""Convert a Qt Enum value to its key as a string.
Args:
base: The object the enum is in, e.g. QFrame.
value: The value to get.
Return:
The key associated with the value as a string, or None.
"""
klass = value.__class__
try:
idx = klass.staticMetaObject.indexOfEnumerator(klass.__name__)
except AttributeError:
idx = -1
if idx != -1:
return klass.staticMetaObject.enumerator(idx).valueToKey(value)
else:
for name, obj in vars(base).items():
if isinstance(obj, klass) and obj == value:
return name
return None