Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 20da503

Browse files
authored
Merge pull request #14507 from anntzer/qtkeys
Simplify handling of Qt modifier keys.
2 parents f624f60 + 59074a6 commit 20da503

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

doc/api/api_changes_3.3/deprecations.rst

Lines changed: 10 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 22 additions & 25 deletions
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
@@ -391,22 +387,24 @@ def _get_key(self, event):
391387
event_mods = int(event.modifiers()) # actually a bitmask
392388

393389
# get names of the pressed modifier keys
390+
# 'control' is named 'control' when a standalone key, but 'ctrl' when a
391+
# modifier
394392
# bit twiddling to pick out modifier keys from event_mods bitmask,
395393
# 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]
394+
mods = [SPECIAL_KEYS[key].replace('control', 'ctrl')
395+
for mod, key in _MODIFIER_KEYS
396+
if event_key != key and event_mods & mod]
398397
try:
399398
# for certain keys (enter, left, backspace, etc) use a word for the
400399
# key, rather than unicode
401400
key = SPECIAL_KEYS[event_key]
402401
except KeyError:
403-
# unicode defines code points up to 0x0010ffff
402+
# unicode defines code points up to 0x10ffff (sys.maxunicode)
404403
# QT will use Key_Codes larger than that for keyboard keys that are
405404
# are not unicode characters (like multimedia keys)
406405
# skip these
407406
# if you really want them, you should add them to SPECIAL_KEYS
408-
MAX_UNICODE = 0x10ffff
409-
if event_key > MAX_UNICODE:
407+
if event_key > sys.maxunicode:
410408
return None
411409

412410
key = chr(event_key)
@@ -417,7 +415,6 @@ def _get_key(self, event):
417415
else:
418416
key = key.lower()
419417

420-
mods.reverse()
421418
return '+'.join(mods + [key])
422419

423420
def flush_events(self):

0 commit comments

Comments
 (0)