comparison src/tools/xml_tools.py @ 968:75f3b3b430ff

tools, frontends, memory: param definition and XMLUI handle multi-selection for list widgets: - we need to update urwid_satext to revision 79 - no more "value" attribute value in the "list" element, use HTML-style "select" attribute in the "option" elements instead - /!\ param saving do not handle multiple values yet!
author souliane <souliane@mailoo.org>
date Tue, 01 Apr 2014 21:21:13 +0200
parents 9754c0ebadba
children 5c7707c958d8
comparison
equal deleted inserted replaced
967:242bd4fc654c 968:75f3b3b430ff
205 205
206 value = param.getAttribute('value') or None 206 value = param.getAttribute('value') or None
207 callback_id = param.getAttribute('callback_id') or None 207 callback_id = param.getAttribute('callback_id') or None
208 208
209 if type_ == 'list': 209 if type_ == 'list':
210 options = _getParamListOptions(param) 210 options, selected = _getParamListOptions(param)
211 widget_kwargs['options'] = options 211 widget_kwargs['options'] = options
212 widget_kwargs['selected'] = selected
212 213
213 if type_ in ("button", "text"): 214 if type_ in ("button", "text"):
214 param_ui.addEmpty() 215 param_ui.addEmpty()
215 value = param_label 216 value = param_label
216 else: 217 else:
228 229
229 return param_ui.toXml() 230 return param_ui.toXml()
230 231
231 232
232 def _getParamListOptions(param): 233 def _getParamListOptions(param):
233 """Retrieve the options for list element. Allow listing the <option/> 234 """Retrieve the options for list element. The <option/> tags
234 tags directly in <param/> or in an intermediate <options/> tag.""" 235 must be direct children of <param/>."""
235 elems = param.getElementsByTagName("options") 236 if len(param.getElementsByTagName("options")) > 0:
236 if len(elems) == 0: 237 raise exceptions.DataError(_("The 'options' tag is not allowed in parameter of type 'list'!"))
237 elems = param.getElementsByTagName("option") 238 elems = param.getElementsByTagName("option")
238 else:
239 elems = elems.item(0).getElementsByTagName("option")
240 if len(elems) == 0: 239 if len(elems) == 0:
241 return [] 240 return []
242 return [elem.getAttribute("value") for elem in elems] 241 options = [elem.getAttribute("value") for elem in elems]
242 selected = [elem.getAttribute("value") for elem in elems if elem.getAttribute("selected") == 'true']
243 return (options, selected)
243 244
244 245
245 ## XMLUI Elements 246 ## XMLUI Elements
246 247
247 248
300 301
301 class OptionElement(Element): 302 class OptionElement(Element):
302 """" Used by ListWidget to specify options """ 303 """" Used by ListWidget to specify options """
303 type = 'option' 304 type = 'option'
304 305
305 def __init__(self, parent, option): 306 def __init__(self, parent, option, selected=False):
307 """
308
309 @param parent
310 @param option (string, tuple)
311 @param selected (boolean)
312 """
306 assert(isinstance(parent, ListWidget)) 313 assert(isinstance(parent, ListWidget))
307 super(OptionElement, self).__init__(parent.xmlui, parent) 314 super(OptionElement, self).__init__(parent.xmlui, parent)
308 if isinstance(option, basestring): 315 if isinstance(option, basestring):
309 value, label = option, option 316 value, label = option, option
310 elif isinstance(option, tuple): 317 elif isinstance(option, tuple):
311 value, label = option 318 value, label = option
312 self.elem.setAttribute('value', value) 319 self.elem.setAttribute('value', value)
313 self.elem.setAttribute('label', label) 320 self.elem.setAttribute('label', label)
321 if selected:
322 self.elem.setAttribute('selected', 'true')
314 323
315 324
316 class RowElement(Element): 325 class RowElement(Element):
317 """" Used by AdvancedListContainer """ 326 """" Used by AdvancedListContainer """
318 type = 'row' 327 type = 'row'
607 super(ButtonWidget, self).__init__(xmlui, name, parent) 616 super(ButtonWidget, self).__init__(xmlui, name, parent)
608 self.elem.setAttribute('callback', callback_id) 617 self.elem.setAttribute('callback', callback_id)
609 if value: 618 if value:
610 self.elem.setAttribute('value', value) 619 self.elem.setAttribute('value', value)
611 for field in fields_back: 620 for field in fields_back:
612 fback_el = FieldBackElement(self, field) 621 FieldBackElement(self, field)
613 622
614 623
615 class ListWidget(InputWidget): 624 class ListWidget(InputWidget):
616 type = 'list' 625 type = 'list'
617 626
618 def __init__(self, xmlui, options, value=None, style=None, name=None, parent=None): 627 def __init__(self, xmlui, options, selected=None, style=None, name=None, parent=None):
628 """
629
630 @param xmlui
631 @param options (list[string, tuple]): each option can be given as:
632 - a string if the label and the value are the same
633 - a couple if the label and the value differ
634 @param selected (list[string]): list of the selected values
635 @param style (string)
636 @param name (string)
637 @param parent
638 """
619 if style is None: 639 if style is None:
620 style = set() 640 style = set()
621 styles = set(style) 641 styles = set(style)
622 if not options: 642 if not options:
623 warning(_('empty "options" list')) 643 warning(_('empty "options" list'))
624 if not styles.issubset(['multi']): 644 if not styles.issubset(['multi']):
625 raise exceptions.DataError(_("invalid styles")) 645 raise exceptions.DataError(_("invalid styles"))
626 super(ListWidget, self).__init__(xmlui, name, parent) 646 super(ListWidget, self).__init__(xmlui, name, parent)
627 self.addOptions(options) 647 self.addOptions(options, selected)
628 if value:
629 self.elem.setAttribute('value', value)
630 for style in styles: 648 for style in styles:
631 self.elem.setAttribute(style, 'yes') 649 self.elem.setAttribute(style, 'yes')
632 650
633 def addOptions(self, options): 651 def addOptions(self, options, selected=None):
634 """i Add options to a multi-values element (e.g. list) """ 652 """Add options to a multi-values element (e.g. list) """
653 if selected:
654 if isinstance(selected, basestring):
655 selected = [selected]
656 else:
657 selected = []
635 for option in options: 658 for option in options:
636 OptionElement(self, option) 659 OptionElement(self, option, option in selected)
637 660
638 661
639 ## XMLUI main class 662 ## XMLUI main class
640 663
641 664