view src/widget_selector.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
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]
            }