comparison frontends/src/primitivus/xmlui.py @ 632:06f44f797a1b

primitivus: really basic implementation of advanced list. Advanced list are here to allow complex lists, with columns, ordering, customization à la amarok 2. This implementation is just a first draft which show the text version of items in a List.
author Goffi <goffi@goffi.org>
date Sun, 08 Sep 2013 18:05:19 +0200
parents c8c07c920e30
children 49587e170f53
comparison
equal deleted inserted replaced
631:694f118d0cd5 632:06f44f797a1b
19 19
20 import urwid 20 import urwid
21 from urwid_satext import sat_widgets 21 from urwid_satext import sat_widgets
22 from logging import debug, info, warning, error 22 from logging import debug, info, warning, error
23 from xml.dom import minidom 23 from xml.dom import minidom
24
25 def getText(node):
26 """Get child text nodes
27 @param node: dom Node
28 @return: joined unicode text of all nodes
29
30 """
31 data = []
32 for child in node.childNodes:
33 if child.nodeType == child.TEXT_NODE:
34 data.append(child.wholeText)
35 return u"".join(data)
24 36
25 class Pairs(urwid.WidgetWrap): 37 class Pairs(urwid.WidgetWrap):
26 38
27 def __init__(self, weight_0='1', weight_1='1'): 39 def __init__(self, weight_0='1', weight_1='1'):
28 self.idx = 0 40 self.idx = 0
62 for elem in node.childNodes: 74 for elem in node.childNodes:
63 if elem.nodeName != "elem": 75 if elem.nodeName != "elem":
64 message=_("Unmanaged tag") 76 message=_("Unmanaged tag")
65 error(message) 77 error(message)
66 raise Exception(message) 78 raise Exception(message)
67 id = elem.getAttribute("id") 79 id_ = elem.getAttribute("id")
68 name = elem.getAttribute("name") 80 name = elem.getAttribute("name")
69 type = elem.getAttribute("type") 81 type_ = elem.getAttribute("type")
70 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' 82 value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
71 if type=="empty": 83 if type_=="empty":
72 ctrl = urwid.Text('') 84 ctrl = urwid.Text('')
73 elif type=="text": 85 elif type_=="text":
74 try: 86 try:
75 value = elem.childNodes[0].wholeText 87 value = elem.childNodes[0].wholeText
76 except IndexError: 88 except IndexError:
77 warning (_("text node has no child !")) 89 warning (_("text node has no child !"))
78 ctrl = urwid.Text(value) 90 ctrl = urwid.Text(value)
79 elif type=="label": 91 elif type_=="label":
80 ctrl = urwid.Text(value+": ") 92 ctrl = urwid.Text(value+": ")
81 elif type=="string": 93 elif type_=="string":
82 ctrl = sat_widgets.AdvancedEdit(edit_text = value) 94 ctrl = sat_widgets.AdvancedEdit(edit_text = value)
83 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 95 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
84 elif type=="password": 96 elif type_=="password":
85 ctrl = sat_widgets.Password(edit_text = value) 97 ctrl = sat_widgets.Password(edit_text = value)
86 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 98 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
87 elif type=="textbox": 99 elif type_=="textbox":
88 ctrl = sat_widgets.AdvancedEdit(edit_text = value, multiline=True) 100 ctrl = sat_widgets.AdvancedEdit(edit_text = value, multiline=True)
89 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 101 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
90 elif type=="bool": 102 elif type_=="bool":
91 ctrl = urwid.CheckBox('', state = value=="true") 103 ctrl = urwid.CheckBox('', state = value=="true")
92 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 104 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
93 elif type=="list": 105 elif type_=="list":
94 style=[] if elem.getAttribute("multi")=='yes' else ['single'] 106 style=[] if elem.getAttribute("multi")=='yes' else ['single']
95 ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style) 107 ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style)
96 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 108 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
97 elif type=="button": 109 elif type_=="button":
98 callback_id = elem.getAttribute("callback_id") 110 callback_id = elem.getAttribute("callback_id")
99 ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress) 111 ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress)
100 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) 112 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
101 else: 113 elif type_=="advanced_list":
102 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! 114 ctrl = sat_widgets.List(options=[getText(txt_elt) for txt_elt in elem.getElementsByTagName("text")], style=['can_select_none'], max_height=20)
115
116 self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
117 else:
118 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type_) #FIXME !
103 raise NotImplementedError 119 raise NotImplementedError
104 if self.type == 'param': 120 if self.type == 'param':
105 if isinstance(ctrl,urwid.Edit) or isinstance(ctrl,urwid.CheckBox): 121 if isinstance(ctrl,urwid.Edit) or isinstance(ctrl,urwid.CheckBox):
106 urwid.connect_signal(ctrl,'change',self.onParamChange) 122 urwid.connect_signal(ctrl,'change',self.onParamChange)
107 ctrl._param_category = self._current_category 123 ctrl._param_category = self._current_category
234 else: 250 else:
235 data.append((ctrl_name, ctrl['control'].get_edit_text())) 251 data.append((ctrl_name, ctrl['control'].get_edit_text()))
236 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned 252 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
237 raise NotImplementedError 253 raise NotImplementedError
238 self.host.debug() 254 self.host.debug()
239 elif self.misc.has_key('callback'): 255 elif 'callback' in self.misc:
240 self.misc['callback'](data) 256 try:
257 self.misc['callback'](data, *self.misc['callback_args'])
258 except KeyError:
259 self.misc['callback'](data)
260
241 else: 261 else:
242 warning (_("The form data is not sent back, the type is not managed properly")) 262 warning (_("The form data is not sent back, the type is not managed properly"))
243 self.host.removePopUp() 263 self.host.removePopUp()
244 264
245 def onFormCancelled(self, button): 265 def onFormCancelled(self, button):