Mercurial > libervia-desktop-kivy
comparison cagou/core/common.py @ 348:38fd457b2158
core (common, share_widget): new JidSelector widget:
- A widget to select jid among a list is commonly needed, so it has been implemented in
core.common, based on the code from ShareWidget
- ShareWidget has been adapted accordingly
- new ContactButton class in core.common
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 17 Jan 2020 18:44:32 +0100 |
parents | e2b51663d8b8 |
children | 4d660b252487 |
comparison
equal
deleted
inserted
replaced
347:bf9474e164f3 | 348:38fd457b2158 |
---|---|
1 #!/usr//bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | |
3 | 2 |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | 3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
5 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) | 4 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 5 |
7 # This program is free software: you can redistribute it and/or modify | 6 # This program is free software: you can redistribute it and/or modify |
17 # You should have received a copy of the GNU Affero General Public License | 16 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 18 |
20 """common simple widgets""" | 19 """common simple widgets""" |
21 | 20 |
21 import json | |
22 from functools import partial | |
22 from sat.core.i18n import _ | 23 from sat.core.i18n import _ |
24 from sat.core import log as logging | |
23 from kivy.uix.image import Image | 25 from kivy.uix.image import Image |
24 from kivy.uix.label import Label | 26 from kivy.uix.label import Label |
25 from kivy.uix.behaviors import ButtonBehavior | 27 from kivy.uix.behaviors import ButtonBehavior |
26 from kivy.uix.behaviors import ToggleButtonBehavior | 28 from kivy.uix.behaviors import ToggleButtonBehavior |
27 from kivy.uix.boxlayout import BoxLayout | 29 from kivy.uix.boxlayout import BoxLayout |
30 from kivy.uix.scrollview import ScrollView | |
31 from kivy.event import EventDispatcher | |
28 from kivy.metrics import dp | 32 from kivy.metrics import dp |
33 from kivy import properties | |
29 from cagou.core.constants import Const as C | 34 from cagou.core.constants import Const as C |
30 from kivy import properties | |
31 from cagou import G | 35 from cagou import G |
32 import json | |
33 from sat.core import log as logging | |
34 | 36 |
35 log = logging.getLogger(__name__) | 37 log = logging.getLogger(__name__) |
36 | 38 |
37 UNKNOWN_SYMBOL = 'Unknown symbol name' | 39 UNKNOWN_SYMBOL = 'Unknown symbol name' |
38 | 40 |
44 class Avatar(Image): | 46 class Avatar(Image): |
45 pass | 47 pass |
46 | 48 |
47 | 49 |
48 class ContactItem(BoxLayout): | 50 class ContactItem(BoxLayout): |
51 """An item from ContactList | |
52 | |
53 The item will drawn as an icon (JID avatar) with its jid below | |
54 """ | |
49 base_width = dp(150) | 55 base_width = dp(150) |
50 profile = properties.StringProperty() | 56 profile = properties.StringProperty() |
51 data = properties.DictProperty() | 57 data = properties.DictProperty() |
52 jid = properties.StringProperty('') | 58 jid = properties.StringProperty('') |
59 | |
60 | |
61 class ContactButton(ButtonBehavior, ContactItem): | |
62 pass | |
53 | 63 |
54 | 64 |
55 class JidItem(BoxLayout): | 65 class JidItem(BoxLayout): |
56 bg_color = properties.ListProperty([0.2, 0.2, 0.2, 1]) | 66 bg_color = properties.ListProperty([0.2, 0.2, 0.2, 1]) |
57 color = properties.ListProperty([1, 1, 1, 1]) | 67 color = properties.ListProperty([1, 1, 1, 1]) |
142 icon_wid = Image(source=icon_src, allow_stretch=True) | 152 icon_wid = Image(source=icon_src, allow_stretch=True) |
143 self.add_widget(icon_wid) | 153 self.add_widget(icon_wid) |
144 else: | 154 else: |
145 icon_wid = ActionSymbol(symbol=symbol) | 155 icon_wid = ActionSymbol(symbol=symbol) |
146 self.add_widget(icon_wid) | 156 self.add_widget(icon_wid) |
157 | |
158 | |
159 class JidSelector(ScrollView, EventDispatcher): | |
160 layout = properties.ObjectProperty(None) | |
161 | |
162 def __init__(self, **kwargs): | |
163 self.register_event_type('on_select') | |
164 super().__init__(**kwargs) | |
165 | |
166 def on_kv_post(self, wid): | |
167 self.addRosterContacts() | |
168 | |
169 def on_select(self, wid): | |
170 pass | |
171 | |
172 def on_parent(self, wid, parent): | |
173 if parent is None: | |
174 log.debug("removing contactsFilled listener") | |
175 G.host.removeListener("contactsFilled", self.onContactsFilled) | |
176 else: | |
177 G.host.addListener("contactsFilled", self.onContactsFilled) | |
178 | |
179 def onContactsFilled(self, profile): | |
180 log.debug("onContactsFilled event received") | |
181 self.addRosterContacts() | |
182 | |
183 def addRosterContacts(self): | |
184 log.debug("starting addRosterContacts") | |
185 self.layout.clear_widgets() | |
186 for profile in G.host.profiles: | |
187 contact_list = G.host.contact_lists[profile] | |
188 for entity_jid in sorted(contact_list.roster): | |
189 item = ContactButton( | |
190 jid=entity_jid, | |
191 data=contact_list.getItem(entity_jid), | |
192 profile=profile, | |
193 ) | |
194 item.bind(on_press=partial(self.dispatch, 'on_select')) | |
195 self.layout.add_widget(item) |