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

Contents of /trunk/pygtk/observer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 855 - (show annotations) (download) (as text)
Wed Sep 20 14:00:41 2006 UTC (14 years ago) by johnpye
File MIME type: text/x-python
File size: 6769 byte(s)
Removed some verbage in Integrator output
1 import pygtk
2 pygtk.require('2.0')
3 import gtk
4 import gtk.glade
5 import pango
6
7 OBSERVER_EDIT_COLOR = "#008800"
8 OBSERVER_NOEDIT_COLOR = "#000088"
9 OBSERVER_NORMAL_COLOR = "black"
10
11 # This code uses the technique described in
12 # http://www.daa.com.au/pipermail/pygtk/2006-February/011777.html
13
14 OBSERVER_NUM=0
15
16 class ObserverColumn:
17 """
18 A class to identify the instance that relates to a specify column
19 and the units of measurement and column title, etc.
20 """
21 def __init__(self,instance,index,name=None,units=None,browser=None):
22 self.instance = instance
23 self.name = name
24 self.index = index
25
26 if name==None:
27 if browser == None:
28 name = "UNNAMED"
29 else:
30 name = browser.sim.getInstanceName(instance)
31
32 if units == None:
33 units = instance.getType().getPreferredUnits()
34 if units == None:
35 units = instance.getType().getDimensions().getDefaultUnits()
36
37 uname = str(units.getName())
38 if uname.find("/")!=-1:
39 uname = "["+uname+"]"
40
41 if uname == "":
42 _title = "%s" % (name)
43 else:
44 _title = "%s / %s" % (name, uname)
45
46 self.title = _title
47 self.units = units
48 self.uname = uname
49 self.name = name
50
51 def __repr__(self):
52 return "ObserverColumn(name="+self.name+")"
53
54 def cellvalue(self, column, cell, model, iter):
55 #print "RENDERING COLUMN",self.index
56 _rowobject = model.get_value(iter,0)
57
58 cell.set_property('editable',False)
59 cell.set_property('weight',400)
60 try:
61 if _rowobject.active:
62 _rawval = self.instance.getRealValue()
63 if self.instance.getType().isRefinedSolverVar():
64 if self.instance.isFixed():
65 cell.set_property('editable',True)
66 cell.set_property('weight',700)
67 cell.set_property('foreground',OBSERVER_EDIT_COLOR)
68 else:
69 cell.set_property('foreground',OBSERVER_NOEDIT_COLOR)
70 else:
71 cell.set_property('foreground',OBSERVER_NORMAL_COLOR)
72 _rawval = _rowobject.values[self.index]
73 _dataval = _rawval / self.units.getConversion()
74 except KeyError:
75 _dataval = ""
76
77 cell.set_property('text', _dataval)
78
79 class ObserverRow:
80 """
81 Just a container for a vector of values, but with columns that
82 should correspond to those in the Observer object's vector of
83 ObserverColumn objects.
84 """
85 def __init__(self,values=None,active=True):
86 if values==None:
87 values=[]
88
89 self.values = values
90 self.active = active
91
92 def make_static(self,table):
93 self.active = False
94 print "TABLE COLS:",table.cols
95 print "ROW VALUES:",self.values
96 r=0;
97 for index,col in table.cols.iteritems():
98 print "ROW",r,"; INDEX: ",index,"; COL: ",col
99 try:
100 self.values[index] = col.instance.getRealValue()
101 except IndexError,e:
102 print "Index error: e=",str(e)
103 self.values[index] = None
104 r=r+1
105 print "Made static, values:",self.values
106
107 def get_values(self,table):
108 if not self.active:
109 return self.values
110 else:
111 _v = []
112 for index,col in table.cols.iteritems():
113 _v.append( col.instance.getRealValue() / col.units.getConversion() )
114 return _v
115
116 class ObserverTab:
117
118 def __init__(self,xml,browser,tab,name=None,alive=True):
119 global OBSERVER_NUM
120 self.colindex = 0
121 if name==None:
122 OBSERVER_NUM=OBSERVER_NUM+1
123 name = "Observer %d" % OBSERVER_NUM
124 self.name = name
125 self.browser=browser
126 xml.signal_autoconnect(self)
127 self.view = xml.get_widget('observerview')
128 self.tab = tab
129 self.alive=alive
130 if self.alive:
131 self.browser.reporter.reportNote("New observer is 'alive'")
132
133 self.keptimg = gtk.Image()
134 self.activeimg = gtk.Image()
135 self.activeimg.set_from_file("glade/active.png")
136 # create PixBuf objects from these?
137 self.rows = []
138 _store = gtk.TreeStore(object)
139 self.cols = {}
140
141 # create the 'active' pixbuf column
142 _renderer = gtk.CellRendererPixbuf()
143 _col = gtk.TreeViewColumn()
144 _col.set_title("")
145 _col.pack_start(_renderer,False)
146 _col.set_cell_data_func(_renderer, self.activepixbufvalue)
147 self.view.append_column(_col);
148
149 # initially there will not be any other columns
150
151 if self.alive:
152 # for a 'live' Observer, create the 'active' bottom row
153 self.browser.reporter.reportNote("Adding empty row to store")
154 _row = ObserverRow()
155 self.activeiter = _store.append(None, [_row] )
156 self.rows.append(_row)
157
158 self.view.set_model(_store)
159 self.browser.reporter.reportNote("Created observer '%s'" % self.name)
160
161 def activepixbufvalue(self,column,cell,model,iter):
162 _rowobject = model.get_value(iter,0)
163 if _rowobject.active:
164 cell.set_property('pixbuf',self.activeimg.get_pixbuf())
165 else:
166 cell.set_property('pixbuf',self.keptimg.get_pixbuf())
167
168 def on_add_clicked(self,*args):
169 self.do_add_row()
170
171 def on_clear_clicked(self,*args):
172 _store = self.view.get_model()
173 _store.clear();
174 self.rows = {}
175 self.activeiter = _store.append(None, [ObserverRow()] )
176
177 def do_add_row(self,values=None):
178 _store = self.view.get_model()
179 if self.alive:
180 _row = ObserverRow()
181 self.rows.append(_row)
182 _oldrow = _store.get_value(self.activeiter,0)
183 _oldrow.make_static(self)
184 self.activeiter = _store.append(None,[_row])
185 _path = _store.get_path(self.activeiter)
186 _oldpath,_oldcol = self.view.get_cursor()
187 self.view.set_cursor(_path, _oldcol)
188 else:
189 _row = ObserverRow(values=values,active=False)
190 self.rows.append(_row)
191 _store.append(None,[_row])
192 self.browser.reporter.reportNote("Added data row")
193
194 def on_view_cell_edited(self, renderer, path, newtext, col):
195 # we can assume it's always the self.activeiter that is edited...
196 if col.instance.isFixed():
197 val = float(newtext) * col.units.getConversion()
198 col.instance.setRealValue( val )
199 self.browser.reporter.reportNote("Updated value to %f" % float(newtext))
200 else:
201 self.browser.reporter.reportError("Can't set a FREE variable from the Observer")
202 return
203 self.browser.do_solve_if_auto()
204
205 def sync(self):
206 self.view.queue_draw()
207 self.browser.reporter.reportNote("SYNC performed")
208
209 def add_instance(self,instance):
210 _col = ObserverColumn(instance,self.colindex,browser=self.browser)
211 self.cols[self.colindex] = _col
212 self.colindex = self.colindex + 1
213
214 # create a new column
215 _renderer = gtk.CellRendererText()
216 _renderer.connect('edited',self.on_view_cell_edited, _col)
217 _tvcol = gtk.TreeViewColumn()
218 _tvcol.set_title(_col.title)
219 _tvcol.pack_start(_renderer,False)
220 _tvcol.set_cell_data_func(_renderer, _col.cellvalue)
221 self.view.append_column(_tvcol);
222 self.browser.reporter.reportError("cols = "+str(self.cols))
223
224 def copy_to_clipboard(self,clip):
225 _s = []
226 _s.append('\t'.join([_v.title for _k,_v in self.cols.iteritems()]))
227 print "COPYING %d ROWS" % len(self.rows)
228 for _r in self.rows:
229 _s.append("\t".join([`_v` for _v in _r.get_values(self)]))
230
231 clip.set_text('\n'.join(_s),-1)
232
233 self.browser.reporter.reportNote("Observer '%s' data copied to clipboard" % self.name)

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