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

Skip to content

Commit 64d9340

Browse files
committed
Various backend cleanups.
Split out from the qt5cairo and wxcairo PRs. - GTK3: is_drawable is the same as mapped & visible (https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-is-drawable) - Synchronous draw appears unnecessary on GTK3 based on tests (probably was only needed in early animation code). - Most of the Wx printout code has been removed in 84ffb60; this PR just deprecates some remnants of it. MenuButtonWx is likewise unused since the removal of "classic" toolbars in 4243470; this PR deprecates it. - Don't initialize the wx statusbar to "None" (which just looks strange) -- other backends just start with an empty status text. - _convert_agg_to_wx_image is unused; _WX28_clipped_agg_as_bitmap can reasonably be inlined into _convert_agg_to_wx_bitmap.
1 parent d9b5b4e commit 64d9340

File tree

8 files changed

+50
-120
lines changed

8 files changed

+50
-120
lines changed

doc/sphinxext/mock_gui_toolkits.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
class MyCairoCffi(MagicMock):
6+
__name__ = "cairocffi"
67
version_info = (1, 4, 0)
78

89

@@ -109,14 +110,7 @@ def getapi(*args):
109110

110111

111112
class MyWX(MagicMock):
112-
class Panel(object):
113-
pass
114-
115-
class ToolBar(object):
116-
pass
117-
118-
class Frame(object):
119-
pass
113+
Button = Frame = Panel = ToolBar = type("", (), {})
120114

121115

122116
def setup(app):

lib/matplotlib/backends/backend_agg.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,25 +412,21 @@ def restore_region(self, region, bbox=None, xy=None):
412412
return renderer.restore_region(region, bbox, xy)
413413

414414
def draw(self):
415-
"""
416-
Draw the figure using the renderer
417-
"""
415+
"""Draw the figure using the renderer."""
418416
self.renderer = self.get_renderer(cleared=True)
419-
# acquire a lock on the shared font cache
420-
RendererAgg.lock.acquire()
421-
422417
toolbar = self.toolbar
423418
try:
424419
# if toolbar:
425420
# toolbar.set_cursor(cursors.WAIT)
426-
self.figure.draw(self.renderer)
427-
# A GUI class may be need to update a window using this draw, so
428-
# don't forget to call the superclass.
429-
super().draw()
421+
with RendererAgg.lock:
422+
self.figure.draw(self.renderer)
423+
# A GUI class may be need to update a window using this draw,
424+
# so don't forget to call the superclass.
425+
super().draw()
430426
finally:
431427
# if toolbar:
432428
# toolbar.set_cursor(toolbar._lastCursor)
433-
RendererAgg.lock.release()
429+
pass
434430

435431
def get_renderer(self, cleared=False):
436432
l, b, w, h = self.figure.bbox.bounds

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,8 @@ def on_draw_event(self, widget, ctx):
273273
pass
274274

275275
def draw(self):
276-
if self.get_visible() and self.get_mapped():
276+
if self.is_drawable():
277277
self.queue_draw()
278-
# do a synchronous draw (its less efficient than an async draw,
279-
# but is required if/when animation is used)
280-
self.get_property("window").process_updates(False)
281278

282279
def draw_idle(self):
283280
if self._idle_draw_id != 0:

lib/matplotlib/backends/backend_gtk3cairo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from . import backend_cairo, backend_gtk3
22
from .backend_cairo import cairo, HAS_CAIRO_CFFI
33
from .backend_gtk3 import _BackendGTK3
4+
from matplotlib import cbook
45
from matplotlib.backend_bases import cursors
56

67

@@ -40,11 +41,11 @@ def on_draw_event(self, widget, ctx):
4041
return False # finish event propagation?
4142

4243

44+
@cbook.deprecated("3.0", "backend_gtk3.FigureManagerGTK3")
4345
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
4446
pass
4547

4648

4749
@_BackendGTK3.export
4850
class _BackendGTK3Cairo(_BackendGTK3):
4951
FigureCanvas = FigureCanvasGTK3Cairo
50-
FigureManager = FigureManagerGTK3Cairo

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from __future__ import (absolute_import, division, print_function,
2-
unicode_literals)
31
import six
42

53
import functools
64
import os
75
import re
86
import signal
97
import sys
10-
from six import unichr
118
import traceback
129

1310
import matplotlib
@@ -437,7 +434,7 @@ def _get_key(self, event):
437434
if event_key > MAX_UNICODE:
438435
return None
439436

440-
key = unichr(event_key)
437+
key = chr(event_key)
441438
# qt delivers capitalized letters. fix capitalization
442439
# note that capslock is ignored
443440
if 'shift' in mods:

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Render to qt from agg
2+
Render to qt from agg.
33
"""
44

55
import ctypes
@@ -30,8 +30,8 @@ def paintEvent(self, e):
3030
return
3131
self._draw_idle() # Only does something if a draw is pending.
3232

33-
# if the canvas does not have a renderer, then give up and wait for
34-
# FigureCanvasAgg.draw(self) to be called
33+
# If the canvas does not have a renderer, then give up and wait for
34+
# FigureCanvasAgg.draw(self) to be called.
3535
if not hasattr(self, 'renderer'):
3636
return
3737

lib/matplotlib/backends/backend_wx.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
Copyright (C) Jeremy O'Donoghue & John Hunter, 2003-4.
88
"""
99

10-
import six
11-
1210
import os.path
1311
import math
1412
import sys
@@ -21,13 +19,13 @@
2119
NavigationToolbar2, RendererBase, TimerBase, cursors)
2220
from matplotlib.backend_bases import _has_pil
2321

22+
from matplotlib import cbook, rcParams, backend_tools
2423
from matplotlib._pylab_helpers import Gcf
2524
from matplotlib.cbook import is_writable_file_like, warn_deprecated
2625
from matplotlib.figure import Figure
2726
from matplotlib.path import Path
2827
from matplotlib.transforms import Affine2D
2928
from matplotlib.widgets import SubplotTool
30-
from matplotlib import cbook, rcParams, backend_tools
3129

3230
import wx
3331

@@ -37,30 +35,25 @@
3735
# traceback is performed, and pdb activated, for all uncaught exceptions in
3836
# this case
3937
_DEBUG = 5
40-
if _DEBUG < 5:
41-
import traceback
42-
import pdb
4338
_DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
4439

4540

4641
def DEBUG_MSG(string, lvl=3, o=None):
4742
if lvl >= _DEBUG:
48-
cls = o.__class__
49-
# Jeremy, often times the commented line won't print but the
50-
# one below does. I think WX is redefining stderr, damned
51-
# beast
52-
# print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls),
53-
# file=sys.stderr)
54-
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls))
43+
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, type(o)))
5544

5645

46+
@cbook.deprecated("3.0")
5747
def debug_on_error(type, value, tb):
5848
"""Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
49+
import pdb
50+
import traceback
5951
traceback.print_exception(type, value, tb)
6052
print()
61-
pdb.pm() # jdh uncomment
53+
pdb.pm()
6254

6355

56+
@cbook.deprecated("3.0")
6457
class fake_stderr(object):
6558
"""
6659
Wx does strange things with stderr, as it makes the assumption that
@@ -95,9 +88,10 @@ def error_msg_wx(msg, parent=None):
9588
return None
9689

9790

91+
@cbook.deprecated("3.0")
9892
def raise_msg_to_str(msg):
9993
"""msg is a return arg from a raise. Join with new lines."""
100-
if not isinstance(msg, six.string_types):
94+
if not isinstance(msg, str):
10195
msg = '\n'.join(map(str, msg))
10296
return msg
10397

@@ -1109,22 +1103,16 @@ def _print_image(self, filename, filetype, *args, **kwargs):
11091103
image = self.bitmap.ConvertToImage()
11101104
image.SetOption(wx.IMAGE_OPTION_QUALITY, str(jpeg_quality))
11111105

1112-
# Now that we have rendered into the bitmap, save it
1113-
# to the appropriate file type and clean up
1114-
if isinstance(filename, six.string_types):
1106+
# Now that we have rendered into the bitmap, save it to the appropriate
1107+
# file type and clean up.
1108+
if isinstance(filename, str):
11151109
if not image.SaveFile(filename, filetype):
1116-
DEBUG_MSG('print_figure() file save error', 4, self)
1117-
raise RuntimeError(
1118-
'Could not save figure to %s\n' %
1119-
(filename))
1110+
raise RuntimeError('Could not save figure to %s' % filename)
11201111
elif is_writable_file_like(filename):
11211112
if not isinstance(image, wx.Image):
11221113
image = image.ConvertToImage()
11231114
if not image.SaveStream(filename, filetype):
1124-
DEBUG_MSG('print_figure() file save error', 4, self)
1125-
raise RuntimeError(
1126-
'Could not save figure to %s\n' %
1127-
(filename))
1115+
raise RuntimeError('Could not save figure to %s' % filename)
11281116

11291117
# Restore everything to normal
11301118
self.bitmap = origBitmap
@@ -1297,11 +1285,7 @@ def resize(self, width, height):
12971285
self.canvas.SetInitialSize(wx.Size(width, height))
12981286
self.window.GetSizer().Fit(self.window)
12991287

1300-
# Identifiers for toolbar controls - images_wx contains bitmaps for the images
1301-
# used in the controls. wxWindows does not provide any stock images, so I've
1302-
# 'stolen' those from GTK2, and transformed them into the appropriate format.
1303-
# import images_wx
1304-
1288+
# Identifiers for toolbar controls.
13051289

13061290
_NTB_AXISMENU = wx.NewId()
13071291
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1313,7 +1297,6 @@ def resize(self, width, height):
13131297
_NTB_Y_PAN_DOWN = wx.NewId()
13141298
_NTB_Y_ZOOMIN = wx.NewId()
13151299
_NTB_Y_ZOOMOUT = wx.NewId()
1316-
# _NTB_SUBPLOT =wx.NewId()
13171300
_NTB_SAVE = wx.NewId()
13181301
_NTB_CLOSE = wx.NewId()
13191302

@@ -1337,6 +1320,7 @@ def _load_bitmap(filename):
13371320
return bmp
13381321

13391322

1323+
@cbook.deprecated("3.0")
13401324
class MenuButtonWx(wx.Button):
13411325
"""
13421326
wxPython does not permit a menu to be incorporated directly into a toolbar.
@@ -1414,7 +1398,7 @@ def updateAxes(self, maxAxis):
14141398
"""Ensures that there are entries for max_axis axes in the menu
14151399
(selected by default)."""
14161400
if maxAxis > len(self._axisId):
1417-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1401+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14181402
menuId = wx.NewId()
14191403
self._axisId.append(menuId)
14201404
self._menu.Append(menuId, "Axis %d" % i,
@@ -1687,16 +1671,10 @@ class StatusBarWx(wx.StatusBar):
16871671
def __init__(self, parent):
16881672
wx.StatusBar.__init__(self, parent, -1)
16891673
self.SetFieldsCount(2)
1690-
self.SetStatusText("None", 1)
1691-
# self.SetStatusText("Measurement: None", 2)
1692-
# self.Reposition()
16931674

16941675
def set_function(self, string):
16951676
self.SetStatusText("%s" % string, 1)
16961677

1697-
# def set_measurement(self, string):
1698-
# self.SetStatusText("Measurement: %s" % string, 2)
1699-
17001678

17011679
# tools for matplotlib.backend_managers.ToolManager:
17021680
# for now only SaveFigure, SetCursor and Rubberband are implemented
@@ -1842,6 +1820,7 @@ def remove_rubberband(self, dc=None):
18421820

18431821
# < Additions for printing support: Matt Newville
18441822

1823+
@cbook.deprecated("3.0")
18451824
class PrintoutWx(wx.Printout):
18461825
"""
18471826
Simple wrapper around wx Printout class -- all the real work
@@ -1923,7 +1902,6 @@ def OnPrintPage(self, page):
19231902
self.canvas.figure.dpi = fig_dpi
19241903
self.canvas.draw()
19251904
return True
1926-
# >
19271905

19281906

19291907
@_Backend.export

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import six
2-
31
import wx
42

53
import matplotlib
@@ -64,33 +62,11 @@ def blit(self, bbox=None):
6462
srcDC.SelectObject(wx.NullBitmap)
6563
self.gui_repaint()
6664

67-
filetypes = FigureCanvasAgg.filetypes
68-
69-
70-
# agg/wxPython image conversion functions (wxPython >= 2.8)
71-
72-
def _convert_agg_to_wx_image(agg, bbox):
73-
"""
74-
Convert the region of the agg buffer bounded by bbox to a wx.Image. If
75-
bbox is None, the entire buffer is converted.
76-
77-
Note: agg must be a backend_agg.RendererAgg instance.
78-
"""
79-
if bbox is None:
80-
# agg => rgb -> image
81-
image = wx.Image(int(agg.width), int(agg.height))
82-
image.SetData(agg.tostring_rgb())
83-
return image
84-
else:
85-
# agg => rgba buffer -> bitmap => clipped bitmap => image
86-
return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox))
87-
8865

8966
def _convert_agg_to_wx_bitmap(agg, bbox):
9067
"""
9168
Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
9269
bbox is None, the entire buffer is converted.
93-
9470
Note: agg must be a backend_agg.RendererAgg instance.
9571
"""
9672
if bbox is None:
@@ -99,36 +75,27 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
9975
agg.buffer_rgba())
10076
else:
10177
# agg => rgba buffer -> bitmap => clipped bitmap
102-
return _WX28_clipped_agg_as_bitmap(agg, bbox)
78+
l, b, width, height = bbox.bounds
79+
r = l + width
80+
t = b + height
10381

82+
srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
83+
agg.buffer_rgba())
84+
srcDC = wx.MemoryDC()
85+
srcDC.SelectObject(srcBmp)
10486

105-
def _WX28_clipped_agg_as_bitmap(agg, bbox):
106-
"""
107-
Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap.
108-
109-
Note: agg must be a backend_agg.RendererAgg instance.
110-
"""
111-
l, b, width, height = bbox.bounds
112-
r = l + width
113-
t = b + height
114-
115-
srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height),
116-
agg.buffer_rgba())
117-
srcDC = wx.MemoryDC()
118-
srcDC.SelectObject(srcBmp)
119-
120-
destBmp = wx.Bitmap(int(width), int(height))
121-
destDC = wx.MemoryDC()
122-
destDC.SelectObject(destBmp)
87+
destBmp = wx.Bitmap(int(width), int(height))
88+
destDC = wx.MemoryDC()
89+
destDC.SelectObject(destBmp)
12390

124-
x = int(l)
125-
y = int(int(agg.height) - t)
126-
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
91+
x = int(l)
92+
y = int(int(agg.height) - t)
93+
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
12794

128-
srcDC.SelectObject(wx.NullBitmap)
129-
destDC.SelectObject(wx.NullBitmap)
95+
srcDC.SelectObject(wx.NullBitmap)
96+
destDC.SelectObject(wx.NullBitmap)
13097

131-
return destBmp
98+
return destBmp
13299

133100

134101
@_BackendWx.export

0 commit comments

Comments
 (0)