/[ascend]/trunk/pygtk/interface/solverparameters.py
ViewVC logotype

Contents of /trunk/pygtk/interface/solverparameters.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 230 - (show annotations) (download) (as text)
Sun Jan 29 07:16:06 2006 UTC (14 years, 5 months ago) by johnpye
File MIME type: text/x-python
File size: 7613 byte(s)
Solverparameters GUI code split into separate file.
Help launcher started, will need more work.
Corrected wrong cpu_elapsed values in GUI by direct use of tm_cpu_time()
Added 'about' box.
Fixed 'auto' menu option.
1
2 #======================================================
3 # SOLVER PARAMETERS WINDOW
4
5 class SolverParametersWindow:
6 def __init__(self,sim,reporter):
7 self.sim = sim
8 self.params = self.sim.getSolverParameters();
9
10 self.reporter = reporter
11
12 _xml = gtk.glade.XML(GLADE_FILE,"paramswin")
13 self.window = _xml.get_widget("paramswin")
14 self.paramdescription = _xml.get_widget("paramdescription")
15 self.solvername = _xml.get_widget("solvername")
16
17 _xml.signal_autoconnect(self)
18
19 self.solvername.set_text(self.sim.getSolver().getName())
20
21 self.paramsview = _xml.get_widget("paramsview")
22 self.otank = {}
23 self.paramstore = gtk.TreeStore(str,str,str,bool,str,int)
24 self.paramsview.set_model(self.paramstore)
25
26 # name column
27 _renderer0 = gtk.CellRendererText()
28 _col0 = gtk.TreeViewColumn("Name", _renderer0, text=0, background=4, weight=5)
29 self.paramsview.append_column(_col0)
30
31 # value column: 'editable' set by column 3 of the model data.
32 _renderer1 = gtk.CellRendererText()
33 _renderer1.connect('edited',self.on_paramsview_edited)
34 _col1 = gtk.TreeViewColumn("Value", _renderer1, text=1, editable=3, background=4)
35 self.paramsview.append_column(_col1)
36
37 # range column
38 _renderer2 = gtk.CellRendererText()
39 _col2 = gtk.TreeViewColumn("Range", _renderer2, text=2, background=4)
40 self.paramsview.append_column(_col2)
41
42 self.populate()
43
44 self.paramsview.expand_all()
45
46 #def on_paramswin_key_press_event(self,widget,event):
47 # if event.keyval == ESCAPE_KEY:
48 # if not gtk.gdk.events_pending():
49 # self.do_destroy()
50
51 def on_paramsview_row_activated(self,treeview,path,view_column,*args,**kwargs):
52 # get back the object we just clicked
53
54 if not self.otank.has_key(path):
55 return
56
57 _iter,_param = self.otank[path]
58
59 if _param.isBool():
60 newvalue = not _param.getBoolValue()
61 _param.setBoolValue(newvalue)
62 if newvalue:
63 self.paramstore.set_value(_iter,1,SOLVERPARAM_BOOL_TRUE)
64 else:
65 self.paramstore.set_value(_iter,1,SOLVERPARAM_BOOL_FALSE)
66 self.paramstore.set_value(_iter,4, CHANGED_COLOR)
67
68 def on_paramsview_button_press_event(self,widget,event):
69 if event.button == 1:
70 _x = int(event.x)
71 _y = int(event.y)
72 _time = event.time
73 _pathinfo = self.paramsview.get_path_at_pos(_x, _y)
74 if _pathinfo != None:
75 _path, _col, _cellx, _celly = _pathinfo
76 if not self.otank.has_key(_path):
77 return
78 _iter, _param = self.otank[_path]
79
80 # update the description field
81 self.paramdescription.set_text(_param.getDescription())
82
83 if _param.isStr():
84 _menu = gtk.Menu();
85 _head = gtk.ImageMenuItem("Options",True)
86 _head.show()
87 _head.set_sensitive(False)
88 _img = gtk.Image()
89 _img.set_from_file('icons/folder-open.png')
90 _head.set_image(_img)
91 _menu.append(_head)
92 _sep = gtk.SeparatorMenuItem(); _sep.show()
93 _menu.append(_sep);
94
95 _item = None;
96 for i in _param.getStrOptions():
97 _item = gtk.RadioMenuItem(group=_item, label=i);
98 if i == _param.getStrValue():
99 _item.set_active(True)
100 else:
101 _item.set_active(False)
102 _item.show()
103 _item.connect('activate', self.on_menu_activate, _param, _iter, i);
104 _menu.append(_item)
105
106 _menu.show()
107 _menu.popup(None, None, None, event.button, _time)
108
109 def on_menu_activate(self, menuitem, param, iter, newvalue):
110 if param.getStrValue() != newvalue:
111 param.setStrValue(newvalue)
112 self.paramstore.set_value(iter, 1, newvalue)
113 self.paramstore.set_value(iter, 4, CHANGED_COLOR)
114 else:
115 print "NOT CHANGED"
116
117 def on_paramsview_cursor_changed(self, *args, **kwargs):
118 _path, _col = self.paramsview.get_cursor()
119 if not self.otank.has_key(_path):
120 self.paramdescription.set_text("")
121 return
122 _iter, _param = self.otank[_path]
123 self.paramdescription.set_text(_param.getDescription())
124 #self.paramsview.set_cursor(_path,self.paramsview.get_column(1));
125
126 def on_paramsview_edited(self, renderer, path, newtext, **kwargs):
127 # get back the Instance object we just edited (having to use this seems like a bug)
128 path = tuple( map(int,path.split(":")) )
129
130 if not self.otank.has_key(path):
131 raise RuntimeError("cell_edited_callback: invalid path '%s'" % path)
132 return
133
134 _iter,_param = self.otank[path]
135 # you can only edit real, int, str:
136
137 _changed = False
138 if _param.isInt():
139 newvalue = int(newtext)
140 if _param.isBounded():
141 if newvalue > _param.getIntUpperBound():
142 self.doErrorDialog()
143 return False
144 if newvalue < _param.getIntLowerBound():
145 self.doErrorDialog()
146 return False
147 if _param.getIntValue() != newvalue:
148 _param.setIntValue(newvalue)
149 _changed = True
150 elif _param.isReal():
151 newvalue = float(newtext)
152 if _param.isBounded():
153 if newvalue > _param.getRealUpperBound():
154 self.doErrorDialog()
155 return False
156 if newvalue < _param.getRealLowerBound():
157 self.doErrorDialog()
158 return False
159 if _param.getRealValue() != newvalue:
160 _param.setRealValue(newvalue)
161 _changed = True
162 elif _param.isStr():
163 newvalue = str(newtext)
164 if _param.getStrValue() != newvalue:
165 _param.setStrValue(newvalue)
166 _changed = True
167
168 if _changed:
169 self.paramstore.set_value(_iter, 1, newvalue)
170 self.paramstore.set_value(_iter, 4, CHANGED_COLOR)
171 else:
172 print "NO CHANGE"
173
174 def create_row_data(self,p):
175 _row = [p.getLabel()];
176 if p.isStr():
177 _row.extend([p.getStrValue(), str(len(p.getStrOptions()))+" options", False]);
178 elif p.isBool():
179 if p.getBoolValue():
180 _val = SOLVERPARAM_BOOL_TRUE
181 else:
182 _val = SOLVERPARAM_BOOL_FALSE
183 _row.extend([_val,"",False])
184 elif p.isReal():
185 if not p.isBounded():
186 _row.extend([str(p.getRealValue()), "",True])
187 else:
188 _row.extend([str(p.getRealValue()), "[ "+str(p.getRealLowerBound())+", "+str(p.getRealUpperBound())+" ]",True])
189 elif p.isInt():
190 if not p.isBounded():
191 _row.extend([str(p.getIntValue()), "", True])
192 else:
193 _row.extend([str(p.getIntValue()), "[ "+str(p.getIntLowerBound())+", "+str(p.getIntUpperBound())+" ]", True])
194
195 else:
196 raise RuntimeError("invalid type")
197
198 _row.extend(["white",pango.WEIGHT_NORMAL])
199 return _row;
200
201 def populate(self):
202 # Fill the paramstore with data
203
204 data = {}
205 for i in self.params:
206 if not data.has_key(i.getPage()):
207 data[i.getPage()] = {}
208 data[i.getPage()][i.getNumber()] = i;
209
210 _pagenum = 1;
211 for _page in sorted(data.keys()):
212 if len(data[_page].keys()):
213 _pageiter = self.paramstore.append( None, ["Page "+str(_pagenum), "", "", False, "white", pango.WEIGHT_BOLD])
214 for _number in sorted(data[_page].keys()):
215 _param = data[_page][_number]
216 _piter = self.paramstore.append( _pageiter, self.create_row_data(_param) )
217 _path = self.paramstore.get_path(_piter)
218 self.otank[ _path ] = (_piter, _param)
219 _pagenum = _pagenum + 1
220
221 def doErrorDialog(self,msg=None):
222 _dialog = gtk.Dialog("Out of bounds", parent=self.window, flags=gtk.DIALOG_MODAL, buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK) )
223 if msg:
224 _label = gtk.Label(msg)
225 else:
226 _label = gtk.Label("Please enter a value that is within the\ndisplayed upper and lower bounds")
227
228 _dialog.vbox.pack_start(_label, True, True, 0)
229 _label.show()
230 _dialog.run()
231 _dialog.destroy()
232
233 def show(self):
234 self.window.show()
235
236 def on_paramscancel_clicked(self,*args,**kwargs):
237 self.do_destroy()
238
239 def on_paramsapply_clicked(self,*args,**kwargs):
240 self.sim.setSolverParameters(self.params);
241 self.do_destroy()
242
243 def on_paramswin_destroy(self,*args,**kwargs):
244 self.do_destroy()
245
246 def do_destroy(self):
247 self.window.hide()
248 del(self.window)
249 del(self.params)

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