comparison frontends/src/tools/xmlui.py @ 796:46aa5ada61bf

core, frontends: XMLUI refactoring: - now there is a base XMLUI class. Frontends inherits from this class, and add their specific widgets/containers/behaviour - wix: param.py has been removed, as the same behaviour has been reimplemented through XMLUI - removed "misc" argument in XMLUI.__init__ - severals things are broken (gateway, directory search), following patches will fix them - core: elements names in xml_tools.dataForm2XMLUI now use a construction with category_name/param_name to avoid name conflicts (could happen with 2 parameters of the same name in differents categories).
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:02:35 +0100
parents frontends/src/primitivus/xmlui.py@bfabeedbf32e
children 9007bb133009
comparison
equal deleted inserted replaced
795:6625558371db 796:46aa5ada61bf
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SàT frontend tools
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 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 from sat.core.i18n import _
21 from sat_frontends.constants import Const
22 from logging import debug, info, warning, error
23
24
25 class InvalidXMLUI(Exception):
26 pass
27
28
29 def getText(node):
30 """Get child text nodes
31 @param node: dom Node
32 @return: joined unicode text of all nodes
33
34 """
35 data = []
36 for child in node.childNodes:
37 if child.nodeType == child.TEXT_NODE:
38 data.append(child.wholeText)
39 return u"".join(data)
40
41
42 class Widget(object):
43 """ base Widget """
44 pass
45
46
47 class EmptyWidget(Widget):
48 """ Just a placeholder widget """
49 pass
50
51
52 class TextWidget(Widget):
53 """ Non interactive text """
54 pass
55
56
57 class StringWidget(Widget):
58 """ Input widget with require a string
59 often called Edit in toolkits
60
61 """
62
63
64 class PasswordWidget(Widget):
65 """ Input widget with require a masked string
66
67 """
68
69
70 class TextBoxWidget(Widget):
71 """ Input widget with require a long, possibly multilines string
72 often called TextArea in toolkits
73
74 """
75
76
77 class BoolWidget(Widget):
78 """ Input widget with require a boolean value
79 often called CheckBox in toolkits
80
81 """
82
83
84 class ButtonWidget(Widget):
85 """ A clickable widget """
86
87
88 class ListWidget(Widget):
89 """ A widget able to show/choose one or several strings in a list """
90
91
92 class AdvancedListWidget(Widget):
93 pass #TODO
94
95 class Container(Widget):
96 """ Widget which can contain other ones with a specific layout """
97
98 @classmethod
99 def _xmluiAdapt(cls, instance):
100 """ Make cls as instance.__class__
101 cls must inherit from original instance class
102 Usefull when you get a class from UI toolkit
103
104 """
105 assert instance.__class__ in cls.__bases__
106 instance.__class__ = type(cls.__name__, cls.__bases__, dict(cls.__dict__))
107
108
109 class PairsContainer(Container):
110 """ Widgets are disposed in rows of two (usually label/input) """
111 pass
112
113
114 class TabsContainer(Container):
115 """ A container which several other containers in tabs
116 Often called Notebook in toolkits
117
118 """
119
120
121 class VerticalContainer(Container):
122 """ Widgets are disposed vertically """
123 pass
124
125
126 class XMLUI(object):
127 """ Base class to construct SàT XML User Interface
128 New frontends can inherite this class to easily implement XMLUI
129 @property widget_factory: factory to create frontend-specific widgets
130 @proporety dialog_factory: factory to create frontend-specific dialogs
131
132 """
133 widget_factory = None
134 dialog_factory = None # TODO
135
136 def __init__(self, host, xml_data, title = None, flags = None, dom_parse=None, dom_free=None):
137 """ Initialise the XMLUI instance
138 @param host: %(doc_host)s
139 @param xml_data: the raw XML containing the UI
140 @param title: force the title, or use XMLUI one if None
141 @param flags: list of string which can be:
142 - NO_CANCEL: the UI can't be cancelled
143 @param dom_parse: methode equivalent to minidom.parseString (but which must manage unicode), or None to use default one
144 @param dom_free: method used to free the parsed DOM
145
146 """
147 if dom_parse is None:
148 from xml.dom import minidom
149 self.dom_parse = lambda xml_data: minidom.parseString(xml_data.encode('utf-8'))
150 self.dom_free = lambda cat_dom: cat_dom.unlink()
151 else:
152 self.dom_parse = dom_parse
153 self.dom_free = dom_free or (lambda cat_dom: None)
154 self.host = host
155 self.title = title or ""
156 if flags is None:
157 flags = []
158 self.flags = flags
159 self.ctrl_list = {} # usefull to access ctrl
160 self.constructUI(xml_data)
161
162 def _parseElems(self, node, parent, post_treat=None):
163 """ Parse elements inside a <layout> tags, and add them to the parent
164 @param node: current XMLUI node
165 @param parent: parent container
166 @param post_treat: frontend specific treatments do to on each element
167
168 """
169 for elem in node.childNodes:
170 if elem.nodeName != "elem":
171 raise NotImplementedError(_('Unknown tag [%s]') % elem.nodeName)
172 id_ = elem.getAttribute("id")
173 name = elem.getAttribute("name")
174 type_ = elem.getAttribute("type")
175 value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
176 if type_=="empty":
177 ctrl = self.widget_factory.createEmptyWidget(parent)
178 elif type_=="text":
179 try:
180 value = elem.childNodes[0].wholeText
181 except IndexError:
182 warning (_("text node has no child !"))
183 ctrl = self.widget_factory.createTextWidget(parent, value)
184 elif type_=="label":
185 ctrl = self.widget_factory.createTextWidget(parent, value+": ")
186 elif type_=="string":
187 ctrl = self.widget_factory.createStringWidget(parent, value)
188 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
189 elif type_=="password":
190 ctrl = self.widget_factory.createPasswordWidget(parent, value)
191 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
192 elif type_=="textbox":
193 ctrl = self.widget_factory.createTextBoxWidget(parent, value)
194 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
195 elif type_=="bool":
196 ctrl = self.widget_factory.createBoolWidget(parent, value=='true')
197 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
198 elif type_=="list":
199 style=[] if elem.getAttribute("multi")=='yes' else ['single']
200 _options = [(option.getAttribute("value"), option.getAttribute("label")) for option in elem.getElementsByTagName("option")]
201 ctrl = self.widget_factory.createListWidget(parent, _options, style)
202 ctrl._xmluiSelectValue(elem.getAttribute("value"))
203 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
204 elif type_=="button":
205 callback_id = elem.getAttribute("callback")
206 ctrl = self.widget_factory.createButtonWidget(parent, value, self.onButtonPress)
207 ctrl._xmlui_param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
208 elif type_=="advanced_list":
209 _options = [getText(txt_elt) for txt_elt in elem.getElementsByTagName("text")]
210 ctrl = self.widget_factory.createListWidget(parent, _options, ['can_select_none'])
211 ctrl._xmluiSelectValue(elem.getAttribute("value"))
212 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
213 else:
214 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type_)
215 raise NotImplementedError(_("FIXME FIXME FIXME: type [%s] is not implemented") % type_)
216
217 if self.type == 'param':
218 try:
219 ctrl._xmluiOnChange(self.onParamChange)
220 ctrl._param_category = self._current_category
221 ctrl._param_name = name.split(Const.SAT_PARAM_SEPARATOR)[1]
222 except AttributeError:
223 if not isinstance(ctrl, (EmptyWidget, TextWidget)):
224 warning(_("No change listener on [%s]" % ctrl))
225
226 if post_treat is not None:
227 post_treat(ctrl, id_, name, type_, value)
228 parent._xmluiAppend(ctrl)
229
230 def _parseChilds(self, current, elem, wanted = ['layout'], data = None):
231 """ Recursively parse childNodes of an elemen
232 @param current: widget container with '_xmluiAppend' method
233 @param elem: element from which childs will be parsed
234 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant
235 @param data: additionnal data which are needed in some cases
236
237 """
238 for node in elem.childNodes:
239 if wanted and not node.nodeName in wanted:
240 raise InvalidXMLUI
241 if node.nodeName == "layout":
242 type_ = node.getAttribute('type')
243 if type_ == "tabs":
244 tab_cont = self.widget_factory.createTabsContainer(current)
245 self._parseChilds(current, node, ['category'], tab_cont)
246 current._xmluiAppend(tab_cont)
247 elif type_ == "vertical":
248 self._parseElems(node, current)
249 elif type_ == "pairs":
250 pairs = self.widget_factory.createPairsContainer(current)
251 self._parseElems(node, pairs)
252 current._xmluiAppend(pairs)
253 else:
254 warning(_("Unknown layout [%s], using default one") % type_)
255 self._parseElems(node, current)
256 elif node.nodeName == "category":
257 name = node.getAttribute('name')
258 label = node.getAttribute('label')
259 if not name or not isinstance(data, TabsContainer):
260 raise InvalidXMLUI
261 if self.type == 'param':
262 self._current_category = name #XXX: awful hack because params need category and we don't keep parent
263 tab_cont = data
264 new_tab = tab_cont._xmluiAddTab(label or name)
265 self._parseChilds(new_tab, node, ['layout'])
266 else:
267 raise NotImplementedError(_('Unknown tag'))
268
269 def constructUI(self, xml_data, post_treat=None):
270 """ Actually construct the UI
271 @param xml_data: raw XMLUI
272 @param post_treat: frontend specific treatments to do once the UI is constructed
273 @return: constructed widget
274 """
275 ret_wid = self.widget_factory.createVerticalContainer(self)
276
277 cat_dom = self.dom_parse(xml_data)
278 top=cat_dom.documentElement
279 self.type = top.getAttribute("type")
280 self.title = self.title or top.getAttribute("title") or u""
281 self.session_id = top.getAttribute("session_id") or None
282 self.submit_id = top.getAttribute("submit") or None
283 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
284 raise InvalidXMLUI
285
286 if self.type == 'param':
287 self.param_changed = set()
288
289 self._parseChilds(ret_wid, cat_dom.documentElement)
290
291 if post_treat is not None:
292 ret_wid = post_treat(ret_wid)
293
294 self.dom_free(cat_dom)
295
296 return ret_wid
297
298
299 def _xmluiClose(self):
300 """ Close the window/popup/... where the constructeur XMLUI is
301 this method must be overrided
302
303 """
304 raise NotImplementedError
305
306 ##EVENTS##
307
308 def onParamChange(self, ctrl):
309 """ Called when type is param and a widget to save is modified
310 @param ctrl: widget modified
311
312 """
313 assert(self.type == "param")
314 self.param_changed.add(ctrl)
315
316 def onButtonPress(self, button):
317 """ Called when an XMLUI button is clicked
318 Launch the action associated to the button
319 @param button: the button clicked
320
321 """
322 callback_id, fields = button._xmlui_param_id
323 data = {}
324 for field in fields:
325 ctrl = self.ctrl_list[field]
326 if isinstance(ctrl['control'], ListWidget):
327 data[field] = u'\t'.join(ctrl['control']._xmluiGetSelected())
328 else:
329 data[field] = ctrl['control']._xmluiGetValue()
330 self.host.launchAction(callback_id, data, profile_key = self.host.profile)
331
332 def onFormSubmitted(self, ignore=None):
333 """ An XMLUI form has been submited
334 call the submit action associated with this form
335
336 """
337 selected_values = []
338 for ctrl_name in self.ctrl_list:
339 escaped = u"%s%s" % (Const.SAT_FORM_PREFIX, ctrl_name)
340 ctrl = self.ctrl_list[ctrl_name]
341 if isinstance(ctrl['control'], ListWidget):
342 selected_values.append((escaped, u'\t'.join(ctrl['control']._xmluiGetSelectedValues())))
343 else:
344 selected_values.append((escaped, ctrl['control']._xmluiGetValue()))
345 if self.submit_id is not None:
346 data = dict(selected_values)
347 if self.session_id is not None:
348 data["session_id"] = self.session_id
349 self.host.launchAction(self.submit_id, data, profile_key=self.host.profile)
350
351 else:
352 warning (_("The form data is not sent back, the type is not managed properly"))
353 self._xmluiClose()
354
355 def onFormCancelled(self, ignore=None):
356 """ Called when a form is cancelled """
357 debug(_("Cancelling form"))
358 self._xmluiClose()
359
360
361 def onSaveParams(self, ignore=None):
362 """ Params are saved, we send them to backend
363 self.type must be param
364
365 """
366 assert(self.type == 'param')
367 for ctrl in self.param_changed:
368 if isinstance(ctrl, ListWidget):
369 value = u'\t'.join(ctrl._xmluiGetSelectedValues())
370 else:
371 value = ctrl._xmluiGetValue()
372 self.host.bridge.setParam(ctrl._param_name, value, ctrl._param_category,
373 profile_key=self.host.profile)
374 self._xmluiClose()