view 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
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()