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

Contents of /trunk/pygtk/observer.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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