/[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 848 by johnpye, Mon Apr 24 02:23:08 2006 UTC revision 849 by johnpye, Mon Sep 18 15:10:48 2006 UTC
# Line 16  OBSERVER_NULL = 0 # value that gets adde Line 16  OBSERVER_NULL = 0 # value that gets adde
16  # Update: there is a technique for doing this, in fact:  # Update: there is a technique for doing this, in fact:
17  # http://www.daa.com.au/pipermail/pygtk/2006-February/011777.html  # http://www.daa.com.au/pipermail/pygtk/2006-February/011777.html
18    
19    OBSERVER_NUM=0
20    
21    class ObserverColumn:
22        """
23            A class to identify the instance that relates to a specify column
24            and the units of measurement and column title, etc.
25        """
26        def __init__(self,instance,index,name=None,units=None,browser=None):
27            self.instance = instance
28            self.name = name
29            self.index = index
30    
31            if name==None:
32                if browser == None:
33                    name = "UNNAMED"
34                else:
35                    name = browser.sim.getInstanceName(instance)
36    
37            if units == None:
38                units = instance.getType().getPreferredUnits()
39            if units == None:
40                units = instance.getType().getDimensions().getDefaultUnits()
41            
42            uname = str(units.getName())
43            if uname.find("/")!=-1:
44                uname = "["+uname+"]"
45    
46            if uname == "":
47                _title = "%s" % (name)
48            else:
49                _title = "%s / %s" % (name, uname)
50    
51            self.title = _title
52            self.units = units
53            self.uname = uname
54            self.name = name
55        
56        def __repr__(self):
57            return "ObserverColumn(name="+self.name+")"
58    
59        def cellvalue(self, column, cell, model, iter):
60            print "RENDERING COLUMN",self.index
61            _rowobject = model.get_value(iter,0)
62    
63            try:
64                if _rowobject.active:
65                    _rawval = self.instance.getRealValue()
66                else:
67                     _rawval = _rowobject.values[self.index]
68                _dataval = _rawval / self.units.getConversion()
69            except KeyError:
70                _dataval = "N/A"
71    
72            cell.set_property('text', _dataval)
73    
74    class ObserverRow:
75        """
76            Just a container for a vector of values, but with columns that
77            should correspond to those in the Observer object's vector of
78            ObserverColumn objects.
79        """
80        def __init__(self,values=None,active=False):
81            if values==None:    
82                values={}
83    
84            self.values = values
85            self.active = active
86    
87        def make_static(self,table):
88            self.active = False
89            print "TABLE COLS:",table.cols
90            print "ROW VALUES:",self.values
91            for index,col in table.cols.iteritems():
92                print "INDEX: ",index,"; COL: ",col
93                self.values[index] = col.instance.getRealValue()
94            print "Made static, values:",self.values
95    
96  class ObserverTab:  class ObserverTab:
97    
98        def __init__(self,xml,browser,tab,name=None,alive=True):
99            global OBSERVER_NUM
100            self.colindex = 0
101            if name==None:
102                OBSERVER_NUM=OBSERVER_NUM+1
103                name = "Observer %d" % OBSERVER_NUM
104            self.name = name
105            self.browser=browser
106            xml.signal_autoconnect(self)
107            self.view = xml.get_widget('observerview')
108            self.tab = tab
109            self.alive=alive
110            if self.alive:
111                self.browser.reporter.reportNote("New observer is 'alive'")
112    
113            self.keptimg =  gtk.Image()
114            self.activeimg = gtk.Image()
115            self.activeimg.set_from_file("glade/active.png")
116            # create PixBuf objects from these?
117            self.rows = []
118            _store = gtk.TreeStore(object,int)
119            self.cols = {}
120    
121            # create the 'active' pixbuf column
122            _renderer = gtk.CellRendererPixbuf()
123            _col = gtk.TreeViewColumn()
124            _col.set_title("")
125            _col.pack_start(_renderer,False)
126            _col.set_cell_data_func(_renderer, self.activepixbufvalue)
127            self.view.append_column(_col);
128            
129            # initially there will not be any other columns
130    
131            if self.alive:
132                # for a 'live' Observer, create the 'active' bottom row
133                self.browser.reporter.reportNote("Adding empty row to store")
134                self.activeiter = _store.append(None, [ObserverRow(active=True),0] )
135    
136            self.view.set_model(_store)
137            self.browser.reporter.reportNote("Created observer '%s'" % self.name)
138    
139        def activepixbufvalue(self,column,cell,model,iter):
140            _rowobject = model.get_value(iter,0)
141            if _rowobject.active:
142                cell.set_property('pixbuf',self.activeimg.get_pixbuf())
143            else:
144                cell.set_property('pixbuf',self.keptimg.get_pixbuf())
145    
146        def on_add_clicked(self,*args):
147            self.do_add_row()
148    
149        def on_clear_clicked(self,*args):
150            self.browser.reporter.reportError("CLEAR not implemented")
151    
152        def do_add_row(self):
153            if self.alive:
154                _rowobject = ObserverRow(active=True)
155                _store = self.view.get_model()
156                _oldrow = _store.get_value(self.activeiter,0)
157                _oldrow.make_static(self)
158                self.activeiter = _store.append(None,[ObserverRow([],True),0])
159            else:
160                self.browser.reporter.reportError("Can't add row: incorrect observer type")
161    
162        def on_view_cell_edited(self, renderer, path, newtext, datacolumn):
163            self.browser.reporter.reportError("EDIT not implemented")
164    
165        def sync(self):
166            _store = self.view.get_model()
167            _activerow = _store.get_value(self.activeiter,0)
168            _store.set(self.activeiter,1,0  )
169            self.browser.reporter.reportNote("SYNC performed")
170    
171        def add_instance(self,instance):
172            _col = ObserverColumn(instance,self.colindex,browser=self.browser)
173            self.cols[self.colindex] = _col
174            self.colindex = self.colindex + 1
175    
176            # create a new column
177            _renderer = gtk.CellRendererText()
178            _tvcol = gtk.TreeViewColumn()
179            _tvcol.set_title(_col.title)
180            _tvcol.pack_start(_renderer,False)
181            _tvcol.set_cell_data_func(_renderer, _col.cellvalue)
182            self.view.append_column(_tvcol);
183            
184            self.browser.reporter.reportError("cols = "+str(self.cols))
185    
186    #-------------------------------------------------------------------------------
187    # OLD STUFF
188    
189    class ObserverTab1:
190        """
191            An 'Observer' tab in the Browser interface. Multiple tabs should be
192            possible.
193        """
194      def __init__(self,xml,name,browser,tab):      def __init__(self,xml,name,browser,tab):
195          xml.signal_autoconnect(self);          xml.signal_autoconnect(self);
196    
197          self.view = xml.get_widget('observerview')          self.view = xml.get_widget('observerview')
198          self.tab = tab          self.tab = tab
199    
         #self.activeimg = gtk.Image()  
         #self.activeimg.set_from_file("glade/active.png")  
200          self.activeimg = None          self.activeimg = None
201          self.keptimg = None          self.keptimg = None
202                    
# Line 44  class ObserverTab: Line 216  class ObserverTab:
216          self.rows.append([])          self.rows.append([])
217    
218          # work towards having multiple observers for multiple simulations          # work towards having multiple observers for multiple simulations
219          self.name = name          self.name = nameself.view.set_model(_store)
220          self.browser = browser          self.browser = browser
221    
222          # create the 'active' pixvuf columns              # create the 'active' pixbuf columns    
223          _renderer = gtk.CellRendererPixbuf()          _renderer = gtk.CellRendererPixbuf()
224          _col = gtk.TreeViewColumn()          _col = gtk.TreeViewColumn()
225          _col.set_title("")          _col.set_title("")
# Line 140  class ObserverTab: Line 312  class ObserverTab:
312          return _r                return _r      
313    
314      def add_instance(self, inst):      def add_instance(self, inst):
315            # TODO big changes here....
316          if not inst.getType().isRefinedSolverVar():          if not inst.getType().isRefinedSolverVar():
317              self.browser.reporter.reportError("Instance is not a refined solver variable: can't 'observe'.");              self.browser.reporter.reportError("Instance is not a refined solver variable: can't 'observe'.");
318              return              return
# Line 206  class ObserverTab: Line 379  class ObserverTab:
379    
380          self.browser.reporter.reportNote("Added variable '%s' to observer '%s'" % (_colname,self.name))          self.browser.reporter.reportNote("Added variable '%s' to observer '%s'" % (_colname,self.name))
381    
382    

Legend:
Removed from v.848  
changed lines
  Added in v.849

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