comparison src/plugin_wid_widget_selector.py @ 14:21a432afd06d

plugin system, first draft: - widgets are now handled with plugins, ContactList and WidgetSelector have been changed to be pluggins - everything in PLUGIN_INFO (including PLUGIN_INFO itself) is optional. Best guess is used if a value is missing - WidgetsHandler use default widget from plugins, which is WidgetSelector for now - PLUGIN_INFO is used in the same way as for backed plugins, with (for now) following possible keys: - "name": human readable name - "description": long description of what widget do - "import_name": unique short name used as reference for import - "main": main class name - "factory": callback used to instanciate a widget (see Cagou._defaultFactory) - "kv_file": path to the kv language file to load (same filename with ".kv" will be used if key is not found) other data should be added quickly, as a path to a file used as icon - host.getPluggedWidgets can be used to find loaded widgets. except_cls can be used to excluse a class (self.__class__ usually) - fixed host.switchWidget when old widget is already a CagouWidget - CagouWidget header new display the available widgets
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 16:02:44 +0200
parents src/widget_selector.py@30f6586f904b
children
comparison
equal deleted inserted replaced
13:12a189fbb9ba 14:21a432afd06d
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # 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/>.
19
20
21 from sat.core import log as logging
22 log = logging.getLogger(__name__)
23 from sat.core.i18n import _
24 from constants import Const as C
25 from kivy.uix.boxlayout import BoxLayout
26 from kivy.uix.listview import ListView
27 from kivy.adapters.listadapter import ListAdapter
28 from kivy import properties
29 from kivy.uix.behaviors import ButtonBehavior
30 from cagou_widget import CagouWidget
31
32
33 PLUGIN_INFO = {
34 "name": _(u"widget selector"),
35 "import_name": C.WID_SELECTOR,
36 "main": "WidgetSelector",
37 "description": _(u"show available widgets and allow to select one"),
38 }
39
40
41 class WidgetSelItem(ButtonBehavior, BoxLayout):
42 plugin_info = properties.DictProperty()
43
44 def __init__(self, **kwargs):
45 super(WidgetSelItem, self).__init__(**kwargs)
46 self.host = kwargs['host']
47
48 def select(self, *args):
49 log.debug(u"widget selection: {}".format(self.plugin_info["name"]))
50 factory = self.plugin_info["factory"]
51 self.host.switchWidget(self, factory(self.host, self.plugin_info, None, None))
52
53 def deselect(self, *args):
54 pass
55
56
57 class WidgetSelector(CagouWidget):
58
59 def __init__(self, host):
60 super(WidgetSelector, self).__init__(host)
61 self.host = host
62 self.adapter = ListAdapter(
63 data=host.getPluggedWidgets(except_cls=self.__class__),
64 cls=WidgetSelItem,
65 args_converter=self.dataConverter,
66 selection_mode='single',
67 allow_empty_selection=True,
68 )
69 self.add_widget(ListView(adapter=self.adapter))
70
71 @classmethod
72 def factory(cls, host, plugin_info, target, profiles):
73 return cls(host)
74
75 def dataConverter(self, idx, plugin_info):
76 return {
77 "host": self.host,
78 "plugin_info": plugin_info,
79 }
80
81
82 PLUGIN_INFO["factory"] = WidgetSelector.factory