annotate src/cagou.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 12a189fbb9ba
children 56838ad5c84b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents: 0
diff changeset
1 #!/usr//bin/env python2
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
21 from sat.core.i18n import _
6
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents: 0
diff changeset
22 import logging_setter
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents: 0
diff changeset
23 logging_setter.set_logging()
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents: 0
diff changeset
24 from constants import Const as C
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import log as logging
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 log = logging.getLogger(__name__)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat_frontends.quick_frontend.quick_app import QuickApp
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat_frontends.bridge.DBus import DBusBridgeFrontend
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 # from sat_frontends.quick_frontend import quick_utils
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import kivy
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 kivy.require('1.9.1')
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import kivy.support
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 kivy.support.install_gobject_iteration()
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from kivy.app import App
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
35 from kivy.lang import Builder
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 import xmlui
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from profile_manager import ProfileManager
13
12a189fbb9ba widget handler first draft:
Goffi <goffi@goffi.org>
parents: 12
diff changeset
38 from widgets_handler import WidgetsHandler
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
39 from kivy.uix.boxlayout import BoxLayout
11
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
40 from cagou_widget import CagouWidget
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
41 from importlib import import_module
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
42 import os.path
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
43 import glob
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
44
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
45
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
46 class CagouRootWidget(BoxLayout):
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
47
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
48 def __init__(self, widgets):
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
49 super(CagouRootWidget, self).__init__(orientation=("vertical"))
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
50 for wid in widgets:
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
51 self.add_widget(wid)
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
52
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
53 def change_widgets(self, widgets):
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
54 self.clear_widgets()
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
55 for wid in widgets:
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
56 self.add_widget(wid)
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 class CagouApp(App):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """Kivy App for Cagou"""
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def build(self):
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
63 return CagouRootWidget([ProfileManager(self.host)])
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 class Cagou(QuickApp):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 MB_HANDLE = False
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def __init__(self):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 super(Cagou, self).__init__(create_bridge=DBusBridgeFrontend, xmlui=xmlui)
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.app = CagouApp()
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.app.host = self
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
73 media_dir = self.app.media_dir = self.bridge.getConfig("", "media_dir")
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
74 self.app.default_avatar = os.path.join(media_dir, "misc/default_avatar.png")
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
75 self._plg_wids = [] # widget plugins
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
76 self._import_plugins()
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def run(self):
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.app.run()
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
81 def _defaultFactory(self, host, plugin_info, target, profiles):
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
82 """factory used to create widget instance when PLUGIN_INFO["factory"] is not set"""
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
83 main_cls = plugin_info['main']
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
84 return self.widgets.getOrCreateWidget(main_cls, target, on_new_widget=None, profiles=profiles)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
85
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
86 def _import_plugins(self):
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
87 """Import all plugins"""
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
88 self.default_wid = None
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
89 plugins_path = os.path.dirname(__file__)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
90 plug_lst = [os.path.splitext(p)[0] for p in map(os.path.basename, glob.glob(os.path.join(plugins_path, "plugin*.py")))]
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
91 imported_names = set() # use to avoid loading 2 times plugin with same import name
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
92 for plug in plug_lst:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
93 mod = import_module(plug)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
94 try:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
95 plugin_info = mod.PLUGIN_INFO
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
96 except AttributeError:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
97 plugin_info = {}
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
98
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
99 # import name is used to differentiate plugins
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
100 if 'import_name' not in plugin_info:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
101 plugin_info['import_name'] = plug
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
102 if 'import_name' in imported_names:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
103 log.warning(_(u"there is already a plugin named {}, ignoring new one").format(plugin_info['import_name']))
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
104 continue
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
105 if plugin_info['import_name'] == C.WID_SELECTOR:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
106 # if WidgetSelector exists, it will be our default widget
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
107 self.default_wid = plugin_info
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
108
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
109 # we want everything optional, so we use plugin file name
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
110 # if actual name is not found
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
111 if 'name' not in plugin_info:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
112 plugin_info['name'] = plug[plug.rfind('_')+1:]
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
113
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
114 # we need to load the kv file
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
115 if 'kv_file' not in plugin_info:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
116 plugin_info['kv_file'] = u'{}.kv'.format(plug)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
117 Builder.load_file(plugin_info['kv_file'])
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
118
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
119 # what is the main class ?
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
120 main_cls = getattr(mod, plugin_info['main'])
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
121 plugin_info['main'] = main_cls
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
122
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
123 # factory is used to create the instance
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
124 # if not found, we use a defaut one with getOrCreateWidget
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
125 if 'factory' not in plugin_info:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
126 plugin_info['factory'] = self._defaultFactory
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
127
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
128 self._plg_wids.append(plugin_info)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
129 if not self._plg_wids:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
130 log.error(_(u"no widget plugin found"))
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
131 return
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
132
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
133 # we want widgets sorted by names
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
134 self._plg_wids.sort(key=lambda p: p['name'].lower())
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
135
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
136 if self.default_wid is None:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
137 # we have no selector widget, we use the first widget as default
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
138 self.default_wid = self._plg_wids[0]
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
139
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
140 def getPluggedWidgets(self, except_cls=None):
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
141 """get available widgets plugin infos
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
142
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
143 @param except_cls(None, class): if not None,
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
144 widgets from this class will be excluded
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
145 @return (list[dict]): available widgets plugin infos
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
146 """
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
147 lst = self._plg_wids[:]
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
148 if except_cls is not None:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
149 for plugin_info in lst:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
150 if plugin_info['main'] == except_cls:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
151 lst.remove(plugin_info)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
152 break
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
153 return lst
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
154
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
155 def plugging_profiles(self):
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
156 self.app.root.change_widgets([WidgetsHandler(self)])
9
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
157
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
158 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
159 log.info(u"Profile presence status set to {show}/{status}".format(show=show, status=status))
7b0a53d2afd3 contact list: first draft
Goffi <goffi@goffi.org>
parents: 6
diff changeset
160
11
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
161 def switchWidget(self, old, new):
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
162 """Replace old widget by new one
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
163
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
164 old(CagouWidget): CagouWidget instance or a child
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
165 new(CagouWidget): new widget instance
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
166 """
14
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
167 to_change = None
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
168 if isinstance(old, CagouWidget):
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
169 to_change = old
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
170 else:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
171 for w in old.walk_reverse():
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
172 if isinstance(w, CagouWidget):
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
173 to_change = w
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
174 break
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
175
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
176 if to_change is None:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
177 log.error(u"no CagouWidget found when trying to switch widget")
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
178 else:
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
179 parent = to_change.parent
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
180 idx = parent.children.index(to_change)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
181 parent.remove_widget(to_change)
21a432afd06d plugin system, first draft:
Goffi <goffi@goffi.org>
parents: 13
diff changeset
182 parent.add_widget(new, index=idx)
11
49d30fc15884 core: added switchWidget method, to change a CagouWidget for an other one
Goffi <goffi@goffi.org>
parents: 9
diff changeset
183
0
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if __name__ == '__main__':
160cc95ad7ea initial commit:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 Cagou().run()