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

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

Parent Directory Parent Directory | Revision Log Revision Log


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