diff frontends/src/primitivus/xmlui.py @ 976:68faf7d77a42

frontends (xmlui): add setter methods + fixes: - add _xmluiSetValue for all widgets + _xmluiAddValues for ListWidget - fix popup dialog OK button + _xmluiOnClick for ButtonWidget
author souliane <souliane@mailoo.org>
date Thu, 03 Apr 2014 14:49:05 +0200
parents b37b1d183ac3
children 73a0b7f94674
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Thu Apr 03 14:38:48 2014 +0200
+++ b/frontends/src/primitivus/xmlui.py	Thu Apr 03 14:49:05 2014 +0200
@@ -19,6 +19,7 @@
 
 from sat.core.i18n import _
 import urwid
+import copy
 from urwid_satext import sat_widgets
 from logging import debug, info, warning, error
 from xml.dom import minidom
@@ -78,11 +79,15 @@
 
         urwid.Divider.__init__(self, div_char)
 
+
 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
 
     def __init__(self, parent, value):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
 
+    def _xmluiSetValue(self, value):
+        self.set_edit_text(value)
+
     def _xmluiGetValue(self):
         return self.get_edit_text()
 
@@ -92,6 +97,9 @@
     def __init__(self, parent, value):
         sat_widgets.Password.__init__(self, edit_text=value)
 
+    def _xmluiSetValue(self, value):
+        self.set_edit_text(value)
+
     def _xmluiGetValue(self):
         return self.get_edit_text()
 
@@ -101,6 +109,9 @@
     def __init__(self, parent, value):
         sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True)
 
+    def _xmluiSetValue(self, value):
+        self.set_edit_text(value)
+
     def _xmluiGetValue(self):
         return self.get_edit_text()
 
@@ -110,6 +121,9 @@
     def __init__(self, parent, state):
         urwid.CheckBox.__init__(self, '', state=state)
 
+    def _xmluiSetValue(self, value):
+        self.set_state(value == "true")
+
     def _xmluiGetValue(self):
         return "true" if self.get_state() else "false"
 
@@ -119,6 +133,9 @@
     def __init__(self, parent, value, click_callback):
         sat_widgets.CustomButton.__init__(self, value, on_press=click_callback)
 
+    def _xmluiOnClick(self, callback):
+            urwid.connect_signal(self, 'click', callback)
+
 
 class PrimitivusListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
 
@@ -135,6 +152,21 @@
     def _xmluiGetSelectedValues(self):
         return [option.value for option in self.getSelectedValues()]
 
+    def _xmluiAddValues(self, values, select=True):
+        current_values = self.getAllValues()
+        new_values = copy.deepcopy(current_values)
+        for value in values:
+            if value not in current_values:
+                new_values.append(value)
+        if select:
+            selected = self._xmluiGetSelectedValues()
+        self.changeValues(new_values)
+        if select:
+            for value in values:
+                if value not in selected:
+                    selected.append(value)
+            self._xmluiSelectValues(selected)
+
 
 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents):
 
@@ -160,6 +192,7 @@
         """ Call callback with widget as only argument """
         urwid.connect_signal(self, 'click', self._event_callback, callback)
 
+
 class PrimitivusPairsContainer(xmlui.PairsContainer, sat_widgets.TableContainer):
 
     def __init__(self, parent):
@@ -233,13 +266,16 @@
         def postTreat():
             assert self.main_cont.body
 
-            if self.type == 'form':
+            if self.type in ('form', 'popup'):
                 buttons = []
-                buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted))
-                if not 'NO_CANCEL' in self.flags:
-                    buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled))
+                if self.type == 'form':
+                    buttons.append(urwid.Button(_('Submit'), self.onFormSubmitted))
+                    if not 'NO_CANCEL' in self.flags:
+                        buttons.append(urwid.Button(_('Cancel'), self.onFormCancelled))
+                else:
+                    buttons.append(urwid.Button(_('OK'), lambda dummy: self._xmluiClose()))
                 max_len = max([len(button.get_label()) for button in buttons])
-                grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center')
+                grid_wid = urwid.GridFlow(buttons, max_len + 4, 1, 0, 'center')
                 self.main_cont.body.append(grid_wid)
             elif self.type == 'param':
                 tabs_cont = self.main_cont.body[0].base_widget