comparison src/cagou/core/menu.py @ 51:3f8599d9a766

core: menus first draft: - menus are handled. Global menus are shown above notification for the moment, but may be displayed differently depending on plateform (e.g. using a button on mobile plateforms) - menu are displayed after profile is connected - backends menus are displayed but not working yet - help/about menu has been added.
author Goffi <goffi@goffi.org>
date Sun, 11 Sep 2016 12:15:41 +0200
parents
children 65775152aac1
comparison
equal deleted inserted replaced
50:c45d6e9ec731 51:3f8599d9a766
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from sat.core.i18n import _
22 from sat.core import log as logging
23 log = logging.getLogger(__name__)
24 from cagou.core.constants import Const as C
25 from kivy.uix.scrollview import ScrollView
26 from kivy.uix.gridlayout import GridLayout
27 from kivy.uix.widget import Widget
28 from kivy.uix.label import Label
29 from kivy.uix.button import Button
30 from kivy.uix.popup import Popup
31 from kivy.uix.dropdown import DropDown
32 from kivy import properties
33 from sat_frontends.quick_frontend import quick_menus
34 from cagou import G
35 import webbrowser
36
37 ABOUT_TITLE = _(u"About {}".format(C.APP_NAME))
38 ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{}
39
40 Cagou is a libre communication tool based on libre standard XMPP.
41
42 Cagou is part of the "Salut à Toi" project
43 more informations at [color=5500ff][ref=website]salut-a-toi.org[/ref][/color]
44 """).format(C.APP_VERSION)
45
46
47 class AboutContent(Label):
48
49 def on_ref_press(self, value):
50 if value == "website":
51 webbrowser.open("https://salut-a-toi.org")
52
53
54 class AboutPopup(Popup):
55
56 def on_touch_down(self, touch):
57 if self.collide_point(*touch.pos):
58 self.dismiss()
59 return super(AboutPopup, self).on_touch_down(touch)
60
61
62 class MenuItem(Button):
63 item = properties.ObjectProperty()
64
65 def on_item(self, instance, item):
66 self.text = item.name
67
68 def on_release(self):
69 try:
70 self.parent.parent.dismiss()
71 except AttributeError:
72 pass
73 selected = G.host.selected_widget
74 profile = None
75 if selected is not None:
76 try:
77 profile = selected.profile
78 except AttributeError:
79 pass
80
81 if profile is None:
82 try:
83 profile = list(selected.profiles)[0]
84 except (AttributeError, IndexError):
85 try:
86 profile = list(G.host.profiles)[0]
87 except IndexError:
88 log.warning(u"Can't find profile")
89
90 self.item.call(selected, profile)
91
92
93 class MenuSeparator(Widget):
94 pass
95
96
97 class MenuContainer(Button):
98
99 def __init__(self, **kwargs):
100 super(MenuContainer, self).__init__(**kwargs)
101 self.dropdown = DropDown(auto_width=False, size_hint_x=None, width=400)
102
103 def on_release(self):
104 self.dropdown.open(self)
105
106 def add_widget(self, widget):
107 widget.size_hint_y = None
108 self.dropdown.add_widget(widget)
109
110
111 class MenusWidget(ScrollView):
112
113 def __init__(self, **kwargs):
114 super(MenusWidget, self).__init__(**kwargs)
115 self._grid = GridLayout(rows=1, size_hint=(None, 1))
116 self._grid.width = self._grid.minimum_width
117 super(MenusWidget, self).add_widget(self._grid)
118
119 def add_widget(self, widget):
120 self._grid.add_widget(widget)
121
122 def clear_widgets(self, children=None):
123 self._grid.clear_widgets(children)
124
125 def update(self, type_, caller=None):
126 """Method to call when menus have changed
127
128 @param type_(unicode): menu type like in sat.core.sat_main.importMenu
129 @param caller(Widget): instance linked to the menus
130 """
131 self.menus_container = G.host.menus.getMainContainer(type_)
132 self.createMenus(caller)
133
134 def _buildMenus(self, container, caller=None):
135 """Recursively build menus of the container
136
137 @param container(quick_menus.MenuContainer): menu container
138 @param caller(Widget): instance linked to the menus
139 """
140 if caller is None:
141 caller = self
142 for child in container.getActiveMenus():
143 if isinstance(child, quick_menus.MenuContainer):
144 menu_container = MenuContainer()
145 menu_container.text = child.name
146 caller.add_widget(menu_container)
147 self._buildMenus(child, caller=menu_container)
148 elif isinstance(child, quick_menus.MenuSeparator):
149 wid = MenuSeparator()
150 caller.add_widget(wid)
151 elif isinstance(child, quick_menus.MenuItem):
152 wid = MenuItem(item=child)
153 caller.add_widget(wid)
154 else:
155 log.error(u"Unknown child type: {}".format(child))
156
157 def createMenus(self, caller):
158 self.clear_widgets()
159 self._buildMenus(self.menus_container, caller)
160
161 def onAbout(self):
162 about = AboutPopup()
163 about.title = ABOUT_TITLE
164 about.content = AboutContent(text=ABOUT_CONTENT, markup=True)
165 about.open()