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

Skip to content

Commit a19495c

Browse files
committed
Dedupe _get_toolmanager and move it to base class init.
It's mostly simple, except specifically for the wx backend which for some reason previously attached the toolmanager to the Frame (aka "Window" in other toolkits parlance) so needs some properties to redirect things to the right place.
1 parent ca26408 commit a19495c

File tree

5 files changed

+20
-43
lines changed

5 files changed

+20
-43
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

@@ -2590,7 +2591,9 @@ def __init__(self, canvas, num):
25902591
'button_press_event',
25912592
self.button_press)
25922593

2593-
self.toolmanager = None
2594+
self.toolmanager = (ToolManager(canvas.figure)
2595+
if mpl.rcParams['toolbar'] == 'toolmanager'
2596+
else None)
25942597
self.toolbar = None
25952598

25962599
@self.canvas.figure.add_axobserver

lib/matplotlib/backends/_backend_tk.py

Lines changed: 0 additions & 11 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
@@ -412,9 +411,6 @@ def __init__(self, canvas, num, window):
412411
self.window = window
413412
self.window.withdraw()
414413
self.set_window_title("Figure %d" % num)
415-
# If using toolmanager it has to be present when initializing the
416-
# toolbar
417-
self.toolmanager = self._get_toolmanager()
418414
# packing toolbar first, because if space is getting low, last packed
419415
# widget is getting shrunk first (-> the canvas)
420416
self.toolbar = self._get_toolbar()
@@ -439,13 +435,6 @@ def _get_toolbar(self):
439435
toolbar = None
440436
return toolbar
441437

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

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: 0 additions & 9 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)
@@ -533,7 +532,6 @@ def __init__(self, canvas, num):
533532

534533
self.window._destroying = False
535534

536-
self.toolmanager = self._get_toolmanager()
537535
self.toolbar = self._get_toolbar(self.canvas, self.window)
538536
self.statusbar = None
539537

@@ -598,13 +596,6 @@ def _get_toolbar(self, canvas, parent):
598596
toolbar = None
599597
return toolbar
600598

601-
def _get_toolmanager(self):
602-
if matplotlib.rcParams['toolbar'] == 'toolmanager':
603-
toolmanager = ToolManager(self.canvas.figure)
604-
else:
605-
toolmanager = None
606-
return toolmanager
607-
608599
def resize(self, width, height):
609600
# these are Qt methods so they return sizes in 'virtual' pixels
610601
# so we do not need to worry about dpi scaling here.

lib/matplotlib/backends/backend_wx.py

Lines changed: 16 additions & 12 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

0 commit comments

Comments
 (0)