Skip to content

Commit 8fd4f9b

Browse files
anntzerbrunobeltran
authored andcommitted
Simplify handling of Qt modifier keys.
- Replace MODIFIER_KEYS by a simpler private table that doesn't include the names used by Matplotlib ("super"/"alt"/...), instead just get these names from SPECIAL_KEYS (this avoids having to also update _MODIFIER_KEYS for osx, where we use different names). - Deprecate the globals SUPER, ALT, CTLR, SHIFT which were just indices into MODIFIER_KEYS but otherwise meaningless. - Invert the order of _MODIFIERS_KEYS to avoid having to do a `reverse()` later in the code. - Get max_unicode value from Python's sys module.
1 parent 58aeff5 commit 8fd4f9b

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

doc/api/api_changes_3.3/deprecations.rst

+10
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,20 @@ APIs which support the values True, False, and "TeX" for ``ismath``.
594594
~~~~~~~~~~~~~~~~~~~~~
595595
This module is deprecated.
596596

597+
597598
Stricter PDF metadata keys in PGF
598599
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
599600
Saving metadata in PDF with the PGF backend currently normalizes all keys to
600601
lowercase, unlike the PDF backend, which only accepts the canonical case. This
601602
is deprecated; in a future version, only the canonically cased keys listed in
602603
the PDF specification (and the `~.backend_pgf.PdfPages` documentation) will be
603604
accepted.
605+
606+
607+
Qt modifier keys
608+
~~~~~~~~~~~~~~~~
609+
The ``MODIFIER_KEYS``, ``SUPER``, ``ALT``, ``CTRL``, and ``SHIFT``
610+
global variables of the :mod:`matplotlib.backends.backend_qt4agg`,
611+
:mod:`matplotlib.backends.backend_qt4cairo`,
612+
:mod:`matplotlib.backends.backend_qt5agg` and
613+
:mod:`matplotlib.backends.backend_qt5cairo` modules are deprecated.

lib/matplotlib/backends/backend_qt4.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .. import cbook
22
from .backend_qt5 import (
3-
backend_version, SPECIAL_KEYS, SUPER, ALT, CTRL, SHIFT, MODIFIER_KEYS,
3+
backend_version, SPECIAL_KEYS,
4+
SUPER, ALT, CTRL, SHIFT, MODIFIER_KEYS, # These are deprecated.
45
cursord, _create_qApp, _BackendQT5, TimerQT, MainWindow, FigureCanvasQT,
56
FigureManagerQT, NavigationToolbar2QT, SubplotToolQt, exception_handler)
67

lib/matplotlib/backends/backend_qt5.py

+19-25
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,34 @@
5757
QtCore.Qt.Key_Pause: 'pause',
5858
QtCore.Qt.Key_SysReq: 'sysreq',
5959
QtCore.Qt.Key_Clear: 'clear', }
60-
61-
# define which modifier keys are collected on keyboard events.
62-
# elements are (Matplotlib modifier names, Modifier Flag, Qt Key) tuples
63-
SUPER = 0
64-
ALT = 1
65-
CTRL = 2
66-
SHIFT = 3
67-
MODIFIER_KEYS = [('super', QtCore.Qt.MetaModifier, QtCore.Qt.Key_Meta),
68-
('alt', QtCore.Qt.AltModifier, QtCore.Qt.Key_Alt),
69-
('ctrl', QtCore.Qt.ControlModifier, QtCore.Qt.Key_Control),
70-
('shift', QtCore.Qt.ShiftModifier, QtCore.Qt.Key_Shift),
71-
]
72-
7360
if sys.platform == 'darwin':
7461
# in OSX, the control and super (aka cmd/apple) keys are switched, so
7562
# switch them back.
7663
SPECIAL_KEYS.update({QtCore.Qt.Key_Control: 'cmd', # cmd/apple key
7764
QtCore.Qt.Key_Meta: 'control',
7865
})
79-
MODIFIER_KEYS[0] = ('cmd', QtCore.Qt.ControlModifier,
80-
QtCore.Qt.Key_Control)
81-
MODIFIER_KEYS[2] = ('ctrl', QtCore.Qt.MetaModifier,
82-
QtCore.Qt.Key_Meta)
83-
84-
66+
# Define which modifier keys are collected on keyboard events.
67+
# Elements are (Modifier Flag, Qt Key) tuples.
68+
# Order determines the modifier order (ctrl+alt+...) reported by Matplotlib.
69+
_MODIFIER_KEYS = [
70+
(QtCore.Qt.ShiftModifier, QtCore.Qt.Key_Shift),
71+
(QtCore.Qt.ControlModifier, QtCore.Qt.Key_Control),
72+
(QtCore.Qt.AltModifier, QtCore.Qt.Key_Alt),
73+
(QtCore.Qt.MetaModifier, QtCore.Qt.Key_Meta),
74+
]
8575
cursord = {
8676
cursors.MOVE: QtCore.Qt.SizeAllCursor,
8777
cursors.HAND: QtCore.Qt.PointingHandCursor,
8878
cursors.POINTER: QtCore.Qt.ArrowCursor,
8979
cursors.SELECT_REGION: QtCore.Qt.CrossCursor,
9080
cursors.WAIT: QtCore.Qt.WaitCursor,
9181
}
82+
SUPER = 0 # Deprecated.
83+
ALT = 1 # Deprecated.
84+
CTRL = 2 # Deprecated.
85+
SHIFT = 3 # Deprecated.
86+
MODIFIER_KEYS = [ # Deprecated.
87+
(SPECIAL_KEYS[key], mod, key) for mod, key in _MODIFIER_KEYS]
9288

9389

9490
# make place holder
@@ -393,20 +389,19 @@ def _get_key(self, event):
393389
# get names of the pressed modifier keys
394390
# bit twiddling to pick out modifier keys from event_mods bitmask,
395391
# if event_key is a MODIFIER, it should not be duplicated in mods
396-
mods = [name for name, mod_key, qt_key in MODIFIER_KEYS
397-
if event_key != qt_key and (event_mods & mod_key) == mod_key]
392+
mods = [SPECIAL_KEYS[key] for mod, key in _MODIFIER_KEYS
393+
if event_key != key and event_mods & mod]
398394
try:
399395
# for certain keys (enter, left, backspace, etc) use a word for the
400396
# key, rather than unicode
401397
key = SPECIAL_KEYS[event_key]
402398
except KeyError:
403-
# unicode defines code points up to 0x0010ffff
399+
# unicode defines code points up to 0x10ffff (sys.maxunicode)
404400
# QT will use Key_Codes larger than that for keyboard keys that are
405401
# are not unicode characters (like multimedia keys)
406402
# skip these
407403
# if you really want them, you should add them to SPECIAL_KEYS
408-
MAX_UNICODE = 0x10ffff
409-
if event_key > MAX_UNICODE:
404+
if event_key > sys.maxunicode:
410405
return None
411406

412407
key = chr(event_key)
@@ -417,7 +412,6 @@ def _get_key(self, event):
417412
else:
418413
key = key.lower()
419414

420-
mods.reverse()
421415
return '+'.join(mods + [key])
422416

423417
def flush_events(self):

0 commit comments

Comments
 (0)