Mercurial > libervia-backend
comparison frontends/src/primitivus/xmlui.py @ 223:86d249b6d9b7
Files reorganisation
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 29 Dec 2010 01:06:29 +0100 |
parents | frontends/primitivus/xmlui.py@3198bfd66daa |
children | fd9b7834d98a |
comparison
equal
deleted
inserted
replaced
222:3198bfd66daa | 223:86d249b6d9b7 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
23 from logging import debug, info, warning, error | |
24 from urwid_satext import sat_widgets | |
25 from xml.dom import minidom | |
26 | |
27 class Pairs(urwid.WidgetWrap): | |
28 | |
29 def __init__(self, weight_0='1', weight_1='1'): | |
30 self.idx = 0 | |
31 self.weight_0 = weight_0 | |
32 self.weight_1 = weight_1 | |
33 columns = urwid.Columns([urwid.Text(''), urwid.Text('')]) | |
34 #XXX: empty Text hack needed because Pile doesn't support empty list | |
35 urwid.WidgetWrap.__init__(self,columns) | |
36 | |
37 def append(self, widget): | |
38 pile = self._w.widget_list[self.idx] | |
39 if isinstance(pile, urwid.Text): | |
40 self._w.widget_list[self.idx] = urwid.Pile([widget]) | |
41 if self.idx == 1: | |
42 self._w.set_focus(1) | |
43 else: | |
44 pile.widget_list.append(widget) | |
45 pile.item_types.append(('weight',getattr(self,'weight_'+str(self.idx)))) | |
46 self.idx = (self.idx + 1) % 2 | |
47 | |
48 class InvalidXMLUI(Exception): | |
49 pass | |
50 | |
51 class XMLUI(urwid.WidgetWrap): | |
52 | |
53 def __init__(self, host, xml_data, title = None, options = [], misc={}): | |
54 self.host = host | |
55 self.title = title | |
56 self.options = options | |
57 self.misc = misc | |
58 self.__dest = "window" | |
59 self.ctrl_list = {} # usefull to access ctrl | |
60 widget = self.constructUI(xml_data) | |
61 urwid.WidgetWrap.__init__(self,widget) | |
62 | |
63 def __parseElems(self, node, parent): | |
64 """Parse elements inside a <layout> tags, and add them to the parent""" | |
65 for elem in node.childNodes: | |
66 if elem.nodeName != "elem": | |
67 message=_("Unmanaged tag") | |
68 error(message) | |
69 raise Exception(message) | |
70 id = elem.getAttribute("id") | |
71 name = elem.getAttribute("name") | |
72 type = elem.getAttribute("type") | |
73 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' | |
74 if type=="empty": | |
75 ctrl = urwid.Text('') | |
76 elif type=="text": | |
77 try: | |
78 value = elem.childNodes[0].wholeText | |
79 except IndexError: | |
80 warning (_("text node has no child !")) | |
81 ctrl = urwid.Text(value) | |
82 elif type=="label": | |
83 ctrl = urwid.Text(value+": ") | |
84 elif type=="string": | |
85 ctrl = sat_widgets.AdvancedEdit(edit_text = value) | |
86 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
87 elif type=="password": | |
88 ctrl = sat_widgets.Password(edit_text = value) | |
89 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
90 elif type=="textbox": | |
91 ctrl = sat_widgets.AdvancedEdit(edit_text = value, multiline=True) | |
92 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
93 elif type=="bool": | |
94 ctrl = urwid.CheckBox('', state = value=="true") | |
95 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
96 elif type=="list": | |
97 style=[] if elem.getAttribute("multi")=='yes' else ['single'] | |
98 ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style) | |
99 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
100 elif type=="button": | |
101 callback_id = elem.getAttribute("callback_id") | |
102 ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress) | |
103 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) | |
104 else: | |
105 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! | |
106 raise NotImplementedError | |
107 if self.type == 'param': | |
108 if isinstance(ctrl,urwid.Edit) or isinstance(ctrl,urwid.CheckBox): | |
109 urwid.connect_signal(ctrl,'change',self.onParamChange) | |
110 ctrl._param_category = self._current_category | |
111 ctrl._param_name = name | |
112 parent.append(ctrl) | |
113 | |
114 def __parseChilds(self, current, elem, wanted = ['layout'], data = None): | |
115 """Recursively parse childNodes of an elemen | |
116 @param current: widget container with 'append' method | |
117 @param elem: element from which childs will be parsed | |
118 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant""" | |
119 for node in elem.childNodes: | |
120 if wanted and not node.nodeName in wanted: | |
121 raise InvalidXMLUI | |
122 if node.nodeName == "layout": | |
123 type = node.getAttribute('type') | |
124 if type == "tabs": | |
125 tab_cont = sat_widgets.TabsContainer() | |
126 self.__parseChilds(current, node, ['category'], tab_cont) | |
127 current.append(tab_cont) | |
128 elif type == "vertical": | |
129 self.__parseElems(node, current) | |
130 elif type == "pairs": | |
131 pairs = Pairs() | |
132 self.__parseElems(node, pairs) | |
133 current.append(pairs) | |
134 else: | |
135 warning(_("Unknown layout, using default one")) | |
136 self.__parseElems(node, current) | |
137 elif node.nodeName == "category": | |
138 name = node.getAttribute('name') | |
139 label = node.getAttribute('label') | |
140 if not name or not isinstance(data,sat_widgets.TabsContainer): | |
141 raise InvalidXMLUI | |
142 if self.type == 'param': | |
143 self._current_category = name #XXX: awful hack because params need category and we don't keep parent | |
144 tab_cont = data | |
145 listbox = tab_cont.addTab(label or name) | |
146 self.__parseChilds(listbox.body, node, ['layout']) | |
147 else: | |
148 message=_("Unknown tag") | |
149 error(message) | |
150 raise NotImplementedError | |
151 | |
152 def constructUI(self, xml_data): | |
153 | |
154 ret_wid = urwid.ListBox(urwid.SimpleListWalker([])) | |
155 | |
156 cat_dom = minidom.parseString(xml_data.encode('utf-8')) | |
157 top=cat_dom.documentElement | |
158 self.type = top.getAttribute("type") | |
159 self.title = top.getAttribute("title") or self.title | |
160 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: | |
161 raise InvalidXMLUI | |
162 | |
163 if self.type == 'param': | |
164 self.param_changed = set() | |
165 | |
166 self.__parseChilds(ret_wid.body, cat_dom.documentElement) | |
167 | |
168 assert ret_wid.body | |
169 | |
170 if isinstance(ret_wid.body[0],sat_widgets.TabsContainer): | |
171 ret_wid = ret_wid.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox | |
172 | |
173 | |
174 if self.type == 'form': | |
175 buttons = [] | |
176 buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted)) | |
177 if not 'NO_CANCEL' in self.options: | |
178 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled)) | |
179 max_len = max([len(button.get_label()) for button in buttons]) | |
180 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center') | |
181 ret_wid.body.append(grid_wid) | |
182 elif self.type == 'param': | |
183 assert(isinstance(ret_wid,sat_widgets.TabsContainer)) | |
184 buttons = [] | |
185 buttons.append(sat_widgets.CustomButton(_('Save'),self.onSaveParams)) | |
186 buttons.append(sat_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow())) | |
187 max_len = max([button.getSize() for button in buttons]) | |
188 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center') | |
189 ret_wid.addFooter(grid_wid) | |
190 return ret_wid | |
191 | |
192 def show(self,show_type = 'popup'): | |
193 """Show the constructed UI | |
194 @param show_type: how to show the UI: | |
195 - popup | |
196 - window""" | |
197 self.__dest = "popup" | |
198 decorated = sat_widgets.LabelLine(self, sat_widgets.SurroundedText(self.title or '')) | |
199 if show_type == 'popup': | |
200 self.host.showPopUp(decorated) | |
201 elif show_type == 'window': | |
202 self.host.addWindow(decorated) | |
203 else: | |
204 error(_('INTERNAL ERROR: Unmanaged show_type (%s)') % show_type) | |
205 assert(False) | |
206 self.host.redraw() | |
207 | |
208 | |
209 ##EVENTS## | |
210 | |
211 def onButtonPress(self, button): | |
212 callback_id, fields = button.param_id | |
213 data = {"callback_id":callback_id} | |
214 for field in fields: | |
215 ctrl = self.ctrl_list[field] | |
216 if isinstance(ctrl['control'],sat_widgets.List): | |
217 data[field] = '\t'.join(ctrl['control'].getSelectedValues()) | |
218 else: | |
219 data[field] = ctrl['control'].getValue() | |
220 | |
221 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile) | |
222 self.host.current_action_ids.add(id) | |
223 | |
224 def onParamChange(self, widget, extra=None): | |
225 """Called when type is param and a widget to save is modified""" | |
226 assert(self.type == "param") | |
227 self.param_changed.add(widget) | |
228 | |
229 def onFormSubmitted(self, button): | |
230 data = [] | |
231 for ctrl_name in self.ctrl_list: | |
232 ctrl = self.ctrl_list[ctrl_name] | |
233 if isinstance(ctrl['control'], sat_widgets.List): | |
234 data.append((ctrl_name, ctrl['control'].getSelectedValue())) | |
235 elif isinstance(ctrl['control'], urwid.CheckBox): | |
236 data.append((ctrl_name, "true" if ctrl['control'].get_state() else "false")) | |
237 else: | |
238 data.append((ctrl_name, ctrl['control'].get_edit_text())) | |
239 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned | |
240 raise NotImplementedError | |
241 self.host.debug() | |
242 elif self.misc.has_key('callback'): | |
243 self.misc['callback'](data) | |
244 else: | |
245 warning (_("The form data is not sent back, the type is not managed properly")) | |
246 self.host.removePopUp() | |
247 | |
248 def onFormCancelled(self, button): | |
249 if self.__dest == 'window': | |
250 self.host.removeWindow() | |
251 else: | |
252 self.host.removePopUp() | |
253 | |
254 def onSaveParams(self, button): | |
255 for ctrl in self.param_changed: | |
256 if isinstance(ctrl, urwid.CheckBox): | |
257 value = "true" if ctrl.get_state() else "false" | |
258 else: | |
259 value = ctrl.get_edit_text() | |
260 self.host.bridge.setParam(ctrl._param_name, value, ctrl._param_category, profile_key = self.host.profile) | |
261 self.host.removeWindow() |