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

Diff of /trunk/pygtk/observer.py

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

revision 851 by johnpye, Mon Sep 18 15:10:48 2006 UTC revision 852 by johnpye, Tue Sep 19 14:57:42 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_EDIT_COLOR = "#008800"
8    OBSERVER_NOEDIT_COLOR = "#000088"
9    OBSERVER_NORMAL_COLOR = "black"
10    
11  OBSERVER_INITIAL_COLS = 3 # how many cells are at the start of the table?  OBSERVER_INITIAL_COLS = 3 # how many cells are at the start of the table?
12  OBSERVER_ICON, OBSERVER_WEIGHT, OBSERVER_EDIT = range(0,OBSERVER_INITIAL_COLS) # column indices for the start of the TreeStore  OBSERVER_ICON, OBSERVER_WEIGHT, OBSERVER_EDIT = range(0,OBSERVER_INITIAL_COLS) # column indices for the start of the TreeStore
13  OBSERVER_NULL = 0 # value that gets added to empty cells in a new column  OBSERVER_NULL = 0 # value that gets added to empty cells in a new column
# Line 57  class ObserverColumn: Line 61  class ObserverColumn:
61          return "ObserverColumn(name="+self.name+")"          return "ObserverColumn(name="+self.name+")"
62    
63      def cellvalue(self, column, cell, model, iter):      def cellvalue(self, column, cell, model, iter):
64          print "RENDERING COLUMN",self.index          #print "RENDERING COLUMN",self.index
65          _rowobject = model.get_value(iter,0)          _rowobject = model.get_value(iter,0)
66    
67            cell.set_property('editable',False)
68            cell.set_property('weight',400)
69          try:          try:
70              if _rowobject.active:              if _rowobject.active:
71                  _rawval = self.instance.getRealValue()                  _rawval = self.instance.getRealValue()
72                    if self.instance.getType().isRefinedSolverVar():
73                        if self.instance.isFixed():
74                            cell.set_property('editable',True)
75                            cell.set_property('weight',700)
76                            cell.set_property('foreground',OBSERVER_EDIT_COLOR)
77                        else:
78                            cell.set_property('foreground',OBSERVER_NOEDIT_COLOR)
79              else:              else:
80                   _rawval = _rowobject.values[self.index]                  cell.set_property('foreground',OBSERVER_NORMAL_COLOR)
81                    _rawval = _rowobject.values[self.index]
82              _dataval = _rawval / self.units.getConversion()              _dataval = _rawval / self.units.getConversion()
83          except KeyError:          except IndexError:
84              _dataval = "N/A"              _dataval = "N/A"
85    
86          cell.set_property('text', _dataval)          cell.set_property('text', _dataval)
# Line 77  class ObserverRow: Line 91  class ObserverRow:
91          should correspond to those in the Observer object's vector of          should correspond to those in the Observer object's vector of
92          ObserverColumn objects.          ObserverColumn objects.
93      """      """
94      def __init__(self,values=None,active=False):      def __init__(self,values=None,active=True):
95          if values==None:              if values==None:    
96              values={}              values={}
97    
# Line 88  class ObserverRow: Line 102  class ObserverRow:
102          self.active = False          self.active = False
103          print "TABLE COLS:",table.cols          print "TABLE COLS:",table.cols
104          print "ROW VALUES:",self.values          print "ROW VALUES:",self.values
105            r=0;
106          for index,col in table.cols.iteritems():          for index,col in table.cols.iteritems():
107              print "INDEX: ",index,"; COL: ",col              print "ROW",r,"; INDEX: ",index,"; COL: ",col
108              self.values[index] = col.instance.getRealValue()              try:
109                    self.values[index] = col.instance.getRealValue()
110                except KeyError,e:
111                    print "Key error: e=",str(e)
112                    self.values[index] = None
113                r=r+1
114          print "Made static, values:",self.values          print "Made static, values:",self.values
115    
116        def get_values(self,table):
117            if not self.active:
118                return self.values.values()
119            else:
120                _v = []
121                for index,col in table.cols.iteritems():
122                    _v.append( col.instance.getRealValue() / col.units.getConversion() )
123                return _v
124    
125  class ObserverTab:  class ObserverTab:
126    
127      def __init__(self,xml,browser,tab,name=None,alive=True):      def __init__(self,xml,browser,tab,name=None,alive=True):
# Line 115  class ObserverTab: Line 144  class ObserverTab:
144          self.activeimg.set_from_file("glade/active.png")          self.activeimg.set_from_file("glade/active.png")
145          # create PixBuf objects from these?          # create PixBuf objects from these?
146          self.rows = []          self.rows = []
147          _store = gtk.TreeStore(object,int)          _store = gtk.TreeStore(object)
148          self.cols = {}          self.cols = {}
149    
150          # create the 'active' pixbuf column          # create the 'active' pixbuf column
# Line 131  class ObserverTab: Line 160  class ObserverTab:
160          if self.alive:          if self.alive:
161              # for a 'live' Observer, create the 'active' bottom row              # for a 'live' Observer, create the 'active' bottom row
162              self.browser.reporter.reportNote("Adding empty row to store")              self.browser.reporter.reportNote("Adding empty row to store")
163              self.activeiter = _store.append(None, [ObserverRow(active=True),0] )              _row = ObserverRow()
164                self.activeiter = _store.append(None, [_row] )
165                self.rows.append(_row)
166    
167          self.view.set_model(_store)          self.view.set_model(_store)
168          self.browser.reporter.reportNote("Created observer '%s'" % self.name)          self.browser.reporter.reportNote("Created observer '%s'" % self.name)
# Line 147  class ObserverTab: Line 178  class ObserverTab:
178          self.do_add_row()          self.do_add_row()
179    
180      def on_clear_clicked(self,*args):      def on_clear_clicked(self,*args):
181          self.browser.reporter.reportError("CLEAR not implemented")          _store = self.view.get_model()
182            _store.clear();
183            self.rows = {}
184            self.activeiter = _store.append(None, [ObserverRow()] )
185    
186      def do_add_row(self):      def do_add_row(self):
187          if self.alive:          if self.alive:
188              _rowobject = ObserverRow(active=True)              _row = ObserverRow()
189                self.rows.append(_row)
190              _store = self.view.get_model()              _store = self.view.get_model()
191              _oldrow = _store.get_value(self.activeiter,0)              _oldrow = _store.get_value(self.activeiter,0)
192              _oldrow.make_static(self)              _oldrow.make_static(self)
193              self.activeiter = _store.append(None,[ObserverRow([],True),0])              self.activeiter = _store.append(None,[_row])
194                _path = _store.get_path(self.activeiter)
195                _oldpath,_oldcol = self.view.get_cursor()
196                self.view.set_cursor(_path, _oldcol)
197          else:          else:
198              self.browser.reporter.reportError("Can't add row: incorrect observer type")              self.browser.reporter.reportError("Can't add row: incorrect observer type")
199    
200      def on_view_cell_edited(self, renderer, path, newtext, datacolumn):      def on_view_cell_edited(self, renderer, path, newtext, col):
201          self.browser.reporter.reportError("EDIT not implemented")          # we can assume it's always the self.activeiter that is edited...
202            if col.instance.isFixed():
203                val = float(newtext) * col.units.getConversion()
204                col.instance.setRealValue( val )
205                self.browser.reporter.reportNote("Updated value to %f" % float(newtext))
206            else:
207                self.browser.reporter.reportError("Can't set a FREE variable from the Observer")
208                return
209            self.browser.do_solve_if_auto()
210    
211      def sync(self):      def sync(self):
212          _store = self.view.get_model()          self.view.queue_draw()
         _activerow = _store.get_value(self.activeiter,0)  
         _store.set(self.activeiter,1,0  )  
213          self.browser.reporter.reportNote("SYNC performed")          self.browser.reporter.reportNote("SYNC performed")
214    
215      def add_instance(self,instance):      def add_instance(self,instance):
# Line 175  class ObserverTab: Line 219  class ObserverTab:
219    
220          # create a new column          # create a new column
221          _renderer = gtk.CellRendererText()          _renderer = gtk.CellRendererText()
222            _renderer.connect('edited',self.on_view_cell_edited, _col)
223          _tvcol = gtk.TreeViewColumn()          _tvcol = gtk.TreeViewColumn()
224          _tvcol.set_title(_col.title)          _tvcol.set_title(_col.title)
225          _tvcol.pack_start(_renderer,False)          _tvcol.pack_start(_renderer,False)
226          _tvcol.set_cell_data_func(_renderer, _col.cellvalue)          _tvcol.set_cell_data_func(_renderer, _col.cellvalue)
227          self.view.append_column(_tvcol);          self.view.append_column(_tvcol);
           
228          self.browser.reporter.reportError("cols = "+str(self.cols))          self.browser.reporter.reportError("cols = "+str(self.cols))
229    
230        def copy_to_clipboard(self,clip):
231            _s = []
232            _s.append('\t'.join([_v.title for _k,_v in self.cols.iteritems()]))
233            print "COPYING %d ROWS" % len(self.rows)
234            for _r in self.rows:
235                _s.append("\t".join([`_v` for _v in _r.get_values(self)]))
236    
237            clip.set_text('\n'.join(_s),-1)
238    
239            self.browser.reporter.reportNote("Observer '%s' data copied to clipboard" % self.name)
240    
241  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
242  # OLD STUFF  # OLD STUFF
243    

Legend:
Removed from v.851  
changed lines
  Added in v.852

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