view src/widget_selector.py @ 12:30f6586f904b

widget selector: this widget will be the default one and display the different activities a user can do (chat, blog, file sharing, games, etc).
author Goffi <goffi@goffi.org>
date Fri, 08 Jul 2016 20:18:36 +0200
parents
children
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 import log as logging
log = logging.getLogger(__name__)
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListView
from kivy.adapters.listadapter import ListAdapter
from kivy import properties
from contact_list import ContactList
from kivy.uix.behaviors import ButtonBehavior
from cagou_widget import CagouWidget


class WidgetSelItem(ButtonBehavior, BoxLayout):
    widget_type = properties.StringProperty('')

    def __init__(self, **kwargs):
        super(WidgetSelItem, self).__init__(**kwargs)
        self.host = kwargs['host']
        self.callback = kwargs['callback']

    def select(self, *args):
        log.debug(u"widget selection: {}".format(self.widget_type))
        self.host.switchWidget(self, self.callback())

    def deselect(self, *args):
        pass


class WidgetSelector(CagouWidget):

    def __init__(self, host):
        super(WidgetSelector, self).__init__()
        self.host = host
        self.widget_factory = {
            'contacts': lambda: host.widgets.getOrCreateWidget(ContactList, None, on_new_widget=None),
            }
        self.adapter = ListAdapter(data=self.widget_factory,
            cls=WidgetSelItem,
            args_converter=self.dataConverter,
            selection_mode='single',
            allow_empty_selection=True,
           )
        self.add_widget(ListView(adapter=self.adapter))

    def dataConverter(self, idx, widget_type):
        return {
            "host": self.host,
            "widget_type": widget_type,
            "callback": self.widget_factory[widget_type]
            }