changeset 166:b318d2b58887

Primitivus: parameters management via XMLUI
author Goffi <goffi@goffi.org>
date Mon, 09 Aug 2010 21:39:41 +0800
parents 8a2053de6f8c
children 6fd053c99421
files frontends/primitivus/primitivus frontends/primitivus/xmlui.py tools/memory.py
diffstat 3 files changed, 66 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/primitivus/primitivus	Mon Aug 09 21:36:31 2010 +0800
+++ b/frontends/primitivus/primitivus	Mon Aug 09 21:39:41 2010 +0800
@@ -81,7 +81,7 @@
     
     def __init__(self):
         self.CM = QuickContactManagement() #FIXME: not the best place
-        QuickApp.__init__(self) 
+        QuickApp.__init__(self)
         
         ## main loop setup ##
         self.main_widget = ProfileManager(self)
@@ -183,6 +183,7 @@
         general = _("General")
         menu.addMenu(general, _("Connect"), self.onConnectRequest)
         menu.addMenu(general, _("Disconnect"), self.onDisconnectRequest)
+        menu.addMenu(general, _("Parameters"), self.onParam)
         menu.addMenu(general, _("About"), self.onAboutRequest)
         menu.addMenu(general, _("Exit"), self.onExitRequest, 'ctrl x')
         contact = _("Contact")
@@ -236,8 +237,28 @@
             self.notBar.addPopUp(pop_up_widget)
 
     def notify(self, message):
+        """"Notify message to user via notification bar"""
         self.notBar.addMessage(message)
 
+    def addWindow(self, widget):
+        """Display a window if possible,
+        else add it in the notification bar queue
+        @param widget: BoxWidget"""
+        assert(len(self.center_part.widget_list)<=2)
+        wid_idx = len(self.center_part.widget_list)-1
+        self.center_part.widget_list[wid_idx] = widget
+        self.menu_roller.removeMenu(_('Chat menu'))
+        self.contactList.unselectAll()
+        self.redraw()
+
+    def removeWindow(self):
+        """Remove window showed on the right column"""
+        #TODO: to a better Window management than this crappy hack
+        assert(len(self.center_part.widget_list)<=2)
+        wid_idx = len(self.center_part.widget_list)-1
+        self.center_part.widget_list[wid_idx] = urwid.Filler(urwid.Text(''))
+        self.redraw()
+    
     def contactSelected(self, contact_list):
         contact = contact_list.get_contact()
         if contact:
@@ -299,11 +320,7 @@
                 misc['target'] = data['target']
                 misc['action_back'] = self.bridge.gatewayRegister
             ui = XMLUI(self, title=title, xml_data = data['xml'], misc = misc)
-            assert(len(self.center_part.widget_list)==2)
-            self.center_part.widget_list[1] = ui
-            self.menu_roller.removeMenu(_('Chat menu'))
-            self.contactList.selected = None
-            self.redraw()
+            self.addWindow(ui)
         else:
             error (_("FIXME FIXME FIXME: type [%s] not implemented") % type)
             raise NotImplementedError
@@ -341,6 +358,10 @@
     def onDisconnectRequest(self, menu):
         self.bridge.disconnect(self.profile)
 
+    def onParam(self, menu):
+        params = XMLUI(self,xml_data=self.bridge.getParamsUI(self.profile))
+        self.addWindow(params)
+
     def onExitRequest(self, menu):
         raise urwid.ExitMainLoop()
 
--- 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()
--- a/tools/memory.py	Mon Aug 09 21:36:31 2010 +0800
+++ b/tools/memory.py	Mon Aug 09 21:39:41 2010 +0800
@@ -282,7 +282,7 @@
         return prof_xml
 
 
-    def getParamsUI(self, profile_key='@DEFAULT'):
+    def getParamsUI(self, profile_key='@DEFAULT@'):
         """Return a SàT XMLUI for parameters, with given profile"""
         profile = self.getProfileName(profile_key)
         if not profile: