8000 Various backend cleanups. · matplotlib/matplotlib@a9d101e · GitHub
[go: up one dir, main page]

Skip to content

Commit a9d101e

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

File tree

8000

9 files changed

+45
-110
lines changed

9 files changed

+45
-110
lines changed

doc/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
#
31
# Matplotlib documentation build configuration file, created by
42
# sphinx-quickstart on Fri May 2 12:33:25 2008.
53
#

doc/sphinxext/mock_gui_toolkits.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
class MyCairoCffi(MagicMock):
10+
__name__ = "cairocffi"
1011
version_info = (1, 4, 0)
1112

1213

@@ -113,15 +114,7 @@ def getapi(*args):
113114

114115

115116
class MyWX(MagicMock):
116-
class Panel(object):
117-
pass
118-
119-
class ToolBar(object):
120-
pass
121-
122-
class Frame(object):
123-
pass
124-
117+
Button = Frame = Panel = Printout = ToolBar = type("", (), {})
125118
VERSION_STRING = '2.9'
126119

127120

lib/matplotlib/backends/backend_agg.py

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

419419
def draw(self):
420-
"""
421-
Draw the figure using the renderer
422-
"""
420+
"""Draw the figure using the renderer."""
423421
self.renderer = self.get_renderer(cleared=True)
424-
# acquire a lock on the shared font cache
425-
RendererAgg.lock.acquire()
426-
427422
toolbar = self.toolbar
428423
try:
429424
# if toolbar:
430425
# toolbar.set_cursor(cursors.WAIT)
431-
self.figure.draw(self.renderer)
432-
# A GUI class may be need to update a window using this draw, so
433-
# don't forget to call the superclass.
434-
super().draw()
426+
with RendererAgg.lock:
427+
self.figure.draw(self.renderer)
428+
# A GUI class may be need to update a window using this draw,
429+
# so don't forget to call the superclass.
430+
super().draw()
435431
finally:
436432
# if toolbar:
437433
# toolbar.set_cursor(toolbar._lastCursor)
438-
RendererAgg.lock.release()
434+
pass
439435

440436
def get_renderer(self, cleared=False):
441437
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("3.0", "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 & 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
@@ -438,7 +435,7 @@ def _get_key(self, event):
438435
if event_key > MAX_UNICODE:
439436
return None
440437

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

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""
2-
Render to qt from agg
2+
Render to qt from agg.
33
"""
4-
from __future__ import (absolute_import, division, print_function,
5-
unicode_literals)
6-
74
import six
85

96
import ctypes
@@ -40,8 +37,8 @@ def paintEvent(self, e):
4037
return
4138
self._draw_idle() # Only does something if a draw is pending.
4239

43-
# if the canvas does not have a renderer, then give up and wait for
44-
# FigureCanvasAgg.draw(self) to be called
40+
# If the canvas does not have a renderer, then give up and wait for
41+
# FigureCanvasAgg.draw(self) to be called.
4542
if not hasattr(self, 'renderer'):
4643
return
4744

lib/matplotlib/backends/backend_wx.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
should be included with this source code.
1414
1515
"""
16-
from __future__ import (absolute_import, division, print_function,
17-
unicode_literals)
1816

1917
import six
2018

21-
import sys
22-
import os
2319
import os.path
2420
import math
21+
import sys
2522
import weakref
2623
import warnings
2724

@@ -31,13 +28,13 @@
3128
NavigationToolbar2, RendererBase, TimerBase, cursors)
3229
from matplotlib.backend_bases import _has_pil
3330

31+
from matplotlib import cbook, rcParams, backend_tools
3432
from matplotlib._pylab_helpers import Gcf
3533
from matplotlib.cbook import is_writable_file_like, warn_deprecated
3634
from matplotlib.figure import Figure
3735
from matplotlib.path import Path
3836
from matplotlib.transforms import Affine2D
3937
from matplotlib.widgets import SubplotTool
40-
from matplotlib import cbook, rcParams, backend_tools
4138

4239
from . import wx_compat as wxc
4340
import wx
@@ -48,30 +45,25 @@
4845
# traceback is performed, and pdb activated, for all uncaught exceptions in
4946
# this case
5047
_DEBUG = 5
51-
if _DEBUG < 5:
52-
import traceback
53-
import pdb
5448
_DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
5549

5650

5751
def DEBUG_MSG(string, lvl=3, o=None):
5852
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("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls),
64-
# file=sys.stderr)
65-
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls))
53+
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, type(o)))
6654

6755

56+
@cbook.deprecated("3.0")
6857
def debug_on_error(type, value, tb):
6958
"""Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
59+
import pdb
60+
import traceback
7061
traceback.print_exception(type, value, tb)
7162
print()
72-
pdb.pm() # jdh uncomment
63+
pdb.pm()
7364

7465

66+
@cbook.deprecated("3.0")
7567
class fake_stderr(object):
7668
"""
7769
Wx does strange things with stderr, as it makes the assumption that
@@ -106,6 +98,7 @@ def error_msg_wx(msg, parent=None):
10698
return None
10799

108100

101+
@cbook.deprecated("3.0")
109102
def raise_msg_to_str(msg):
110103
"""msg is a return arg from a raise. Join with new lines."""
111104
if not isinstance(msg, six.string_types):
@@ -1313,11 +1306,7 @@ def resize(self, width, height):
13131306
self.canvas.SetInitialSize(wx.Size(width, height))
13141307
self.window.GetSizer().Fit(self.window)
13151308

1316-
# Identifiers for toolbar controls - images_wx contains bitmaps for the images
1317-
# used in the controls. wxWindows does not provide any stock images, so I've
1318-
# 'stolen' those from GTK2, and transformed them into the appropriate format.
1319-
# import images_wx
1320-
1309+
# Identifiers for toolbar controls.
13211310

13221311
_NTB_AXISMENU = wx.NewId()
13231312
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1329,7 +1318,6 @@ def resize(self, width, height):
13291318
_NTB_Y_PAN_DOWN = wx.NewId()
13301319
_NTB_Y_ZOOMIN = wx.NewId()
13311320
_NTB_Y_ZOOMOUT = wx.NewId()
1332-
# _NTB_SUBPLOT =wx.NewId()
13331321
_NTB_SAVE = wx.NewId()
13341322
_NTB_CLOSE = wx.NewId()
13351323

@@ -1353,6 +1341,7 @@ def _load_bitmap(filename):
13531341
return bmp
13541342

13551343

1344+
@cbook.deprecated("3.0")
13561345
class MenuButtonWx(wx.Button):
13571346
"""
13581347
wxPython does not permit a menu to be incorporated directly into a toolbar.
@@ -1434,7 +1423,7 @@ def updateAxes(self, maxAxis):
14341423
"""Ensures that there are entries for max_axis axes in the menu
14351424
(selected by default)."""
14361425
if maxAxis > len(self._axisId):
1437-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1426+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14381427
menuId = wx.NewId()
14391428
self._axisId.append(menuId)
14401429
self._menu.Append(menuId, "Axis %d" % i,
@@ -1873,6 +1862,7 @@ def remove_rubberband(self, dc=None):
18731862

18741863
# < Additions for printing support: Matt Newville
18751864

1865+
@cbook.deprecated("3.0")
18761866
class PrintoutWx(wx.Printout):
18771867
"""
18781868
Simple wrapper around wx Printout class -- all the real work
@@ -1957,7 +1947,6 @@ def OnPrintPage(self, page):
19571947
self.canvas.figure.dpi = fig_dpi
19581948
self.canvas.draw()
19591949
return True
1960-
# >
19611950

19621951

19631952
@_Backend.export

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from __future__ import (absolute_import, division, print_function,
2-
unicode_literals)
3-
41
import six
52

63
import wx
@@ -69,33 +66,12 @@ def blit(self, bbox=None):
6966
srcDC.SelectObject(wx.NullBitmap)
7067
self.gui_repaint()
7168

72-
filetypes = FigureCanvasAgg.filetypes
73-
7469

7570
@cbook.deprecated("2.2", alternative="NavigationToolbar2WxAgg")
7671
class Toolbar(NavigationToolbar2WxAgg):
7772
pass
7873

7974

80-
# agg/wxPython image conversion functions (wxPython >= 2.8)
81-
82-
def _convert_agg_to_wx_image(agg, bbox):
83-
"""
84-
Convert the region of the agg buffer bounded by bbox to a wx.Image. If
85-
bbox is None, the entire buffer is converted.
86-
87-
Note: agg must be a backend_agg.RendererAgg instance.
88-
"""
89-
if bbox is None:
90-
# agg => rgb -> image
91-
image = wxc.EmptyImage(int(agg.width), int(agg.height))
92-
image.SetData(agg.tostring_rgb())
93-
return image
94-
else:
95-
# agg => rgba buffer -> bitmap => clipped bitmap => image
96-
return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox))
97-
98-
9975
def _convert_agg_to_wx_bitmap(agg, bbox):
10076
"""
10177
Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
@@ -109,36 +85,27 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
10985
agg.buffer_rgba())
11086
else:
11187
# agg => rgba buffer -> bitmap => clipped bitmap
112-
return _WX28_clipped_agg_as_bitmap(agg, bbox)
88+
l, b, width, height = bbox.bounds
89+
r = l + width
90+
t = b + height
11391

92+
srcBmp = wxc.BitmapFromBuffer(int(agg.width), int(agg.height),
93+
agg.buffer_rgba())
94+
srcDC = wx.MemoryDC()
95+
srcDC.SelectObject(srcBmp)
11496

115-
def _WX28_clipped_agg_as_bitmap(agg, bbox):
116-
"""
117-
Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap.
118-
119-
Note: agg must be a backend_agg.RendererAgg instance.
120-
"""
121-
l, b, width, height = bbox.bounds
122-
r = l + width
123-
t = b + height
124-
125-
srcBmp = wxc.BitmapFromBuffer(int(agg.width), int(agg.height),
126-
agg.buffer_rgba())
127-
srcDC = wx.MemoryDC()
128-
srcDC.SelectObject(srcBmp)
129-
130-
destBmp = wxc.EmptyBitmap(int(width), int(height))
131-
destDC = wx.MemoryDC()
132-
destDC.SelectObject(destBmp)
97+
destBmp = wxc.EmptyBitmap(int(width), int(height))
98+
destDC = wx.MemoryDC()
99+
destDC.SelectObject(destBmp)
133100

134-
x = int(l)
135-
y = int(int(agg.height) - t)
136-
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
101+
x = int(l)
102+
y = int(int(agg.height) - t)
103+
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
137104

138-
srcDC.SelectObject(wx.NullBitmap)
139-
destDC.SelectObject(wx.NullBitmap)
105+
srcDC.SelectObject(wx.NullBitmap)
106+
destDC.SelectObject(wx.NullBitmap)
140107

141-
return destBmp
108+
return destBmp
142109

143110

144111
@_BackendWx.export

0 commit comments

Comments
 (0)
0