Mercurial > libervia-web
annotate browser/sat_browser/base_menu.py @ 1174:e0b3f1d78f8c
pages (g/e): use redirect instead of duplicating code of "event_view"
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 03 May 2019 20:52:13 +0200 |
parents | 2af117bfe6cc |
children |
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 |
1144 | 5 # Copyright (C) 2011-2019 Jérôme Poisson <goffi@goffi.org> |
494
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 from sat.core.log import getLogger |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 log = getLogger(__name__) |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
29 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
30 from pyjamas.ui.MenuBar import MenuBar |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
31 from pyjamas.ui.MenuItem import MenuItem |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
32 from pyjamas import Window |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
33 from sat_frontends.quick_frontend import quick_menus |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
34 from sat_browser import html_tools |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
35 |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 |
662
ebb602d8b3f2
browser_side: replace all instances of 'str' with 'unicode'
souliane <souliane@mailoo.org>
parents:
648
diff
changeset
|
37 unicode = str # FIXME: pyjamas workaround |
ebb602d8b3f2
browser_side: replace all instances of 'str' with 'unicode'
souliane <souliane@mailoo.org>
parents:
648
diff
changeset
|
38 |
ebb602d8b3f2
browser_side: replace all instances of 'str' with 'unicode'
souliane <souliane@mailoo.org>
parents:
648
diff
changeset
|
39 |
632
c2abadf31afb
browser side (menu): minor improvments:
Goffi <goffi@goffi.org>
parents:
589
diff
changeset
|
40 class MenuCmd(object): |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 """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
|
42 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
43 def __init__(self, menu_item, caller=None): |
517
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
44 """ |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
45 @param menu_item(quick_menu.MenuItem): instance of a callbable MenuItem |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
46 @param caller: menu caller |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
47 """ |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
48 self.item = menu_item |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
49 self._caller = caller |
494
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 execute(self): |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
52 self.item.call(self._caller) |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
55 class SimpleCmd(object): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
56 """Return an object with an "executre" method that launch a callback""" |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
57 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
58 def __init__(self, callback): |
509
35ccb3ff8245
browser_side: add method GenericMenuBar.addCategory + fixes MenuNode.addMenuItem when callback argument is a sub-menu
souliane <souliane@mailoo.org>
parents:
508
diff
changeset
|
59 """ |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
60 @param callback: method to call when menu is selected |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
61 """ |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
62 self.callback = callback |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
63 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
64 def execute(self): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
65 self.callback() |
494
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 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
68 class GenericMenuBar(MenuBar): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
69 """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
|
70 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
71 def __init__(self, host, vertical=False, styles=None, flat_level=0, **kwargs): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
72 """ |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
73 @param host (SatWebFrontend): host instance |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
74 @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
|
75 @param styles (dict): specific styles to be applied: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
76 - key: a value in ('moved_popup', 'menu_bar') |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
77 - value: a CSS class name |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
78 @param flat_level (int): sub-menus until that level see their items |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
79 displayed in the parent menu bar instead of in a callback popup. |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
80 """ |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
81 MenuBar.__init__(self, vertical, **kwargs) |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
82 self.host = host |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
83 self.styles = {} |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
84 if styles: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
85 self.styles.update(styles) |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
86 try: |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
87 self.setStyleName(self.styles['menu_bar']) |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
88 except KeyError: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
89 pass |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
90 self.menus_container = None |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
91 self.flat_level = flat_level |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
92 |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
93 def update(self, type_, caller=None): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
94 """Method to call when menus have changed |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
95 |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
96 @param type_: menu type like in sat.core.sat_main.importMenu |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
97 @param caller: instance linked to the menus |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
98 """ |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
99 self.menus_container = self.host.menus.getMainContainer(type_) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
100 self._caller=caller |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
101 self.createMenus() |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
102 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
103 @classmethod |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
104 def getCategoryHTML(cls, category): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
105 """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
|
106 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
107 Inheriting classes may overwrite this method. |
690
76a67d04c63e
browser_side: improve comments for menus-related methods
souliane <souliane@mailoo.org>
parents:
685
diff
changeset
|
108 @param category (quick_menus.MenuCategory): category to add |
76a67d04c63e
browser_side: improve comments for menus-related methods
souliane <souliane@mailoo.org>
parents:
685
diff
changeset
|
109 @return unicode: HTML to display |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
110 """ |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
111 return html_tools.html_sanitize(category.name) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
112 |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
113 def _buildMenus(self, container, flat_level, caller=None): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
114 """Recursively build menus of the container |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
115 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
116 @param container: a quick_menus.MenuContainer instance |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
117 @param caller: instance linked to the menus |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
118 """ |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
119 for child in container.getActiveMenus(): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
120 if isinstance(child, quick_menus.MenuContainer): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
121 item = self.addCategory(child, flat=bool(flat_level)) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
122 submenu = item.getSubMenu() |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
123 if submenu is None: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
124 submenu = self |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
125 submenu._buildMenus(child, flat_level-1 if flat_level else 0, caller) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
126 elif isinstance(child, quick_menus.MenuSeparator): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
127 item = MenuItem(text='', asHTML=None, StyleName="menuSeparator") |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
128 self.addItem(item) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
129 elif isinstance(child, quick_menus.MenuItem): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
130 self.addItem(child.name, False, MenuCmd(child, caller) if child.CALLABLE else None) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
131 else: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
132 log.error(u"Unknown child type: {}".format(child)) |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
133 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
134 def createMenus(self): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
135 self.clearItems() |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
136 if self.menus_container is None: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
137 log.debug("Menu is empty") |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
138 return |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
139 self._buildMenus(self.menus_container, self.flat_level, self._caller) |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
140 |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
141 def doItemAction(self, item, fireCommand): |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
142 """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
|
143 MenuBar.doItemAction(self, item, fireCommand) |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
144 if not self.popup: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
145 return |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
146 if self.vertical: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
147 # 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
|
148 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
|
149 new_left = self.getAbsoluteLeft() - self.popup.getOffsetWidth() + 1 |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
150 top = item.getAbsoluteTop() |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
151 else: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
152 # 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
|
153 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
|
154 new_left = max_left |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
155 top = self.getAbsoluteTop() + self.getOffsetHeight() - 1 |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
156 if item.getAbsoluteLeft() > max_left: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
157 self.popup.setPopupPosition(new_left, top) |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
158 # eventually smooth the popup edges to fit the menu own style |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
159 try: |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
160 self.popup.addStyleName(self.styles['moved_popup']) |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
161 except KeyError: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
162 pass |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
163 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
164 def addCategory(self, category, menu_bar=None, flat=False): |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
165 """Add a new category. |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
166 |
690
76a67d04c63e
browser_side: improve comments for menus-related methods
souliane <souliane@mailoo.org>
parents:
685
diff
changeset
|
167 @param category (quick_menus.MenuCategory): category to add |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
168 @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu. |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
169 """ |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
170 html = self.getCategoryHTML(category) |
509
35ccb3ff8245
browser_side: add method GenericMenuBar.addCategory + fixes MenuNode.addMenuItem when callback argument is a sub-menu
souliane <souliane@mailoo.org>
parents:
508
diff
changeset
|
171 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
172 if menu_bar is not None: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
173 assert not flat # can't have a menu_bar and be flat at the same time |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
174 sub_menu = menu_bar |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
175 elif not flat: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
176 sub_menu = GenericMenuBar(self.host, vertical=True) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
177 else: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
178 sub_menu = None |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
179 |
676
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
180 item = self.addItem(html, True, sub_menu) |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
181 if flat: |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
182 item.setStyleName("menuFlattenedCategory") |
849ffb24d5bf
browser side: menus refactorisation:
Goffi <goffi@goffi.org>
parents:
662
diff
changeset
|
183 return item |