comparison src/tools/xml_tools.py @ 2380:59636c4db2d0

core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI: This argument allow to create widgets which will be put first in LabelContainer of generated XMLUI. An iterable of *args for XMLUI.addWidget must be used.
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2017 07:36:03 +0200
parents 318f0434d830
children 2e05921df16a
comparison
equal deleted inserted replaced
2379:42a54cbc1872 2380:59636c4db2d0
100 widget_kwargs["name"] = field.var 100 widget_kwargs["name"] = field.var
101 101
102 return widget_type, widget_args, widget_kwargs 102 return widget_type, widget_args, widget_kwargs
103 103
104 104
105 def dataForm2Widgets(form_ui, form, read_only=False): 105 def dataForm2Widgets(form_ui, form, read_only=False, prepend=None):
106 """Complete an existing XMLUI with widget converted from XEP-0004 data forms. 106 """Complete an existing XMLUI with widget converted from XEP-0004 data forms.
107 107
108 @param form_ui (XMLUI): XMLUI instance 108 @param form_ui (XMLUI): XMLUI instance
109 @param form (data_form.Form): Wokkel's implementation of data form 109 @param form (data_form.Form): Wokkel's implementation of data form
110 @param read_only (bool): if True and it makes sense, create a read only input widget 110 @param read_only (bool): if True and it makes sense, create a read only input widget
111 @param prepend(iterable, None): widgets to prepend to main LabelContainer
112 if not None, must be an iterable of *args for addWidget. Those widgets will
113 be added first to the container.
111 @return: the completed XMLUI instance 114 @return: the completed XMLUI instance
112 """ 115 """
113 if form.instructions: 116 if form.instructions:
114 form_ui.addText('\n'.join(form.instructions), 'instructions') 117 form_ui.addText('\n'.join(form.instructions), 'instructions')
115 118
116 form_ui.changeContainer("label") 119 form_ui.changeContainer("label")
120
121 if prepend is not None:
122 for widget_args in prepend:
123 form_ui.addWidget(*widget_args)
117 124
118 for field in form.fieldList: 125 for field in form.fieldList:
119 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only) 126 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only)
120 label = field.label or field.var 127 label = field.label or field.var
121 if label: 128 if label:
226 except exceptions.DataError: 233 except exceptions.DataError:
227 parsed_form = data_form.Form.fromElement(form_elt) 234 parsed_form = data_form.Form.fromElement(form_elt)
228 dataForm2Widgets(xml_ui, parsed_form, read_only=True) 235 dataForm2Widgets(xml_ui, parsed_form, read_only=True)
229 return xml_ui 236 return xml_ui
230 237
231 def dataFormResult2XMLUI(result_form, base_form, session_id=None): 238 def dataFormResult2XMLUI(result_form, base_form, session_id=None, prepend=None):
232 """Convert data form result to SàT XMLUI. 239 """Convert data form result to SàT XMLUI.
233 240
234 @param result_form (data_form.Form): result form to convert 241 @param result_form (data_form.Form): result form to convert
235 @param base_form (data_form.Form): initial form (i.e. of form type "form") 242 @param base_form (data_form.Form): initial form (i.e. of form type "form")
236 this one is necessary to reconstruct options when needed (e.g. list elements) 243 this one is necessary to reconstruct options when needed (e.g. list elements)
237 @param session_id (unicode): session id to return with the data 244 @param session_id (unicode): session id to return with the data
245 @param prepend: same as for [dataForm2Widgets]
238 @return: XMLUI instance 246 @return: XMLUI instance
239 """ 247 """
240 form = deepcopy(result_form) 248 form = deepcopy(result_form)
241 for name, field in form.fields.iteritems(): 249 for name, field in form.fields.iteritems():
242 try: 250 try:
243 base_field = base_form.fields[name] 251 base_field = base_form.fields[name]
244 except KeyError: 252 except KeyError:
245 continue 253 continue
246 field.options = base_field.options 254 field.options = base_field.options
247 xml_ui = XMLUI("window", "vertical", session_id=session_id) 255 xml_ui = XMLUI("window", "vertical", session_id=session_id)
248 dataForm2Widgets(xml_ui, form, read_only=True) 256 dataForm2Widgets(xml_ui, form, read_only=True, prepend=prepend)
249 return xml_ui 257 return xml_ui
250 258
251 259
252 def _cleanValue(value): 260 def _cleanValue(value):
253 """Workaround method to avoid DBus types with D-Bus bridge. 261 """Workaround method to avoid DBus types with D-Bus bridge.