comparison src/cagou/core/menu.py @ 85:c2a7234d13d2

menu: use of garden's contextmenu for menus
author Goffi <goffi@goffi.org>
date Sat, 24 Dec 2016 14:20:49 +0100
parents 65775152aac1
children c711be670ecd
comparison
equal deleted inserted replaced
84:2caee196d19a 85:c2a7234d13d2
20 20
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat.core import log as logging 22 from sat.core import log as logging
23 log = logging.getLogger(__name__) 23 log = logging.getLogger(__name__)
24 from cagou.core.constants import Const as C 24 from cagou.core.constants import Const as C
25 from kivy.uix.scrollview import ScrollView 25 from kivy.uix.boxlayout import BoxLayout
26 from kivy.uix.gridlayout import GridLayout
27 from kivy.uix.widget import Widget
28 from kivy.uix.label import Label 26 from kivy.uix.label import Label
29 from kivy.uix.button import Button
30 from kivy.uix.popup import Popup 27 from kivy.uix.popup import Popup
31 from kivy.uix.dropdown import DropDown
32 from kivy import properties 28 from kivy import properties
29 from kivy.garden import contextmenu
33 from sat_frontends.quick_frontend import quick_menus 30 from sat_frontends.quick_frontend import quick_menus
34 from cagou import G 31 from cagou import G
35 import webbrowser 32 import webbrowser
36 33
37 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME)) 34 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME))
57 if self.collide_point(*touch.pos): 54 if self.collide_point(*touch.pos):
58 self.dismiss() 55 self.dismiss()
59 return super(AboutPopup, self).on_touch_down(touch) 56 return super(AboutPopup, self).on_touch_down(touch)
60 57
61 58
62 class MenuItem(Button): 59 class MainMenu(contextmenu.AppMenu):
60 pass
61
62
63 class MenuItem(contextmenu.ContextMenuTextItem):
63 item = properties.ObjectProperty() 64 item = properties.ObjectProperty()
64 65
65 def on_item(self, instance, item): 66 def on_item(self, instance, item):
66 self.text = item.name 67 self.text = item.name
67 68
68 def on_release(self): 69 def on_release(self):
69 try: 70 super(MenuItem, self).on_release()
70 self.parent.parent.dismiss() 71 self.parent.hide()
71 except AttributeError:
72 pass
73 selected = G.host.selected_widget 72 selected = G.host.selected_widget
74 profile = None 73 profile = None
75 if selected is not None: 74 if selected is not None:
76 try: 75 try:
77 profile = selected.profile 76 profile = selected.profile
87 except IndexError: 86 except IndexError:
88 log.warning(u"Can't find profile") 87 log.warning(u"Can't find profile")
89 self.item.call(selected, profile) 88 self.item.call(selected, profile)
90 89
91 90
92 class MenuSeparator(Widget): 91 class MenuSeparator(contextmenu.ContextMenuDivider):
93 pass 92 pass
94 93
95 94
96 class MenuContainer(Button): 95 class RootMenuContainer(contextmenu.AppMenuTextItem):
97 96 pass
98 def __init__(self, **kwargs):
99 super(MenuContainer, self).__init__(**kwargs)
100 self.dropdown = DropDown(auto_width=False, size_hint_x=None, width=400)
101
102 def on_release(self):
103 self.dropdown.open(self)
104
105 def add_widget(self, widget):
106 widget.size_hint_y = None
107 self.dropdown.add_widget(widget)
108 97
109 98
110 class MenusWidget(ScrollView): 99 class MenuContainer(contextmenu.ContextMenuTextItem):
100 pass
111 101
112 def __init__(self, **kwargs):
113 super(MenusWidget, self).__init__(**kwargs)
114 self._grid = GridLayout(rows=1, size_hint=(None, 1))
115 self._grid.width = self._grid.minimum_width
116 super(MenusWidget, self).add_widget(self._grid)
117 102
118 def add_widget(self, widget): 103 class MenusWidget(BoxLayout):
119 self._grid.add_widget(widget)
120
121 def clear_widgets(self, children=None):
122 self._grid.clear_widgets(children)
123 104
124 def update(self, type_, caller=None): 105 def update(self, type_, caller=None):
125 """Method to call when menus have changed 106 """Method to call when menus have changed
126 107
127 @param type_(unicode): menu type like in sat.core.sat_main.importMenu 108 @param type_(unicode): menu type like in sat.core.sat_main.importMenu
135 116
136 @param container(quick_menus.MenuContainer): menu container 117 @param container(quick_menus.MenuContainer): menu container
137 @param caller(Widget): instance linked to the menus 118 @param caller(Widget): instance linked to the menus
138 """ 119 """
139 if caller is None: 120 if caller is None:
140 caller = self 121 main_menu = MainMenu()
122 self.add_widget(main_menu)
123 caller = main_menu
124 else:
125 context_menu = contextmenu.ContextMenu()
126 caller.add_widget(context_menu)
127 # FIXME: next line is needed after parent is set to avoid a display bug in contextmenu
128 # TODO: fix this upstream
129 context_menu._on_visible(False)
130
131 caller = context_menu
132
141 for child in container.getActiveMenus(): 133 for child in container.getActiveMenus():
142 if isinstance(child, quick_menus.MenuContainer): 134 if isinstance(child, quick_menus.MenuContainer):
143 menu_container = MenuContainer() 135 if isinstance(caller, MainMenu):
136 menu_container = RootMenuContainer()
137 else:
138 menu_container = MenuContainer()
144 menu_container.text = child.name 139 menu_container.text = child.name
145 caller.add_widget(menu_container) 140 caller.add_widget(menu_container)
146 self._buildMenus(child, caller=menu_container) 141 self._buildMenus(child, caller=menu_container)
147 elif isinstance(child, quick_menus.MenuSeparator): 142 elif isinstance(child, quick_menus.MenuSeparator):
148 wid = MenuSeparator() 143 wid = MenuSeparator()