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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download) (as text)
Wed Feb 8 08:31:56 2006 UTC (18 years, 4 months ago) by johnpye
File MIME type: text/x-python
File size: 6370 byte(s)
Some work on adding the 'block diagnosis' window
1 import pygtk
2 pygtk.require('2.0')
3 import gtk
4 import gtk.glade
5 import pango
6
7 OBSERVER_INITIAL_COLS = 3 # how many cells are at the start of the table?
8 OBSERVER_ICON, OBSERVER_WEIGHT, OBSERVER_EDIT = range(0,OBSERVER_INITIAL_COLS) # column indices for the start of the TreeStore
9 OBSERVER_NULL = 0 # value that gets added to empty cells in a new column
10
11 # This is messy code since it doesn't observe the convention of keeping your model
12 # separate from your view. It's all mixed up together. Yuck. Part of the
13 # difficulty with that was the fact that TreeStores don't support the adding of
14 # columns.
15
16
17 # Update: there is a technique for doing this, in fact:
18 # http://www.daa.com.au/pipermail/pygtk/2006-February/011777.html
19
20 class ObserverTab:
21 def __init__(self,xml,name,browser,tab):
22 xml.signal_autoconnect(self);
23
24 self.view = xml.get_widget('observerview')
25 self.tab = tab
26
27 #self.activeimg = gtk.Image()
28 #self.activeimg.set_from_file("icons/active.png")
29 self.activeimg = None
30 self.keptimg = None
31
32 # no instances yet in the observer:
33 self.columninstances = []
34
35 self.columns = [gtk.gdk.Pixbuf,int,bool]
36
37 # units for each data column
38 self.units = []
39 self.titles = []
40
41 _store = gtk.TreeStore(*self.columns)
42 self.rows = []
43
44 # add an empty first row
45 self.rows.append([])
46
47 # work towards having multiple observers for multiple simulations
48 self.name = name
49 self.browser = browser
50
51 # create the 'active' pixvuf columns
52 _renderer = gtk.CellRendererPixbuf()
53 _col = gtk.TreeViewColumn()
54 _col.set_title("")
55 _col.pack_start(_renderer,False)
56 _col.add_attribute(_renderer, 'pixbuf', OBSERVER_ICON)
57 self.view.append_column(_col);
58
59 # create the first row
60 print "Adding row",self.rows[0],"to store"
61 _store.append(None, self.make_row(True, self.rows[0]) )
62
63 self.activerow = 0
64
65 self.view.set_model(_store)
66
67 self.browser.reporter.reportNote("Created observer '%s'" % self.name)
68
69 def on_add_clicked(self,*args):
70 self.do_add_row()
71
72 def do_add_row(self):
73 _rownum = len(self.rows)
74
75 # add a copy of the last row
76 self.rows.append(self.rows[_rownum-1])
77 self.activerow = _rownum
78
79 _m = self.view.get_model()
80 _m.set(self.activeiter,OBSERVER_ICON,self.keptimg,OBSERVER_WEIGHT,pango.WEIGHT_NORMAL,OBSERVER_EDIT,False)
81 self.activeiter = _m.append(None,self.make_row(True,self.rows[_rownum]))
82 self.browser.reporter.reportNote("Kept current values");
83
84 # if the observer is the active tab, move the cursor to the new row.
85 if self.browser.maintabs.get_current_page() == self.tab:
86 self.view.set_cursor(_m.get_path(self.activeiter))
87
88 def on_clear_clicked(self,*args):
89 self.rows = []
90 _r = [_i.getRealValue() for _i in self.columninstances]
91 self.rows.append(_r)
92 self.view.get_model().clear();
93 self.view.get_model().append(None,self.make_row(True,_r))
94 self.browser.reporter.reportNote("Observer '%s' cleared" % self.name)
95
96 def on_view_cell_edited(self, renderer, path, newtext, datacolumn):
97 # we can assume it's always the self.activeiter that is edited...
98 if self.columninstances[datacolumn].isFixed():
99 self.columninstances[datacolumn].setRealValue( float(newtext) * self.units[datacolumn].getConversion() )
100 else:
101 self.browser.reporter.reportError("Can't set a FIXED variable from the Observer")
102 return
103 self.browser.do_solve_if_auto()
104
105 def sync(self):
106 #new row data
107 _r = [self.columninstances[_i].getRealValue() / self.units[_i].getConversion() for _i in range(0,len(self.columninstances)) ]
108
109 _r1 = self.make_row(True,_r)
110
111 # stick the row data into the TreeStore
112 _m = self.view.get_model()
113 _i = 0
114 for _c in _r1:
115 _m.set(self.activeiter, _i, _c)
116 _i = _i + 1
117
118 # keep the data in self.rows as well
119 self.rows[self.activerow] = _r;
120
121 def copy_to_clipboard(self,clip):
122 _s = []
123 _s.append('\t'.join(self.titles))
124 for _r in self.rows:
125 _s.append( '\t'.join([`_c` for _c in _r]) )
126
127 clip.set_text('\n'.join(_s),-1)
128
129 self.browser.reporter.reportNote("Observer '%s' data copied to clipboard" % self.name)
130
131 def make_row(self,isactive,row):
132 # add the initial OBSERVER_INITIAL_COLS fields:
133 if isactive:
134 _r = [self.activeimg, pango.WEIGHT_BOLD, True]
135 else:
136 _r = [self.keptimg, pango.WEIGHT_NORMAL, False]
137
138 for _c in row:
139 _r.append(_c)
140
141 return _r
142
143 def add_instance(self, inst):
144 if not inst.getType().isRefinedSolverVar():
145 self.browser.reporter.reportError("Instance is not a refined solver variable: can't 'observe'.");
146 return
147
148 _colnum = len(self.columns)
149 _colname = self.browser.sim.getInstanceName(inst)
150 _rownum = len(self.rows)-1
151
152 # store the instances in self.columninstances for sync purposes
153 self.columninstances.append(inst)
154
155 # create new TreeStore, copy of old, plus one columm
156 self.columns.append(float)
157 _units = inst.getType().getPreferredUnits()
158 if _units == None:
159 _units = inst.getType().getDimensions().getDefaultUnits()
160
161 _uname = str(_units.getName())
162 if _uname.find("/")!=-1:
163 _uname = "["+_uname+"]"
164
165 _title = "%s / %s" % (_colname, _uname)
166
167 self.titles.append(_title);
168 self.units.append(_units) # we keep a track of the preferred units for the column at the time of the column creation
169
170 _store = gtk.TreeStore(*(self.columns))
171
172 _iter = None
173 _i = 0
174 _active = False
175 for _r in self.rows:
176 _r.append(OBSERVER_NULL)
177 if _i == _rownum:
178 _active = True
179 _iter = _store.append(None, self.make_row(_active,_r) )
180 _i = _i + 1
181
182 self.activeiter = _iter
183
184 # add newest data point in bottom-right
185 _datacol = _colnum - OBSERVER_INITIAL_COLS
186 _dataval = inst.getRealValue() / self.units[_datacol].getConversion() # convert value to units specified when col created
187 self.rows[_rownum][_datacol] = _dataval
188 _store.set_value(self.activeiter, _colnum, _dataval)
189
190 # re-assign store to TreeView
191 self.view.set_model(_store)
192
193 _renderer = gtk.CellRendererText()
194 _renderer.connect('edited',self.on_view_cell_edited, _datacol)
195 _col = gtk.TreeViewColumn(_title, _renderer)
196 _col.add_attribute(_renderer, 'text', _colnum)
197 _col.add_attribute(_renderer, 'weight', OBSERVER_WEIGHT)
198 _col.add_attribute(_renderer, 'editable', OBSERVER_EDIT)
199 _col.set_alignment(0.0)
200 _col.set_reorderable(True)
201 _col.set_sort_column_id(_colnum)
202
203 self.view.append_column(_col);
204
205 self.browser.reporter.reportNote("Added variable '%s' to observer '%s'" % (_colname,self.name))
206

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