annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import log as logging
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 log = logging.getLogger(__name__)
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from kivy.uix.boxlayout import BoxLayout
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.uix.listview import ListView
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.adapters.listadapter import ListAdapter
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy import properties
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from contact_list import ContactList
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy.uix.behaviors import ButtonBehavior
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from cagou_widget import CagouWidget
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 class WidgetSelItem(ButtonBehavior, BoxLayout):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 widget_type = properties.StringProperty('')
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def __init__(self, **kwargs):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 super(WidgetSelItem, self).__init__(**kwargs)
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.host = kwargs['host']
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.callback = kwargs['callback']
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def select(self, *args):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 log.debug(u"widget selection: {}".format(self.widget_type))
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.host.switchWidget(self, self.callback())
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def deselect(self, *args):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 pass
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 class WidgetSelector(CagouWidget):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, host):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 super(WidgetSelector, self).__init__()
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.host = host
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.widget_factory = {
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 'contacts': lambda: host.widgets.getOrCreateWidget(ContactList, None, on_new_widget=None),
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.adapter = ListAdapter(data=self.widget_factory,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 cls=WidgetSelItem,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 args_converter=self.dataConverter,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 selection_mode='single',
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 allow_empty_selection=True,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 )
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.add_widget(ListView(adapter=self.adapter))
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def dataConverter(self, idx, widget_type):
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 return {
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "host": self.host,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "widget_type": widget_type,
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 "callback": self.widget_factory[widget_type]
30f6586f904b widget selector:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 }