changeset 1084:03dcb6ca7e49

core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
author Goffi <goffi@goffi.org>
date Wed, 25 Jun 2014 14:01:55 +0200
parents e8731b02f5ea
children 7a39ae3950f7
files src/tools/xml_tools.py
diffstat 1 files changed, 39 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Mon Jun 23 16:07:13 2014 +0200
+++ b/src/tools/xml_tools.py	Wed Jun 25 14:01:55 2014 +0200
@@ -34,10 +34,11 @@
 
 # Helper functions
 
-def _dataFormField2XMLUIData(field):
+def _dataFormField2XMLUIData(field, read_only=False):
     """ Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field
     field can be modified (if it's fixed and it has no value)
     @param field: data_form.Field (it uses field.value, field.fieldType, field.label and field.var)
+    @param read_only: if True and it make sens, create a read only input widget
     @return: widget_type, widget_args, widget_kwargs
 
     """
@@ -55,15 +56,19 @@
             widget_args[0] = field.value
     elif field.fieldType == 'text-single':
         widget_type = "string"
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'text-multi':
         widget_type = "textbox"
         widget_args[0] = u'\n'.join(field.values)
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'text-private':
         widget_type = "password"
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'boolean':
         widget_type = "bool"
         if widget_args[0] is None:
             widget_args[0] = 'false'
+        widget_kwargs['read_only'] = read_only
     elif field.fieldType == 'list-single':
         widget_type = "list"
         widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options]
@@ -72,13 +77,14 @@
     else:
         log.error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
         widget_type = "string"
+        widget_kwargs['read_only'] = read_only
 
     if field.var:
         widget_kwargs["name"] = field.var
 
     return widget_type, widget_args, widget_kwargs
 
-def dataForm2Widgets(form_ui, form):
+def dataForm2Widgets(form_ui, form, read_only=False):
     """Complete an existing XMLUI with widget converted frot XEP-0004 data forms
 
     @param form_ui: XMLUI instance
@@ -91,7 +97,7 @@
     form_ui.changeContainer("pairs")
 
     for field in form.fieldList:
-        widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
+        widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only)
         label = field.label or field.var
         if label:
             form_ui.addLabel(label)
@@ -102,14 +108,14 @@
 
     return form_ui
 
-def dataForm2XMLUI(form, submit_id, session_id=None):
+def dataForm2XMLUI(form, submit_id, session_id=None, read_only=False):
     """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT XML
 
     @param submit_id: callback id to call when submitting form
     @param session_id: id to return with the data
     """
     form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
-    return dataForm2Widgets(form_ui, form)
+    return dataForm2Widgets(form_ui, form, read_only=read_only)
 
 def dataFormResult2AdvancedList(xmlui, form_xml):
     """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list
@@ -164,7 +170,7 @@
         dataFormResult2AdvancedList(xml_ui, form_elt)
     except exceptions.DataError:
         parsed_form = data_form.Form.fromElement(form_elt)
-        dataForm2Widgets(xml_ui, parsed_form)
+        dataForm2Widgets(xml_ui, parsed_form, read_only=True)
     return xml_ui
 
 def _cleanValue(value):
@@ -592,15 +598,13 @@
             InternalDataElement(self, data_elts)
 
 
-class InputWidget(Widget):
-    pass
-
-
 class EmptyWidget(Widget):
+    """Place holder widget"""
     type = 'empty'
 
 
 class TextWidget(Widget):
+    """Used for blob of text"""
     type = 'text'
 
     def __init__(self, xmlui, value, name=None, parent=None):
@@ -610,6 +614,9 @@
 
 
 class LabelWidget(Widget):
+    """Used for one line blob of text,
+    most of time to display the desciption or name of the next widget
+    """
     type='label'
 
     def __init__(self, xmlui, label, name=None, parent=None):
@@ -618,6 +625,7 @@
 
 
 class JidWidget(Widget):
+    """Used to display a Jabber ID, some specific methods can be added"""
     type='jid'
 
     def __init__(self, xmlui, jid, name=None, parent=None):
@@ -648,11 +656,24 @@
         self.elem.setAttribute('style', style)
 
 
+### Inputs ###
+
+
+class InputWidget(Widget):
+    """Input widget are widget which can accept user inputs,
+    used mainly in forms
+    """
+    def __init__(self, xmlui, name=None, parent=None, read_only=False):
+        super(InputWidget, self).__init__(xmlui, name, parent)
+        if read_only:
+            self.elem.setAttribute('read_only', 'true')
+
+
 class StringWidget(InputWidget):
     type = 'string'
 
-    def __init__(self, xmlui, value=None, name=None, parent=None):
-        super(StringWidget, self).__init__(xmlui, name, parent)
+    def __init__(self, xmlui, value=None, name=None, parent=None, read_only=False):
+        super(StringWidget, self).__init__(xmlui, name, parent, read_only=read_only)
         if value:
             self.elem.setAttribute('value', value)
 
@@ -665,10 +686,14 @@
     type = 'textbox'
 
 
+class JidInputWidget(StringWidget):
+    type = 'jid_input'
+
+
 class BoolWidget(InputWidget):
     type = 'bool'
 
-    def __init__(self, xmlui, value='false', name=None, parent=None):
+    def __init__(self, xmlui, value='false', name=None, parent=None, read_only=False):
         if isinstance(value, bool):
             value = 'true' if value else 'false'
         elif value == '0':
@@ -677,7 +702,7 @@
             value='true'
         if not value in ('true', 'false'):
             raise exceptions.DataError(_("Value must be 0, 1, false or true"))
-        super(BoolWidget, self).__init__(xmlui, name, parent)
+        super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
         self.elem.setAttribute('value', value)