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

Skip to content

Commit 969a924

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 4dbbd22 commit 969a924

File tree

8 files changed

+57
-118
lines changed

8 files changed

+57
-118
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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``,
10+
- ``tk_window_focus``,
11+
- ``mlab.demean``,

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
@@ -391,17 +391,11 @@ def draw(self):
391391
Draw the figure using the renderer.
392392
"""
393393
self.renderer = self.get_renderer(cleared=True)
394-
# acquire a lock on the shared font cache
395-
RendererAgg.lock.acquire()
396-
397-
toolbar = self.toolbar
398-
try:
394+
with RendererAgg.lock:
399395
self.figure.draw(self.renderer)
400-
# A GUI class may be need to update a window using this draw, so
401-
# don't forget to call the superclass.
396+
# A GUI class may be need to update a window using this draw,
397+
# so don't forget to call the superclass.
402398
super().draw()
403-
finally:
404-
RendererAgg.lock.release()
405399

406400
def get_renderer(self, cleared=False):
407401
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):
@@ -1095,22 +1091,16 @@ def _print_image(self, filename, filetype, *args, **kwargs):
10951091
image = self.bitmap.ConvertToImage()
10961092
image.SetOption(wx.IMAGE_OPTION_QUALITY, str(jpeg_quality))
10971093

1098-
# Now that we have rendered into the bitmap, save it
1099-
# to the appropriate file type and clean up
1094+
# Now that we have rendered into the bitmap, save it to the appropriate
1095+
# file type and clean up.
11001096
if isinstance(filename, str):
11011097
if not image.SaveFile(filename, filetype):
1102-
DEBUG_MSG('print_figure() file save error', 4, self)
1103-
raise RuntimeError(
1104-
'Could not save figure to %s\n' %
1105-
(filename))
1098+
raise RuntimeError('Could not save figure to %s' % filename)
11061099
elif cbook.is_writable_file_like(filename):
11071100
if not isinstance(image, wx.Image):
11081101
image = image.ConvertToImage()
11091102
if not image.SaveStream(filename, filetype):
1110-
DEBUG_MSG('print_figure() file save error', 4, self)
1111-
raise RuntimeError(
1112-
'Could not save figure to %s\n' %
1113-
(filename))
1103+
raise RuntimeError('Could not save figure to %s' % filename)
11141104

11151105
# Restore everything to normal
11161106
self.bitmap = origBitmap
@@ -1296,11 +1286,7 @@ def resize(self, width, height):
12961286
self.canvas.SetInitialSize(wx.Size(width, height))
12971287
self.window.GetSizer().Fit(self.window)
12981288

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

13051291
_NTB_AXISMENU = wx.NewId()
13061292
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1312,7 +1298,6 @@ def resize(self, width, height):
13121298
_NTB_Y_PAN_DOWN = wx.NewId()
13131299
_NTB_Y_ZOOMIN = wx.NewId()
13141300
_NTB_Y_ZOOMOUT = wx.NewId()
1315-
# _NTB_SUBPLOT =wx.NewId()
13161301
_NTB_SAVE = wx.NewId()
13171302
_NTB_CLOSE = wx.NewId()
13181303

@@ -1350,6 +1335,7 @@ def _set_frame_icon(frame):
13501335
frame.SetIcons(bundle)
13511336

13521337

1338+
@cbook.deprecated("3.1")
13531339
class MenuButtonWx(wx.Button):
13541340
"""
13551341
wxPython does not permit a menu to be incorporated directly into a toolbar.
@@ -1427,7 +1413,7 @@ def updateAxes(self, maxAxis):
14271413
"""Ensures that there are entries for max_axis axes in the menu
14281414
(selected by default)."""
14291415
if maxAxis > len(self._axisId):
1430-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1416+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14311417
menuId = wx.NewId()
14321418
self._axisId.append(menuId)
14331419
self._menu.Append(menuId, "Axis %d" % i,
@@ -1700,16 +1686,10 @@ class StatusBarWx(wx.StatusBar):
17001686
def __init__(self, parent, *args, **kwargs):
17011687
wx.StatusBar.__init__(self, parent, -1)
17021688
self.SetFieldsCount(2)
1703-
self.SetStatusText("None", 1)
1704-
# self.SetStatusText("Measurement: None", 2)
1705-
# self.Reposition()
17061689

17071690
def set_function(self, string):
17081691
self.SetStatusText("%s" % string, 1)
17091692

1710-
# def set_measurement(self, string):
1711-
# self.SetStatusText("Measurement: %s" % string, 2)
1712-
17131693

17141694
# tools for matplotlib.backend_managers.ToolManager:
17151695

@@ -2018,6 +1998,7 @@ def trigger(self, *args, **kwargs):
20181998

20191999
# < Additions for printing support: Matt Newville
20202000

2001+
@cbook.deprecated("3.1")
20212002
class PrintoutWx(wx.Printout):
20222003
"""
20232004
Simple wrapper around wx Printout class -- all the real work
@@ -2099,7 +2080,6 @@ def OnPrintPage(self, page):
20992080
self.canvas.figure.dpi = fig_dpi
21002081
self.canvas.draw()
21012082
return True
2102-
# >
21032083

21042084

21052085
@_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)