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

Contents of /trunk/pygtk/properties.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2495 - (show annotations) (download) (as text)
Tue Aug 16 11:35:08 2011 UTC (12 years, 10 months ago) by aakash
File MIME type: text/x-python
File size: 8457 byte(s)
Merge further changes from Aakash to trunk.
1 # GUI for ASCEND solver_var properties
2
3 import gtk
4 import ascpy
5 from varentry import *
6 from infodialog import *
7
8 class RelPropsWin:
9 def __init__(self,browser,instance):
10 self.instance = instance;
11 self.browser = browser;
12
13 # GUI config
14 self.browser.builder.add_objects_from_file(self.browser.glade_file, ["relpropswin"])
15 self.window = self.browser.builder.get_object("relpropswin")
16 self.window.set_transient_for(self.browser.window)
17
18 self.relname = self.browser.builder.get_object("relname")
19 self.residual = self.browser.builder.get_object("residual")
20 self.expr = self.browser.builder.get_object("expr")
21 self.included = self.browser.builder.get_object("included")
22 self.active = self.browser.builder.get_object("active")
23 self.exprbuff = gtk.TextBuffer();
24 self.expr.set_buffer(self.exprbuff)
25 self.morepropsbutton = self.browser.builder.get_object("morepropsbutton");
26
27 self.statusimg = self.browser.builder.get_object("rel_statusimg");
28 self.statusmessage = self.browser.builder.get_object("rel_statusmessage");
29
30 self.fill_values()
31 self.browser.builder.connect_signals(self)
32
33 def fill_values(self):
34 self.relname.set_text( self.browser.sim.getInstanceName(self.instance) )
35 self.residual.set_text( str( self.instance.getResidual() ) )
36 self.exprbuff.set_text( self.instance.getRelationAsString(self.browser.sim.getModel() ) )
37 self.included.set_active( self.instance.isIncluded() )
38
39 _status = self.instance.getStatus()
40 self.statusimg.set_from_pixbuf(self.browser.statusicons[_status]);
41 self.statusmessage.set_text(self.browser.statusmessages[_status]);
42
43
44 def on_relpropswin_close(self,*args):
45 self.window.response(gtk.RESPONSE_CANCEL)
46
47 def on_entry_key_press_event(self,widget,event):
48 keyname = gtk.gdk.keyval_name(event.keyval)
49 if keyname=="Escape":
50 self.window.response(gtk.RESPONSE_CLOSE)
51 return True;
52 return False;
53
54 def run(self):
55 self.window.run()
56 self.window.hide()
57
58 def on_morepropsbutton_clicked(self,*args):
59 title = "All properties of '%s'" % self.browser.sim.getInstanceName(self.instance)
60 text = title + "\n\n"
61 c = self.instance.getChildren()
62 if c:
63 for i in c:
64 text += "%s = %s\n" % (self.browser.sim.getInstanceName(i), i.getValue())
65 else:
66 text += "This relation has no 'child' properties"
67 _dialog = InfoDialog(self.browser,self.window,text,title)
68 _dialog.run()
69
70 def on_included_toggled(self,widget,*args):
71 self.instance.setIncluded(widget.get_active())
72 self.browser.do_solve_if_auto()
73
74 class VarPropsWin:
75 def __init__(self,browser,instance):
76 self.instance = instance;
77 self.browser = browser;
78
79 # GUI config
80 self.browser.builder.add_objects_from_file(self.browser.glade_file, ["varpropswin"])
81 self.window = self.browser.builder.get_object("varpropswin")
82 self.window.set_transient_for(self.browser.window)
83
84 self.varname = self.browser.builder.get_object("varname")
85 self.valueentry= self.browser.builder.get_object("valueentry");
86 self.lowerentry = self.browser.builder.get_object("lowerentry");
87 self.upperentry = self.browser.builder.get_object("upperentry");
88 self.nominalentry = self.browser.builder.get_object("nominalentry");
89 self.fixed = self.browser.builder.get_object("fixed");
90 self.free = self.browser.builder.get_object("free");
91
92 self.statusimg = self.browser.builder.get_object("var_statusimg");
93 assert self.statusimg
94 self.statusmessage = self.browser.builder.get_object("var_statusmessage");
95
96 self.cliquebutton = self.browser.builder.get_object("cliquebutton");
97 self.morepropsbutton = self.browser.builder.get_object("morepropsbutton");
98
99 self.fill_values()
100
101 self.browser.builder.connect_signals(self)
102
103 def fill_values(self):
104 # all the values here use the same preferred units for this instance type
105
106 _u = self.instance.getType().getPreferredUnits();
107 if _u is None:
108 _conversion = 1
109 _u = self.instance.getDimensions().getDefaultUnits().getName().toString()
110 else:
111 _conversion = _u.getConversion() # displayvalue x conversion = SI
112 _u = _u.getName().toString()
113
114 _arr = {
115 self.valueentry: self.instance.getRealValue()
116 ,self.lowerentry: self.instance.getLowerBound()
117 ,self.upperentry: self.instance.getUpperBound()
118 ,self.nominalentry: self.instance.getNominal()
119 }
120 for _k,_v in _arr.iteritems():
121 _t = str(_v / _conversion)+" "+_u
122 _k.set_text(_t)
123 self.parse_entry(_k)
124
125 self.varname.set_text(self.browser.sim.getInstanceName(self.instance));
126
127 if self.instance.isFixed():
128 self.fixed.set_active(True);
129 else:
130 self.free.set_active(True);
131
132 _status = self.instance.getStatus()
133
134 self.statusimg.set_from_pixbuf(self.browser.statusicons[_status]);
135 self.statusmessage.set_text(self.browser.statusmessages[_status]);
136
137 def apply_changes(self):
138 print "APPLY"
139 # check the units of the entered values are acceptable
140
141 _arr = {
142 self.valueentry: self.instance.setRealValue
143 ,self.lowerentry: self.instance.setLowerBound
144 ,self.upperentry: self.instance.setUpperBound
145 ,self.nominalentry: self.instance.setNominal
146 }
147 failed = False;
148 for _k,_v in _arr.iteritems():
149 i = RealAtomEntry(self.instance, _k.get_text())
150 try:
151 i.checkEntry()
152 self.taint_entry(_k,"white");
153 _v(i.getValue())
154 except InputError, e:
155 print "INPUT ERROR: ",str(e)
156 self.taint_entry(_k,"#FFBBBB");
157 failed = True;
158
159 self.instance.setFixed(self.fixed.get_active())
160
161 if failed:
162 raise InputError(None) # no message
163
164 self.browser.do_solve_if_auto()
165
166 def taint_entry(self, entry, color):
167 entry.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
168 entry.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
169 entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
170 entry.modify_base(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
171 if color == "#FFBBBB":
172 entry.set_property("secondary-icon-stock", 'gtk-dialog-error')
173 elif color == "white":
174 entry.set_property("secondary-icon-stock", 'gtk-yes')
175 entry.set_property("secondary-icon-tooltip-text", "")
176
177 def parse_entry(self, entry):
178 # A simple function to get the real value from the entered text
179 # and taint the entry box accordingly
180 i = RealAtomEntry(self.instance, entry.get_text())
181 try:
182 i.checkEntry()
183 _value = i.getValue()
184 except InputError, e:
185 _value = None
186 _error = re.split('Input Error: ', str(e), 1)
187 entry.set_property("secondary-icon-tooltip-text", _error[1])
188
189 if _value is not None:
190 self.taint_entry(entry, "white")
191 else:
192 self.taint_entry(entry, "#FFBBBB")
193 return _value
194
195 def on_varpropswin_close(self,*args):
196 self.window.response(gtk.RESPONSE_CANCEL)
197
198 def on_entry_key_press_event(self,widget,event):
199 keyname = gtk.gdk.keyval_name(event.keyval)
200 if keyname=="Return":
201 self.window.response(gtk.RESPONSE_OK)
202 return True
203 elif keyname=="Escape":
204 self.window.response(gtk.RESPONSE_CANCEL)
205 return True;
206 return False;
207
208 def on_cliquebutton_clicked(self,*args):
209 title = "Clique of '%s'"%self.browser.sim.getInstanceName(self.instance)
210 text = title + "\n\n"
211 s = self.instance.getClique();
212 if s:
213 for i in s:
214 text += "%s\n"%self.browser.sim.getInstanceName(i)
215 else:
216 text += "CLIQUE IS EMPTY"
217 _dialog = InfoDialog(self.browser,self.window,text,title)
218 _dialog.run()
219
220 def on_aliasesbutton_clicked(self,*args):
221 title = "Aliases of '%s'"%self.browser.sim.getInstanceName(self.instance)
222 text = title + "\n\n"
223 s = self.instance.getAliases();
224 if s:
225 for i in sorted(s):
226 text += "%s\n" % i
227 else:
228 text += "NO ALIASES"
229 _dialog = InfoDialog(self.browser,self.window,text,title)
230 _dialog.run()
231
232 def on_morepropsbutton_clicked(self,*args):
233 title = "All properties of '%s'" % self.browser.sim.getInstanceName(self.instance)
234 text = title + "\n\n"
235 c = self.instance.getChildren()
236 if c:
237 for i in c:
238 text += "%s = %s\n" % (self.browser.sim.getInstanceName(i), i.getValue())
239 else:
240 text += "This variable has no 'child' properties"
241 _dialog = InfoDialog(self.browser,self.window,text,title)
242 _dialog.run()
243
244 def run(self):
245 self.valueentry.grab_focus()
246 _continue = True;
247 while _continue:
248 _res = self.window.run();
249 try:
250 if _res == gtk.RESPONSE_APPLY or _res == gtk.RESPONSE_OK:
251 self.apply_changes();
252 except InputError:
253 # if input error, assume that the gui has been updated appropriately
254 continue;
255
256 if _res == gtk.RESPONSE_OK or _res==gtk.RESPONSE_CANCEL:
257 _continue = False;
258
259 self.window.destroy();
260
261
262

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