Primitivus: XMLUI: tabs layout management + CustomButton now used instead of urwid's buttons

This commit is contained in:
Goffi 2010-07-31 00:32:51 +08:00
commit e35ee355ba

View file

@ -35,7 +35,7 @@ class Pairs(urwid.WidgetWrap):
def append(self, widget):
pile = self._w.widget_list[self.idx]
if pile.__class__ == urwid.Text:
if isinstance(pile, urwid.Text):
self._w.widget_list[self.idx] = urwid.Pile([widget])
if self.idx == 1:
self._w.set_focus(1)
@ -44,6 +44,8 @@ class Pairs(urwid.WidgetWrap):
pile.item_types.append(('weight',getattr(self,'weight_'+str(self.idx))))
self.idx = (self.idx + 1) % 2
class InvalidXMLUI(Exception):
pass
class XMLUI(urwid.WidgetWrap):
@ -78,13 +80,13 @@ class XMLUI(urwid.WidgetWrap):
elif type=="label":
ctrl = urwid.Text(value+": ")
elif type=="string":
ctrl = urwid.Edit(edit_text = value)
ctrl = custom_widgets.AdvancedEdit(edit_text = value)
self.ctrl_list[name] = ({'type':type, 'control':ctrl})
elif type=="password":
ctrl = custom_widgets.Password(edit_text = value)
self.ctrl_list[name] = ({'type':type, 'control':ctrl})
elif type=="textbox":
ctrl = urwid.Edit(edit_text = value, multiline=True)
ctrl = custom_widgets.AdvancedEdit(edit_text = value, multiline=True)
self.ctrl_list[name] = ({'type':type, 'control':ctrl})
elif type=="list":
style=[] if elem.getAttribute("multi")=='yes' else ['single']
@ -92,66 +94,65 @@ class XMLUI(urwid.WidgetWrap):
self.ctrl_list[name] = ({'type':type, 'control':ctrl})
elif type=="button":
callback_id = elem.getAttribute("callback_id")
ctrl = urwid.Button(value, on_press=self.onButtonPress)
ctrl = custom_widgets.CustomButton(value, on_press=self.onButtonPress)
ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
else:
error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
raise NotImplementedError
parent.append(ctrl)
def __parseChilds(self, current, elem, wanted = ['layout']):
def __parseChilds(self, current, elem, wanted = ['layout'], data = None):
"""Recursively parse childNodes of an elemen
@param current: widget container with 'append' method
@param elem: element from which childs will be parsed
@param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant"""
for node in elem.childNodes:
if wanted and not node.nodeName in wanted:
raise Exception("Invalid XMLUI") #TODO: make a custom exception
raise InvalidXMLUI
if node.nodeName == "layout":
type = node.getAttribute('type')
if type == "tabs":
raise NotImplementedError
self.__parseChilds(current, None, node, ['category'])
tab_cont = custom_widgets.TabsContainer()
self.__parseChilds(current, node, ['category'], tab_cont)
current.append(tab_cont)
elif type == "vertical":
self.__parseElems(node, current)
elif type == "pairs":
pairs = Pairs()
self.__parseElems(node, pairs)
current.append(pairs)
else:
if type == "vertical":
pass
elif type == "pairs":
pairs = Pairs()
current.append(pairs)
current = pairs
else:
warning(_("Unknown layout, using default one"))
warning(_("Unknown layout, using default one"))
self.__parseElems(node, current)
elif node.nodeName == "category":
raise NotImplementedError
"""name = node.getAttribute('name')
if not node.nodeName in wanted or not name or not isinstance(parent,wx.Notebook):
raise Exception("Invalid XMLUI") #TODO: make a custom exception
notebook = parent
tab_panel = wx.Panel(notebook, -1)
tab_panel.sizer = wx.BoxSizer(wx.VERTICAL)
tab_panel.SetSizer(tab_panel.sizer)
notebook.AddPage(tab_panel, name)
self.__parseChilds(tab_panel, None, node, ['layout'])"""
name = node.getAttribute('name')
if not name or not isinstance(data,custom_widgets.TabsContainer):
raise InvalidXMLUI
tab_cont = data
listbox = tab_cont.addTab(name)
self.__parseChilds(listbox.body, node, ['layout'])
else:
message=_("Unknown tag")
error(message)
raise Exception(message) #TODO: raise a custom exception here
raise NotImplementedError
def constructUI(self, xml_data):
list_box = urwid.ListBox(urwid.SimpleListWalker([]))
cat_dom = minidom.parseString(xml_data.encode('utf-8'))
top= cat_dom.documentElement
top=cat_dom.documentElement
self.type = top.getAttribute("type")
if not self.title:
self.title = top.getAttribute("title") #TODO: manage title
if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
raise Exception("Invalid XMLUI") #TODO: make a custom exception
raise InvalidXMLUI
self.__parseChilds(list_box.body, cat_dom.documentElement)
assert list_box.body
if isinstance(list_box.body[0],custom_widgets.TabsContainer):
return list_box.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox
if self.type == 'form':
buttons = []
@ -162,7 +163,6 @@ class XMLUI(urwid.WidgetWrap):
grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center')
list_box.body.append(grid_wid)
return list_box
def show(self):