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

Contents of /trunk/pygtk/properties.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1704 - (show annotations) (download) (as text)
Sun Jan 6 04:54:18 2008 UTC (12 years, 3 months ago) by jpye
File MIME type: text/x-python
File size: 6887 byte(s)
Added context menu for 'select units'.

1 # GUI for ASCEND solver_var properties
2
3 import gtk, gtk.glade
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 _xml = gtk.glade.XML(browser.glade_file,"relpropswin")
15 self.window = _xml.get_widget("relpropswin")
16 self.window.set_transient_for(self.browser.window)
17
18 self.relname = _xml.get_widget("relname")
19 self.residual = _xml.get_widget("residual")
20 self.expr = _xml.get_widget("expr")
21 self.included = _xml.get_widget("included")
22 self.active = _xml.get_widget("active")
23 self.exprbuff = gtk.TextBuffer();
24 self.expr.set_buffer(self.exprbuff)
25 self.morepropsbutton = _xml.get_widget("morepropsbutton");
26
27 self.statusimg = _xml.get_widget("statusimg");
28 self.statusmessage = _xml.get_widget("statusmessage");
29
30 self.fill_values()
31 _xml.signal_autoconnect(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 _xml = gtk.glade.XML(browser.glade_file,"varpropswin")
81 self.window = _xml.get_widget("varpropswin")
82 self.window.set_transient_for(self.browser.window)
83
84 self.varname = _xml.get_widget("varname")
85 self.valueentry= _xml.get_widget("valueentry");
86 self.lowerentry = _xml.get_widget("lowerentry");
87 self.upperentry = _xml.get_widget("upperentry");
88 self.nominalentry = _xml.get_widget("nominalentry");
89 self.fixed = _xml.get_widget("fixed");
90 self.free = _xml.get_widget("free");
91
92 self.statusimg = _xml.get_widget("statusimg");
93 self.statusmessage = _xml.get_widget("statusmessage");
94
95 self.cliquebutton = _xml.get_widget("cliquebutton");
96 self.morepropsbutton = _xml.get_widget("morepropsbutton");
97
98 self.fill_values()
99
100 _xml.signal_autoconnect(self)
101
102 def fill_values(self):
103 # all the values here use the same preferred units for this instance type
104
105 _u = self.instance.getType().getPreferredUnits();
106 if _u is None:
107 _conversion = 1
108 _u = self.instance.getDimensions().getDefaultUnits().getName().toString()
109 else:
110 _conversion = _u.getConversion() # displayvalue x conversion = SI
111 _u = _u.getName().toString()
112
113 _arr = {
114 self.valueentry: self.instance.getRealValue()
115 ,self.lowerentry: self.instance.getLowerBound()
116 ,self.upperentry: self.instance.getUpperBound()
117 ,self.nominalentry: self.instance.getNominal()
118 }
119 for _k,_v in _arr.iteritems():
120 _t = str(_v / _conversion)+" "+_u
121 _k.set_text(_t)
122
123 self.varname.set_text(self.browser.sim.getInstanceName(self.instance));
124
125 if self.instance.isFixed():
126 self.fixed.set_active(True);
127 else:
128 self.free.set_active(True);
129
130 _status = self.instance.getStatus()
131
132 self.statusimg.set_from_pixbuf(self.browser.statusicons[_status]);
133 self.statusmessage.set_text(self.browser.statusmessages[_status]);
134
135 def apply_changes(self):
136 print "APPLY"
137 # check the units of the entered values are acceptable
138
139 _arr = {
140 self.valueentry: self.instance.setRealValue
141 ,self.lowerentry: self.instance.setLowerBound
142 ,self.upperentry: self.instance.setUpperBound
143 ,self.nominalentry: self.instance.setNominal
144 }
145 failed = False;
146 for _k,_v in _arr.iteritems():
147 i = RealAtomEntry(self.instance, _k.get_text())
148 try:
149 i.checkEntry()
150 self.color_entry(_k,"white");
151 _v(i.getValue())
152 except InputError, e:
153 print "INPUT ERROR: ",str(e)
154 self.color_entry(_k,"#FFBBBB");
155 failed = True;
156
157 self.instance.setFixed(self.fixed.get_active())
158
159 if failed:
160 raise InputError(None) # no message
161
162 self.browser.do_solve_if_auto()
163
164 def color_entry(self,entry,color):
165 entry.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
166 entry.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
167 entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
168 entry.modify_base(gtk.STATE_ACTIVE, gtk.gdk.color_parse(color))
169
170 def on_varpropswin_close(self,*args):
171 self.window.response(gtk.RESPONSE_CANCEL)
172
173 def on_entry_key_press_event(self,widget,event):
174 keyname = gtk.gdk.keyval_name(event.keyval)
175 if keyname=="Return":
176 self.window.response(gtk.RESPONSE_OK)
177 return True
178 elif keyname=="Escape":
179 self.window.response(gtk.RESPONSE_CANCEL)
180 return True;
181 return False;
182
183 def on_cliquebutton_clicked(self,*args):
184 title = "Clique of '%s'"%self.browser.sim.getInstanceName(self.instance)
185 text = title + "\n\n"
186 s = self.instance.getClique();
187 if s:
188 for i in s:
189 text += "%s\n"%self.browser.sim.getInstanceName(i)
190 else:
191 text += "CLIQUE IS EMPTY"
192 _dialog = InfoDialog(self.browser,self.window,text,title)
193 _dialog.run()
194
195 def on_morepropsbutton_clicked(self,*args):
196 title = "All properties of '%s'" % self.browser.sim.getInstanceName(self.instance)
197 text = title + "\n\n"
198 c = self.instance.getChildren()
199 if c:
200 for i in c:
201 text += "%s = %s\n" % (self.browser.sim.getInstanceName(i), i.getValue())
202 else:
203 text += "This variable has no 'child' properties"
204 _dialog = InfoDialog(self.browser,self.window,text,title)
205 _dialog.run()
206
207 def run(self):
208 self.valueentry.grab_focus()
209 _continue = True;
210 while _continue:
211 _res = self.window.run();
212 try:
213 if _res == gtk.RESPONSE_APPLY or _res == gtk.RESPONSE_OK:
214 self.apply_changes();
215 except InputError:
216 # if input error, assume that the gui has been updated appropriately
217 continue;
218
219 if _res == gtk.RESPONSE_OK or _res==gtk.RESPONSE_CANCEL:
220 _continue = False;
221
222 self.window.destroy();
223
224
225

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