1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22  """Definition of a common menu for every object.""" 
 23  import gtk 
 24   
 26      """A dictionary that keeps in mind the order 
 27      the items have been added""" 
 28   
 30          self.items = [] 
 31          self.keys = [] 
  32   
 36   
 38          return self.items[self.keys.index(key)] 
  39   
 41          """Return the values""" 
 42          return self.items 
   43   
 44   
 46      """A common menu. A menu is supposed to be shared 
 47      between different instances of the same class(or type). 
 48       
 49      All the action possible on a class is described by L{add_items}. 
 50      However all  
 51      the possible results available for a class may not be offered 
 52      by every instance, so the menu will be adapted according 
 53      to the instance status.  
 54      """ 
 55   
 57          gtk.Menu.__init__(self) 
 58           
 59          self.items = OrderDictionary() 
 60          self.properties = { 
 61              "vtk" : [], 
 62              "gtk" : [], 
 63              "pylab" : [] 
 64          } 
 65          if description: 
 66              for key in description.keys(): 
 67                  self.add_items(key, description[key]) 
 68          if build: 
 69              self.build() 
  70   
 72          """Add items to describe the menu.  
 73           
 74          @type key: a string 
 75          @param key: the type of menu action, 'vtk', 'gtk' or 'pylab'. 
 76           
 77          @type description: a tuple 
 78          @param description: the first argument is a key used to retrieved 
 79                  the action to apply on the object. The second argument 
 80                  is a menu widget that will be connected in L{connect_with_key}. 
 81          """ 
 82          for prop, display in description: 
 83               
 84              if isinstance(display, gtk.Widget): 
 85                  self.items[prop] = display 
 86              else: 
 87                  self.items[prop] = gtk.CheckMenuItem(display) 
 88              self.properties[key].append(prop) 
  89   
 91          """Execute L{add_items} with 'vtk' as a key.""" 
 92          self.add_items("vtk", description) 
  93       
 95          """Build the menu""" 
 96          for item in self.items.values(): 
 97              self.append(item) 
  98   
100          """Hide all the items in the menu""" 
101          for item in self.items.values(): 
102              item.hide() 
 103   
105          """Adapt the items visibility and activation  
106          according to the object properties (results available) 
107          and status.""" 
108          self._hide_all() 
109          for feature_key in obj.feature_keys: 
110              gtk_item = self.items[feature_key] 
111              gtk_item.show() 
112              if isinstance(gtk_item, gtk.CheckMenuItem): 
113                  if obj.boolean[feature_key]: 
114                      gtk_item.set_active(True) 
115                  else: 
116                      gtk_item.set_active(False) 
 117   
119          """Connect every description key set in L{add_items} 
120          to a callaback""" 
121          self.items[key].connect("activate", callback, key) 
 122   
124          """Popup the gtk menu with the event""" 
125          self.popup(None, None, None, event.button, event.time) 
  126   
127   
128  MenuBase = BaseMenu 
129