Mercurial > libervia-desktop-kivy
view src/cagou/core/menu.py @ 89:d249df379fa3
core: kv files are not mandatory anymore for plugins
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Dec 2016 22:53:52 +0100 |
parents | 3dc526bb4a5a |
children | 5d2289127bb7 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- # Cagou: desktop/mobile frontend for Salut à Toi XMPP client # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from sat.core.i18n import _ from sat.core import log as logging log = logging.getLogger(__name__) from cagou.core.constants import Const as C from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy import properties from kivy.garden import contextmenu from sat_frontends.quick_frontend import quick_menus from cagou import G import webbrowser ABOUT_TITLE = _(u"About {}".format(C.APP_NAME)) ABOUT_CONTENT = _(u"""Cagou (Salut à Toi) v{} Cagou is a libre communication tool based on libre standard XMPP. Cagou is part of the "Salut à Toi" project more informations at [color=5500ff][ref=website]salut-a-toi.org[/ref][/color] """).format(C.APP_VERSION) class AboutContent(Label): def on_ref_press(self, value): if value == "website": webbrowser.open("https://salut-a-toi.org") class AboutPopup(Popup): def on_touch_down(self, touch): if self.collide_point(*touch.pos): self.dismiss() return super(AboutPopup, self).on_touch_down(touch) class MainMenu(contextmenu.AppMenu): pass class MenuItem(contextmenu.ContextMenuTextItem): item = properties.ObjectProperty() def on_item(self, instance, item): self.text = item.name def on_release(self): super(MenuItem, self).on_release() self.parent.hide() selected = G.host.selected_widget profile = None if selected is not None: try: profile = selected.profile except AttributeError: pass if profile is None: try: profile = list(selected.profiles)[0] except (AttributeError, IndexError): try: profile = list(G.host.profiles)[0] except IndexError: log.warning(u"Can't find profile") self.item.call(selected, profile) class MenuSeparator(contextmenu.ContextMenuDivider): pass class RootMenuContainer(contextmenu.AppMenuTextItem): pass class MenuContainer(contextmenu.ContextMenuTextItem): pass class MenusWidget(BoxLayout): def update(self, type_, caller=None): """Method to call when menus have changed @param type_(unicode): menu type like in sat.core.sat_main.importMenu @param caller(Widget): instance linked to the menus """ self.menus_container = G.host.menus.getMainContainer(type_) self.createMenus(caller) def _buildMenus(self, container, caller=None): """Recursively build menus of the container @param container(quick_menus.MenuContainer): menu container @param caller(Widget): instance linked to the menus """ if caller is None: main_menu = MainMenu() self.add_widget(main_menu) caller = main_menu else: context_menu = contextmenu.ContextMenu() caller.add_widget(context_menu) # FIXME: next line is needed after parent is set to avoid a display bug in contextmenu # TODO: fix this upstream context_menu._on_visible(False) caller = context_menu for child in container.getActiveMenus(): if isinstance(child, quick_menus.MenuContainer): if isinstance(caller, MainMenu): menu_container = RootMenuContainer() else: menu_container = MenuContainer() menu_container.text = child.name caller.add_widget(menu_container) self._buildMenus(child, caller=menu_container) elif isinstance(child, quick_menus.MenuSeparator): wid = MenuSeparator() caller.add_widget(wid) elif isinstance(child, quick_menus.MenuItem): wid = MenuItem(item=child) caller.add_widget(wid) else: log.error(u"Unknown child type: {}".format(child)) def createMenus(self, caller): self.clear_widgets() self._buildMenus(self.menus_container, caller) def onAbout(self): about = AboutPopup() about.title = ABOUT_TITLE about.content = AboutContent(text=ABOUT_CONTENT, markup=True) about.open() class UploadMenu(contextmenu.ContextMenu): """upload menu which handle display and callbacks""" # callback will be called with path to file to upload callback = properties.ObjectProperty() # cancel callback need to remove the widget for UI # will be called with the widget to remove as argument cancel_cb = properties.ObjectProperty() # profiles if set will be sent to upload widget, may be used to get specific files profiles = properties.ObjectProperty() def __init__(self, **kwargs): super(UploadMenu, self).__init__(**kwargs) if self.cancel_cb is None: self.cancel_cb = self.onUploadCancelled if self.profiles is None: self.profiles = iter(G.host.profiles) for plug_info in G.host.getPluggedWidgets(type_=C.PLUG_TYPE_UPLOAD): item = contextmenu.ContextMenuTextItem( text = plug_info['name'], on_release = lambda dummy, plug_info=plug_info: self.do_callback(plug_info) ) self.add_widget(item) def show(self, caller_wid=None): if caller_wid is not None: pos = caller_wid.x, caller_wid.top + self.get_height() else: pos = G.host.app.root_window.mouse_pos super(UploadMenu, self).show(*pos) def _closeUI(self, wid): G.host.closeUI() def onUploadCancelled(self, wid, cleaning_cb=None): self._closeUI(wid) if cleaning_cb is not None: cleaning_cb() def do_callback(self, plug_info): self.hide() if self.callback is None: log.warning(u"UploadMenu callback is not set") else: wid = None external = plug_info.get('external', False) def onUploadCb(file_path, cleaning_cb=None): if not external: self._closeUI(wid) self.callback(file_path, cleaning_cb) wid = plug_info['factory'](plug_info, onUploadCb, self.cancel_cb, self.profiles) if not external: G.host.showExtraUI(wid)