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

Skip to content

Commit 6f0c7ab

Browse files
committed
Template Backend plus fix FigManager for non-GUI backends and add generic focus
1 parent ee76451 commit 6f0c7ab

File tree

6 files changed

+51
-62
lines changed

6 files changed

+51
-62
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,9 @@ def _idle_draw_cntx(self):
17841784
def backend(self):
17851785
return self._backend
17861786

1787+
def focus(self):
1788+
pass
1789+
17871790
def is_saving(self):
17881791
"""
17891792
Returns `True` when the renderer is in the process of saving

lib/matplotlib/backend_managers.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ class FigureManager(cbook.EventEmitter):
7575
"""
7676
def __init__(self, figure, num, **kwargs):
7777
super(FigureManager, self).__init__(**kwargs)
78+
self._backend = get_backend()
79+
7880
self.num = num
81+
self.figure = figure
82+
83+
self._is_gui = hasattr(self._backend, 'Window')
84+
if not self._is_gui:
85+
return
7986

80-
self._backend = get_backend()
8187
self._mainloop = self._backend.MainLoop()
8288
self.window = self._backend.Window('Figure %d' % num)
8389
self.window.mpl_connect('window_destroy_event', self.destroy)
8490

85-
self.figure = figure
86-
8791
w = int(self.figure.bbox.width)
8892
h = int(self.figure.bbox.height)
8993

@@ -137,7 +141,7 @@ def destroy(self, *args):
137141

138142
# Make sure we run this routine only once for the FigureManager
139143
# This ensures the nasty __del__ fix below works.
140-
if getattr(self, '_destroying', False):
144+
if getattr(self, '_destroying', False) or self._is_gui is False:
141145
return
142146

143147
self._destroying = True
@@ -157,7 +161,7 @@ def destroy(self, *args):
157161
def show(self):
158162
"""Shows the figure"""
159163
self.window.show()
160-
self.canvas.grab_focus()
164+
self.canvas.focus()
161165

162166
def full_screen_toggle(self):
163167
"""Toggles whether we show fullscreen, alternatively call
@@ -188,13 +192,16 @@ def backend(self):
188192
return self._backend
189193

190194
def _get_toolbar(self):
191-
# must be inited after the window, drawingArea and figure
192-
# attrs are set
193-
if rcParams['toolbar'] == 'toolmanager':
194-
toolbar = self._backend.Toolbar(self.toolmanager)
195-
else:
196-
toolbar = None
197-
return toolbar
195+
try:
196+
# must be inited after the window, drawingArea and figure
197+
# attrs are set
198+
if rcParams['toolbar'] == 'toolmanager':
199+
toolbar = self._backend.Toolbar(self.toolmanager)
200+
else:
201+
toolbar = None
202+
return toolbar
203+
except:
204+
return None
198205

199206
def show_popup(self, msg):
200207
"""

lib/matplotlib/backend_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def init_dialog(self):
704704

705705
w, h = int(tool_fig.bbox.width), int(tool_fig.bbox.height)
706706

707-
self.dialog.add_element(self.tool_canvas, True, 'center')
707+
self.dialog.add_element(self.tool_canvas, 'center')
708708
self.dialog.set_default_size(w, h)
709709

710710
def _window_destroy(self, *args, **kwargs):

lib/matplotlib/backends/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def pylab_setup(name=None):
7171
backend_mod = get_backend(name)
7272

7373
# Things we pull in from all backends
74-
new_figure_manager = backend_mod.new_figure_manager
74+
new_figure_manager = getattr(backend_mod, 'new_figure_manager', None)
7575

7676
# image backends like pdf, agg or svg do not need to do anything
7777
# for "show" or "draw_if_interactive", so if they are not defined

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ def __init__(self, *args, **kwargs):
219219
self._renderer_init()
220220
default_context = GLib.main_context_get_thread_default() or GLib.main_context_default()
221221

222+
def focus(self):
223+
self.grab_focus()
224+
222225
def destroy(self):
223226
#Gtk.DrawingArea.destroy(self)
224227
self.close_event()

lib/matplotlib/backends/backend_template.py

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Copy this to backend_xxx.py and replace all instances of 'template'
1717
with 'xxx'. Then implement the class methods and functions below, and
1818
add 'xxx' to the switchyard in matplotlib/backends/__init__.py and
19-
'xxx' to the backends list in the validate_backend methon in
19+
'xxx' to the backends list in the validate_backend method in
2020
matplotlib/__init__.py and you're off. You can use your backend with::
2121
2222
import matplotlib
@@ -25,14 +25,14 @@
2525
plot([1,2,3])
2626
show()
2727
28-
matplotlib also supports external backends, so you can place you can
29-
use any module in your PYTHONPATH with the syntax::
28+
matplotlib also supports external backends, by placing
29+
any module in your PYTHONPATH and then using the syntax::
3030
3131
import matplotlib
3232
matplotlib.use('module://my_backend')
3333
3434
where my_backend.py is your module name. This syntax is also
35-
recognized in the rc file and in the -d argument in pylab, e.g.,::
35+
recognized in the rc file and also with the -d argument in pylab, e.g.,::
3636
3737
python simple_plot.py -dmodule://my_backend
3838
@@ -48,6 +48,7 @@
4848
4949
matplotlib/backends/backend_your_backend.py
5050
matplotlib/backend_bases.py
51+
matplotlib/backend_managers.py
5152
matplotlib/backends/__init__.py
5253
matplotlib/__init__.py
5354
matplotlib/_pylab_helpers.py
@@ -68,9 +69,9 @@
6869
import six
6970

7071
import matplotlib
71-
from matplotlib._pylab_helpers import Gcf
7272
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
73-
FigureManagerBase, FigureCanvasBase
73+
WindowBase, FigureCanvasBase, MainLoopBase, ToolbarBase
74+
from matplotlib import backend_tools
7475
from matplotlib.figure import Figure
7576
from matplotlib.transforms import Bbox
7677

@@ -178,41 +179,6 @@ def draw_if_interactive():
178179
"""
179180
pass
180181

181-
def show():
182-
"""
183-
For image backends - is not required
184-
For GUI backends - show() is usually the last line of a pylab script and
185-
tells the backend that it is time to draw. In interactive mode, this may
186-
be a do nothing func. See the GTK backend for an example of how to handle
187-
interactive versus batch mode
188-
"""
189-
for manager in Gcf.get_all_fig_managers():
190-
# do something to display the GUI
191-
pass
192-
193-
194-
def new_figure_manager(num, *args, **kwargs):
195-
"""
196-
Create a new figure manager instance
197-
"""
198-
# if a main-level app must be created, this (and
199-
# new_figure_manager_given_figure) is the usual place to
200-
# do it -- see backend_wx, backend_wxagg and backend_tkagg for
201-
# examples. Not all GUIs require explicit instantiation of a
202-
# main-level app (egg backend_gtk, backend_gtkagg) for pylab
203-
FigureClass = kwargs.pop('FigureClass', Figure)
204-
thisFig = FigureClass(*args, **kwargs)
205-
return new_figure_manager_given_figure(num, thisFig)
206-
207-
208-
def new_figure_manager_given_figure(num, figure):
209-
"""
210-
Create a new figure manager instance for the given figure.
211-
"""
212-
canvas = FigureCanvasTemplate(figure)
213-
manager = FigureManagerTemplate(canvas, num)
214-
return manager
215-
216182

217183
class FigureCanvasTemplate(FigureCanvasBase):
218184
"""
@@ -256,19 +222,29 @@ def print_foo(self, filename, *args, **kwargs):
256222
def get_default_filetype(self):
257223
return 'foo'
258224

259-
class FigureManagerTemplate(FigureManagerBase):
260-
"""
261-
Wrap everything up into a window for the pylab interface
262225

263-
For non interactive backends, the base class does all the work
264-
"""
226+
class WindowTemplate(WindowBase):
227+
def show(self):
228+
pass
229+
230+
231+
class RubberbandTemplate(backend_tools.RubberbandBase):
232+
pass
233+
234+
235+
class SetCursorTemplate(backend_tools.SetCursorBase):
265236
pass
266237

267238
########################################################################
268239
#
269-
# Now just provide the standard names that backend.__init__ is expecting
240+
# Now just provide the standard names that backend.__init__ expects
270241
#
271242
########################################################################
272243

273244
FigureCanvas = FigureCanvasTemplate
274-
FigureManager = FigureManagerTemplate
245+
246+
# Needed for a GUI
247+
MainLoop = MainLoopBase
248+
Window = WindowTemplate
249+
ToolRubberband = RubberbandTemplate
250+
ToolSetCursor = SetCursorTemplate

0 commit comments

Comments
 (0)