source: trunk/python/asaplotgui_gtk.py@ 1099

Last change on this file since 1099 was 1089, checked in by mar637, 18 years ago

alternative plotter widget based on GTk. Not used anywhere - yet

File size: 6.7 KB
Line 
1"""
2ASAP plotting class based on matplotlib.
3"""
4
5from asap.asaplotbase import *
6# Force use of the newfangled toolbar.
7import gtk
8import matplotlib
9from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
10matplotlib.use("GTkAgg")
11matplotlib.rcParams['toolbar'] = 'toolbar2'
12
13class asaplotgui(asaplotbase):
14 """
15 ASAP plotting class based on matplotlib.
16 """
17
18 def __init__(self, rows=1, cols=0, title='', size=(8,6), buffering=False):
19 """
20 Create a new instance of the ASAPlot plotting class.
21
22 If rows < 1 then a separate call to set_panels() is required to define
23 the panel layout; refer to the doctext for set_panels().
24 """
25 v = vars()
26 del v['self']
27
28 asaplotbase.__init__(self, **v)
29 self.window = gtk.Window()
30 def dest_callback(val):
31 self.is_dead = True
32 self.window.destroy()
33 self.window.connect("destroy", dest_callback )
34 self.window.set_default_size(800,600)
35 self.subwin = gtk.ScrolledWindow()
36 self.window.add(self.subwin)
37 self.subwin.set_border_width(10)
38 self.canvas = FigureCanvas(self.figure)
39 # Simply instantiating this is enough to get a working toolbar.
40 self.figmgr = None#FigureManagerTkAgg(self.canvas, 1, self.window)
41 self.window.set_title('ASAPlot graphics window')
42
43 self.events = {'button_press':None,
44 'button_release':None,
45 'motion_notify':None}
46
47 self.subwin.add_with_viewport(self.canvas)
48 matplotlib.interactive = True
49 self.buffering = buffering
50 self.canvas.set_size_request(800,600)
51
52 self.canvas.show()
53
54 def map(self):
55 """
56 Reveal the ASAPlot graphics window and bring it to the top of the
57 window stack.
58 """
59 self.window.deiconify()
60 #self.window.lift()
61
62 def position(self):
63 """
64 Use the mouse to get a position from a graph.
65 """
66
67 def position_disable(event):
68 self.register('button_press', None)
69 print '%.4f, %.4f' % (event.xdata, event.ydata)
70
71 print 'Press any mouse button...'
72 self.register('button_press', position_disable)
73
74
75 def quit(self):
76 """
77 Destroy the ASAPlot graphics window.
78 """
79 self.window.destroy()
80
81
82 def region(self):
83 """
84 Use the mouse to get a rectangular region from a plot.
85
86 The return value is [x0, y0, x1, y1] in world coordinates.
87 """
88
89 def region_start(event):
90 height = self.canvas.figure.bbox.height()
91 self.rect = {'fig': None, 'height': height,
92 'x': event.x, 'y': height - event.y,
93 'world': [event.xdata, event.ydata,
94 event.xdata, event.ydata]}
95 self.register('button_press', None)
96 self.register('motion_notify', region_draw)
97 self.register('button_release', region_disable)
98
99 def region_draw(event):
100 self.canvas._tkcanvas.delete(self.rect['fig'])
101 self.rect['fig'] = self.canvas._tkcanvas.create_rectangle(
102 self.rect['x'], self.rect['y'],
103 event.x, self.rect['height'] - event.y)
104
105 def region_disable(event):
106 self.register('motion_notify', None)
107 self.register('button_release', None)
108
109 self.canvas._tkcanvas.delete(self.rect['fig'])
110
111 self.rect['world'][2:4] = [event.xdata, event.ydata]
112 print '(%.2f, %.2f) (%.2f, %.2f)' % (self.rect['world'][0],
113 self.rect['world'][1], self.rect['world'][2],
114 self.rect['world'][3])
115
116 self.register('button_press', region_start)
117
118 # This has to be modified to block and return the result (currently
119 # printed by region_disable) when that becomes possible in matplotlib.
120
121 return [0.0, 0.0, 0.0, 0.0]
122
123
124 def register(self, type=None, func=None):
125 """
126 Register, reregister, or deregister events of type 'button_press',
127 'button_release', or 'motion_notify'.
128
129 The specified callback function should have the following signature:
130
131 def func(event)
132
133 where event is an MplEvent instance containing the following data:
134
135 name # Event name.
136 canvas # FigureCanvas instance generating the event.
137 x = None # x position - pixels from left of canvas.
138 y = None # y position - pixels from bottom of canvas.
139 button = None # Button pressed: None, 1, 2, 3.
140 key = None # Key pressed: None, chr(range(255)), shift,
141 win, or control
142 inaxes = None # Axes instance if cursor within axes.
143 xdata = None # x world coordinate.
144 ydata = None # y world coordinate.
145
146 For example:
147
148 def mouse_move(event):
149 print event.xdata, event.ydata
150
151 a = asaplot()
152 a.register('motion_notify', mouse_move)
153
154 If func is None, the event is deregistered.
155
156 Note that in TkAgg keyboard button presses don't generate an event.
157 """
158
159 if not self.events.has_key(type): return
160
161 if func is None:
162 if self.events[type] is not None:
163 # It's not clear that this does anything.
164 self.canvas.mpl_disconnect(self.events[type])
165 self.events[type] = None
166
167 # It seems to be necessary to return events to the toolbar.
168 if type == 'motion_notify':
169 self.canvas.mpl_connect(type + '_event',
170 self.figmgr.toolbar.mouse_move)
171 elif type == 'button_press':
172 self.canvas.mpl_connect(type + '_event',
173 self.figmgr.toolbar.press)
174 elif type == 'button_release':
175 self.canvas.mpl_connect(type + '_event',
176 self.figmgr.toolbar.release)
177
178 else:
179 self.events[type] = self.canvas.mpl_connect(type + '_event', func)
180
181
182 def show(self):
183 """
184 Show graphics dependent on the current buffering state.
185 """
186 if not self.buffering:
187 asaplotbase.show(self)
188 self.window.deiconify()
189 self.canvas.draw()
190 self.window.show_all()
191
192 def terminate(self):
193 """
194 Clear the figure.
195 """
196 self.window.destroy()
197
198 def unmap(self):
199 """
200 Hide the ASAPlot graphics window.
201 """
202 self.window.wm_withdraw()
Note: See TracBrowser for help on using the repository browser.