/[ascend]/branches/pallav/ascxx/backend_gtk3agg.py
ViewVC logotype

Contents of /branches/pallav/ascxx/backend_gtk3agg.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2868 - (show annotations) (download) (as text)
Mon Mar 23 16:45:33 2015 UTC (7 years, 10 months ago) by pallav
File MIME type: text/x-python
File size: 3223 byte(s)
Added the matplotlib backend files
1 from __future__ import absolute_import, division, print_function, unicode_literals
2
3 import six
4
5 import cairo
6 import numpy as np
7 import sys
8 import warnings
9
10 import backend_agg
11 import backend_gtk3
12 from matplotlib.figure import Figure
13 from matplotlib import transforms
14
15 if six.PY3:
16 warnings.warn("The Gtk3Agg backend is not known to work on Python 3.x.")
17
18
19 class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
20 backend_agg.FigureCanvasAgg):
21 def __init__(self, figure):
22 backend_gtk3.FigureCanvasGTK3.__init__(self, figure)
23 self._bbox_queue = []
24
25 def _renderer_init(self):
26 pass
27
28 def _render_figure(self, width, height):
29 backend_agg.FigureCanvasAgg.draw(self)
30
31 def on_draw_event(self, widget, ctx):
32 """ GtkDrawable draw event, like expose_event in GTK 2.X
33 """
34 allocation = self.get_allocation()
35 w, h = allocation.width, allocation.height
36
37 if not len(self._bbox_queue):
38 if self._need_redraw:
39 self._render_figure(w, h)
40 bbox_queue = [transforms.Bbox([[0, 0], [w, h]])]
41 else:
42 return
43 else:
44 bbox_queue = self._bbox_queue
45
46 for bbox in bbox_queue:
47 area = self.copy_from_bbox(bbox)
48 buf = np.fromstring(area.to_string_argb(), dtype='uint8')
49
50 x = int(bbox.x0)
51 y = h - int(bbox.y1)
52 width = int(bbox.x1) - int(bbox.x0)
53 height = int(bbox.y1) - int(bbox.y0)
54
55 image = cairo.ImageSurface.create_for_data(
56 buf, cairo.FORMAT_ARGB32, width, height)
57 ctx.set_source_surface(image, x, y)
58 ctx.paint()
59
60 if len(self._bbox_queue):
61 self._bbox_queue = []
62
63 return False
64
65 def blit(self, bbox=None):
66 # If bbox is None, blit the entire canvas to gtk. Otherwise
67 # blit only the area defined by the bbox.
68 if bbox is None:
69 bbox = self.figure.bbox
70
71 allocation = self.get_allocation()
72 w, h = allocation.width, allocation.height
73 x = int(bbox.x0)
74 y = h - int(bbox.y1)
75 width = int(bbox.x1) - int(bbox.x0)
76 height = int(bbox.y1) - int(bbox.y0)
77
78 self._bbox_queue.append(bbox)
79 self.queue_draw_area(x, y, width, height)
80
81 def print_png(self, filename, *args, **kwargs):
82 # Do this so we can save the resolution of figure in the PNG file
83 agg = self.switch_backends(backend_agg.FigureCanvasAgg)
84 return agg.print_png(filename, *args, **kwargs)
85
86
87 class FigureManagerGTK3Agg(backend_gtk3.FigureManagerGTK3):
88 pass
89
90
91 def new_figure_manager(num, *args, **kwargs):
92 """
93 Create a new figure manager instance
94 """
95 FigureClass = kwargs.pop('FigureClass', Figure)
96 thisFig = FigureClass(*args, **kwargs)
97 return new_figure_manager_given_figure(num, thisFig)
98
99
100 def new_figure_manager_given_figure(num, figure):
101 """
102 Create a new figure manager instance for the given figure.
103 """
104 canvas = FigureCanvasGTK3Agg(figure)
105 manager = FigureManagerGTK3Agg(canvas, num)
106 return manager
107
108
109 FigureManager = FigureManagerGTK3Agg
110 show = backend_gtk3.show

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22