8000 Cleanups to Qt backend. · matplotlib/matplotlib@dbc024b · GitHub
[go: up one dir, main page]

Skip to content

Commit dbc024b

Browse files
committed
Cleanups to Qt backend.
- Move Qt4/Qt5-dependent logic re: devicePixelRatio to qt_compat; these functions are documented to have existed ever since Qt5 was released. - In Toolbar, no need to set `.canvas` (the base class init does it) or `._parent` (one can just return the canvas' `.parent()`); `.basedir` is just a small helper which doesn't exist in other backends and doesn't warrant being public.
1 parent ab2d200 commit dbc024b

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

doc/api/api_changes_3.3/deprecations.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,12 @@ The ``Fil``, ``Fill``, ``Filll``, ``NegFil``, ``NegFill``, ``NegFilll``, and
366366
``SsGlue`` classes in the :mod:`matplotlib.mathtext` module are deprecated.
367367
As an alternative, directly construct glue instances with ``Glue("fil")``, etc.
368368

369-
NavigationToolbar2QT.parent
370-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
371-
This attribute is deprecated. In order to access the parent window, use
369+
NavigationToolbar2QT.parent and .basedir
370+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371+
These attributes are deprecated. In order to access the parent window, use
372372
``toolbar.canvas.parent()``. Once the deprecation period is elapsed, it will
373-
also be accessible as ``toolbar.parent()``.
373+
also be accessible as ``toolbar.parent()``. The base directory to the icons
374+
is ``os.path.join(mpl.get_data_path(), "images")``.
374375

375376
Path helpers in :mod:`.bezier`
376377
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lib/matplotlib/backends/backend_qt5.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import matplotlib.backends.qt_editor.figureoptions as figureoptions
1717
from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
1818
from matplotlib.backend_managers import ToolManager
19-
19+
from . import qt_compat
2020
from .qt_compat import (
21-
QtCore, QtGui, QtWidgets, _isdeleted, _getSaveFileName,
22-
is_pyqt5, __version__, QT_API)
21+
QtCore, QtGui, QtWidgets, _isdeleted, is_pyqt5, __version__, QT_API)
2322

2423
backend_version = __version__
2524

@@ -255,12 +254,7 @@ def _update_figure_dpi(self):
255254

256255
@property
257256
def _dpi_ratio(self):
258-
# Not available on Qt4 or some older Qt5.
259-
try:
260-
# self.devicePixelRatio() returns 0 in rare cases
261-
return self.devicePixelRatio() or 1
262-
except AttributeError:
263-
return 1
257+
return qt_compat._devicePixelRatio(self)
264258

265259
def _update_dpi(self):
266260
# As described in __init__ above, we need to be careful in cases with
@@ -662,26 +656,27 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
662656

663657
def __init__(self, canvas, parent, coordinates 8000 =True):
664658
"""coordinates: should we show the coordinates on the right?"""
665-
self.canvas = canvas
666-
self._parent = parent
667659
self.coordinates = coordinates
668-
self._actions = {}
669-
"""A mapping of toolitem method names to their QActions"""
670-
660+
self._actions = {} # mapping of toolitem method names to QActions.
671661
QtWidgets.QToolBar.__init__(self, parent)
672662
NavigationToolbar2.__init__(self, canvas)
673663

674664
@cbook.deprecated("3.3", alternative="self.canvas.parent()")
675665
@property
676666
def parent(self):
677-
return self._parent
667+
return self.canvas.parent()
668+
669+
@cbook.deprecated(
670+
"3.3", alternative="os.path.join(mpl.get_data_path(), 'images')")
671+
@property
672+
def basedir(self):
673+
return str(cbook._get_data_path('images'))
678< 8000 code>674

679675
def _icon(self, name, color=None):
680676
if is_pyqt5():
681677
name = name.replace('.png', '_large.png')
682-
pm = QtGui.QPixmap(os.path.join(self.basedir, name))
683-
if hasattr(pm, 'setDevicePixelRatio'):
684-
pm.setDevicePixelRatio(self.canvas._dpi_ratio)
678+
pm = QtGui.QPixmap(str(cbook._get_data_path('images', name)))
679+
qt_compat._setDevicePixelRatio(pm, self.canvas._dpi_ratio)
685680
if color is not None:
686681
mask = pm.createMaskFromColor(QtGui.QColor('black'),
687682
QtCore.Qt.MaskOutColor)
@@ -690,8 +685,6 @@ def _icon(self, name, color=None):
690685
return QtGui.QIcon(pm)
691686

692687
def _init_toolbar(self):
693-
self.basedir = str(cbook._get_data_path('images'))
694-
695688
background_color = self.palette().color(self.backgroundRole())
696689
foreground_color = self.palette().color(self.foregroundRole())
697690
icon_color = (foreground_color
@@ -807,9 +800,9 @@ def save_figure(self, *args):
57DF
807800
filters.append(filter)
808801
filters = ';;'.join(filters)
809802

810-
fname, filter = _getSaveFileName(self.canvas.parent(),
811-
"Choose a filename to save to",
812-
start, filters, selectedFilter)
803+
fname, filter = qt_compat._getSaveFileName(
804+
self.canvas.parent(), "Choose a filename to save to", start,
805+
filters, selectedFilter)
813806
if fname:
814807
# Save dir for next time, unless empty str (i.e., use cwd).
815808
if startpath != "":
@@ -945,8 +938,7 @@ def _add_to_group(self, group, name, button, position):
945938

946939
def _icon(self, name):
947940
pm = QtGui.QPixmap(name)
948-
if hasattr(pm, 'setDevicePixelRatio'):
949-
pm.setDevicePixelRatio(self.toolmanager.canvas._dpi_ratio)
941+
qt_compat._setDevicePixelRatio(pm, self.toolmanager.canvas._dpi_ratio)
950942
return QtGui.QIcon(pm)
951943

952944
def toggle_toolitem(self, name, toggled):

lib/matplotlib/backends/qt_compat.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
def _setup_pyqt5():
6969
global QtCore, QtGui, QtWidgets, __version__, is_pyqt5, \
70-
_isdeleted, _getSaveFileName
70+
_isdeleted, _devicePixelRatio, _setDevicePixelRatio, _getSaveFileName
7171

7272
if QT_API == QT_API_PYQT5:
7373
from PyQt5 import QtCore, QtGui, QtWidgets
@@ -88,10 +88,14 @@ def _isdeleted(obj): return not shiboken2.isValid(obj)
8888
def is_pyqt5():
8989
return True
9090

91+
# self.devicePixelRatio() returns 0 in rare cases
92+
def _devicePixelRatio(obj): return obj.devicePixelRatio() or 1
93+
def _setDevicePixelRatio(obj, factor): obj.setDevicePixelRatio(factor)
94+
9195

9296
def _setup_pyqt4():
9397
global QtCore, QtGui, QtWidgets, __version__, is_pyqt5, \
94-
_isdeleted, _getSaveFileName
98+
_isdeleted, _devicePixelRatio, _setDevicePixelRatio, _getSaveFileName
9599

96100
def _setup_pyqt4_internal(api):
97101
global QtCore, QtGui, QtWidgets, \
@@ -143,6 +147,9 @@ def _isdeleted(obj): return not shiboken.isValid(obj)
143147
def is_pyqt5():
144148
return False
145149

150+
def _devicePixelRatio(obj): return 1
151+
def _setDevicePixelRatio(obj, factor): pass
152+
146153

147154
if QT_API in [QT_API_PYQT5, QT_API_PYSIDE2]:
148155
_setup_pyqt5()

0 commit comments

Comments
 (0)
0