comparison src/browser/sat_browser/base_menu.py @ 509:35ccb3ff8245

browser_side: add method GenericMenuBar.addCategory + fixes MenuNode.addMenuItem when callback argument is a sub-menu
author souliane <souliane@mailoo.org>
date Thu, 21 Aug 2014 16:18:51 +0200
parents 1d41cc5b57b1
children db3436c85fb1
comparison
equal deleted inserted replaced
508:1d41cc5b57b1 509:35ccb3ff8245
81 self.item = item or None # associated menu item 81 self.item = item or None # associated menu item
82 self.menu = menu or None # associated menu bar (sub-menu) 82 self.menu = menu or None # associated menu bar (sub-menu)
83 self.flat_level = max(flat_level, -1) 83 self.flat_level = max(flat_level, -1)
84 self.children = [] 84 self.children = []
85 85
86 def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False): 86 def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False, sub_menu=None):
87 """Return the requested category. If create is True, path_i18n and 87 """Return the requested category. If create is True, path_i18n and
88 types are specified, recursively create the category and its parent. 88 types are specified, recursively create the category and its parent.
89 89
90 @param path (list[str]): path to the category 90 @param path (list[str]): path to the category
91 @param path_i18n (list[str]): internationalized path to the category 91 @param path_i18n (list[str]): internationalized path to the category
92 @param types (list[str]): types of the category and its parents 92 @param types (list[str]): types of the category and its parents
93 @param create (bool): if True, create the category 93 @param create (bool): if True, create the category
94 @param sub_menu (GenericMenuBar): instance to popup as the category
95 sub-menu, if it is created. Otherwise keep the previous sub-menu.
94 @return: MenuNode or None 96 @return: MenuNode or None
95 """ 97 """
96 assert(len(path) > 0 and len(path) == len(path_i18n) == len(types)) 98 assert(len(path) > 0 and len(path) == len(path_i18n) == len(types))
97 if isinstance(path, list) and len(path) > 1: 99 if len(path) > 1:
98 cat = self._getOrCreateCategory(path[:1], path_i18n[:1], types[:1], create) 100 cat = self._getOrCreateCategory(path[:1], path_i18n[:1], types[:1], create)
99 return cat._getOrCreateCategory(path[1:], path_i18n[1:], types[1:], create) if cat else None 101 return cat._getOrCreateCategory(path[1:], path_i18n[1:], types[1:], create, sub_menu) if cat else None
100 cats = [child for child in self.children if child.menu and child.name == path[0]] 102 cats = [child for child in self.children if child.menu and child.name == path[0]]
101 if len(cats) == 1: 103 if len(cats) == 1:
102 return cats[0] 104 return cats[0]
103 assert(cats == []) # there should not be more than 1 category with the same name 105 assert(cats == []) # there should not be more than 1 category with the same name
104 if create: 106 if create:
105 html = self.menu.getCategoryHTML(path_i18n[0], types[0]) 107 html = self.menu.getCategoryHTML(path_i18n[0], types[0])
106 sub_menu = GenericMenuBar(self.menu.host, vertical=True) 108 sub_menu = sub_menu if sub_menu else GenericMenuBar(self.menu.host, vertical=True)
107 return self.addItem(html, True, sub_menu, name=path[0]) 109 return self.addItem(html, True, sub_menu, name=path[0])
108 return None 110 return None
109 111
110 def getCategories(self, target_path=None): 112 def getCategories(self, target_path=None):
111 """Return all the categories of the current node, or those of the 113 """Return all the categories of the current node, or those of the
126 128
127 @param path (list[str], str): path to the item 129 @param path (list[str], str): path to the item
128 @param path_i18n (list[str], str): internationalized path to the item 130 @param path_i18n (list[str], str): internationalized path to the item
129 @param types (list[str], str): types of the item and its parents 131 @param types (list[str], str): types of the item and its parents
130 @param menu_cmd (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to 132 @param menu_cmd (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to
131 execute as a leaf's callback or to popup as a category's sub-menu. 133 execute as a leaf's callback or to popup as a category sub-menu.
132 """ 134 """
133 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback)) 135 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback))
134 136
135 leaf_node = hasattr(callback, "execute") 137 leaf_node = hasattr(callback, "execute")
136 category = hasattr(callback, "onShow") 138 category = isinstance(callback, GenericMenuBar)
137 assert(not leaf_node or not category) 139 assert(not leaf_node or not category)
138 140
139 path = [path] if isinstance(path, str) else path 141 path = [path] if isinstance(path, str) else path
140 path_i18n = [path_i18n] if isinstance(path_i18n, str) else path_i18n 142 path_i18n = [path_i18n] if isinstance(path_i18n, str) else path_i18n
141 types = [types for dummy in range(len(path_i18n))] if isinstance(types, str) else types 143 types = [types for dummy in range(len(path_i18n))] if isinstance(types, str) else types
142 144
143 if category: 145 if category:
144 cat = self._getOrCreateCategory(path, path_i18n, types, True) 146 return self._getOrCreateCategory(path, path_i18n, types, True, callback)
145 cat.item.setSubMenu(callback)
146 return cat
147 147
148 if len(path) == len(path_i18n) - 1: 148 if len(path) == len(path_i18n) - 1:
149 path.append(None) # dummy name for a leaf node 149 path.append(None) # dummy name for a leaf node
150 150
151 parent = self._getOrCreateCategory(path[:-1], path_i18n[:-1], types[:-1], True) 151 parent = self._getOrCreateCategory(path[:-1], path_i18n[:-1], types[:-1], True)
152 return parent.addItem(path_i18n[-1], callback) 152 return parent.addItem(path_i18n[-1], callback)
153
154 def addCategory(self, path, path_i18n, types, menu_bar=None):
155 """Recursively add a new category.
156
157 @param path (list[str], str): path to the category
158 @param path_i18n (list[str], str): internationalized path to the category
159 @param types (list[str], str): types of the category and its parents
160 @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu.
161 """
162 if menu_bar:
163 assert(isinstance(menu_bar, GenericMenuBar))
164 else:
165 menu_bar = GenericMenuBar(self.menu.host, vertical=True)
166 return self.addMenuItem(path, path_i18n, types, menu_bar)
153 167
154 def addItem(self, item, asHTML=None, popup=None, name=None): 168 def addItem(self, item, asHTML=None, popup=None, name=None):
155 """Add a single child to the current node. 169 """Add a single child to the current node.
156 170
157 @param item: see MenuBar.addItem 171 @param item: see MenuBar.addItem
274 @return: list[CategoryItem] 288 @return: list[CategoryItem]
275 """ 289 """
276 return [cat.item for cat in self.node.getCategories(parent_path)] 290 return [cat.item for cat in self.node.getCategories(parent_path)]
277 291
278 def addMenuItem(self, path, path_i18n, types, menu_cmd): 292 def addMenuItem(self, path, path_i18n, types, menu_cmd):
279 self.node.addMenuItem(path, path_i18n, types, menu_cmd) 293 return self.node.addMenuItem(path, path_i18n, types, menu_cmd).item
294
295 def addCategory(self, path, path_i18n, types, menu_bar):
296 return self.node.addCategory(path, path_i18n, types, menu_bar).item
280 297
281 def addItem(self, item, asHTML=None, popup=None): 298 def addItem(self, item, asHTML=None, popup=None):
282 return self.node.addItem(item, asHTML, popup).item 299 return self.node.addItem(item, asHTML, popup).item
283 300
284 def addCachedMenus(self, type_, menu_data=None): 301 def addCachedMenus(self, type_, menu_data=None):