diff frontends/primitivus/xmlui.py @ 166:b318d2b58887

Primitivus: parameters management via XMLUI
author Goffi <goffi@goffi.org>
date Mon, 09 Aug 2010 21:39:41 +0800
parents ae50b53ff868
children 6fd053c99421
line wrap: on
line diff
--- a/frontends/primitivus/xmlui.py	Mon Aug 09 21:36:31 2010 +0800
+++ b/frontends/primitivus/xmlui.py	Mon Aug 09 21:39:41 2010 +0800
@@ -100,6 +100,11 @@
             else:
                 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type)  #FIXME !
                 raise NotImplementedError
+            if self.type == 'param':
+                if isinstance(ctrl,urwid.Edit):
+                    urwid.connect_signal(ctrl,'change',self.onParamChange)
+                ctrl._param_category = self._current_category
+                ctrl._param_name = name
             parent.append(ctrl)
 
     def __parseChilds(self, current, elem, wanted = ['layout'], data = None):
@@ -129,6 +134,8 @@
                 name = node.getAttribute('name') 
                 if not name or not isinstance(data,custom_widgets.TabsContainer):
                     raise InvalidXMLUI 
+                if self.type == 'param':
+                    self._current_category = name #XXX: awful hack because params need category and we don't keep parent
                 tab_cont = data
                 listbox = tab_cont.addTab(name)
                 self.__parseChilds(listbox.body, node, ['layout'])
@@ -139,7 +146,7 @@
 
     def constructUI(self, xml_data):
         
-        list_box = urwid.ListBox(urwid.SimpleListWalker([]))
+        ret_wid = urwid.ListBox(urwid.SimpleListWalker([]))
         
         cat_dom = minidom.parseString(xml_data.encode('utf-8'))
         top=cat_dom.documentElement
@@ -149,11 +156,16 @@
         if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
             raise InvalidXMLUI
 
-        self.__parseChilds(list_box.body, cat_dom.documentElement)
+        if self.type == 'param':
+            self.param_changed = set()
+
+        self.__parseChilds(ret_wid.body, cat_dom.documentElement)
 
-        assert list_box.body
-        if isinstance(list_box.body[0],custom_widgets.TabsContainer):
-            return list_box.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox
+        assert ret_wid.body
+        
+        if isinstance(ret_wid.body[0],custom_widgets.TabsContainer):
+            ret_wid = urwid.Pile([ret_wid.body[0]]) #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox
+        
         
         if self.type == 'form':
             buttons = []
@@ -162,9 +174,18 @@
                 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled))
             max_len = max([len(button.get_label()) for button in buttons])
             grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center')
-            list_box.body.append(grid_wid)
+            ret_wid.body.append(grid_wid)
+        elif self.type == 'param':
+            assert(isinstance(ret_wid,urwid.Pile))
+            buttons = []
+            buttons.append(custom_widgets.CustomButton(_('Save'),self.onSaveParams))
+            buttons.append(custom_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow()))
+            max_len = max([button.getSize() for button in buttons])
+            grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
+            ret_wid.widget_list.append(urwid.Filler(grid_wid))
+            ret_wid.item_types.append(('weight',1))
         
-        return list_box
+        return ret_wid
 
     def show(self):
         """Show the constructed UI"""
@@ -188,6 +209,11 @@
         id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
         self.host.current_action_ids.add(id)
 
+    def onParamChange(self, widget, text):
+        """Called when type is param and a widget to save is modified"""
+        assert(self.type == "param")
+        self.param_changed.add(widget)
+
     def onFormSubmitted(self, button):
         data = []
         for ctrl_name in self.ctrl_list:
@@ -207,3 +233,8 @@
     
     def onFormCancelled(self, button):
         self.host.removePopUp()
+
+    def onSaveParams(self, button):
+        for ctrl in self.param_changed:
+            self.host.bridge.setParam(ctrl._param_name, ctrl.get_edit_text(), ctrl._param_category, profile_key = self.host.profile)
+        self.host.removeWindow()