8000 FigureManager/NavigationToolbar2 cleanups. by anntzer · Pull Request #17146 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FigureManager/NavigationToolbar2 cleanups. #17146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
backend_tools as tools, cbook, colors, textpath, tight_bbox, transforms,
widgets, get_backend, is_interactive, rcParams)
from matplotlib._pylab_helpers import Gcf
from matplotlib.backend_managers import ToolManager
from matplotlib.transforms import Affine2D
from matplotlib.path import Path

Expand Down Expand Up @@ -2590,7 +2591,9 @@ def __init__(self, canvas, num):
'button_press_event',
self.button_press)

self.toolmanager = None
self.toolmanager = (ToolManager(canvas.figure)
if mpl.rcParams['toolbar'] == 'toolmanager'
else None)
self.toolbar = None

@self.canvas.figure.add_axobserver
Expand Down
15 changes: 1 addition & 14 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
StatusbarBase, TimerBase, ToolContainerBase, cursors)
from matplotlib.backend_managers import ToolManager
from matplotlib._pylab_helpers import Gcf
from matplotlib.figure import Figure
from matplotlib.widgets import SubplotTool
Expand Down Expand Up @@ -405,17 +404,13 @@ class FigureManagerTk(FigureManagerBase):
The tk.Toolbar
window : tk.Window
The tk.Window

"""

def __init__(self, canvas, num, window):
FigureManagerBase.__init__(self, canvas, num)
self.window = window
self.window.withdraw()
self.set_window_title("Figure %d" % num)
self.canvas = canvas
# If using toolmanager it has to be present when initializing the
# toolbar
self.toolmanager = self._get_toolmanager()
# packing toolbar first, because if space is getting low, last packed
# widget is getting shrunk first (-> the canvas)
self.toolbar = self._get_toolbar()
Expand All @@ -440,13 +435,6 @@ def _get_toolbar(self):
toolbar = None
return toolbar

def _get_toolmanager(self):
if mpl.rcParams['toolbar'] == 'toolmanager':
toolmanager = ToolManager(self.canvas.figure)
else:
toolmanager = None
return toolmanager

def resize(self, width, height):
self.canvas._tkcanvas.configure(width=width, height=height)

Expand Down Expand Up @@ -502,7 +490,6 @@ class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame):
``pack_toolbar=False``.
"""
def __init__(self, canvas, window, *, pack_toolbar=True):
self.canvas = canvas
# Avoid using self.window (prefer self.canvas.get_tk_widget().master),
# so that Tool implementations can reuse the methods.
self.window = window
Expand Down
10 changes: 0 additions & 10 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
StatusbarBase, TimerBase, ToolContainerBase, cursors)
from matplotlib.backend_managers import ToolManager
from matplotlib.figure import Figure
from matplotlib.widgets import SubplotTool

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

self.toolmanager = self._get_toolmanager()
self.toolbar = self._get_toolbar()
self.statusbar = None

Expand Down Expand Up @@ -420,14 +418,6 @@ def _get_toolbar(self):
toolbar = None
return toolbar

def _get_toolmanager(self):
# must be initialised after toolbar has been set
if mpl.rcParams['toolbar'] == 'toolmanager':
toolmanager = ToolManager(self.canvas.figure)
else:
toolmanager = None
return toolmanager

def get_window_title(self):
return self.window.get_title()

Expand Down
22 changes: 4 additions & 18 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
TimerBase, cursors, ToolContainerBase, StatusbarBase, MouseButton)
import matplotlib.backends.qt_editor.figureoptions as figureoptions
from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
from matplotlib.backend_managers import ToolManager
from . import qt_compat
from .qt_compat import (
QtCore, QtGui, QtWidgets, _isdeleted, is_pyqt5, __version__, QT_API)
Expand Down Expand Up @@ -512,12 +511,10 @@ class FigureManagerQT(FigureManagerBase):
The qt.QToolBar
window : qt.QMainWindow
The qt.QMainWindow

"""

def __init__(self, canvas, num):
FigureManagerBase.__init__(self, canvas, num)
self.canvas = canvas
self.window = MainWindow()
self.window.closing.connect(canvas.close_event)
self.window.closing.connect(self._widgetclosed)
Expand All @@ -526,19 +523,15 @@ def __init__(self, canvas, num):
image = str(cbook._get_data_path('images/matplotlib.svg'))
self.window.setWindowIcon(QtGui.QIcon(image))

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

self.window._destroying = False

self.toolmanager = self._get_toolmanager()
self.toolbar = self._get_toolbar(self.canvas, self.window)
self.statusbar = None

Expand Down Expand Up @@ -603,13 +596,6 @@ def _get_toolbar(self, canvas, parent):
toolbar = None
return toolbar

def _get_toolmanager(self):
if matplotlib.rcParams['toolbar'] == 'toolmanager':
toolmanager = ToolManager(self.canvas.figure)
else:
toolmanager = None
return toolmanager

def resize(self, width, height):
# these are Qt methods so they return sizes in 'virtual' pixels
# so we do not need to worry about dpi scaling here.
Expand Down
29 changes: 16 additions & 13 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,8 @@ def __init__(self, num, fig):
# By adding toolbar in sizer, we are able to put it at the bottom
# of the frame - so appearance is closer to GTK version

self.toolmanager = self._get_toolmanager()
self.figmgr = FigureManagerWx(self.canvas, num, self)

statusbar = (StatusbarWx(self, self.toolmanager)
if self.toolmanager else StatusBarWx(self))
self.SetStatusBar(statusbar)
Expand All @@ -961,15 +962,17 @@ def __init__(self, num, fig):

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

self.figmgr = FigureManagerWx(self.canvas, num, self)

self.Bind(wx.EVT_CLOSE, self._onClose)

@cbook.deprecated("3.2", alternative="self.GetStatusBar()")
@property
def statusbar(self):
return self.GetStatusBar()

@property
def toolmanager(self):
return self.figmgr.toolmanager

def _get_toolbar(self):
if mpl.rcParams['toolbar'] == 'toolbar2':
toolbar = NavigationToolbar2Wx(self.canvas)
Expand All @@ -979,13 +982,6 @@ def _get_toolbar(self):
toolbar = None
return toolbar

def _get_toolmanager(self):
if mpl.rcParams['toolbar'] == 'toolmanager':
toolmanager = ToolManager(self.canvas.figure)
else:
toolmanager = None
return toolmanager

def get_canvas(self, fig):
return FigureCanvasWx(self, -1, fig)

Expand Down Expand Up @@ -1045,8 +1041,16 @@ def __init__(self, canvas, num, frame):
self.frame = frame
self.window = frame

self.toolmanager = getattr(frame, "toolmanager", None)
self.toolbar = frame.GetToolBar()
@property
def toolbar(self):
return self.frame.GetToolBar()

@toolbar.setter
def toolbar(self, value):
# Never allow this, except that base class inits this to None before
# the frame is set up.
if value is not None or hasattr(self, "frame"):
raise AttributeError("can't set attribute")

def show(self):
# docstring inherited
Expand Down Expand Up @@ -1110,7 +1114,6 @@ class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
def __init__(self, canvas):
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
NavigationToolbar2.__init__(self, canvas)
self.canvas = canvas
self._idle = True
self.prevZoomRect = None
# for now, use alternate zoom-rectangle drawing on all
Expand Down
0