diff frontends/src/tools/xmlui.py @ 805:7c05c39156a2

core (XMLUI), frontends: advancedListContainer part 2:
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:21:51 +0100
parents 5174657b3378
children d035c662b357
line wrap: on
line diff
--- a/frontends/src/tools/xmlui.py	Tue Feb 04 18:19:32 2014 +0100
+++ b/frontends/src/tools/xmlui.py	Tue Feb 04 18:21:51 2014 +0100
@@ -177,6 +177,10 @@
         self._main_cont = None
         self.constructUI(xml_data)
 
+    def escape(self, name):
+        """ return escaped name for forms """
+        return u"%s%s" % (Const.SAT_FORM_PREFIX, name)
+
     @property
     def main_cont(self):
         return self._main_cont
@@ -220,8 +224,18 @@
                         columns = int(node.getAttribute('columns'))
                     except (TypeError, ValueError):
                         raise DataError("Invalid columns")
-                    cont = self.widget_factory.createAdvancedListContainer(parent, columns)
-                    self._parseChilds(cont, node, ('row',))
+                    selectable = node.getAttribute('selectable') or 'no'
+                    auto_index = node.getAttribute('auto_index') == 'true'
+                    data = {'index': 0} if auto_index else None
+                    cont = self.widget_factory.createAdvancedListContainer(parent, columns, selectable)
+                    callback_id = node.getAttribute("callback") or None
+                    if callback_id is not None:
+                        if selectable == 'no':
+                            raise ValueError("can't have selectable=='no' and callback_id at the same time")
+                        cont._xmlui_callback_id = callback_id
+                        cont._xmluiOnSelect(self.onAdvListSelect)
+
+                    self._parseChilds(cont, node, ('row',), data)
                 else:
                     warning(_("Unknown container [%s], using default one") % type_)
                     cont = self.widget_factory.createVerticalContainer(parent)
@@ -246,7 +260,12 @@
                 self._parseChilds(new_tab, node, ('widget', 'container'))
 
             elif node.nodeName == 'row':
-                parent._xmluiAddRow()
+                try:
+                    index = str(data['index'])
+                    data['index'] += 1
+                except TypeError:
+                    index = node.getAttribute('index') or None
+                parent._xmluiAddRow(index)
                 self._parseChilds(parent, node, ('widget', 'container'))
 
             elif node.nodeName == "widget":
@@ -299,11 +318,11 @@
                     try:
                         ctrl._xmluiOnChange(self.onParamChange)
                         ctrl._param_category = self._current_category
-                        ctrl._param_name = name.split(Const.SAT_PARAM_SEPARATOR)[1]
                     except AttributeError:
                         if not isinstance(ctrl, (EmptyWidget, TextWidget)):
                             warning(_("No change listener on [%s]" % ctrl))
 
+                ctrl._xmlui_name = name
                 parent._xmluiAppend(ctrl)
 
             else:
@@ -351,6 +370,25 @@
         assert(self.type == "param")
         self.param_changed.add(ctrl)
 
+    def onAdvListSelect(self, ctrl):
+        data = {}
+        widgets = ctrl._xmluiGetSelectedWidgets()
+        for wid in widgets:
+            try:
+                name = self.escape(wid._xmlui_name)
+                value = wid._xmluiGetValue()
+                ret[name] = value
+            except AttributeError:
+                pass
+        idx = ctrl._xmluiGetSelectedIndex()
+        if idx is not None:
+            data['index'] = idx
+        callback_id = ctrl._xmlui_callback_id
+        if callback_id is None:
+            warning(_("No callback_id found"))
+            return
+        self.host.launchAction(callback_id, data, profile_key = self.host.profile)
+
     def onButtonPress(self, button):
         """ Called when an XMLUI button is clicked
         Launch the action associated to the button
@@ -360,11 +398,12 @@
         callback_id, fields = button._xmlui_param_id
         data = {}
         for field in fields:
+            escaped = self.escape(field)
             ctrl = self.ctrl_list[field]
             if isinstance(ctrl['control'], ListWidget):
-                data[field] = u'\t'.join(ctrl['control']._xmluiGetSelected())
+                data[escaped] = u'\t'.join(ctrl['control']._xmluiGetSelected())
             else:
-                data[field] = ctrl['control']._xmluiGetValue()
+                data[escaped] = ctrl['control']._xmluiGetValue()
         self.host.launchAction(callback_id, data, profile_key = self.host.profile)
 
     def onFormSubmitted(self, ignore=None):
@@ -374,7 +413,7 @@
         """
         selected_values = []
         for ctrl_name in self.ctrl_list:
-            escaped = u"%s%s" % (Const.SAT_FORM_PREFIX, ctrl_name)
+            escaped = self.escape(ctrl_name)
             ctrl = self.ctrl_list[ctrl_name]
             if isinstance(ctrl['control'], ListWidget):
                 selected_values.append((escaped, u'\t'.join(ctrl['control']._xmluiGetSelectedValues())))
@@ -407,6 +446,7 @@
                 value = u'\t'.join(ctrl._xmluiGetSelectedValues())
             else:
                 value = ctrl._xmluiGetValue()
-            self.host.bridge.setParam(ctrl._param_name, value, ctrl._param_category,
+            param_name = ctr._xmlui_name.split(Const.SAT_PARAM_SEPARATOR)[1]
+            self.host.bridge.setParam(param_name, value, ctrl._param_category,
                                       profile_key=self.host.profile)
         self._xmluiClose()