From 6f2f1396a976aa0796dfe39a314bfebdd00a81a0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 25 Feb 2018 22:20:35 -0800 Subject: [PATCH] Make FigureCanvasWx ctor signature similar to FigureCanvasBase. Currently, `FigureCanvasWx` has a signature of `(parent, id, figure)`, unlike the Canvas classes of other backends, which only take `figure` as argument. Change `FigureCanvasWx` to be more similar to other backends. A similar fix will later be applied to `FigureManagerWx` to bring it in line with other Manager classes. The ultimate goal is to get rid of `new_figure_manager` and `new_figure_manager_given_figure` in the backend definitions, which can be replaced by `return FigureManager(FigureCanvas(Figure(...)), num)` and `return FigureManager(FigureCanvas(fig, num))` respectively. Also deprecate the aliases `FigureCanvasWx.frame` and `.tb` (which are named `.window` and `.toolbar` in all other backends). --- .../2018-02-15-AL-deprecations.rst | 6 ++ lib/matplotlib/backends/backend_wx.py | 61 +++++++++++++------ lib/matplotlib/backends/backend_wxagg.py | 4 +- lib/matplotlib/backends/backend_wxcairo.py | 15 +++-- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst index 03320fab415c..cb904b529c9e 100644 --- a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst +++ b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst @@ -5,4 +5,10 @@ The following functions and classes are deprecated: - ``cbook.GetRealpathAndStat`` (which is only a helper for ``get_realpath_and_stat``), - ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead), +- ``FigureCanvasWx.frame`` and ``.tb`` (use ``.window`` and ``.toolbar`` + respectively, which is consistent with other backends), +- the ``FigureCanvasWx`` constructor should not be called with ``(parent, id, + figure)`` as arguments anymore, but just ``figure`` (like all other canvas + classes). Call ``Reparent`` and ``SetId`` to set the parent and id of the + canvas. - ``mathtext.unichr_safe`` (use ``chr`` instead), diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 81b0729b65d8..d3c1e7845d2b 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -613,16 +613,29 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel): wx.WXK_NUMPAD_DELETE: 'delete', } - def __init__(self, parent, id, figure): + def __init__(self, *args): """ - Initialise a FigureWx instance. + Initialize a FigureWx instance. - - Initialise the FigureCanvasBase and wxPanel parents. - - Set event handlers for: - EVT_SIZE (Resize event) - EVT_PAINT (Paint event) + Initialize the FigureCanvasBase and wxPanel parents and set event + handlers. + + Note that the canvas parent is set to the top window + (``wx.App().GetTopWindow()``) and it is the responsibility of the + caller to ``Reparent`` the canvas. """ + if len(args) == 3: + # Legacy signature. + cbook.warn_deprecated( + "3.0", "Passing a parent and id to the FigureCanvasWx " + "constructor is deprecated; use 'SetId' and 'Reparent' if " + "needed.") + parent, id, figure = args + else: + parent = wx.GetApp().GetTopWindow() + id = wx.ID_ANY + figure, = args FigureCanvasBase.__init__(self, figure) # Set preferred window size hint - helps the sizer (if one is # connected) @@ -1227,7 +1240,9 @@ def _get_toolbar(self, statbar): return toolbar def get_canvas(self, fig): - return FigureCanvasWx(self, -1, fig) + canvas = FigureCanvasWx(fig) + canvas.Reparent(self) + return canvas def get_figure_manager(self): DEBUG_MSG("get_figure_manager()", 1, self) @@ -1279,18 +1294,25 @@ class FigureManagerWx(FigureManagerBase): def __init__(self, canvas, num, frame): DEBUG_MSG("__init__()", 1, self) FigureManagerBase.__init__(self, canvas, num) - self.frame = frame self.window = frame - - self.tb = frame.GetToolBar() - self.toolbar = self.tb # consistent with other backends + self.toolbar = frame.GetToolBar() def notify_axes_change(fig): 'this will be called whenever the current axes is changed' - if self.tb is not None: - self.tb.update() + if self.toolbar is not None: + self.toolbar.update() self.canvas.figure.add_axobserver(notify_axes_change) + @property + @cbook.deprecated("3.0") + def frame(self): + return self.window + + @property + @cbook.deprecated("3.0") + def tb(self): + return self.toolbar + def show(self): self.frame.Show() self.canvas.draw() @@ -1477,7 +1499,8 @@ def __init__(self, targetfig): wx.Frame.__init__(self, None, -1, "Configure subplots") toolfig = Figure((6, 3)) - canvas = FigureCanvasWx(self, -1, toolfig) + canvas = FigureCanvasWx(toolfig) + canvas.Reparent(self) # Create a figure manager to manage things figmgr = FigureManager(canvas, 1, self) @@ -1493,7 +1516,7 @@ def __init__(self, targetfig): class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar): def __init__(self, canvas): - wx.ToolBar.__init__(self, canvas.GetParent(), -1) + wx.ToolBar.__init__(self, canvas.GetParent()) NavigationToolbar2.__init__(self, canvas) self.canvas = canvas self._idle = True @@ -1506,7 +1529,9 @@ def __init__(self, canvas): self.retinaFix = 'wxMac' in wx.PlatformInfo def get_canvas(self, frame, fig): - return type(self.canvas)(frame, -1, fig) + canvas = type(self.canvas)(fig) + canvas.Reparent(frame) + return canvas def _init_toolbar(self): DEBUG_MSG("_init_toolbar", 1, self) @@ -1537,7 +1562,7 @@ def pan(self, *args): NavigationToolbar2.pan(self, *args) def configure_subplots(self, evt): - frame = wx.Frame(None, -1, "Configure subplots") + frame = wx.Frame(None, title="Configure subplots") toolfig = Figure((6, 3)) canvas = self.get_canvas(frame, toolfig) @@ -1710,7 +1735,7 @@ class StatusBarWx(wx.StatusBar): """ def __init__(self, parent): - wx.StatusBar.__init__(self, parent, -1) + wx.StatusBar.__init__(self, parent) self.SetFieldsCount(2) self.SetStatusText("None", 1) # self.SetStatusText("Measurement: None", 2) diff --git a/lib/matplotlib/backends/backend_wxagg.py b/lib/matplotlib/backends/backend_wxagg.py index 041f274a78b1..c167bf66c109 100644 --- a/lib/matplotlib/backends/backend_wxagg.py +++ b/lib/matplotlib/backends/backend_wxagg.py @@ -16,7 +16,9 @@ class FigureFrameWxAgg(FigureFrameWx): def get_canvas(self, fig): - return FigureCanvasWxAgg(self, -1, fig) + canvas = FigureCanvasWxAgg(fig) + canvas.Reparent(self) + return canvas class FigureCanvasWxAgg(FigureCanvasAgg, _FigureCanvasWxBase): diff --git a/lib/matplotlib/backends/backend_wxcairo.py b/lib/matplotlib/backends/backend_wxcairo.py index fb3290f2bbc4..5686147426a8 100644 --- a/lib/matplotlib/backends/backend_wxcairo.py +++ b/lib/matplotlib/backends/backend_wxcairo.py @@ -4,17 +4,19 @@ import six import wx +import wx.lib.wxcairo as wxcairo from .backend_cairo import cairo, FigureCanvasCairo, RendererCairo from .backend_wx import ( _BackendWx, _FigureCanvasWxBase, FigureFrameWx, NavigationToolbar2Wx as NavigationToolbar2WxCairo) -import wx.lib.wxcairo as wxcairo class FigureFrameWxCairo(FigureFrameWx): def get_canvas(self, fig): - return FigureCanvasWxCairo(self, -1, fig) + canvas = FigureCanvasWxCairo(fig) + canvas.Reparent(self) + return canvas class FigureCanvasWxCairo(_FigureCanvasWxBase, FigureCanvasCairo): @@ -27,12 +29,13 @@ class FigureCanvasWxCairo(_FigureCanvasWxBase, FigureCanvasCairo): we give a hint as to our preferred minimum size. """ - def __init__(self, parent, id, figure): + def __init__(self, *args): # _FigureCanvasWxBase should be fixed to have the same signature as # every other FigureCanvas and use cooperative inheritance, but in the - # meantime the following will make do. - _FigureCanvasWxBase.__init__(self, parent, id, figure) - FigureCanvasCairo.__init__(self, figure) + # meantime the following will make do. (`args[-1]` is always the + # figure.) + _FigureCanvasWxBase.__init__(self, *args) + FigureCanvasCairo.__init__(self, args[-1]) self._renderer = RendererCairo(self.figure.dpi) def draw(self, drawDC=None): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy