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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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