Mercurial > libervia-web
annotate src/browser/sat_browser/base_menu.py @ 626:ba2555c29c84 frontends_multi_profiles
browser_side: fixes RoomAndContactsChooser dialog
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 22 Feb 2015 21:56:18 +0100 |
parents | a5019e62c3e9 |
children | c2abadf31afb |
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 |
517
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
31 from sat.core import exceptions |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
32 from pyjamas.ui.MenuBar import MenuBar |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
33 from pyjamas.ui.UIObject import UIObject |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
34 from pyjamas.ui.MenuItem import MenuItem |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
35 from pyjamas import Window |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
37 import re |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
38 |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
39 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 class MenuCmd: |
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 |
517
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
43 def __init__(self, object_, handler=None, data=None): |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
44 """ |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
45 @param object_ (object): a callable or a class instance |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
46 @param handler (str): method name if object_ is a class instance |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
47 @param data (dict): data to pass as the callback argument |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
48 """ |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
49 if handler is None: |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
50 assert(callable(object_)) |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
51 self.callback = object_ |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
52 else: |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
53 self.callback = getattr(object_, handler) |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
54 self.data = data |
494
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): |
589
a5019e62c3e9
browser side: big refactoring to base Libervia on QuickFrontend, first draft:
Goffi <goffi@goffi.org>
parents:
517
diff
changeset
|
57 log.debug("execute %s" % self.callback) |
517
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
58 self.callback(self.data) if self.data else self.callback() |
494
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 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
61 class PluginMenuCmd: |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
62 """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
|
63 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
64 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
|
65 self.host = host |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 self.action_id = action_id |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 self.menu_data = menu_data |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
68 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
69 def execute(self): |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
70 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
|
71 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
72 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
73 class MenuNode(object): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
74 """MenuNode is a basic data structure to build a menu hierarchy. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
75 When Pyjamas MenuBar and MenuItem defines UI elements, MenuNode |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
76 stores the logical relation between them.""" |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
77 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
78 """This class has been introduced to deal with "flattened menus", when you |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
79 want the items of a sub-menu to be displayed in the parent menu. It was |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
80 needed to break the naive relation of "one MenuBar = one category".""" |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
81 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
82 def __init__(self, name=None, item=None, menu=None, flat_level=0): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
83 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
84 @param name (str): node name |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
85 @param item (MenuItem): associated menu item |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
86 @param menu (GenericMenuBar): associated menu bar |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
87 @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
|
88 displayed in the parent menu bar, instead of in a callback popup. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
89 """ |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
90 self.name = name |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
91 self.item = item or None # associated menu item |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
92 self.menu = menu or None # associated menu bar (sub-menu) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
93 self.flat_level = max(flat_level, -1) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
94 self.children = [] |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
95 |
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
|
96 def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False, sub_menu=None): |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
97 """Return the requested category. If create is True, path_i18n and |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
98 types are specified, recursively create the category and its parent. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
99 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
100 @param path (list[str]): path to the category |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
101 @param path_i18n (list[str]): internationalized path to the category |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
102 @param types (list[str]): types of the category and its parents |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
103 @param create (bool): if True, create the 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
|
104 @param sub_menu (GenericMenuBar): instance to popup as the category |
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
|
105 sub-menu, if it is created. Otherwise keep the previous sub-menu. |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
106 @return: MenuNode or None |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
107 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
108 assert(len(path) > 0 and len(path) == len(path_i18n) == len(types)) |
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
|
109 if len(path) > 1: |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
110 cat = self._getOrCreateCategory(path[:1], path_i18n[:1], types[:1], create) |
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
|
111 return cat._getOrCreateCategory(path[1:], path_i18n[1:], types[1:], create, sub_menu) if cat else None |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
112 cats = [child for child in self.children if child.menu and child.name == path[0]] |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
113 if len(cats) == 1: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
114 return cats[0] |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
115 assert(cats == []) # there should not be more than 1 category with the same name |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
116 if create: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
117 html = self.menu.getCategoryHTML(path_i18n[0], types[0]) |
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
|
118 sub_menu = sub_menu if sub_menu else GenericMenuBar(self.menu.host, vertical=True) |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
119 return self.addItem(html, True, sub_menu, name=path[0]) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
120 return None |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
121 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
122 def getCategories(self, target_path=None): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
123 """Return all the categories of the current node, or those of the |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
124 sub-category which is specified by target_path. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
125 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
126 @param target_path (list[str]): path to the target node |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
127 @return: list[MenuNode] |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
128 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
129 assert(self.menu) # this method applies to category nodes |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
130 if target_path: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
131 assert(isinstance(target_path, list)) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
132 cat = self._getOrCreateCategory(target_path[:-1]) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
133 return cat.getCategories(target_path[-1:]) if cat else None |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
134 return [child for child in self.children if child.menu] |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
135 |
510
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
136 def addMenuItem(self, path, path_i18n, types, callback=None, asHTML=False): |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
137 """Recursively add a new node, which could be a category or a leaf node. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
138 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
139 @param path (list[str], str): path to the item |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
140 @param path_i18n (list[str], str): internationalized path to the item |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
141 @param types (list[str], str): types of the item and its parents |
510
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
142 @param callback (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to |
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
143 execute as a leaf's callback or to popup as a category sub-menu |
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
144 @param asHTML (boolean): True to display the UI item as HTML |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
145 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
146 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback)) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
147 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
148 leaf_node = hasattr(callback, "execute") |
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
|
149 category = isinstance(callback, GenericMenuBar) |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
150 assert(not leaf_node or not category) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
151 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
152 path = [path] if isinstance(path, str) else path |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
153 path_i18n = [path_i18n] if isinstance(path_i18n, str) else path_i18n |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
154 types = [types for dummy in range(len(path_i18n))] if isinstance(types, str) else types |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
155 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
156 if 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
|
157 return self._getOrCreateCategory(path, path_i18n, types, True, callback) |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
158 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
159 if len(path) == len(path_i18n) - 1: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
160 path.append(None) # dummy name for a leaf node |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
161 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
162 parent = self._getOrCreateCategory(path[:-1], path_i18n[:-1], types[:-1], True) |
510
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
163 return parent.addItem(path_i18n[-1], asHTML=asHTML, popup=callback) |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
164 |
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
|
165 def addCategory(self, path, path_i18n, types, menu_bar=None): |
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
|
166 """Recursively add a new category. |
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
|
167 |
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
|
168 @param path (list[str], str): path to the category |
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
|
169 @param path_i18n (list[str], str): internationalized path to the category |
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
|
170 @param types (list[str], str): types of the category and its parents |
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 @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu. |
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
|
172 """ |
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
|
173 if menu_bar: |
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
|
174 assert(isinstance(menu_bar, GenericMenuBar)) |
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
|
175 else: |
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
|
176 menu_bar = GenericMenuBar(self.menu.host, vertical=True) |
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
|
177 return self.addMenuItem(path, path_i18n, types, menu_bar) |
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
|
178 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
179 def addItem(self, item, asHTML=None, popup=None, name=None): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
180 """Add a single child to the current node. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
181 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
182 @param item: see MenuBar.addItem |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
183 @param asHTML: see MenuBar.addItem |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
184 @param popup: see MenuBar.addItem |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
185 @param name (str): the item node's name |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
186 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
187 if item is None: # empty string is allowed to set a separator |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
188 return None |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
189 item = MenuBar.addItem(self.menu, item, asHTML, popup) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
190 node_menu = item.getSubMenu() # node eventually uses it's own menu |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
191 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
192 # XXX: all the dealing with flattened menus is done here |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
193 if self.flat_level > 0: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
194 item.setSubMenu(None) # eventually remove any sub-menu callback |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
195 if item.getCommand(): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
196 node_menu = None # node isn't a category, it needs no menu |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
197 else: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
198 node_menu = self.menu # node uses the menu of its parent |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
199 item.setStyleName(self.menu.styles["flattened-category"]) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
200 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
201 node = MenuNode(name=name, item=item, menu=node_menu, flat_level=self.flat_level - 1) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
202 self.children.append(node) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
203 return node |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
204 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
205 def addCachedMenus(self, type_, menu_data=None): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
206 """Add cached menus to instance. |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
207 |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
208 @param type_: menu type like in sat.core.sat_main.importMenu |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
209 @param menu_data: data to send with these menus |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
210 """ |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
211 menus = self.menu.host.menus.get(type_, []) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
212 for action_id, path, path_i18n in menus: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
213 if len(path) != len(path_i18n): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
214 log.error("inconsistency between menu paths") |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
215 continue |
517
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
216 if isinstance(action_id, str): |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
217 callback = PluginMenuCmd(self.menu.host, action_id, menu_data) |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
218 elif callable(action_id): |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
219 callback = MenuCmd(action_id, data=menu_data) |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
220 else: |
85699d18921f
browser_side: menu callback can be a method not belonging to a class
souliane <souliane@mailoo.org>
parents:
510
diff
changeset
|
221 raise exceptions.InternalError |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
222 self.addMenuItem(path, path_i18n, 'plugins', callback) |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
223 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
224 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
225 class GenericMenuBar(MenuBar): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
226 """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
|
227 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
228 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
|
229 """ |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
230 @param host (SatWebFrontend): host instance |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
231 @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
|
232 @param styles (dict): specific styles to be applied: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
233 - 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
|
234 - value: a CSS class name |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
235 @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
|
236 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
|
237 """ |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
238 MenuBar.__init__(self, vertical, **kwargs) |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
239 self.host = host |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
240 self.styles = {'separator': 'menuSeparator', 'flattened-category': 'menuFlattenedCategory'} |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
241 if styles: |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
242 self.styles.update(styles) |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
243 if 'menu_bar' in self.styles: |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
244 self.setStyleName(self.styles['menu_bar']) |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
245 self.node = MenuNode(menu=self, flat_level=flat_level) |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
246 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
247 @classmethod |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
248 def getCategoryHTML(cls, menu_name_i18n, type_): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
249 """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
|
250 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
251 Inheriting classes may overwrite this method. |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
252 @param menu_name_i18n (str): internationalized category name |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
253 @param type_ (str): category type |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
254 @return: str |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
255 """ |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
256 return menu_name_i18n |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
257 |
508
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
258 def setStyleName(self, style): |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
259 # XXX: pyjamas set the style to object string representation! |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
260 # FIXME: fix the bug upstream |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
261 menu_style = ['gwt-MenuBar'] |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
262 menu_style.append(menu_style[0] + '-' + ('vertical' if self.vertical else 'horizontal')) |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
263 for classname in style.split(' '): |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
264 if classname not in menu_style: |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
265 menu_style.append(classname) |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
266 UIObject.setStyleName(self, ' '.join(menu_style)) |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
267 |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
268 def addStyleName(self, style): |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
269 # XXX: same kind of problem then with setStyleName |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
270 # FIXME: fix the bug upstream |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
271 if not re.search('(^| )%s( |$)' % style, self.getStyleName()): |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
272 UIObject.setStyleName(self, self.getStyleName() + ' ' + style) |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
273 |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
274 def removeStyleName(self, style): |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
275 # XXX: same kind of problem then with setStyleName |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
276 # FIXME: fix the bug upstream |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
277 style = re.sub('(^| )%s( |$)' % style, ' ', self.getStyleName()).strip() |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
278 UIObject.setStyleName(self, style) |
1d41cc5b57b1
browser_side: fixes using addStyleName and removeStyleName on a GenericMenuBar
souliane <souliane@mailoo.org>
parents:
502
diff
changeset
|
279 |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
280 def doItemAction(self, item, fireCommand): |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
281 """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
|
282 MenuBar.doItemAction(self, item, fireCommand) |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
283 if not self.popup: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
284 return |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
285 if self.vertical: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
286 # 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
|
287 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
|
288 new_left = self.getAbsoluteLeft() - self.popup.getOffsetWidth() + 1 |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
289 top = item.getAbsoluteTop() |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
290 else: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
291 # 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
|
292 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
|
293 new_left = max_left |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
294 top = self.getAbsoluteTop() + self.getOffsetHeight() - 1 |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
295 if item.getAbsoluteLeft() > max_left: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
296 self.popup.setPopupPosition(new_left, top) |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
297 # 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
|
298 if 'moved_popup' in self.styles: |
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
299 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
|
300 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
301 def getCategories(self, parent_path=None): |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
302 """Return all the categories items. |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
303 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
304 @return: list[CategoryItem] |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
305 """ |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
306 return [cat.item for cat in self.node.getCategories(parent_path)] |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
307 |
510
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
308 def addMenuItem(self, path, path_i18n, types, menu_cmd, asHTML=False): |
db3436c85fb1
browser_side: the status menu is now based on GenericMenuBar instead of PopupMenuPanel
souliane <souliane@mailoo.org>
parents:
509
diff
changeset
|
309 return self.node.addMenuItem(path, path_i18n, types, menu_cmd, asHTML).item |
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
|
310 |
589
a5019e62c3e9
browser side: big refactoring to base Libervia on QuickFrontend, first draft:
Goffi <goffi@goffi.org>
parents:
517
diff
changeset
|
311 def addCategory(self, path, path_i18n, types, menu_bar=None): |
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
|
312 return self.node.addCategory(path, path_i18n, types, menu_bar).item |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
313 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
314 def addItem(self, item, asHTML=None, popup=None): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
315 return self.node.addItem(item, asHTML, popup).item |
498
60be99de3808
browser_side: menus refactorization + handle levels > 2
souliane <souliane@mailoo.org>
parents:
494
diff
changeset
|
316 |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
317 def addCachedMenus(self, type_, menu_data=None): |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
318 self.node.addCachedMenus(type_, menu_data) |
494
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
319 |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
320 def addSeparator(self): |
5d8632a7bfde
browser_side: refactorisation of menus and LiberviaWidget's header
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
321 """Add a separator between the categories""" |
502
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
322 item = MenuItem(text='', asHTML=None, StyleName=self.styles['separator']) |
4aa627b059df
browser_side: categories of the menus can be "flattened":
souliane <souliane@mailoo.org>
parents:
498
diff
changeset
|
323 return self.addItem(item) |