view src/cagou.py @ 13:12a189fbb9ba

widget handler first draft: This widget handle other widgets location and size. It is currently loosely inspired from Blender's UI, and the implementation is currenlty naïve. It should be updated in the future to have a behaviour more close to Blender one.
author Goffi <goffi@goffi.org>
date Fri, 08 Jul 2016 20:18:43 +0200
parents 30f6586f904b
children 21a432afd06d
line wrap: on
line source

#!/usr//bin/env python2
# -*- 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/>.


import logging_setter
logging_setter.set_logging()
from constants import Const as C
from sat.core import log as logging
log = logging.getLogger(__name__)
from sat_frontends.quick_frontend.quick_app import QuickApp
from sat_frontends.bridge.DBus import DBusBridgeFrontend
# from sat_frontends.quick_frontend import quick_utils
import kivy
kivy.require('1.9.1')
import kivy.support
kivy.support.install_gobject_iteration()
from kivy.app import App
import xmlui
from profile_manager import ProfileManager
from widgets_handler import WidgetsHandler
from kivy.uix.boxlayout import BoxLayout
from widget_selector import WidgetSelector
from cagou_widget import CagouWidget
import os.path


class CagouRootWidget(BoxLayout):

    def __init__(self, widgets):
        super(CagouRootWidget, self).__init__(orientation=("vertical"))
        for wid in widgets:
            self.add_widget(wid)

    def change_widgets(self, widgets):
        self.clear_widgets()
        for wid in widgets:
            self.add_widget(wid)


class CagouApp(App):
    """Kivy App for Cagou"""

    def build(self):
        return CagouRootWidget([ProfileManager(self.host)])


class Cagou(QuickApp):
    MB_HANDLE = False

    def __init__(self):
        super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui)
        self.app = CagouApp()
        self.app.host = self
        media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir")
        self.app.default_avatar = os.path.join(media_dir, "misc/default_avatar.png")

    def run(self):
        self.app.run()

    def plugging_profiles(self):
        widget_selector = WidgetSelector(self)
        self.app.root.change_widgets([WidgetsHandler(self, widget_selector)])

    def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
        log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status))

    def switchWidget(self, old, new):
        """Replace old widget by new one

        old(CagouWidget): CagouWidget instance or a child
        new(CagouWidget): new widget instance
        """
        for w in old.walk_reverse():
            if isinstance(w, CagouWidget):
                parent = w.parent
                idx = parent.children.index(w)
                parent.remove_widget(w)
                parent.add_widget(new, index=idx)
                break


if __name__ == '__main__':
    Cagou().run()