Add autogen.sh and some other files
[oscopy/ivan.git] / oscopy_gui / menus.py
blob4f3cf987e4f50a018e7bd1ca3571bbff69f0339d
1 import gtk
2 import dialogs
4 class FigureMenu(object):
6 def __init__(self):
7 self._store = None
9 def create_menu(self, store, figure, graph, app_exec):
10 self._layout_to_str = {'horiz': 'Horizontal', 'vert':'Vertical',\
11 'quad':'Quad'}
12 self._store = store
13 menu = gtk.Menu()
14 self._create_figure_menu(menu, figure, graph, app_exec)
15 sep = gtk.SeparatorMenuItem()
16 menu.append(sep)
17 self._create_graph_menu(menu, figure, graph, app_exec)
19 return menu
21 def _create_figure_menu(self, menu, fig, graph, app_exec):
22 item_add = gtk.MenuItem('Add graph')
23 item_add.connect('activate', self._graph_menu_item_activated,
24 (fig, app_exec))
25 menu.append(item_add)
26 item_delete = gtk.MenuItem('Delete graph')
27 item_delete.connect('activate', self._delete_graph_menu_item_activated,
28 (fig, graph, app_exec))
29 item_delete.set_sensitive(graph is not None)
30 menu.append(item_delete)
31 item_layout = gtk.MenuItem('Layout')
32 item_layout.set_submenu(self._create_layout_menu(fig, app_exec))
33 menu.append(item_layout)
34 return menu
36 def _graph_menu_item_activated(self, menuitem, user_data):
37 fig, app_exec = user_data
38 app_exec('add')
39 fig.canvas.draw()
41 def _delete_graph_menu_item_activated(self, menuitem, user_data):
42 figure, graph, app_exec = user_data
43 if graph is not None:
44 idx = figure.graphs.index(graph) + 1
45 app_exec('delete %d' % idx)
46 if figure.canvas is not None:
47 figure.canvas.draw()
49 def _create_layout_menu(self, fig, app_exec):
50 menu = gtk.Menu()
51 for layout in self._layout_to_str.keys():
52 item = gtk.CheckMenuItem(self._layout_to_str[layout])
53 item.set_active(fig.layout == layout)
54 item.connect('activate', self._layout_menu_item_activated,
55 (fig, layout, app_exec))
56 menu.append(item)
57 return menu
59 def _layout_menu_item_activated(self, menuitem, user_data):
60 fig, layout, app_exec = user_data
61 app_exec('layout ' + layout)
62 if fig.canvas is not None:
63 fig.canvas.draw()
65 def _create_graph_menu(self, menu, figure, graph, app_exec):
66 graphmenu = GraphMenu()
67 return graphmenu.create_graph_menu(menu, self._store, figure, graph,
68 app_exec)
70 class GraphMenu(object):
72 def __init__(self):
73 self._store = None
74 pass
76 def create_graph_menu(self, menu, store, figure, graph, app_exec):
77 self._scale_to_str = {'lin': 'Linear', 'logx': 'LogX', 'logy': 'LogY',\
78 'loglog': 'Loglog'}
79 self._store = store
80 # menu = gtk.Menu()
81 item_range = gtk.MenuItem('Range...')
82 item_range.connect('activate', self._range_menu_item_activated,
83 (figure, graph, app_exec))
84 item_range.set_sensitive(graph is not None)
85 menu.append(item_range)
86 item_units = gtk.MenuItem('Units...')
87 item_units.connect('activate', self._units_menu_item_activated,
88 (figure, graph, app_exec))
89 item_units.set_sensitive(graph is not None)
90 menu.append(item_units)
91 item_scale = gtk.MenuItem('Scale')
92 item_scale.set_submenu(self._create_scale_menu((figure, graph,
93 app_exec)))
94 item_scale.set_sensitive(graph is not None)
95 menu.append(item_scale)
96 item_add = gtk.MenuItem('Insert signal')
97 item_add.set_submenu(self._create_filename_menu((figure, graph,
98 app_exec)))
99 item_add.set_sensitive(graph is not None)
100 menu.append(item_add)
101 item_remove = gtk.MenuItem('Remove signal')
102 item_remove.set_submenu(self._create_remove_signal_menu((figure, graph,
103 app_exec)))
104 item_remove.set_sensitive(graph is not None)
105 menu.append(item_remove)
106 return menu
108 def _create_units_window(self, figure, graph, app_exec):
109 if graph is None:
110 return
111 unitdlg = dialogs.Enter_Units_Dialog()
112 unitdlg.display(graph.unit, graph.axis_names, graph.scale_factors)
113 units, scale_factors = unitdlg.run()
114 if units and scale_factors:
115 app_exec('unit %s' % ','.join(units))
116 app_exec('factors %s,%s' % (scale_factors[0],scale_factors[1]))
117 if figure.canvas is not None:
118 figure.canvas.draw()
120 def _units_menu_item_activated(self, menuitem, user_data):
121 figure, graph, app_exec = user_data
122 self._create_units_window(figure, graph, app_exec)
124 def _create_range_window(self, figure, graph, app_exec):
125 if graph is None:
126 return
127 rangedlg = dialogs.Enter_Range_Dialog()
128 rangedlg.display(graph.get_range(), graph.axis_names,
129 graph.scale_factors, graph.unit)
130 r = rangedlg.run()
131 if r:
132 app_exec('range %s' % ' '.join(r))
133 if figure.canvas is not None:
134 figure.canvas.draw()
136 def _range_menu_item_activated(self, menuitem, user_data):
137 figure, graph, app_exec = user_data
138 self._create_range_window(figure, graph, app_exec)
140 def _signals_menu_item_activated(self, menuitem, user_data):
141 fig, graph, parent_it, it, app_exec = user_data
142 name, sig = self._store.get(it, 0, 1)
143 if not fig.graphs:
144 app_exec('add %s' % name)
145 else:
146 if graph is not None:
147 app_exec('insert %s' % name)
148 if fig.canvas is not None:
149 fig.canvas.draw()
151 def _scale_menu_item_activated(self, menuitem, user_data):
152 figure, graph, scale, app_exec = user_data
153 if graph is None:
154 return
155 app_exec('scale %s' % scale)
156 if figure.canvas is not None:
157 figure.canvas.draw()
159 def _remove_signal_menu_item_activated(self, menuitem, user_data):
160 figure, graph, signals, app_exec = user_data
161 if graph is None:
162 return
163 app_exec('remove %s' % ' '.join(signals.keys()))
164 if figure.canvas is not None:
165 figure.canvas.draw()
167 def _create_scale_menu(self, data):
168 figure, graph, app_exec = data
169 if graph is None:
170 return
171 menu = gtk.Menu()
172 for scale in self._scale_to_str.keys():
173 item = gtk.CheckMenuItem(self._scale_to_str[scale])
174 item.set_active(graph.scale == scale)
175 item.connect('activate', self._scale_menu_item_activated,
176 (figure, graph, scale, app_exec))
177 menu.append(item)
178 return menu
180 def _create_remove_signal_menu(self, data):
181 figure, graph, app_exec = data
182 menu = gtk.Menu()
183 if graph is None:
184 item_nograph = gtk.MenuItem('No graph selected')
185 menu.append(item_nograph)
186 return menu
187 for name, signal in graph.signals.iteritems():
188 item = gtk.MenuItem(name)
189 item.connect('activate', self._remove_signal_menu_item_activated,
190 (figure, graph, {name: signal}, app_exec))
191 menu.append(item)
192 return menu
194 def _create_signals_menu(self, fig, graph, parent_it, app_exec):
195 menu = gtk.Menu()
196 it = self._store.iter_children(parent_it)
197 while it is not None:
198 name = self._store.get_value(it, 0)
199 item = gtk.MenuItem(name)
200 item.connect('activate', self._signals_menu_item_activated,
201 (fig, graph, parent_it, it, app_exec))
202 menu.append(item)
203 it = self._store.iter_next(it)
204 return menu
206 def _create_filename_menu(self, data):
207 figure, graph, app_exec = data
208 it = self._store.get_iter_root()
209 if it is None:
210 return gtk.Menu()
212 menu = gtk.Menu()
213 while it is not None:
214 filename = self._store.get_value(it, 0)
215 item = gtk.MenuItem(filename)
216 item.set_submenu(self._create_signals_menu(figure, graph, it,
217 app_exec))
218 menu.append(item)
219 it = self._store.iter_next(it)
220 return menu