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

Skip to content

Commit 03e86ee

Browse files
committed
split backend_gtk3agg.py into two parts, the gcf dependent and
gcf independent parts. Same poor naming scheme as everything else in this branch.
1 parent 4f9f5b5 commit 03e86ee

File tree

2 files changed

+135
-110
lines changed

2 files changed

+135
-110
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from __future__ import (absolute_import, division, print_function,
2+
unicode_literals)
3+
4+
import six
5+
6+
import numpy as np
7+
import sys
8+
import warnings
9+
10+
from . import backend_agg
11+
from . import backend_gtk3
12+
from .backend_cairo import cairo, HAS_CAIRO_CFFI
13+
from matplotlib.figure import Figure
14+
from matplotlib import transforms
15+
16+
if six.PY3 and not HAS_CAIRO_CFFI:
17+
warnings.warn(
18+
"The Gtk3Agg backend is known to not work on Python 3.x with pycairo. "
19+
"Try installing cairocffi.")
20+
21+
22+
class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
23+
backend_agg.FigureCanvasAgg):
24+
def __init__(self, figure):
25+
backend_gtk3.FigureCanvasGTK3.__init__(self, figure)
26+
self._bbox_queue = []
27+
28+
def _renderer_init(self):
29+
pass
30+
31+
def _render_figure(self, width, height):
32+
backend_agg.FigureCanvasAgg.draw(self)
33+
34+
def on_draw_event(self, widget, ctx):
35+
""" GtkDrawable draw event, like expose_event in GTK 2.X
36+
"""
37+
allocation = self.get_allocation()
38+
w, h = allocation.width, allocation.height
39+
40+
if not len(self._bbox_queue):
41+
if self._need_redraw:
42+
self._render_figure(w, h)
43+
bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
44+
else:
45+
return
46+
else:
47+
bbox_queue = self._bbox_queue
48+
49+
for bbox in bbox_queue:
50+
area = self.copy_from_bbox(bbox)
51+
buf = np.fromstring(area.to_string_argb(), dtype='uint8')
52+
53+
x = int(bbox.x0)
54+
y = h - int(bbox.y1)
55+
width = int(bbox.x1) - int(bbox.x0)
56+
height = int(bbox.y1) - int(bbox.y0)
57+
58+
if HAS_CAIRO_CFFI:
59+
ctx = cairo.Context._from_pointer(
60+
cairo.ffi.cast('cairo_t **',
61+
id(ctx) + object.__basicsize__)[0],
62+
incref=True)
63+
image = cairo.ImageSurface.create_for_data(
64+
buf.data, cairo.FORMAT_ARGB32, width, height)
65+
else:
66+
image = cairo.ImageSurface.create_for_data(
67+
buf, cairo.FORMAT_ARGB32, width, height)
68+
ctx.set_source_surface(image, x, y)
69+
ctx.paint()
70+
71+
if len(self._bbox_queue):
72+
self._bbox_queue = []
73+
74+
return False
75+
76+
def blit(self, bbox=None):
77+
# If bbox is None, blit the entire canvas to gtk. Otherwise
78+
# blit only the area defined by the bbox.
79+
if bbox is None:
80+
bbox = self.figure.bbox
81+
82+
allocation = self.get_allocation()
83+
w, h = allocation.width, allocation.height
84+
x = int(bbox.x0)
85+
y = h - int(bbox.y1)
86+
width = int(bbox.x1) - int(bbox.x0)
87+
height = int(bbox.y1) - int(bbox.y0)
88+
89+
self._bbox_queue.append(bbox)
90+
self.queue_draw_area(x, y, width, height)
91+
92+
def print_png(self, filename, *args, **kwargs):
93+
# Do this so we can save the resolution of figure in the PNG file
94+
agg = self.switch_backends(backend_agg.FigureCanvasAgg)
95+
return agg.print_png(filename, *args, **kwargs)
96+
97+
98+
def new_figure_manager(num, *args, **kwargs):
99+
"""
100+
Create a new figure manager instance
101+
"""
102+
FigureClass = kwargs.pop('FigureClass', Figure)
103+
thisFig = FigureClass(*args, **kwargs)
104+
return new_figure_manager_given_figure(num, thisFig)
105+
106+
107+
def new_figure_manager_given_figure(num, figure):
108+
"""
109+
Create a new figure manager instance for the given figure.
110+
"""
111+
canvas = FigureCanvasGTK3Agg(figure)
112+
manager = _backend_gtk3.FigureManagerGTK3(canvas, num)
113+
return manager
114+
115+
116+
FigureCanvas = FigureCanvasGTK3Agg
117+
FigureManager = _backend_gtk3.FigureManagerGTK3

lib/matplotlib/backends/backend_gtk3agg.py

Lines changed: 18 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,121 +2,29 @@
22
unicode_literals)
33

44
import six
5-
6-
import numpy as np
7-
import sys
85
import warnings
96

10-
from . import backend_agg
11-
from . import backend_gtk3
12-
from .backend_cairo import cairo, HAS_CAIRO_CFFI
13-
from matplotlib.figure import Figure
14-
from matplotlib import transforms
15-
16-
if six.PY3 and not HAS_CAIRO_CFFI:
17-
warnings.warn(
18-
"The Gtk3Agg backend is known to not work on Python 3.x with pycairo. "
19-
"Try installing cairocffi.")
20-
21-
22-
class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
23-
backend_agg.FigureCanvasAgg):
24-
def __init__(self, figure):
25-
backend_gtk3.FigureCanvasGTK3.__init__(self, figure)
26-
self._bbox_queue = []
27-
28-
def _renderer_init(self):
29-
pass
30-
31-
def _render_figure(self, width, height):
32-
backend_agg.FigureCanvasAgg.draw(self)
33-
34-
def on_draw_event(self, widget, ctx):
35-
""" GtkDrawable draw event, like expose_event in GTK 2.X
36-
"""
37-
allocation = self.get_allocation()
38-
w, h = allocation.width, allocation.height
39-
40-
if not len(self._bbox_queue):
41-
if self._need_redraw:
42-
self._render_figure(w, h)
43-
bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
44-
else:
45-
return
46-
else:
47-
bbox_queue = self._bbox_queue
48-
49-
for bbox in bbox_queue:
50-
area = self.copy_from_bbox(bbox)
51-
buf = np.fromstring(area.to_string_argb(), dtype='uint8')
52-
53-
x = int(bbox.x0)
54-
y = h - int(bbox.y1)
55-
width = int(bbox.x1) - int(bbox.x0)
56-
height = int(bbox.y1) - int(bbox.y0)
57-
58-
if HAS_CAIRO_CFFI:
59-
ctx = cairo.Context._from_pointer(
60-
cairo.ffi.cast('cairo_t **',
61-
id(ctx) + object.__basicsize__)[0],
62-
incref=True)
63-
image = cairo.ImageSurface.create_for_data(
64-
buf.data, cairo.FORMAT_ARGB32, width, height)
65-
else:
66-
image = cairo.ImageSurface.create_for_data(
67-
buf, cairo.FORMAT_ARGB32, width, height)
68-
ctx.set_source_surface(image, x, y)
69-
ctx.paint()
7+
from matplotlib._pylab_helpers import Gcf
8+
from ._backend_gtk3agg import (FigureCanvasGTK3Agg,
9+
new_figure_manager,
10+
new_figure_manager_given_figure
11+
)
7012

71-
if len(self._bbox_queue):
72-
self._bbox_queue = []
13+
from ._backend_gtk3 import FigureManagerGTK3
14+
from .backend_gtk3 import (show,
15+
draw_if_interactive,
16+
_gtk_cleanup,
17+
key_press_handler)
7318

74-
return False
7519

76-
def blit(self, bbox=None):
77-
# If bbox is None, blit the entire canvas to gtk. Otherwise
78-
# blit only the area defined by the bbox.
79-
if bbox is None:
80-
bbox = self.figure.bbox
81-
82-
allocation = self.get_allocation()
83-
w, h = allocation.width, allocation.height
84-
x = int(bbox.x0)
85-
y = h - int(bbox.y1)
86-
width = int(bbox.x1) - int(bbox.x0)
87-
height = int(bbox.y1) - int(bbox.y0)
88-
89-
self._bbox_queue.append(bbox)
90-
self.queue_draw_area(x, y, width, height)
91-
92-
def print_png(self, filename, *args, **kwargs):
93-
# Do this so we can save the resolution of figure in the PNG file
94-
agg = self.switch_backends(backend_agg.FigureCanvasAgg)
95-
return agg.print_png(filename, *args, **kwargs)
96-
97-
98-
class FigureManagerGTK3Agg(backend_gtk3.FigureManagerGTK3):
99-
pass
100-
101-
102-
def new_figure_manager(num, *args, **kwargs):
103-
"""
104-
Create a new figure manager instance
105-
"""
106-
FigureClass = kwargs.pop('FigureClass', Figure)
107-
thisFig = FigureClass(*args, **kwargs)
108-
return new_figure_manager_given_figure(num, thisFig)
109-
110-
111-
def new_figure_manager_given_figure(num, figure):
112-
"""
113-
Create a new figure manager instance for the given figure.
114-
"""
115-
canvas = FigureCanvasGTK3Agg(figure)
116-
manager = FigureManagerGTK3Agg(canvas, num)
117-
return manager
20+
if six.PY3:
21+
warnings.warn("The Gtk3Agg backend is not known to work on Python 3.x.")
11822

11923

12024
FigureCanvas = FigureCanvasGTK3Agg
121-
FigureManager = FigureManagerGTK3Agg
122-
show = backend_gtk3.show
25+
FigureManager = FigureManagerGTK3
26+
27+
# set the call backs
28+
FigureManager._key_press_handler = staticmethod(key_press_handler)
29+
FigureManager._destroy_callback = staticmethod(Gcf.destroy)
30+
FigureManager._gtk_cleanup = staticmethod(_gtk_cleanup)

0 commit comments

Comments
 (0)