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

Skip to content

Commit aaa7f1b

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. - _convert_agg_to_wx_image is unused; _WX28_clipped_agg_as_bitmap can reasonably be inlined into _convert_agg_to_wx_bitmap.
1 parent 1dfa196 commit aaa7f1b

File tree

8 files changed

+46
-101
lines changed

8 files changed

+46
-101
lines changed

doc/conf.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,7 @@ def _check_deps():
336336

337337

338338
class MyWX(MagicMock):
339-
class Panel(object):
340-
pass
341-
342-
class ToolBar(object):
343-
pass
344-
345-
class Frame(object):
346-
pass
347-
339+
Frame = Panel = Printout = ToolBar = type("", (), {})
348340
VERSION_STRING = '2.9'
349341

350342

lib/matplotlib/backends/backend_agg.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,22 +418,17 @@ def restore_region(self, region, bbox=None, xy=None):
418418
return renderer.restore_region(region, bbox, xy)
419419

420420
def draw(self):
421-
"""
422-
Draw the figure using the renderer
423-
"""
421+
"""Draw the figure using the renderer."""
424422
self.renderer = self.get_renderer(cleared=True)
425-
# acquire a lock on the shared font cache
426-
RendererAgg.lock.acquire()
427-
428423
toolbar = self.toolbar
429424
try:
430425
if toolbar:
431426
toolbar.set_cursor(cursors.WAIT)
432-
self.figure.draw(self.renderer)
427+
with RendererAgg.lock:
428+
self.figure.draw(self.renderer)
433429
finally:
434430
if toolbar:
435431
toolbar.set_cursor(toolbar._lastCursor)
436-
RendererAgg.lock.release()
437432

438433
def get_renderer(self, cleared=False):
439434
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
@@ -275,11 +275,8 @@ def on_draw_event(self, widget, ctx):
275275
pass
276276

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

284281
def draw_idle(self):
285282
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
@@ -6,6 +6,7 @@
66
from . import backend_cairo, backend_gtk3
77
from .backend_cairo import cairo, HAS_CAIRO_CFFI
88
from .backend_gtk3 import _BackendGTK3
9+
from matplotlib import cbook
910
from matplotlib.backend_bases import cursors
1011

1112

@@ -45,11 +46,11 @@ def on_draw_event(self, widget, ctx):
4546
return False # finish event propagation?
4647

4748

49+
@cbook.deprecated("2.2", "backend_gtk3.FigureManagerGTK3")
4850
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
4951
pass
5052

5153

5254
@_BackendGTK3.export
5355
class _BackendGTK3Cairo(_BackendGTK3):
5456
FigureCanvas = FigureCanvasGTK3Cairo
55-
FigureManager = FigureManagerGTK3Cairo

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import re
88
import signal
99
import sys
10-
from six import unichr
1110

1211
import matplotlib
1312

@@ -408,7 +407,7 @@ def _get_key(self, event):
408407
if event_key > MAX_UNICODE:
409408
return None
410409

411-
key = unichr(event_key)
410+
key = six.unichr(event_key)
412411
# qt delivers capitalized letters. fix capitalization
413412
# note that capslock is ignored
414413
if 'shift' in mods:

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ def paintEvent(self, e):
5757
In Qt, all drawing should be done inside of here when a widget is
5858
shown onscreen.
5959
"""
60-
# if there is a pending draw, run it now as we need the updated render
61-
# to paint the widget
60+
# If there is a pending draw, run it now as we need the updated render
61+
# to paint the widget.
6262
if self._agg_draw_pending:
6363
self.__draw_idle_agg()
6464
# As described in __init__ above, we need to be careful in cases with
6565
# mixed resolution displays if dpi_ratio is changing between painting
6666
# events.
6767
if self._dpi_ratio != self._dpi_ratio_prev:
68-
# We need to update the figure DPI
68+
# We need to update the figure DPI.
6969
self._update_figure_dpi()
7070
self._dpi_ratio_prev = self._dpi_ratio
7171
# The easiest way to resize the canvas is to emit a resizeEvent
@@ -79,8 +79,8 @@ def paintEvent(self, e):
7979
# resizeEvent triggers a paintEvent itself, so we exit this one.
8080
return
8181

82-
# if the canvas does not have a renderer, then give up and wait for
83-
# FigureCanvasAgg.draw(self) to be called
82+
# If the canvas does not have a renderer, then give up and wait for
83+
# FigureCanvasAgg.draw(self) to be called.
8484
if not hasattr(self, 'renderer'):
8585
return
8686

lib/matplotlib/backends/backend_wx.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
from __future__ import (absolute_import, division, print_function,
1717
unicode_literals)
1818

19-
from six.moves import xrange
19+
import six
2020

21-
import sys
22-
import os
2321
import os.path
2422
import math
23+
import sys
2524
import weakref
2625
import warnings
2726

@@ -31,13 +30,13 @@
3130
NavigationToolbar2, RendererBase, TimerBase, cursors)
3231
from matplotlib.backend_bases import _has_pil
3332

33+
from matplotlib import cbook, rcParams
3434
from matplotlib._pylab_helpers import Gcf
3535
from matplotlib.cbook import is_writable_file_like, warn_deprecated
3636
from matplotlib.figure import Figure
3737
from matplotlib.path import Path
3838
from matplotlib.transforms import Affine2D
3939
from matplotlib.widgets import SubplotTool
40-
from matplotlib import cbook, rcParams
4140

4241
from . import wx_compat as wxc
4342
import wx
@@ -48,29 +47,25 @@
4847
# traceback is performed, and pdb activated, for all uncaught exceptions in
4948
# this case
5049
_DEBUG = 5
51-
if _DEBUG < 5:
52-
import traceback
53-
import pdb
5450
_DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
5551

5652

5753
def DEBUG_MSG(string, lvl=3, o=None):
5854
if lvl >= _DEBUG:
59-
cls = o.__class__
60-
# Jeremy, often times the commented line won't print but the
61-
# one below does. I think WX is redefining stderr, damned
62-
# beast
63-
#print >>sys.stderr, "%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls)
64-
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls))
55+
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, type(o)))
6556

6657

58+
@cbook.deprecated("2.2")
6759
def debug_on_error(type, value, tb):
6860
"""Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
61+
import pdb
62+
import traceback
6963
traceback.print_exception(type, value, tb)
7064
print()
71-
pdb.pm() # jdh uncomment
65+
pdb.pm()
7266

7367

68+
@cbook.deprecated("2.2")
7469
class fake_stderr(object):
7570
"""
7671
Wx does strange things with stderr, as it makes the assumption that
@@ -81,10 +76,6 @@ class fake_stderr(object):
8176
def write(self, msg):
8277
print("Stderr: %s\n\r" % msg)
8378

84-
#if _DEBUG < 5:
85-
#sys.excepthook = debug_on_error
86-
#WxLogger =wx.LogStderr()
87-
#sys.stderr = fake_stderr
8879

8980
# the True dots per inch on the screen; should be display dependent
9081
# see
@@ -109,6 +100,7 @@ def error_msg_wx(msg, parent=None):
109100
return None
110101

111102

103+
@cbook.deprecated("2.2")
112104
def raise_msg_to_str(msg):
113105
"""msg is a return arg from a raise. Join with new lines."""
114106
if not isinstance(msg, six.string_types):
@@ -1303,10 +1295,7 @@ def resize(self, width, height):
13031295
self.canvas.SetInitialSize(wx.Size(width, height))
13041296
self.window.GetSizer().Fit(self.window)
13051297

1306-
# Identifiers for toolbar controls - images_wx contains bitmaps for the images
1307-
# used in the controls. wxWindows does not provide any stock images, so I've
1308-
# 'stolen' those from GTK2, and transformed them into the appropriate format.
1309-
#import images_wx
1298+
# Identifiers for toolbar controls
13101299

13111300
_NTB_AXISMENU = wx.NewId()
13121301
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1318,7 +1307,6 @@ def resize(self, width, height):
13181307
_NTB_Y_PAN_DOWN = wx.NewId()
13191308
_NTB_Y_ZOOMIN = wx.NewId()
13201309
_NTB_Y_ZOOMOUT = wx.NewId()
1321-
#_NTB_SUBPLOT =wx.NewId()
13221310
_NTB_SAVE = wx.NewId()
13231311
_NTB_CLOSE = wx.NewId()
13241312

@@ -1423,7 +1411,7 @@ def updateAxes(self, maxAxis):
14231411
"""Ensures that there are entries for max_axis axes in the menu
14241412
(selected by default)."""
14251413
if maxAxis > len(self._axisId):
1426-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1414+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14271415
menuId = wx.NewId()
14281416
self._axisId.append(menuId)
14291417
self._menu.Append(menuId, "Axis %d" % i,
@@ -1435,7 +1423,7 @@ def updateAxes(self, maxAxis):
14351423
for menuId in self._axisId[maxAxis:]:
14361424
self._menu.Delete(menuId)
14371425
self._axisId = self._axisId[:maxAxis]
1438-
self._toolbar.set_active(list(xrange(maxAxis)))
1426+
self._toolbar.set_active(range(maxAxis))
14391427

14401428
def getActiveAxes(self):
14411429
"""Return a list of the selected axes."""
@@ -1511,8 +1499,8 @@ def _init_toolbar(self):
15111499
continue
15121500
self.wx_ids[text] = wx.NewId()
15131501
wxc._AddTool(self, self.wx_ids, text,
1514-
_load_bitmap(image_file + '.png'),
1515-
tooltip_text)
1502+
_load_bitmap(image_file + '.png'),
1503+
tooltip_text)
15161504

15171505
self.Bind(wx.EVT_TOOL, getattr(self, callback),
15181506
id=self.wx_ids[text])
@@ -1717,6 +1705,7 @@ def set_function(self, string):
17171705

17181706
#< Additions for printing support: Matt Newville
17191707

1708+
@cbook.deprecated("2.2")
17201709
class PrintoutWx(wx.Printout):
17211710
"""
17221711
Simple wrapper around wx Printout class -- all the real work
@@ -1801,7 +1790,7 @@ def OnPrintPage(self, page):
18011790
self.canvas.figure.dpi = fig_dpi
18021791
self.canvas.draw()
18031792
return True
1804-
#>
1793+
18051794

18061795
########################################################################
18071796
#

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ def blit(self, bbox=None):
7777
srcDC.SelectObject(wx.NullBitmap)
7878
self.gui_repaint()
7979

80-
filetypes = FigureCanvasAgg.filetypes
81-
8280
def print_figure(self, filename, *args, **kwargs):
8381
# Use pure Agg renderer to draw
8482
FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
@@ -98,23 +96,6 @@ def get_canvas(self, frame, fig):
9896
# agg/wxPython image conversion functions (wxPython >= 2.8)
9997

10098

101-
def _convert_agg_to_wx_image(agg, bbox):
102-
"""
103-
Convert the region of the agg buffer bounded by bbox to a wx.Image. If
104-
bbox is None, the entire buffer is converted.
105-
106-
Note: agg must be a backend_agg.RendererAgg instance.
107-
"""
108-
if bbox is None:
109-
# agg => rgb -> image
110-
image = wxc.EmptyImage(int(agg.width), int(agg.height))
111-
image.SetData(agg.tostring_rgb())
112-
return image
113-
else:
114-
# agg => rgba buffer -> bitmap => clipped bitmap => image
115-
return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox))
116-
117-
11899
def _convert_agg_to_wx_bitmap(agg, bbox):
119100
"""
120101
Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
@@ -128,36 +109,27 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
128109
agg.buffer_rgba())
129110
else:
130111
# agg => rgba buffer -> bitmap => clipped bitmap
131-
return _WX28_clipped_agg_as_bitmap(agg, bbox)
132-
112+
l, b, width, height = bbox.bounds
113+
r = l + width
114+
t = b + height
133115

134-
def _WX28_clipped_agg_as_bitmap(agg, bbox):
135-
"""
136-
Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap.
137-
138-
Note: agg must be a backend_agg.RendererAgg instance.
139-
"""
140-
l, b, width, height = bbox.bounds
141-
r = l + width
142-
t = b + height
143-
144-
srcBmp = wxc.BitmapFromBuffer(int(agg.width), int(agg.height),
145-
agg.buffer_rgba())
146-
srcDC = wx.MemoryDC()
147-
srcDC.SelectObject(srcBmp)
116+
srcBmp = wxc.BitmapFromBuffer(int(agg.width), int(agg.height),
117+
agg.buffer_rgba())
118+
srcDC = wx.MemoryDC()
119+
srcDC.SelectObject(srcBmp)
148120

149-
destBmp = wxc.EmptyBitmap(int(width), int(height))
150-
destDC = wx.MemoryDC()
151-
destDC.SelectObject(destBmp)
121+
destBmp = wxc.EmptyBitmap(int(width), int(height))
122+
destDC = wx.MemoryDC()
123+
destDC.SelectObject(destBmp)
152124

153-
x = int(l)
154-
y = int(int(agg.height) - t)
155-
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
125+
x = int(l)
126+
y = int(int(agg.height) - t)
127+
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
156128

157-
srcDC.SelectObject(wx.NullBitmap)
158-
destDC.SelectObject(wx.NullBitmap)
129+
srcDC.SelectObject(wx.NullBitmap)
130+
destDC.SelectObject(wx.NullBitmap)
159131

160-
return destBmp
132+
return destBmp
161133

162134

163135
@_BackendWx.export

0 commit comments

Comments
 (0)