annotate src/browser/sat_browser/base_menu.py @ 498:60be99de3808

browser_side: menus refactorization + handle levels > 2
author souliane <souliane@mailoo.org>
date Fri, 25 Jul 2014 02:38:30 +0200
parents 5d8632a7bfde
children 4aa627b059df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
1 #!/usr/bin/python
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
3
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # Libervia: a Salut à Toi frontend
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
6
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
11
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
16
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
19
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
20
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
21 """Base classes for building a menu.
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
22
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
23 These classes have been moved here from menu.py because they are also used
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
24 by base_widget.py, and the import sequence caused a JS runtime error."""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
25
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
26
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
27 import pyjd # this is dummy in pyjs
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from sat.core.log import getLogger
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
29 log = getLogger(__name__)
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
30
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
31 from pyjamas.ui.MenuBar import MenuBar
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
32 from pyjamas.ui.MenuItem import MenuItem
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
33 from pyjamas import Window
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
34
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
35
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
36 class MenuCmd:
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
37 """Return an object with an "execute" method that can be set to a menu item callback"""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
38
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
39 def __init__(self, object_, handler):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
40 self._object = object_
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
41 self._handler = handler
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
42
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
43 def execute(self):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
44 handler = getattr(self._object, self._handler)
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
45 handler()
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
46
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
47
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
48 class PluginMenuCmd:
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
49 """Like MenuCmd, but instead of executing a method, it will command the bridge to launch an action"""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
50
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
51 def __init__(self, host, action_id, menu_data=None):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
52 self.host = host
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
53 self.action_id = action_id
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
54 self.menu_data = menu_data
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
55
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
56 def execute(self):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
57 self.host.launchAction(self.action_id, self.menu_data)
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
58
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
59
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
60 class CategoryItem(MenuItem):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
61 """A category item with a non-internationalized name"""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
62 def __init__(self, name, *args, **kwargs):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
63 MenuItem.__init__(self, *args, **kwargs)
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
64 self.name = name
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
65
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
66
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
67 class GenericMenuBar(MenuBar):
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
68 """A menu bar with sub-categories and items"""
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
69
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
70 def __init__(self, host, vertical=False, styles=None, **kwargs):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
71 """
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
72 @param host (SatWebFrontend): host instance
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
73 @param vertical (bool): True to display the popup menu vertically
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
74 @param styles (dict): specific styles to be applied:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
75 - key: a value in ('moved_popup', 'menu_bar')
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
76 - value: a CSS class
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
77 the popup that are not displayed at the position computed by pyjamas.
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
78 """
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
79 MenuBar.__init__(self, vertical, **kwargs)
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
80 self.host = host
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
81 self.styles = styles or {}
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
82 if 'menu_bar' in self.styles:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
83 # XXX: pyjamas set the style to object string representation!
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
84 # FIXME: fix the bug upstream
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
85 first = 'gwt-MenuBar'
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
86 second = first + '-' + ('vertical' if self.vertical else 'horizontal')
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
87 self.setStyleName(' '.join([first, second, self.styles['menu_bar']]))
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
88
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
89 @classmethod
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
90 def getCategoryHTML(cls, type_, menu_name_i18n):
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
91 """Build the html to be used for displaying a category item.
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
92
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
93 Inheriting classes may overwrite this method.
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
94 @param type_ (str): category type
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
95 @param menu_name_i18n (str): internationalized category name
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
96 @return: str
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
97 """
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
98 return menu_name_i18n
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
99
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
100 def doItemAction(self, item, fireCommand):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
101 """Overwrites the default behavior for the popup menu to fit in the screen"""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
102 MenuBar.doItemAction(self, item, fireCommand)
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
103 if not self.popup:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
104 return
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
105 if self.vertical:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
106 # move the popup if it would go over the screen's viewport
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
107 max_left = Window.getClientWidth() - self.getOffsetWidth() + 1 - self.popup.getOffsetWidth()
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
108 new_left = self.getAbsoluteLeft() - self.popup.getOffsetWidth() + 1
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
109 top = item.getAbsoluteTop()
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
110 else:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
111 # move the popup if it would go over the menu bar right extremity
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
112 max_left = self.getAbsoluteLeft() + self.getOffsetWidth() - self.popup.getOffsetWidth()
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
113 new_left = max_left
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
114 top = self.getAbsoluteTop() + self.getOffsetHeight() - 1
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
115 if item.getAbsoluteLeft() > max_left:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
116 self.popup.setPopupPosition(new_left, top)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
117 # eventually smooth the popup edges to fit the menu own style
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
118 if 'moved_popup' in self.styles:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
119 self.popup.addStyleName(self.styles['moved_popup'])
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
120
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
121 def getCategories(self):
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
122 """Return all the categories items.
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
123
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
124 @return: list[CategoryItem]
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
125 """
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
126 return [item for item in self.items if isinstance(item, CategoryItem)]
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
127
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
128 def getCategoryItem(self, path):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
129 """Return the requested category item
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
130
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
131 @param path (list[str]): path to the category
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
132 @return: CategoryInstance or None
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
133 """
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
134 assert(len(path) > 0)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
135 if len(path) > 1:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
136 menu = self.getCategoryMenu(path[:1])
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
137 return menu.getCategoryItem(path[1:]) if menu else None
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
138 items = [item for item in self.items if isinstance(item, CategoryItem) and item.name == path[0]]
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
139 if len(items) == 1:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
140 return items[0]
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
141 assert(items == []) # there should not be more than 1 category with the same name
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
142 return None
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
143
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
144 def getCategoryMenu(self, path):
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
145 """Return the popup menu for the given category
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
146
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
147 @param path (list[str]): path to the category
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
148 @return: CategoryMenuBar instance or None
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
149 """
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
150 item = self.getCategoryItem(path)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
151 return item.getSubMenu() if item else None
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
152
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
153 def addSeparator(self):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
154 """Add a separator between the categories"""
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
155 self.addItem(CategoryItem(None, text='', asHTML=None, StyleName='menuSeparator'))
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
156
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
157 def addCategory(self, path, path_i18n, type_, sub_menu=None):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
158 """Add a category item and its associated sub-menu.
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
159
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
160 If the category already exists, do not overwrite the current sub-menu.
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
161 @param path (list[str], str): path to the category. Passing a string for
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
162 the category name is also accepted if there's no sub-category.
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
163 @param path_i18n (list[str], str): internationalized path to the category.
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
164 Passing a string for the internationalized category name is also accepted
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
165 if there's no sub-category.
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
166 @param type_ (str): category type
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
167 @param sub_menu (CategoryMenuBar): category sub-menu
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
168 """
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
169 if isinstance(path, str):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
170 path = [path]
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
171 if isinstance(path_i18n, str):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
172 path_i18n = [path_i18n]
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
173 assert(len(path) > 0 and len(path) == len(path_i18n))
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
174 current = self
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
175 count = len(path)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
176 for menu_name, menu_name_i18n in zip(path, path_i18n):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
177 tmp = current.getCategoryMenu([menu_name])
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
178 if not tmp:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
179 html = self.getCategoryHTML(type_, menu_name_i18n)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
180 tmp = CategoryMenuBar(self.host) if (count > 1 or not sub_menu) else sub_menu
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
181 current.addItem(CategoryItem(menu_name, text=html, asHTML=True, subMenu=tmp))
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
182 current = tmp
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
183 count -= 1
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
184
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
185 def addMenuItem(self, path, path_i18n, type_, menu_cmd):
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
186 """Add a new menu item
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
187 @param path (list[str], str): path to the category, completed by a dummy
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
188 value for the item in last position. Passing a string for the category
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
189 name is also accepted if there's no sub-category.
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
190 @param path_i18n (list[str]): internationalized path to the item
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
191 @param type_ (str): category type in ('games', 'help', 'home', 'photos', 'plugins', 'settings', 'social')
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
192 @param menu_cmd (MenuCmd or PluginMenuCmd): instance to execute as the item callback
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
193 """
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
194 if isinstance(path, str):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
195 assert(len(path_i18n) == 2)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
196 path = [path, None]
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
197 assert(len(path) > 1 and len(path) == len(path_i18n))
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
198 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, type_, menu_cmd))
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
199 sub_menu = self.getCategoryMenu(path[:-1])
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
200 if not sub_menu:
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
201 sub_menu = CategoryMenuBar(self.host)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
202 self.addCategory(path[:-1], path_i18n[:-1], type_, sub_menu)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
203 if menu_cmd:
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
204 sub_menu.addItem(path_i18n[-1], menu_cmd)
494
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
205
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
206 def addCachedMenus(self, type_, menu_data=None):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
207 """Add cached menus to instance
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
208 @param type_: menu type like is sat.core.sat_main.importMenu
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
209 @param menu_data: data to send with these menus
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
210 """
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
211 menus = self.host.menus.get(type_, [])
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
212 for action_id, path, path_i18n in menus:
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
213 if len(path) != len(path_i18n):
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
214 log.error("inconsistency between menu paths")
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
215 continue
5d8632a7bfde browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff changeset
216 callback = PluginMenuCmd(self.host, action_id, menu_data)
498
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
217 self.addMenuItem(path, path_i18n, 'plugins', callback)
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
218
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
219
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
220 class CategoryMenuBar(GenericMenuBar):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
221 """A menu bar for a category (sub-menu)"""
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
222 def __init__(self, host):
60be99de3808 browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents: 494
diff changeset
223 GenericMenuBar.__init__(self, host, vertical=True)