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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 251 by johnpye, Wed Feb 1 04:33:46 2006 UTC revision 252 by johnpye, Wed Feb 1 06:29:46 2006 UTC
# Line 4  import gtk Line 4  import gtk
4  import gtk.glade  import gtk.glade
5  import pango  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:  class ObserverTab:
17      def __init__(self,xml,name,browser):      def __init__(self,xml,name,browser,tab):
18          xml.signal_autoconnect(self);          xml.signal_autoconnect(self);
19    
20          self.view = xml.get_widget('observerview')          self.view = xml.get_widget('observerview')
21            self.tab = tab
22    
23          #self.activeimg = gtk.Image()          #self.activeimg = gtk.Image()
24          #self.activeimg.set_from_file("icons/active.png")          #self.activeimg.set_from_file("icons/active.png")
# Line 18  class ObserverTab: Line 28  class ObserverTab:
28          # no instances yet in the observer:          # no instances yet in the observer:
29          self.columninstances = []          self.columninstances = []
30    
31          self.columns = [gtk.gdk.Pixbuf]          self.columns = [gtk.gdk.Pixbuf,int,bool]
32          self.titles = ["T"]  
33            # units for each data column
34            self.units = []
35            self.titles = []
36    
37          _store = gtk.TreeStore(*self.columns)          _store = gtk.TreeStore(*self.columns)
38          self.rows = []          self.rows = []
39    
# Line 33  class ObserverTab: Line 47  class ObserverTab:
47          # create the 'active' pixvuf columns              # create the 'active' pixvuf columns    
48          _renderer = gtk.CellRendererPixbuf()          _renderer = gtk.CellRendererPixbuf()
49          _col = gtk.TreeViewColumn()          _col = gtk.TreeViewColumn()
50          _col.set_title(self.titles[0])          _col.set_title("")
51          _col.pack_start(_renderer,False)          _col.pack_start(_renderer,False)
52          _col.add_attribute(_renderer, 'pixbuf', 0)          _col.add_attribute(_renderer, 'pixbuf', OBSERVER_ICON)
53          self.view.append_column(_col);          self.view.append_column(_col);
54    
55          # create the first row          # create the first row
# Line 44  class ObserverTab: Line 58  class ObserverTab:
58    
59          self.view.set_model(_store)          self.view.set_model(_store)
60    
61          self.browser.reporter.reportNote("Created OBSERVER")          self.browser.reporter.reportNote("Created observer '%s'" % self.name)
62    
63      def on_add_clicked(self,*args):      def on_add_clicked(self,*args):
64          self.do_add_row()          self.do_add_row()
# Line 56  class ObserverTab: Line 70  class ObserverTab:
70          self.rows.append(self.rows[_rownum-1])          self.rows.append(self.rows[_rownum-1])
71          self.activerow = _rownum          self.activerow = _rownum
72    
73          self.view.get_model().set(self.activeiter,0,self.keptimg)          _m = self.view.get_model()
74          self.activeiter = self.view.get_model().append(None,self.make_row(True,self.rows[_rownum]))          _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):      def on_clear_clicked(self,*args):
83          print "CLEAR"          self.rows = []
84          pass          _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):      def sync(self):
         print "SYNCHING ROW",self.activerow  
           
102          #new row data          #new row data
103          _r = [_i.getRealValue() for _i in self.columninstances]          _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          # stick the row data into the TreeStore
108          _m = self.view.get_model()          _m = self.view.get_model()
109          _i = 1          _i = 0
110          for _c in _r:          for _c in _r1:
111              _m.set(self.activeiter, _i, _c)              _m.set(self.activeiter, _i, _c)
112              _i = _i + 1              _i = _i + 1
         print "REPLACEMENT DATA IS",_r  
113    
114          # keep the data in self.rows as well          # keep the data in self.rows as well
115          self.rows[self.activerow] = _r;          self.rows[self.activerow] = _r;
116    
117      def copy_to_clipboard(self,clip):      def copy_to_clipboard(self,clip):
118          _t = ""          _s = []
119            _s.append('\t'.join(self.titles))
120          for _r in self.rows:          for _r in self.rows:
121              _t += '\t'.join([`_c` for _c in _r]) + '\n'              _s.append( '\t'.join([`_c` for _c in _r]) )
122          clip.set_text(_t,len(_t))  
123          print "TEXT ON CLIPBOARD IS",_t          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):      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:          if isactive:
131              _r = [self.activeimg]              _r = [self.activeimg, pango.WEIGHT_BOLD, True]
132          else:          else:
133              _r = [self.keptimg]              _r = [self.keptimg, pango.WEIGHT_NORMAL, False]
134            
135          for _c in row:          for _c in row:
136              _r.append(_c)              _r.append(_c)
137          print "MADE KEPT ROW:",_r  
138          return _r                return _r      
139    
140      def add_instance(self, inst):      def add_instance(self, inst):
# Line 104  class ObserverTab: Line 143  class ObserverTab:
143              return              return
144    
145          _colnum = len(self.columns)          _colnum = len(self.columns)
146          _colname = inst.getName().toString()          _colname = self.browser.sim.getInstanceName(inst)
         _value = float(inst.getRealValue())  
147          _rownum = len(self.rows)-1          _rownum = len(self.rows)-1
148            
149          # store the instances in self.columninstances for sync purposes          # store the instances in self.columninstances for sync purposes
150          self.columninstances.append(inst)          self.columninstances.append(inst)
151            
         print "ROWS IN self.rows",len(self.rows)  
         print "LAST ROW: ",repr(self.rows[_rownum])  
   
         print "COLNUM =",_colnum  
   
         print "ADDING TO OBSERVER '"+self.name+"' COLUMN '"+_colname+"', CURRENT VALUE = ",_value  
   
152          # create new TreeStore, copy of old, plus one columm          # create new TreeStore, copy of old, plus one columm
153          self.columns.append(float)          self.columns.append(float)
154          self.titles.append(_colname);          _units = inst.getType().getPreferredUnits()
155          print "NEW COLUMNS WILL BE",str(self.columns)          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))          _store = gtk.TreeStore(*(self.columns))
168    
169          _iter = None          _iter = None
170          _i = 0          _i = 0
171            _active = False
172          for _r in self.rows:          for _r in self.rows:
173              print "ROW",_i              print "FIXING ROW",_i
174              _r.append(0)              _r.append(OBSERVER_NULL)
175              _iter = _store.append(None, self.make_row(False,_r) )              if _i == _rownum:
176                    _active = True
177                _iter = _store.append(None, self.make_row(_active,_r) )
178              _i = _i + 1              _i = _i + 1
179            
180          self.activeiter = _iter          self.activeiter = _iter
181    
182          # add newest data point in bottom-right          # add newest data point in bottom-right
183          self.rows[_rownum][_colnum-1] = _value          _datacol = _colnum - OBSERVER_INITIAL_COLS
184          _store.set_value(self.activeiter, _colnum, _value)          _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          # re-assign store to TreeView
191          self.view.set_model(_store)     #self.store.set(_currentrow, _colnum, _value)          self.view.set_model(_store)
   
         # add value to current column  
         #self.store.set(_currentrow, _colnum, _value)  
192    
193          _renderer = gtk.CellRendererText()          _renderer = gtk.CellRendererText()
194          _col = gtk.TreeViewColumn(_colname, _renderer)          _col = gtk.TreeViewColumn(_title, _renderer)
195          _col.add_attribute(_renderer, 'text', _colnum)          _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)          _col.set_alignment(0.0)
200          #_col.set_reorderable(True)          #_col.set_reorderable(True)
201          #_col.set_sort_column_id(_colnum)          #_col.set_sort_column_id(_colnum)
202    
203          self.view.append_column(_col);          self.view.append_column(_col);
204    
205            self.browser.reporter.reportNote("Added variable '%s' to observer '%s'" % (_colname,self.name))
206    

Legend:
Removed from v.251  
changed lines
  Added in v.252

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