Package mbdyn :: Package interface :: Module common
[hide private]

Source Code for Module mbdyn.interface.common

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # This file is part of MBDyn sim suite. 
  5  # Copyright (C) 2007 André ESPAZE, as part of a Master thesis supervised by 
  6  # Martin O.L.Hansen (DTU) and Nicolas Chauvat (Logilab) 
  7   
  8  # MBDyn sim suite is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 21  # 
 22  """Definition of a common menu for every object.""" 
 23  import gtk 
 24   
25 -class OrderDictionary:
26 """A dictionary that keeps in mind the order 27 the items have been added""" 28
29 - def __init__(self):
30 self.items = [] 31 self.keys = []
32
33 - def __setitem__(self, key, item):
34 self.items.append(item) 35 self.keys.append(key)
36
37 - def __getitem__(self, key):
38 return self.items[self.keys.index(key)]
39
40 - def values(self):
41 """Return the values""" 42 return self.items
43 44
45 -class BaseMenu(gtk.Menu):
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
56 - def __init__(self, description=None, build=False):
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
71 - def add_items(self, key, description):
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 # CheckMenuItem are set by default 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
90 - def add_vtk_items(self, description):
91 """Execute L{add_items} with 'vtk' as a key.""" 92 self.add_items("vtk", description)
93
94 - def build(self):
95 """Build the menu""" 96 for item in self.items.values(): 97 self.append(item)
98
99 - def _hide_all(self):
100 """Hide all the items in the menu""" 101 for item in self.items.values(): 102 item.hide()
103
104 - def set_for(self, obj):
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
118 - def connect_with_key(self, key, callback):
119 """Connect every description key set in L{add_items} 120 to a callaback""" 121 self.items[key].connect("activate", callback, key)
122
123 - def custom_popup(self, event):
124 """Popup the gtk menu with the event""" 125 self.popup(None, None, None, event.button, event.time)
126 127 # A stupid mistake to correct, it should be 'MenuBase' as class name 128 MenuBase = BaseMenu 129