comparison frontends/src/tools/xmlui.py @ 805:7c05c39156a2

core (XMLUI), frontends: advancedListContainer part 2:
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:21:51 +0100
parents 5174657b3378
children d035c662b357
comparison
equal deleted inserted replaced
804:5174657b3378 805:7c05c39156a2
174 flags = [] 174 flags = []
175 self.flags = flags 175 self.flags = flags
176 self.ctrl_list = {} # usefull to access ctrl 176 self.ctrl_list = {} # usefull to access ctrl
177 self._main_cont = None 177 self._main_cont = None
178 self.constructUI(xml_data) 178 self.constructUI(xml_data)
179
180 def escape(self, name):
181 """ return escaped name for forms """
182 return u"%s%s" % (Const.SAT_FORM_PREFIX, name)
179 183
180 @property 184 @property
181 def main_cont(self): 185 def main_cont(self):
182 return self._main_cont 186 return self._main_cont
183 187
218 elif type_ == "advanced_list": 222 elif type_ == "advanced_list":
219 try: 223 try:
220 columns = int(node.getAttribute('columns')) 224 columns = int(node.getAttribute('columns'))
221 except (TypeError, ValueError): 225 except (TypeError, ValueError):
222 raise DataError("Invalid columns") 226 raise DataError("Invalid columns")
223 cont = self.widget_factory.createAdvancedListContainer(parent, columns) 227 selectable = node.getAttribute('selectable') or 'no'
224 self._parseChilds(cont, node, ('row',)) 228 auto_index = node.getAttribute('auto_index') == 'true'
229 data = {'index': 0} if auto_index else None
230 cont = self.widget_factory.createAdvancedListContainer(parent, columns, selectable)
231 callback_id = node.getAttribute("callback") or None
232 if callback_id is not None:
233 if selectable == 'no':
234 raise ValueError("can't have selectable=='no' and callback_id at the same time")
235 cont._xmlui_callback_id = callback_id
236 cont._xmluiOnSelect(self.onAdvListSelect)
237
238 self._parseChilds(cont, node, ('row',), data)
225 else: 239 else:
226 warning(_("Unknown container [%s], using default one") % type_) 240 warning(_("Unknown container [%s], using default one") % type_)
227 cont = self.widget_factory.createVerticalContainer(parent) 241 cont = self.widget_factory.createVerticalContainer(parent)
228 self._parseChilds(cont, node, ('widget', 'container')) 242 self._parseChilds(cont, node, ('widget', 'container'))
229 try: 243 try:
244 tab_cont = data 258 tab_cont = data
245 new_tab = tab_cont._xmluiAddTab(label or name) 259 new_tab = tab_cont._xmluiAddTab(label or name)
246 self._parseChilds(new_tab, node, ('widget', 'container')) 260 self._parseChilds(new_tab, node, ('widget', 'container'))
247 261
248 elif node.nodeName == 'row': 262 elif node.nodeName == 'row':
249 parent._xmluiAddRow() 263 try:
264 index = str(data['index'])
265 data['index'] += 1
266 except TypeError:
267 index = node.getAttribute('index') or None
268 parent._xmluiAddRow(index)
250 self._parseChilds(parent, node, ('widget', 'container')) 269 self._parseChilds(parent, node, ('widget', 'container'))
251 270
252 elif node.nodeName == "widget": 271 elif node.nodeName == "widget":
253 id_ = node.getAttribute("id") 272 id_ = node.getAttribute("id")
254 name = node.getAttribute("name") 273 name = node.getAttribute("name")
297 316
298 if self.type == 'param': 317 if self.type == 'param':
299 try: 318 try:
300 ctrl._xmluiOnChange(self.onParamChange) 319 ctrl._xmluiOnChange(self.onParamChange)
301 ctrl._param_category = self._current_category 320 ctrl._param_category = self._current_category
302 ctrl._param_name = name.split(Const.SAT_PARAM_SEPARATOR)[1]
303 except AttributeError: 321 except AttributeError:
304 if not isinstance(ctrl, (EmptyWidget, TextWidget)): 322 if not isinstance(ctrl, (EmptyWidget, TextWidget)):
305 warning(_("No change listener on [%s]" % ctrl)) 323 warning(_("No change listener on [%s]" % ctrl))
306 324
325 ctrl._xmlui_name = name
307 parent._xmluiAppend(ctrl) 326 parent._xmluiAppend(ctrl)
308 327
309 else: 328 else:
310 raise NotImplementedError(_('Unknown tag [%s]') % node.nodeName) 329 raise NotImplementedError(_('Unknown tag [%s]') % node.nodeName)
311 330
349 368
350 """ 369 """
351 assert(self.type == "param") 370 assert(self.type == "param")
352 self.param_changed.add(ctrl) 371 self.param_changed.add(ctrl)
353 372
373 def onAdvListSelect(self, ctrl):
374 data = {}
375 widgets = ctrl._xmluiGetSelectedWidgets()
376 for wid in widgets:
377 try:
378 name = self.escape(wid._xmlui_name)
379 value = wid._xmluiGetValue()
380 ret[name] = value
381 except AttributeError:
382 pass
383 idx = ctrl._xmluiGetSelectedIndex()
384 if idx is not None:
385 data['index'] = idx
386 callback_id = ctrl._xmlui_callback_id
387 if callback_id is None:
388 warning(_("No callback_id found"))
389 return
390 self.host.launchAction(callback_id, data, profile_key = self.host.profile)
391
354 def onButtonPress(self, button): 392 def onButtonPress(self, button):
355 """ Called when an XMLUI button is clicked 393 """ Called when an XMLUI button is clicked
356 Launch the action associated to the button 394 Launch the action associated to the button
357 @param button: the button clicked 395 @param button: the button clicked
358 396
359 """ 397 """
360 callback_id, fields = button._xmlui_param_id 398 callback_id, fields = button._xmlui_param_id
361 data = {} 399 data = {}
362 for field in fields: 400 for field in fields:
401 escaped = self.escape(field)
363 ctrl = self.ctrl_list[field] 402 ctrl = self.ctrl_list[field]
364 if isinstance(ctrl['control'], ListWidget): 403 if isinstance(ctrl['control'], ListWidget):
365 data[field] = u'\t'.join(ctrl['control']._xmluiGetSelected()) 404 data[escaped] = u'\t'.join(ctrl['control']._xmluiGetSelected())
366 else: 405 else:
367 data[field] = ctrl['control']._xmluiGetValue() 406 data[escaped] = ctrl['control']._xmluiGetValue()
368 self.host.launchAction(callback_id, data, profile_key = self.host.profile) 407 self.host.launchAction(callback_id, data, profile_key = self.host.profile)
369 408
370 def onFormSubmitted(self, ignore=None): 409 def onFormSubmitted(self, ignore=None):
371 """ An XMLUI form has been submited 410 """ An XMLUI form has been submited
372 call the submit action associated with this form 411 call the submit action associated with this form
373 412
374 """ 413 """
375 selected_values = [] 414 selected_values = []
376 for ctrl_name in self.ctrl_list: 415 for ctrl_name in self.ctrl_list:
377 escaped = u"%s%s" % (Const.SAT_FORM_PREFIX, ctrl_name) 416 escaped = self.escape(ctrl_name)
378 ctrl = self.ctrl_list[ctrl_name] 417 ctrl = self.ctrl_list[ctrl_name]
379 if isinstance(ctrl['control'], ListWidget): 418 if isinstance(ctrl['control'], ListWidget):
380 selected_values.append((escaped, u'\t'.join(ctrl['control']._xmluiGetSelectedValues()))) 419 selected_values.append((escaped, u'\t'.join(ctrl['control']._xmluiGetSelectedValues())))
381 else: 420 else:
382 selected_values.append((escaped, ctrl['control']._xmluiGetValue())) 421 selected_values.append((escaped, ctrl['control']._xmluiGetValue()))
405 for ctrl in self.param_changed: 444 for ctrl in self.param_changed:
406 if isinstance(ctrl, ListWidget): 445 if isinstance(ctrl, ListWidget):
407 value = u'\t'.join(ctrl._xmluiGetSelectedValues()) 446 value = u'\t'.join(ctrl._xmluiGetSelectedValues())
408 else: 447 else:
409 value = ctrl._xmluiGetValue() 448 value = ctrl._xmluiGetValue()
410 self.host.bridge.setParam(ctrl._param_name, value, ctrl._param_category, 449 param_name = ctr._xmlui_name.split(Const.SAT_PARAM_SEPARATOR)[1]
450 self.host.bridge.setParam(param_name, value, ctrl._param_category,
411 profile_key=self.host.profile) 451 profile_key=self.host.profile)
412 self._xmluiClose() 452 self._xmluiClose()