8000 Merge pull request #17146 from anntzer/mantool · matplotlib/matplotlib@d44ceae · GitHub
[go: up one dir, main page]

Skip to content

Commit d44ceae

Browse files
authored
Merge pull request #17146 from anntzer/mantool
MNT: FigureManager/NavigationToolbar2 cleanups.
2 parents a96b83d + a19495c commit d44ceae

File tree

5 files changed

+25
-56
lines changed

5 files changed

+25
-56
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
backend_tools as tools, cbook, colors, textpath, tight_bbox, transforms,
4747
widgets, get_backend, is_interactive, rcParams)
4848
from matplotlib._pylab_helpers import Gcf
49+
from matplotlib.backend_managers import ToolManager
4950
from matplotlib.transforms import Affine2D
5051
from matplotlib.path import Path
5152

@@ -2586,7 +2587,9 @@ def __init__(self, canvas, num):
25862587
'button_press_event',
25872588
self.button_press)
25882589

2589-
self.toolmanager = None
2590+
self.toolmanager = (ToolManager(canvas.figure)
2591+
if mpl.rcParams['toolbar'] == 'toolmanager'
2592+
else None)
25902593
self.toolbar = None
25912594

25922595
@self.canvas.figure.add_axobserver

lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from matplotlib.backend_bases import (
1616
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1717
StatusbarBase, TimerBase, ToolContainerBase, cursors)
18-
from matplotlib.backend_managers import ToolManager
1918
from matplotlib._pylab_helpers import Gcf
2019
from matplotlib.figure import Figure
2120
from matplotlib.widgets import SubplotTool
@@ -405,17 +404,13 @@ class FigureManagerTk(FigureManagerBase):
405404
The tk.Toolbar
406405
window : tk.Window
407406
The tk.Window
408-
409407
"""
408+
410409
def __init__(self, canvas, num, window):
411410
FigureManagerBase.__init__(self, canvas, num)
412411
self.window = window
413412
self.window.withdraw()
414413
self.set_window_title("Figure %d" % num)
415-
self.canvas = canvas
416-
# If using toolmanager it has to be present when initializing the
417-
# toolbar
418-
self.toolmanager = self._get_toolmanager()
419414
# packing toolbar first, because if space is getting low, last packed
420415
# widget is getting shrunk first (-> the canvas)< 8000 /div>
421416
self.toolbar = self._get_toolbar()
@@ -440,13 +435,6 @@ def _get_toolbar(self):
440435
toolbar = None
441436
return toolbar
442437

443-
def _get_toolmanager(self):
444-
if mpl.rcParams['toolbar'] == 'toolmanager':
445-
toolmanager = ToolManager(self.canvas.figure)
446-
else:
447-
toolmanager = None
448-
return toolmanager
449-
450438
def resize(self, width, height):
451439
self.canvas._tkcanvas.configure(width=width, height=height)
452440

@@ -502,7 +490,6 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
502490
``pack_toolbar=False``.
503491
"""
504492
def __init__(self, canvas, window, *, pack_toolbar=True):
505-
self.canvas = canvas
506493
# Avoid using self.window (prefer self.canvas.get_tk_widget().master),
507494
# so that Tool implementations can reuse the methods.
508495
self.window = window

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from matplotlib.backend_bases import (
1111
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1212
StatusbarBase, TimerBase, ToolContainerBase, cursors)
13-
from matplotlib.backend_managers import ToolManager
1413
from matplotlib.figure import Figure
1514
from matplotlib.widgets import SubplotTool
1615

@@ -343,7 +342,6 @@ def __init__(self, canvas, num):
343342
w = int(self.canvas.figure.bbox.width)
344343
h = int(self.canvas.figure.bbox.height)
345344

346-
self.toolmanager = self._get_toolmanager()
347345
self.toolbar = self._get_toolbar()
348346
self.statusbar = None
349347

@@ -420,14 +418,6 @@ def _get_toolbar(self):
420418
toolbar = None
421419
return toolbar
422420

423-
def _get_toolmanager(self):
424-
# must be initialised after toolbar has been set
425-
if mpl.rcParams['toolbar'] == 'toolmanager':
426-
toolmanager = ToolManager(self.canvas.figure)
427-
else:
428-
toolmanager = None
429-
return toolmanager
430-
431421
def get_window_title(self):
432422
return self.window.get_title()
433423

lib/matplotlib/backends/backend_qt5.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
TimerBase, cursors, ToolContainerBase, StatusbarBase, MouseButton)
1616
import matplotlib.backends.qt_editor.figureoptions as figureoptions
1717
from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
18-
from matplotlib.backend_managers import ToolManager
1918
from . import qt_compat
2019
from .qt_compat import (
2120
QtCore, QtGui, QtWidgets, _isdeleted, is_pyqt5, __version__, QT_API)
@@ -512,12 +511,10 @@ class FigureManagerQT(FigureManagerBase):
512511
The qt.QToolBar
513512
window : qt.QMainWindow
514513
The qt.QMainWindow
515-
516514
"""
517515

518516
def __init__(self, canvas, num):
519517
FigureManagerBase.__init__(self, canvas, num)
520-
self.canvas = canvas
521518
self.window = MainWindow()
522519
self.window.closing.connect(canvas.close_event)
523520
self.window.closing.connect(self._widgetclosed)
@@ -526,19 +523,15 @@ def __init__(self, canvas, num):
526523
image = str(cbook._get_data_path('images/matplotlib.svg'))
527524
self.window.setWindowIcon(QtGui.QIcon(image))
528525

529-
# Give the keyboard focus to the figure instead of the
530-
# manager; StrongFocus accepts both tab and click to focus and
531-
# will enable the canvas to process event w/o clicking.
532-
# ClickFocus only takes the focus is the window has been
533-
# clicked
534-
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
535-
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
526+
# Give the keyboard focus to the figure instead of the manager:
527+
# StrongFocus accepts both tab and click to focus and will enable the
528+
# canvas to process event without clicking.
529+
# https://doc.qt.io/qt-5/qt.html#FocusPolicy-enum
536530
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
537531
self.canvas.setFocus()
538532

539533
self.window._destroying = False
540534

541-
self.toolmanager = self._get_toolmanager()
542535
self.toolbar = self._get_toolbar(self.canvas, self.window)
543536
self.statusbar = None
544537

@@ -603,13 +596,6 @@ def _get_toolbar(self, canvas, parent):
603596
toolbar = None
604597
return toolbar
605598

606-
def _get_toolmanager(self):
607-
if matplotlib.rcParams['toolbar'] == 'toolmanager':
608-
toolmanager = ToolManager(self.canvas.figure)
609-
else:
610-
toolmanager = None
611-
return toolmanager
612-
613599
def resize(self, width, height):
614600
# these are Qt methods so they return sizes in 'virtual' pixels
615601
# so we do not need to worry about dpi scaling here.

lib/matplotlib/backends/backend_wx.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,8 @@ def __init__(self, num, fig):
935935
# By adding toolbar in sizer, we are able to put it at the bottom
936936
# of the frame - so appearance is closer to GTK version
937937

938-
self.toolmanager = self._get_toolmanager()
938+
self.figmgr = FigureManagerWx(self.canvas, num, self)
939+
939940
statusbar = (StatusbarWx(self, self.toolmanager)
940941
if self.toolmanager else StatusBarWx(self))
941942
self.SetStatusBar(statusbar)
@@ -961,15 +962,17 @@ def __init__(self, num, fig):
961962

962963
self.canvas.SetMinSize((2, 2))
963964

964-
self.figmgr = FigureManagerWx(self.canvas, num, self)
965-
966965
self.Bind(wx.EVT_CLOSE, self._onClose)
967966

968967
@cbook.deprecated("3.2", alternative="self.GetStatusBar()")
969968
@property
970969
def statusbar(self):
971970
return self.GetStatusBar()
972971

972+
@property
973+
def toolmanager(self):
974+
return self.figmgr.toolmanager
975+
973976
def _get_toolbar(self):
974977
if mpl.rcParams['toolbar'] == 'toolbar2':
975978
toolbar = NavigationToolbar2Wx(self.canvas)
@@ -979,13 +982,6 @@ def _get_toolbar(self):
979982
toolbar = None
980983
return toolbar
981984

982-
def _get_toolmanager(self):
983-
if mpl.rcParams['toolbar'] == 'toolmanager':
984-
toolmanager = ToolManager(self.canvas.figure)
985-
else:
986-
toolmanager = None
987-
return toolmanager
988-
989985
def get_canvas(self, fig):
990986
return FigureCanvasWx(self, -1, fig)
991987

@@ -1045,8 +1041,16 @@ def __init__(self, canvas, num, frame):
10451041
self.frame = frame
10461042
self.window = frame
10471043

1048-
self.toolmanager = getattr(frame, "toolmanager", None)
1049-
self.toolbar = frame.GetToolBar()
1044+
@property
1045+
def toolbar(self):
1046+
return self.frame.GetToolBar()
1047+
1048+
@toolbar.setter
1049+
def toolbar(self, value):
1050+
# Never allow this, except that base class inits this to None before
1051+
# the frame is set up.
1052+
if value is not None or hasattr(self, "frame"):
1053+
raise AttributeError("can't set attribute")
10501054

10511055
def show(self):
10521056
# docstring inherited
@@ -1110,7 +1114,6 @@ class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
11101114
def __init__(self, canvas):
11111115
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
11121116
NavigationToolbar2.__init__(self, canvas)
1113-
self.canvas = canvas
11141117
self._idle = True
11151118
self.prevZoomRect = None
11161119
# for now, use alternate zoom-rectangle drawing on all

0 commit comments

Comments
 (0)
0