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

Skip to content

Commit 9288a0c

Browse files
committed
Rewrite manager creation [skip ci]
1 parent 8bb2bb1 commit 9288a0c

9 files changed

+78
-87
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,8 @@ class FigureCanvasBase:
15761576
A high-level figure instance.
15771577
"""
15781578

1579+
manager_class = FigureManagerBase
1580+
15791581
# Set to one of {"qt", "gtk3", "gtk4", "wx", "tk", "macosx"} if an
15801582
# interactive framework is required, or None otherwise.
15811583
required_interactive_framework = None
@@ -1670,7 +1672,7 @@ def new_manager(cls, figure, num):
16701672
Backends should override this method to instantiate the correct figure
16711673
manager subclass, and perform any additional setup that may be needed.
16721674
"""
1673-
return FigureManagerBase(cls(figure), num)
1675+
return cls.manager_class.create_with_canvas(cls, figure, num)
16741676

16751677
@contextmanager
16761678
def _idle_draw_cntx(self):
@@ -2769,6 +2771,11 @@ def notify_axes_change(fig):
27692771
if self.toolmanager is None and self.toolbar is not None:
27702772
self.toolbar.update()
27712773

2774+
@classmethod
2775+
def create_with_canvas(cls, canvas_class, figure, num):
2776+
canvas = canvas_class(figure)
2777+
return cls(canvas, num)
2778+
27722779
def show(self):
27732780
"""
27742781
For GUI backends, show the figure window and redraw.

lib/matplotlib/backends/_backend_tk.py

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def _on_timer(self):
161161

162162

163163
class FigureCanvasTk(FigureCanvasBase):
164+
manager_class = FigureManagerTk
164165
required_interactive_framework = "tk"
165166

166167
def __init__(self, figure=None, master=None):
@@ -223,43 +224,6 @@ def _update_device_pixel_ratio(self, event=None):
223224
w, h = self.get_width_height(physical=True)
224225
self._tkcanvas.configure(width=w, height=h)
225226

226-
@classmethod
227-
def new_manager(cls, figure, num):
228-
# docstring inherited
229-
with _restore_foreground_window_at_end():
230-
if cbook._get_running_interactive_framework() is None:
231-
cbook._setup_new_guiapp()
232-
_c_internal_utils.Win32_SetProcessDpiAwareness_max()
233-
window = tk.Tk(className="matplotlib")
234-
window.withdraw()
235-
236-
# Put a Matplotlib icon on the window rather than the default tk
237-
# icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
238-
#
239-
# `ImageTk` can be replaced with `tk` whenever the minimum
240-
# supported Tk version is increased to 8.6, as Tk 8.6+ natively
241-
# supports PNG images.
242-
icon_fname = str(cbook._get_data_path(
243-
'images/matplotlib.png'))
244-
icon_img = ImageTk.PhotoImage(file=icon_fname, master=window)
245-
246-
icon_fname_large = str(cbook._get_data_path(
247-
'images/matplotlib_large.png'))
248-
icon_img_large = ImageTk.PhotoImage(
249-
file=icon_fname_large, master=window)
250-
try:
251-
window.iconphoto(False, icon_img_large, icon_img)
252-
except Exception as exc:
253-
# log the failure (due e.g. to Tk version), but carry on
254-
_log.info('Could not load matplotlib icon: %s', exc)
255-
256-
canvas = cls(figure, master=window)
257-
manager = FigureManagerTk(canvas, num, window)
258-
if mpl.is_interactive():
259-
manager.show()
260-
canvas.draw_idle()
261-
return manager
262-
263227
def resize(self, event):
264228
width, height = event.width, event.height
265229

@@ -468,6 +432,44 @@ def __init__(self, canvas, num, window):
468432

469433
self._shown = False
470434

435+
@classmethod
436+
def create_with_canvas(cls, canvas_class, figure, num):
437+
438+
with _restore_foreground_window_at_end():
439+
if cbook._get_running_interactive_framework() is None:
440+
cbook._setup_new_guiapp()
441+
_c_internal_utils.Win32_SetProcessDpiAwareness_max()
442+
window = tk.Tk(className="matplotlib")
443+
window.withdraw()
444+
445+
# Put a Matplotlib icon on the window rather than the default tk
446+
# icon. See https://www.tcl.tk/man/tcl/TkCmd/wm.html#M50
447+
#
448+
# `ImageTk` can be replaced with `tk` whenever the minimum
449+
# supported Tk version is increased to 8.6, as Tk 8.6+ natively
450+
# supports PNG images.
451+
icon_fname = str(cbook._get_data_path(
452+
'images/matplotlib.png'))
453+
icon_img = ImageTk.PhotoImage(file=icon_fname, master=window)
454+
455+
icon_fname_large = str(cbook._get_data_path(
456+
'images/matplotlib_large.png'))
457+
icon_img_large = ImageTk.PhotoImage(
458+
file=icon_fname_large, master=window)
459+
try:
460+
window.iconphoto(False, icon_img_large, icon_img)
461+
except Exception as exc:
462+
# log the failure (due e.g. to Tk version), but carry on
463+
_log.info('Could not load matplotlib icon: %s', exc)
464+
465+
canvas = canvas_class(figure, master=window)
466+
manager = FigureManagerTk(canvas, num, window)
467+
if mpl.is_interactive():
468+
manager.show()
469+
canvas.draw_idle()
470+
return manager
471+
472+
471473
def _update_window_dpi(self, *args):
472474
newdpi = self._window_dpi.get()
473475
self.window.call('tk', 'scaling', newdpi / 72)

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def _mpl_to_gtk_cursor(mpl_cursor):
7171
class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
7272
required_interactive_framework = "gtk3"
7373
_timer_cls = TimerGTK3
74+
manager_class = FigureManagerGTK3
7475
# Setting this as a static constant prevents
7576
# this resulting expression from leaking
7677
event_mask = (Gdk.EventMask.BUTTON_PRESS_MASK
@@ -115,11 +116,6 @@ def __init__(self, figure=None):
115116
style_ctx.add_provider(css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
116117
style_ctx.add_class("matplotlib-canvas")
117118

118-
@classmethod
119-
def new_manager(cls, figure, num):
120-
# docstring inherited
121-
return FigureManagerGTK3(cls(figure), num)
122-
123119
def destroy(self):
124120
self.close_event()
125121

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FigureCanvasGTK4(Gtk.DrawingArea, FigureCanvasBase):
3333
required_interactive_framework = "gtk4"
3434
supports_blit = False
3535
_timer_cls = TimerGTK4
36+
manager_class = FigureManagerGTK4
3637
_context_is_scaled = False
3738

3839
def __init__(self, figure=None):
@@ -78,11 +79,6 @@ def __init__(self, figure=None):
7879
style_ctx.add_provider(css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
7980
style_ctx.add_class("matplotlib-canvas")
8081

81-
@classmethod
82-
def new_manager(cls, figure, num):
83-
# docstring inherited
84-
return FigureManagerGTK4(cls(figure), num)
85-
8682
def pick(self, mouseevent):
8783
# GtkWidget defines pick in GTK4, so we need to override here to work
8884
# with the base implementation we want.

lib/matplotlib/backends/backend_macosx.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class FigureCanvasMac(_macosx.FigureCanvas, FigureCanvasAgg):
2525

2626
required_interactive_framework = "macosx"
2727
_timer_cls = TimerMac
28+
manager_class = FigureManagerMac
2829

2930
def __init__(self, figure):
3031
FigureCanvasBase.__init__(self, figure)
@@ -33,11 +34,6 @@ def __init__(self, figure):
3334
self._draw_pending = False
3435
self._is_drawing = False
3536

36-
@classmethod
37-
def new_manager(cls, figure, num):
38-
# docstring inherited
39-
return FigureManagerMac(cls(figure), num)
40-
4137
def set_cursor(self, cursor):
4238
# docstring inherited
4339
_macosx.set_cursor(cursor)

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ def __init__(self, canvas, num):
7878
self._shown = False
7979
super().__init__(canvas, num)
8080

81+
def create_with_canvas(self, canvas_class, figure, num):
82+
canvas = canvas_class(figure)
83+
manager = FigureManagerNbAgg(canvas, num)
84+
if is_interactive():
85+
manager.show()
86+
figure.canvas.draw_idle()
87+
88+
def destroy(event):
89+
canvas.mpl_disconnect(cid)
90+
Gcf.destroy(manager)
91+
92+
cid = canvas.mpl_connect('close_event', destroy)
93+
return manager
94+
8195
def display_js(self):
8296
# XXX How to do this just once? It has to deal with multiple
8397
# browser instances using the same kernel (require.js - but the
@@ -143,20 +157,7 @@ def remove_comm(self, comm_id):
143157

144158

145159
class FigureCanvasNbAgg(FigureCanvasWebAggCore):
146-
@classmethod
147-
def new_manager(cls, figure, num):
148-
canvas = cls(figure)
149-
manager = FigureManagerNbAgg(canvas, num)
150-
if is_interactive():
151-
manager.show()
152-
figure.canvas.draw_idle()
153-
154-
def destroy(event):
155-
canvas.mpl_disconnect(cid)
156-
Gcf.destroy(manager)
157-
158-
cid = canvas.mpl_connect('close_event', destroy)
159-
return manager
160+
manager_class = FigureManagerNbAgg
160161

161162

162163
class CommSocket:

lib/matplotlib/backends/backend_qt.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def _timer_stop(self):
232232
class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
233233
required_interactive_framework = "qt"
234234
_timer_cls = TimerQT
235+
manager_class = FigureManagerQT
235236

236237
buttond = {
237238
getattr(_enum("QtCore.Qt.MouseButton"), k): v for k, v in [
@@ -260,11 +261,6 @@ def __init__(self, figure=None):
260261
palette = QtGui.QPalette(QtGui.QColor("white"))
261262
self.setPalette(palette)
262263

263-
@classmethod
264-
def new_manager(cls, figure, num):
265-
# docstring inherited
266-
return FigureManagerQT(cls(figure), num)
267-
268264
def _update_pixel_ratio(self):
269265
if self._set_device_pixel_ratio(_devicePixelRatioF(self)):
270266
# The easiest way to resize the canvas is to emit a resizeEvent

lib/matplotlib/backends/backend_webagg.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def run(self):
4949

5050

5151
class FigureCanvasWebAgg(core.FigureCanvasWebAggCore):
52-
@classmethod
53-
def new_manager(cls, figure, num):
54-
# docstring inherited
55-
return core.FigureManagerWebAgg(cls(figure), num)
52+
manager_class = core.FigureManagerWebAgg
5653

5754

5855
class FigureManagerWebAgg(core.FigureManagerWebAgg):

lib/matplotlib/backends/backend_wx.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
427427

428428
required_interactive_framework = "wx"
429429
_timer_cls = TimerWx
430+
manager_class = FigureManagerWx
430431

431432
keyvald = {
432433
wx.WXK_CONTROL: 'control',
@@ -537,17 +538,6 @@ def __init__(self, parent, id, figure=None):
537538
self.SetBackgroundStyle(wx.BG_STYLE_PAINT) # Reduce flicker.
538539
self.SetBackgroundColour(wx.WHITE)
539540

540-
@classmethod
541-
def new_manager(cls, figure, num):
542-
# docstring inherited
543-
wxapp = wx.GetApp() or _create_wxapp()
544-
frame = FigureFrameWx(num, figure, canvas_class=cls)
545-
figmgr = frame.get_figure_manager()
546-
if mpl.is_interactive():
547-
figmgr.frame.Show()
548-
figure.canvas.draw_idle()
549-
return figmgr
550-
551541
def Copy_to_Clipboard(self, event=None):
552542
"""Copy bitmap of canvas to system clipboard."""
553543
bmp_obj = wx.BitmapDataObject()
@@ -990,6 +980,16 @@ def __init__(self, canvas, num, frame):
990980
self.frame = self.window = frame
991981
super().__init__(canvas, num)
992982

983+
@classmethod
984+
def create_with_canvas(cls, canvas_class, figure, num):
985+
wxapp = wx.GetApp() or _create_wxapp()
986+
frame = FigureFrameWx(num, figure, canvas_class=canvas_class)
987+
figmgr = frame.get_figure_manager()
988+
if mpl.is_interactive():
989+
figmgr.frame.Show()
990+
figure.canvas.draw_idle()
991+
return figmgr
992+
993993
def show(self):
994994
# docstring inherited
995995
self.frame.Show()

0 commit comments

Comments
 (0)