changeset 1478:90130847a0a8

frontends, primitivus: JidsList XMLUI widget management /!\ urwid SàText must be updated
author Goffi <goffi@goffi.org>
date Thu, 20 Aug 2015 18:36:53 +0200
parents ac522c4dab0e
children 057f0714f27e
files frontends/src/primitivus/xmlui.py frontends/src/tools/xmlui.py
diffstat 2 files changed, 38 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/xmlui.py	Thu Aug 20 18:35:27 2015 +0200
+++ b/frontends/src/primitivus/xmlui.py	Thu Aug 20 18:36:53 2015 +0200
@@ -217,6 +217,27 @@
                     selected.append(value)
             self._xmluiSelectValues(selected)
 
+class PrimitivusJidsListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
+
+    def __init__(self, _xmlui_parent, jids, styles):
+        sat_widgets.List.__init__(self, options=jids+[''], # the empty field is here to add new jids if needed
+                                  option_type=lambda txt, align: sat_widgets.AdvancedEdit(edit_text=txt, align=align),
+                                  on_change=self._onChange)
+        self.delete=0
+
+    def _onChange(self, list_widget, jid_widget=None, text=None):
+        if jid_widget is not None:
+            if jid_widget != list_widget.contents[-1] and not text:
+                # if a field is empty, we delete the line (except for the last line)
+                list_widget.contents.remove(jid_widget)
+            elif jid_widget == list_widget.contents[-1] and text:
+                # we always want an empty field as last value to be able to add jids
+                list_widget.contents.append(sat_widgets.AdvancedEdit())
+
+    def _xmluiGetSelectedValues(self):
+        # XXX: there is not selection in this list, so we return all non empty values
+        return [jid_ for jid_ in self.getAllValues() if jid_]
+
 
 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents):
 
@@ -386,7 +407,7 @@
                 self.main_cont.body.append(grid_wid)
             elif self.type == 'param':
                 tabs_cont = self.main_cont.body[0].base_widget
-                assert(isinstance(tabs_cont,sat_widgets.TabsContainer))
+                assert isinstance(tabs_cont,sat_widgets.TabsContainer)
                 buttons = []
                 buttons.append(sat_widgets.CustomButton(_('Save'),self.onSaveParams))
                 buttons.append(sat_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow()))
@@ -422,7 +443,7 @@
         elif show_type == 'window':
             self.host.newWidget(self)
         else:
-            assert(False)
+           assert False
         self.host.redraw()
 
     def _xmluiClose(self):
--- a/frontends/src/tools/xmlui.py	Thu Aug 20 18:35:27 2015 +0200
+++ b/frontends/src/tools/xmlui.py	Thu Aug 20 18:36:53 2015 +0200
@@ -128,6 +128,11 @@
     pass
 
 
+class JidsListWidget(Widget):
+    """A widget able to show/choose one or several strings in a list"""
+    pass
+
+
 class Container(Widget):
     """Widget which can contain other ones with a specific layout"""
 
@@ -429,6 +434,11 @@
                     _selected = [option.getAttribute("value") for option in node.getElementsByTagName("option") if option.getAttribute('selected') == C.BOOL_TRUE]
                     ctrl = self.widget_factory.createListWidget(_xmlui_parent, _options, _selected, style)
                     self.ctrl_list[name] = ({'type': type_, 'control': ctrl})
+                elif type_ == "jids_list":
+                    style = []
+                    jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
+                    ctrl = self.widget_factory.createJidsListWidget(_xmlui_parent, jids, style)
+                    self.ctrl_list[name] = ({'type': type_, 'control': ctrl})
                 elif type_=="button":
                     callback_id = node.getAttribute("callback")
                     ctrl = self.widget_factory.createButtonWidget(_xmlui_parent, value, self.onButtonPress)
@@ -445,7 +455,7 @@
                         if not isinstance(ctrl, (EmptyWidget, TextWidget, LabelWidget, JidWidget)):
                             log.warning(_("No change listener on [%s]") % ctrl)
 
-                if type_ != 'text':
+                elif type_ != 'text':
                     callback = node.getAttribute("internal_callback") or None
                     if callback:
                         fields = [field.getAttribute('name') for field in node.getElementsByTagName("internal_field")]
@@ -499,7 +509,7 @@
 
         @param ctrl: widget modified
         """
-        assert(self.type == "param")
+        assert self.type == "param"
         self.param_changed.add(ctrl)
 
     def onAdvListSelect(self, ctrl):
@@ -574,8 +584,8 @@
 
         def groups_of_contact(source, target):
             """Select in target the groups of the contact which is selected in source."""
-            assert(isinstance(source, ListWidget))
-            assert(isinstance(target, ListWidget))
+            assert isinstance(source, ListWidget)
+            assert isinstance(target, ListWidget)
             try:
                 contact_jid_s = source._xmluiGetSelectedValues()[0]
             except IndexError:
@@ -649,7 +659,7 @@
 
         self.type must be param
         """
-        assert(self.type == 'param')
+        assert self.type == 'param'
         for ctrl in self.param_changed:
             if isinstance(ctrl, ListWidget):
                 value = u'\t'.join(ctrl._xmluiGetSelectedValues())