XMLUI refactoring, step 1

This commit is contained in:
Goffi 2010-06-23 00:23:26 +08:00
commit eaeb313e54
3 changed files with 220 additions and 73 deletions

View file

@ -26,7 +26,7 @@ import os.path, glob
import pdb
from logging import debug, info, error
from tools.jid import JID
from form import Form
from xmlui import XMLUI
CARD_WIDTH = 74
CARD_HEIGHT = 136

View file

@ -27,7 +27,7 @@ import wx
from contact_list import ContactList
from chat import Chat
from param import Param
from form import Form
from xmlui import XMLUI
from gateways import GatewaysManager
from profile import Profile
from profile_manager import ProfileManager
@ -293,17 +293,17 @@ class MainWindow(wx.Frame, QuickApp):
)
dlg.ShowModal()
dlg.Destroy()
elif type == "FORM":
elif type == "XMLUI":
self.current_action_ids.remove(id)
debug (_("Form received"))
debug (_("XML user interface received"))
misc = {}
#FIXME FIXME FIXME: must clean all this crap !
title = _('Form')
if data['type'] == _('registration'):
title = 'Registration'
title = _('Registration')
misc['target'] = data['target']
misc['action_back'] = self.bridge.gatewayRegister
form=Form(self, title=title, xml_data = data['xml'], misc = misc)
XMLUI(self, title=title, xml_data = data['xml'], misc = misc)
elif type == "RESULT":
self.current_action_ids.remove(id)
if self.current_action_ids_cb.has_key(id):

View file

@ -28,12 +28,12 @@ from logging import debug, info, warning, error
from tools.jid import JID
class Form(wx.Frame):
"""Create a form from a SàT xml"""
class XMLUI(wx.Frame):
"""Create an user interface from a SàT xml"""
def __init__(self, host, xml_data='', title="Form", options=[], misc={}):
style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in options else wx.DEFAULT_FRAME_STYLE #FIXME: gof: Q&D tmp hack
super(Form, self).__init__(None, title=title, style=style)
super(XMLUI, self).__init__(None, title=title, style=style)
self.host = host
self.options = options
@ -50,55 +50,92 @@ class Form(wx.Frame):
self.MakeModal()
self.constructForm(xml_data)
self.constructUI(xml_data)
self.Show()
def constructForm(self, xml_data):
def __parse_elems(self, childs, parent, sizer):
"""Parse elements inside a <layout> tags, and add them to the sizer"""
for elem in childs:
if elem.nodeName != "elem":
message=_("Unmanaged tag")
error(message)
raise Exception(message)
_proportion = 0
name = elem.getAttribute("name")
type = elem.getAttribute("type")
value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
if type=="empty":
ctrl = wx.Window(parent, -1)
elif type=="text":
try:
value = elem.childNodes[0].wholeText
except KeyError:
warning (_("text node has no child !"))
ctrl = wx.StaticText(parent, -1, value)
elif type=="label":
ctrl = wx.StaticText(parent, -1, value+": ")
elif type=="string":
ctrl = wx.TextCtrl(parent, -1, value)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
_proportion = 1
elif type=="password":
ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_PASSWORD)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
_proportion = 1
elif type=="list":
ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
_proportion = 1
else:
error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
raise NotImplementedError
sizer.Add(ctrl, _proportion, flag=wx.EXPAND)
def constructUI(self, xml_data):
panel=wx.Panel(self)
panel.sizer = wx.BoxSizer(wx.VERTICAL)
cat_dom = minidom.parseString(xml_data.encode('utf-8'))
for elem in cat_dom.documentElement.getElementsByTagName("elem"):
name = elem.getAttribute("name")
label = elem.getAttribute("label") if elem.hasAttribute('label') else name
type = elem.getAttribute("type")
value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
sizer = wx.BoxSizer(wx.HORIZONTAL)
if type=="text":
ctrl = wx.StaticText(panel, -1, value)
elif type=="string":
label=wx.StaticText(panel, -1, label+": ")
ctrl = wx.TextCtrl(panel, -1, value)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
sizer.Add(label)
elif type=="password":
label=wx.StaticText(panel, -1, label+": ")
ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
sizer.Add(label)
elif type=="list":
label=wx.StaticText(panel, -1, label+": ")
ctrl = wx.ListBox(panel, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE)
self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
else:
error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
raise NotImplementedError
sizer.Add(ctrl, 1, flag=wx.EXPAND)
#self.ctl_list[(name, category)] = ctrl
panel.sizer.Add(sizer, flag=wx.EXPAND)
top= cat_dom.documentElement
self.type = top.getAttribute("type")
if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
message = _("XML UI received is invalid")
error(message)
raise Exception(message)
dialogButtons = wx.StdDialogButtonSizer()
submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
dialogButtons.AddButton(submitButton)
panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
if not 'NO_CANCEL' in self.options:
cancelButton = wx.Button(panel,wx.ID_CANCEL)
dialogButtons.AddButton(cancelButton)
panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
dialogButtons.Realize()
panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
for node in cat_dom.documentElement.childNodes:
if node.nodeName == "layout":
layout_panel = wx.Panel(panel, -1)
if node.getAttribute('type') == "vertical":
current_sizer = wx.BoxSizer(wx.VERTICAL)
elif node.getAttribute('type') == "pairs":
current_sizer = wx.FlexGridSizer(cols=2)
current_sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs
else:
warning(_("Unknown layout, using default one"))
current_sizer = wx.BoxSizer(wx.VERTICAL)
layout_panel.SetSizer(current_sizer)
self.__parse_elems(node.childNodes, layout_panel, current_sizer)
panel.sizer.Add(layout_panel, flag=wx.EXPAND)
else:
message=_("Unknown tag")
error(message)
raise Exception(message) #TODO: raise a custom exception here
if self.type == 'form':
dialogButtons = wx.StdDialogButtonSizer()
submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
dialogButtons.AddButton(submitButton)
panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
if not 'NO_CANCEL' in self.options:
cancelButton = wx.Button(panel,wx.ID_CANCEL)
dialogButtons.AddButton(cancelButton)
panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
dialogButtons.Realize()
panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
panel.SetSizer(panel.sizer)
panel.SetAutoLayout(True)