-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
MEP27
.. contents:: Table of Contents
.. author:: OceanWolf
.. date:: March 2, 2015
Discussion
Main PR (including GTK3):
Backend specific branch diffs:
- https://github.com/OceanWolf/matplotlib/compare/backend-refactor...OceanWolf:backend-refactor-tkagg
- https://github.com/OceanWolf/matplotlib/compare/backend-refactor...OceanWolf:backend-refactor-qt
- https://github.com/OceanWolf/matplotlib/compare/backend-refactor...backend-refactor-wx
This MEP refactors the backends to give a more structured and consistent API, removing generic code and consolidate existing code. To do this we propose splitting:
-
FigureManagerBaseand its derived classes into the core functionality classFigureManagerand a backend specific classWindowBaseand -
ShowBaseand its derived classes intoGcf.show_allandMainLoopBase.
This MEP aims to consolidate the backends API into one single uniform API, removing generic code out of the backend (which includes _pylab_helpers and Gcf), and push code to a more appropriate level in matplotlib. With this we automatically remove inconsistencies that appear in the backends, such as FigureManagerBase.resize(w, h) which sometimes sets the canvas, and other times set the entire window to the dimensions given, depending on the backend.
Two main places for generic code appear in the classes derived from FigureManagerBase and ShowBase.
-
FigureManagerBasehas three jobs at the moment:- The documentation describes it as a ``Helper class for pyplot mode, wraps everything up into a neat bundle''
- But it doesn't just wrap the canvas and toolbar, it also does all of the windowing tasks itself. The conflation of these two tasks gets seen the best in the following line:
self.set_window_title("Figure %d" % num)
```
This combines backend specific code self.set_window_title(title) with matplotlib generic code ``title = "Figure %d" % num``.
3. Currently the backend specific subclass of ``FigureManager`` decides when to end the mainloop. This also seems very wrong as the figure should have no control over the other figures.
-
ShowBasehas two jobs:- It has the job of going through all figure managers registered in
_pylab_helpers.Gcfand telling them to show themselves. - And secondly it has the job of starting a mainloop in the backend to block the main programme and thus keep the figures from dying.
- It has the job of going through all figure managers registered in
The description of this MEP gives us most of the solution:
- To remove the windowing aspect out of
FigureManagerBaseletting it simply wrap this new class along with the other backend classes. Create a newWindowBaseclass that can handle this functionality, with pass-through methods toWindowBase. Classes that extend fromWindowBaseshould also extend from the GUI specific window class to ensure backward compatibility (manager.window == manager.window). - Refactor the mainloop of
ShowBaseintoMainLoopBase, which encapsulates the end of the loop as well. We give an instance ofMainLooptoFigureManageras a key unlock the exit method (requiring all keys returned before the loop can die). Note this opens the possibility for multiple backends to run concurrently. - Now that
FigureManagerBasehas no backend specifics in it, to rename it toFigureManager, and move to a new filebackend_managers.pynoting that:- This allows us to break up the conversion of backends into separate PRs as we can keep the existing
FigureManagerBaseclass and its dependencies intact. - and this also anticipates MEP22 where the new
NavigationBasehas morphed into a backend independentToolManager.
- This allows us to break up the conversion of backends into separate PRs as we can keep the existing
- With just one
FigureManagerclass, we create a newnew_figure_manager, we don't actually need this, but it fullfills as a stopgap measure for backward compatibility.
As eluded to above when discussing MEP 22, this refactor makes it easy to add in new generic features. At the moment, MEP 22 has to make ugly hacks to each class extending from FigureManagerBase. With this code, this only needs to get made in the single FigureManager class. This also makes the later deprecation of NavigationToolbar2 very straightforward, only needing to touch the single FigureManager class
MEP 23 makes for another use case where this refactored code will come in very handy.
As we leave all backend code intact, only adding missing methods to existing classes, this should work seamlessly for all use cases. The only difference will lie for backends that used FigureManager.resize to resize the canvas and not the window, due to the standardisation of the API.
I would envision that the classes made obsolete by this refactor get deprecated and removed on the same timetable as NavigationToolbar2.
If there were any alternative solutions to solving the same problem, they should be discussed here, along with a justification for the chosen approach.