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

Skip to content

Commit 798b1f8

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. - Remove some unused private tk apis. - Simplify toolkit mocking in the docs. In particular pycairo is Py3-compatible only since 1.11.0 so the version_check is unneeded. - Removed the old list of deprecations (18-02-15) that has already been integrated into the 3.0 release notes in favor of a new list (for 3.1).
1 parent f8b425d commit 798b1f8

File tree

8 files changed

+55
-118
lines changed

8 files changed

+55
-118
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
API deprecations
2+
````````````````
3+
4+
The following API elements are deprecated:
5+
6+
- ``backend_gtk3cairo.FigureCanvasGTK3Cairo``,
7+
- ``backend_wx.debug_on_error``, ``backend_wx.fake_stderr``,
8+
``backend_wx.raise_msg_to_str``, ``backend_wx.MenuButtonWx``,
9+
``backend_wx.PrintoutWx``,

lib/matplotlib/backends/_backend_tk.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import math
1+
from contextlib import contextmanager
22
import logging
3+
import math
34
import os.path
45
import sys
56
import tkinter as tk
67
from tkinter.simpledialog import SimpleDialog
78
import tkinter.filedialog
89
import tkinter.messagebox
9-
from contextlib import contextmanager
1010

1111
import numpy as np
1212

13-
from . import _tkagg
14-
1513
import matplotlib
16-
from matplotlib import backend_tools, rcParams
14+
from matplotlib import backend_tools, cbook, rcParams
1715
from matplotlib.backend_bases import (
1816
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1917
StatusbarBase, TimerBase, ToolContainerBase, cursors)
2018
from matplotlib.backend_managers import ToolManager
2119
from matplotlib._pylab_helpers import Gcf
2220
from matplotlib.figure import Figure
2321
from matplotlib.widgets import SubplotTool
22+
from . import _tkagg
2423

2524
try:
2625
from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow
@@ -56,18 +55,6 @@ def _restore_foreground_window_at_end():
5655
}
5756

5857

59-
def raise_msg_to_str(msg):
60-
"""msg is a return arg from a raise. Join with new lines"""
61-
if not isinstance(msg, str):
62-
msg = '\n'.join(map(str, msg))
63-
return msg
64-
65-
66-
def error_msg_tkpaint(msg, parent=None):
67-
import tkinter.messagebox
68-
tkinter.messagebox.showerror("matplotlib", msg)
69-
70-
7158
def blit(photoimage, aggimage, offsets, bbox=None):
7259
"""
7360
Blit *aggimage* to *photoimage*.

lib/matplotlib/backends/backend_agg.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,11 @@ def draw(self):
386386
Draw the figure using the renderer.
387387
"""
388388
self.renderer = self.get_renderer(cleared=True)
389-
# acquire a lock on the shared font cache
390-
RendererAgg.lock.acquire()
391-
392-
toolbar = self.toolbar
393-
try:
389+
with RendererAgg.lock:
394390
self.figure.draw(self.renderer)
395-
# A GUI class may be need to update a window using this draw, so
396-
# don't forget to call the superclass.
391+
# A GUI class may be need to update a window using this draw,
392+
# so don't forget to call the superclass.
397393
super().draw()
398-
finally:
399-
RendererAgg.lock.release()
400394

401395
def get_renderer(self, cleared=False):
402396
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
@@ -278,11 +278,8 @@ def on_draw_event(self, widget, ctx):
278278
pass
279279

280280
def draw(self):
281-
if self.get_visible() and self.get_mapped():
281+
if self.is_drawable():
282282
self.queue_draw()
283-
# do a synchronous draw (its less efficient than an async draw,
284-
# but is required if/when animation is used)
285-
self.get_property("window").process_updates(False)
286283

287284
def draw_idle(self):
288285
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,5 +1,6 @@
11
from . import backend_cairo, backend_gtk3
22
from .backend_gtk3 import Gtk, _BackendGTK3
3+
from matplotlib import cbook
34
from matplotlib.backend_bases import cursors
45

56

@@ -35,11 +36,11 @@ def on_draw_event(self, widget, ctx):
3536
return False # finish event propagation?
3637

3738

39+
@cbook.deprecated("3.1", alternative="backend_gtk3.FigureManagerGTK3")
3840
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
3941
pass
4042

4143

4244
@_BackendGTK3.export
4345
class _BackendGTK3Cairo(_BackendGTK3):
4446
FigureCanvas = FigureCanvasGTK3Cairo
45-
FigureManager = FigureManagerGTK3Cairo

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
@@ -31,8 +31,8 @@ def paintEvent(self, event):
3131
return
3232
self._draw_idle() # Only does something if a draw is pending.
3333

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

lib/matplotlib/backends/backend_wx.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
StatusbarBase)
2121
from matplotlib.backend_bases import _has_pil
2222

23+
from matplotlib import cbook, rcParams, backend_tools
2324
from matplotlib._pylab_helpers import Gcf
25+
from matplotlib.backend_managers import ToolManager
2426
from matplotlib.figure import Figure
2527
from matplotlib.path import Path
2628
from matplotlib.transforms import Affine2D
2729
from matplotlib.widgets import SubplotTool
28-
from matplotlib.backend_managers import ToolManager
29-
from matplotlib import cbook, rcParams, backend_tools
3030

3131
import wx
3232

@@ -38,30 +38,25 @@
3838
# traceback is performed, and pdb activated, for all uncaught exceptions in
3939
# this case
4040
_DEBUG = 5
41-
if _DEBUG < 5:
42-
import traceback
43-
import pdb
4441
_DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
4542

4643

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

5748

49+
@cbook.deprecated("3.1")
5850
def debug_on_error(type, value, tb):
5951
"""Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
52+
import pdb
53+
import traceback
6054
traceback.print_exception(type, value, tb)
6155
print()
62-
pdb.pm() # jdh uncomment
56+
pdb.pm()
6357

6458

59+
@cbook.deprecated("3.1")
6560
class fake_stderr(object):
6661
"""
6762
Wx does strange things with stderr, as it makes the assumption that
@@ -96,6 +91,7 @@ def error_msg_wx(msg, parent=None):
9691
return None
9792

9893

94+
@cbook.deprecated("3.1")
9995
def raise_msg_to_str(msg):
10096
"""msg is a return arg from a raise. Join with new lines."""
10197
if not isinstance(msg, str):
@@ -1092,22 +1088,16 @@ def _print_image(self, filename, filetype, *args, **kwargs):
10921088
image = self.bitmap.ConvertToImage()
10931089
image.SetOption(wx.IMAGE_OPTION_QUALITY, str(jpeg_quality))
10941090

1095-
# Now that we have rendered into the bitmap, save it
1096-
# to the appropriate file type and clean up
1091+
# Now that we have rendered into the bitmap, save it to the appropriate
1092+
# file type and clean up.
10971093
if isinstance(filename, str):
10981094
if not image.SaveFile(filename, filetype):
1099-
DEBUG_MSG('print_figure() file save error', 4, self)
1100-
raise RuntimeError(
1101-
'Could not save figure to %s\n' %
1102-
(filename))
1095+
raise RuntimeError('Could not save figure to %s' % filename)
11031096
elif cbook.is_writable_file_like(filename):
11041097
if not isinstance(image, wx.Image):
11051098
image = image.ConvertToImage()
11061099
if not image.SaveStream(filename, filetype):
1107-
DEBUG_MSG('print_figure() file save error', 4, self)
1108-
raise RuntimeError(
1109-
'Could not save figure to %s\n' %
1110-
(filename))
1100+
raise RuntimeError('Could not save figure to %s' % filename)
11111101

11121102
# Restore everything to normal
11131103
self.bitmap = origBitmap
@@ -1293,11 +1283,7 @@ def resize(self, width, height):
12931283
self.canvas.SetInitialSize(wx.Size(width, height))
12941284
self.window.GetSizer().Fit(self.window)
12951285

1296-
# Identifiers for toolbar controls - images_wx contains bitmaps for the images
1297-
# used in the controls. wxWindows does not provide any stock images, so I've
1298-
# 'stolen' those from GTK2, and transformed them into the appropriate format.
1299-
# import images_wx
1300-
1286+
# Identifiers for toolbar controls.
13011287

13021288
_NTB_AXISMENU = wx.NewId()
13031289
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1309,7 +1295,6 @@ def resize(self, width, height):
13091295
_NTB_Y_PAN_DOWN = wx.NewId()
13101296
_NTB_Y_ZOOMIN = wx.NewId()
13111297
_NTB_Y_ZOOMOUT = wx.NewId()
1312-
# _NTB_SUBPLOT =wx.NewId()
13131298
_NTB_SAVE = wx.NewId()
13141299
_NTB_CLOSE = wx.NewId()
13151300

@@ -1347,6 +1332,7 @@ def _set_frame_icon(frame):
13471332
frame.SetIcons(bundle)
13481333

13491334

1335+
@cbook.deprecated("3.1")
13501336
class MenuButtonWx(wx.Button):
13511337
"""
13521338
wxPython does not permit a menu to be incorporated directly into a toolbar.
@@ -1424,7 +1410,7 @@ def updateAxes(self, maxAxis):
14241410
"""Ensures that there are entries for max_axis axes in the menu
14251411
(selected by default)."""
14261412
if maxAxis > len(self._axisId):
1427-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1413+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14281414
menuId = wx.NewId()
14291415
self._axisId.append(menuId)
14301416
self._menu.Append(menuId, "Axis %d" % i,
@@ -1697,16 +1683,10 @@ class StatusBarWx(wx.StatusBar):
16971683
def __init__(self, parent, *args, **kwargs):
16981684
wx.StatusBar.__init__(self, parent, -1)
16991685
self.SetFieldsCount(2)
1700-
self.SetStatusText("None", 1)
1701-
# self.SetStatusText("Measurement: None", 2)
1702-
# self.Reposition()
17031686

17041687
def set_function(self, string):
17051688
self.SetStatusText("%s" % string, 1)
17061689

1707-
# def set_measurement(self, string):
1708-
# self.SetStatusText("Measurement: %s" % string, 2)
1709-
17101690

17111691
# tools for matplotlib.backend_managers.ToolManager:
17121692

@@ -2015,6 +1995,7 @@ def trigger(self, *args, **kwargs):
20151995

20161996
# < Additions for printing support: Matt Newville
20171997

1998+
@cbook.deprecated("3.1")
20181999
class PrintoutWx(wx.Printout):
20192000
"""
20202001
Simple wrapper around wx Printout class -- all the real work
@@ -2096,7 +2077,6 @@ def OnPrintPage(self, page):
20962077
self.canvas.figure.dpi = fig_dpi
20972078
self.canvas.draw()
20982079
return True
2099-
# >
21002080

21012081

21022082
@_Backend.export

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,11 @@ def blit(self, bbox=None):
6161
srcDC.SelectObject(wx.NullBitmap)
6262
self.gui_repaint()
6363

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

8665
def _convert_agg_to_wx_bitmap(agg, bbox):
8766
"""
8867
Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
8968
bbox is None, the entire buffer is converted.
90-
9169
Note: agg must be a backend_agg.RendererAgg instance.
9270
"""
9371
if bbox is None:
@@ -96,36 +74,27 @@ def _convert_agg_to_wx_bitmap(agg, bbox):
9674
agg.buffer_rgba())
9775
else:
9876
# agg => rgba buffer -> bitmap => clipped bitmap
99-
return _WX28_clipped_agg_as_bitmap(agg, bbox)
77+
l, b, width, height = bbox.bounds
78+
r = l + width
79+
t = b + height
10080

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

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

121-
x = int(l)
122-
y = int(int(agg.height) - t)
123-
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
90+
x = int(l)
91+
y = int(int(agg.height) - t)
92+
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
12493

125-
srcDC.SelectObject(wx.NullBitmap)
126-
destDC.SelectObject(wx.NullBitmap)
94+
srcDC.SelectObject(wx.NullBitmap)
95+
destDC.SelectObject(wx.NullBitmap)
12796

128-
return destBmp
97+
return destBmp
12998

13099

131100
@_BackendWx.export

0 commit comments

Comments
 (0)