changeset 655:56f8a9c99194

core, primitivus: better support for parameter of type list
author souliane <souliane@mailoo.org>
date Wed, 02 Oct 2013 17:38:29 +0200
parents 5c5cf5bca240
children 7d6e5807504a
files frontends/src/primitivus/xmlui.py src/tools/xml_tools.py
diffstat 2 files changed, 30 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Thu Sep 26 10:44:57 2013 +0200
+++ b/frontends/src/primitivus/xmlui.py	Wed Oct 02 17:38:29 2013 +0200
@@ -105,22 +105,29 @@
                 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
             elif type_=="list":
                 style=[] if elem.getAttribute("multi")=='yes' else ['single']
-                ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style)
+                ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style, on_change=self.onParamChange)
+                ctrl.selectValue(elem.getAttribute("value"))
                 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
             elif type_=="button":
                 callback_id = elem.getAttribute("callback_id")
                 ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress)
                 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
             elif type_=="advanced_list":
-                ctrl = sat_widgets.List(options=[getText(txt_elt) for txt_elt in elem.getElementsByTagName("text")], style=['can_select_none'], max_height=20)
-
+                ctrl = sat_widgets.List(options=[getText(txt_elt) for txt_elt in elem.getElementsByTagName("text")], style=['can_select_none'], max_height=20, on_change=self.onParamChange)
+                ctrl.selectValue(elem.getAttribute("value"))
                 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
             else:
                 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type_)  #FIXME !
                 raise NotImplementedError
             if self.type == 'param':
-                if isinstance(ctrl,urwid.Edit) or isinstance(ctrl,urwid.CheckBox):
+                if isinstance(ctrl, urwid.Edit) or isinstance(ctrl, urwid.CheckBox):
                     urwid.connect_signal(ctrl,'change',self.onParamChange)
+                elif isinstance(ctrl, sat_widgets.List):
+                    # the GenericList member triggers the events, not List itself
+                    # TODO: create a method GenericList.setParamData() for that,
+                    # or later in onSaveParams do something like ctrl.getParent()
+                    ctrl.genericList._param_category = self._current_category
+                    ctrl.genericList._param_name = name
                 ctrl._param_category = self._current_category
                 ctrl._param_name = name
             parent.append(ctrl)
@@ -228,7 +235,7 @@
         for field in fields:
             ctrl = self.ctrl_list[field]
             if isinstance(ctrl['control'],sat_widgets.List):
-                data[field] = '\t'.join(ctrl['control'].getSelectedValues())
+                data[field] = u'\t'.join(ctrl['control'].getSelectedValues())
             else:
                 data[field] = ctrl['control'].getValue()
 
@@ -245,7 +252,7 @@
         for ctrl_name in self.ctrl_list:
             ctrl = self.ctrl_list[ctrl_name]
             if isinstance(ctrl['control'], sat_widgets.List):
-                data.append((ctrl_name, ctrl['control'].getSelectedValue()))
+                data.append((ctrl_name, u'\t'.join(ctrl['control'].getSelectedValues())))
             elif isinstance(ctrl['control'], urwid.CheckBox):
                 data.append((ctrl_name, "true" if ctrl['control'].get_state() else "false"))
             else:
@@ -273,6 +280,8 @@
         for ctrl in self.param_changed:
             if isinstance(ctrl, urwid.CheckBox):
                 value = "true" if ctrl.get_state() else "false"
+            elif isinstance(ctrl, sat_widgets.GenericList):
+                value = u'\t'.join(ctrl.getSelectedValues())
             else:
                 value = ctrl.get_edit_text()
             self.host.bridge.setParam(ctrl._param_name, value, ctrl._param_category,
--- a/src/tools/xml_tools.py	Thu Sep 26 10:44:57 2013 +0200
+++ b/src/tools/xml_tools.py	Wed Oct 02 17:38:29 2013 +0200
@@ -148,16 +148,30 @@
                 assert(False)
             type_ = param.getAttribute('type')
             value = param.getAttribute('value') or None
+            options = getOptions(param)
             callback_id = param.getAttribute('callback_id') or None
             if type_ == "button":
                 param_ui.addEmpty()
             else:
                 param_ui.addLabel(label or name)
-            param_ui.addElement(name=name, type_=type_, value=value, callback_id=callback_id)
+            param_ui.addElement(name=name, type_=type_, value=value, options=options, callback_id=callback_id)
 
     return param_ui.toXml()
 
 
+def getOptions(param):
+    """Retrieve the options for list element. Allow listing the <option/>
+    tags directly in <param/> or in an intermediate <options/> tag."""
+    elems = param.getElementsByTagName("options")
+    if len(elems) == 0:
+        elems = param.getElementsByTagName("option")
+    else:
+        elems = elems.item(0).getElementsByTagName("option")
+    if len(elems) == 0:
+        return []
+    return [elem.getAttribute("value") for elem in elems]
+
+
 class Header(object):
     """AdvandeList's header"""