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

Contents of /trunk/pygtk/solverparameters.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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