Use set/get_[x|y]bound instead of set/get_[x|y]lim
[oscopy.git] / oscopy / gui / dialogs.py
blob1b2d509572ee283b06556ade4698fdf7fdfc6ae3
1 import os
2 import gobject
3 import gtk
4 import pty
5 import sys
6 import readline
7 import math
9 from oscopy import factors_to_names, abbrevs_to_factors
11 class Enter_Units_Dialog(object):
12 def __init__(self):
13 self._dlg = None
14 self._entry_xunits = None
15 self._entry_yunits = None
16 self._scale_factors = gtk.ListStore(gobject.TYPE_STRING,
17 gobject.TYPE_STRING)
18 sorted_list = factors_to_names.keys()
19 sorted_list.sort()
20 for factor in sorted_list:
21 self._scale_factors.append((factors_to_names[factor][0],
22 factors_to_names[factor][1]))
24 def display(self, units, xy, scale_factors):
26 sorted_list = factors_to_names.keys()
27 sorted_list.sort()
29 self._dlg = gtk.Dialog(_('Enter graph units'),
30 flags=gtk.DIALOG_NO_SEPARATOR,
31 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
32 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
33 self._dlg.set_default_response(gtk.RESPONSE_ACCEPT)
34 table = gtk.Table(2, 3, False)
35 table.set_col_spacing(0, 12)
36 table.set_col_spacing(1, 12)
37 # Label, scale factor and entry for X axis
38 label_xunits = gtk.Label(xy[0])
39 align = gtk.Alignment(0, 0.5)
40 align.add(label_xunits)
41 table.attach(align, 0, 1, 0, 1)
42 self._entry_xunits = gtk.Entry()
43 self._entry_xunits.set_text(units[0])
44 self._entry_xunits.set_width_chars(7)
45 self._entry_xunits.set_activates_default(True)
46 table.attach(self._entry_xunits, 2, 3, 0, 1)
47 self._combox_fact = gtk.ComboBox(self._scale_factors)
48 self._combox_fact.set_active(sorted_list.index(scale_factors[0]))
49 cell = gtk.CellRendererText()
50 self._combox_fact.pack_start(cell, True)
51 self._combox_fact.add_attribute(cell, 'text', 1)
52 table.attach(self._combox_fact, 1, 2, 0, 1)
54 # Label, scale factor and entry for Y axis
55 label_yunits = gtk.Label(xy[1])
56 align = gtk.Alignment(0, 0.5)
57 align.add(label_yunits)
58 table.attach(align, 0, 1, 1, 2)
59 self._entry_yunits = gtk.Entry()
60 self._entry_yunits.set_text(units[1])
61 self._entry_yunits.set_width_chars(7)
62 self._entry_yunits.set_activates_default(True)
63 table.attach(self._entry_yunits, 2, 3, 1, 2)
64 self._comboy_fact = gtk.ComboBox(self._scale_factors)
65 self._comboy_fact.set_active(sorted_list.index(scale_factors[1]))
66 cell = gtk.CellRendererText()
67 self._comboy_fact.pack_start(cell, True)
68 self._comboy_fact.add_attribute(cell, 'text', 1)
69 table.attach(self._comboy_fact, 1, 2, 1, 2)
70 self._dlg.vbox.pack_start(table)
72 self._dlg.show_all()
74 def run(self):
75 units = ()
76 scale_factors = []
77 resp = self._dlg.run()
78 if resp == gtk.RESPONSE_ACCEPT:
79 units = (self._entry_xunits.get_text(),
80 self._entry_yunits.get_text())
81 x_factor_index = self._combox_fact.get_active_iter()
82 y_factor_index = self._comboy_fact.get_active_iter()
83 scale_factors = [self._scale_factors.get(x_factor_index, 0)[0],
84 self._scale_factors.get(y_factor_index, 0)[0]]
85 self._dlg.destroy()
86 return units, scale_factors
88 class Enter_Range_Dialog(object):
89 def __init__(self):
90 self._dlg = None
91 self._entries = None
93 def display(self, r, xy, scale_factors, units):
94 self._dlg = gtk.Dialog(_('Enter graph range'),
95 flags=gtk.DIALOG_NO_SEPARATOR,
96 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
97 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
98 self._dlg.set_default_response(gtk.RESPONSE_ACCEPT)
100 self._entries = []
101 minmax = [_('From'), _('To')]
103 hbox = gtk.HBox(False)
104 for col in range(0, 2):
105 frame = gtk.Frame('')
106 frame.get_label_widget().set_markup('<b>'+ xy[col] +'</b>')
107 frame.set_shadow_type(gtk.SHADOW_NONE)
108 table = gtk.Table(1, 3, False)
109 entries_row = []
110 for row in range(0, 2):
111 label = gtk.Label(minmax[row])
112 align_lbl = gtk.Alignment(0, 0.5)
113 align_lbl.add(label)
114 step = abs(float(r[col][0] - r[col][1]))/100.0
115 print step
116 adj = gtk.Adjustment(r[col][row], -1e99, 1e99,
117 step, step * 10.0, 0)
118 entry = gtk.SpinButton(adj, 1,
119 int(math.ceil(abs(math.log10(step)))))
120 entry.set_activates_default(True)
121 units_label = gtk.Label(factors_to_names[scale_factors[col]][0]
122 + units[col])
123 align_units = gtk.Alignment(0, 0.5)
124 align_units.add(units_label)
125 table.attach(align_lbl, 0, 1, row, row + 1, xpadding=3)
126 table.attach(entry, 1, 2, row, row + 1, xpadding=3)
127 table.attach(align_units, 2, 3, row, row + 1, xpadding=3)
128 table.set_row_spacing(row, 6)
129 entries_row.append(entry)
130 self._entries.append(entries_row)
131 box = gtk.HBox(False)
132 box.pack_start(table, False, False, 12)
133 frame.add(box)
134 hbox.pack_start(frame, False, False, 0)
135 self._dlg.vbox.pack_start(hbox, False, False, 6)
137 self._dlg.show_all()
139 def run(self):
140 r = []
141 resp = self._dlg.run()
142 if resp == gtk.RESPONSE_ACCEPT:
143 r = [str(self._entries[0][0].get_value()),
144 str(self._entries[0][1].get_value()),
145 str(self._entries[1][0].get_value()),
146 str(self._entries[1][1].get_value())]
147 self._dlg.destroy()
148 return r
150 DEFAULT_NETLISTER_COMMAND = 'gnetlist -g spice-sdb -O sort_mode -o %s.net %s.sch'
151 DEFAULT_SIMULATOR_COMMAND = 'gnucap -b %s.net'
153 class Run_Netlister_and_Simulate_Dialog(object):
154 def __init__(self):
155 self._dlg = None
156 pass
158 def _make_check_entry(self, name, do_run, commands, default_command):
159 # returns a tuple (check_button, combo_box_entry)
160 combo = gtk.combo_box_entry_new_text()
161 if not commands:
162 commands = [default_command]
163 for cmd in commands:
164 combo.append_text(cmd)
165 combo.set_active(0)
166 combo.set_sensitive(do_run)
168 btn = gtk.CheckButton(_('Run %s:') % name)
169 btn.set_active(do_run)
170 btn.connect('toggled', self._check_button_toggled, combo)
171 return btn, combo
173 def display(self, actions):
174 self._dlg = gtk.Dialog(_("Run netlister and simulate"),
175 flags=gtk.DIALOG_NO_SEPARATOR,
176 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
177 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
178 self._dlg.set_default_response(gtk.RESPONSE_ACCEPT)
180 # netlister part
181 box = gtk.HBox()
182 do_run, commands = actions['run_netlister']
183 btn, combo = self._make_check_entry(_('netlister'), do_run, commands,
184 DEFAULT_NETLISTER_COMMAND)
185 self._ckbutton_netl, self._entry_netl = btn, combo
186 box.pack_start(btn, False, False, 12)
187 box.pack_start(combo, True, True)
188 self._dlg.vbox.pack_start(box, False, False, 6)
190 # simulator part
191 box = gtk.HBox()
192 do_run, commands = actions['run_simulator']
193 btn, combo = self._make_check_entry(_('simulator'), do_run, commands,
194 DEFAULT_SIMULATOR_COMMAND)
195 self._ckbutton_sim, self._entry_sim = btn, combo
196 box.pack_start(btn, False, False, 12)
197 box.pack_start(combo, True, True)
198 self._dlg.vbox.pack_start(box, False, False, 6)
200 group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
201 group.add_widget(self._ckbutton_netl)
202 group.add_widget(self._ckbutton_sim)
204 frame = gtk.Frame('')
205 frame.get_label_widget().set_markup(_('<b>Options</b>'))
206 frame.set_shadow_type(gtk.SHADOW_NONE)
207 vbox = gtk.VBox()
208 box = gtk.HBox(False, 12)
209 label = gtk.Label()
210 label.set_markup(_('Run from directory:'))
211 box.pack_start(label, False, False, 12)
212 dialog = gtk.FileChooserDialog(_('Choose directory'),
213 None,
214 gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
215 buttons=(gtk.STOCK_CANCEL,
216 gtk.RESPONSE_REJECT,
217 gtk.STOCK_OK,
218 gtk.RESPONSE_ACCEPT))
219 dialog.set_filename(actions['run_from'])
220 self._filechoose = gtk.FileChooserButton(dialog)
221 box.pack_start(self._filechoose)
222 vbox.pack_start(box, False)
223 box = gtk.HBox()
224 self._ckbutton_upd = gtk.CheckButton(_('Update readers once terminated'))
225 self._ckbutton_upd.set_active(actions['update'])
226 box.pack_start(self._ckbutton_upd, False, False, 12)
227 vbox.pack_start(box, False, False, 6)
228 frame.add(vbox)
229 self._dlg.vbox.pack_start(frame, False, False, 6)
231 self._dlg.resize(400, 100)
232 self._dlg.show_all()
234 def _collect_data(self):
235 # make sure that the command to run is always the first
236 # element of the list (more recent commands are at the
237 # beginning of the list) and eliminate duplicates
238 netlister_cmds = [row[0] for row in self._entry_netl.get_model()]
239 if self._entry_netl.get_active_text() in netlister_cmds:
240 netlister_cmds.remove(self._entry_netl.get_active_text())
241 netlister_cmds.insert(0, self._entry_netl.get_active_text())
243 simulator_cmds = [row[0] for row in self._entry_sim.get_model()]
244 if self._entry_sim.get_active_text() in simulator_cmds:
245 simulator_cmds.remove(self._entry_sim.get_active_text())
246 simulator_cmds.insert(0, self._entry_sim.get_active_text())
248 actions = {}
249 actions['run_netlister'] = (self._ckbutton_netl.get_active(), netlister_cmds)
250 actions['run_simulator'] = (self._ckbutton_sim.get_active(), simulator_cmds)
251 actions['update'] = self._ckbutton_upd.get_active()
252 actions['run_from'] = self._filechoose.get_filename()
253 return actions
255 def run(self):
256 actions = None
257 if self._dlg.run() == gtk.RESPONSE_ACCEPT:
258 actions = self._collect_data()
259 self._dlg.destroy()
260 return actions
262 def _check_button_toggled(self, button, entry):
263 entry.set_sensitive(button.get_active())