comparison frontends/wix/form.py @ 91:39c672544593

Tarot: bidding phase - quick_app: command line is now parsed, "profile" option allow to select it - xml_tools: list-single is now managed - plugin tarot: method and signal to manage contract (contrat): tarotChooseContrat & tarotGameContratChoosed - wix: Q&D Form hack to manage more generic form (not only registration), used to show contract selection form
author Goffi <goffi@goffi.org>
date Thu, 27 May 2010 19:26:19 +0930
parents 8f2ed279784b
children 2503de7fb4c7
comparison
equal deleted inserted replaced
90:4020931569b8 91:39c672544593
22 22
23 23
24 import wx 24 import wx
25 import pdb 25 import pdb
26 from xml.dom import minidom 26 from xml.dom import minidom
27 from logging import debug, info, error 27 from logging import debug, info, warning, error
28 from tools.jid import JID 28 from tools.jid import JID
29 29
30 30
31 class Form(wx.Frame): 31 class Form(wx.Frame):
32 def __init__(self, host, target, xml_data='', title="Form", type="Form"): 32 """Create a form from a SàT xml"""
33
34 def __init__(self, host, xml_data='', title="Form", options=[], misc={}):
33 super(Form, self).__init__(None, title=title) 35 super(Form, self).__init__(None, title=title)
34 36
35 self.host = host 37 self.host = host
36 self.target = target 38 self.options = options
39 self.misc = misc
37 self.ctl_list = [] # usefull to access ctrl 40 self.ctl_list = [] # usefull to access ctrl
38 41
39 self.sizer = wx.BoxSizer(wx.VERTICAL) 42 self.sizer = wx.BoxSizer(wx.VERTICAL)
40 self.SetSizer(self.sizer) 43 self.SetSizer(self.sizer)
41 self.SetAutoLayout(True) 44 self.SetAutoLayout(True)
42 45
43 #events 46 #events
44 self.Bind(wx.EVT_CLOSE, self.onClose, self) 47 if not 'NO_CANCEL' in self.options:
48 self.Bind(wx.EVT_CLOSE, self.onClose, self)
45 49
46 self.MakeModal() 50 self.MakeModal()
47 51
48 self.constructForm(xml_data) 52 self.constructForm(xml_data)
49 53
57 61
58 for elem in cat_dom.documentElement.getElementsByTagName("elem"): 62 for elem in cat_dom.documentElement.getElementsByTagName("elem"):
59 name = elem.getAttribute("name") 63 name = elem.getAttribute("name")
60 label = elem.getAttribute("label") if elem.hasAttribute('label') else name 64 label = elem.getAttribute("label") if elem.hasAttribute('label') else name
61 type = elem.getAttribute("type") 65 type = elem.getAttribute("type")
62 value = elem.firstChild.wholeText if elem.firstChild else u'' #TODO: must check if the first child is really the text value 66 value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
63 sizer = wx.BoxSizer(wx.HORIZONTAL) 67 sizer = wx.BoxSizer(wx.HORIZONTAL)
64 if type=="text": 68 if type=="text":
65 ctrl = wx.StaticText(panel, -1, value) 69 ctrl = wx.StaticText(panel, -1, value)
66 elif type=="string": 70 elif type=="string":
67 label=wx.StaticText(panel, -1, label+": ") 71 label=wx.StaticText(panel, -1, label+": ")
71 elif type=="password": 75 elif type=="password":
72 label=wx.StaticText(panel, -1, label+": ") 76 label=wx.StaticText(panel, -1, label+": ")
73 ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD) 77 ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD)
74 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) 78 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
75 sizer.Add(label) 79 sizer.Add(label)
80 elif type=="list":
81 label=wx.StaticText(panel, -1, label+": ")
82 ctrl = wx.ListBox(panel, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE)
83 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
76 else: 84 else:
77 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! 85 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
78 raise NotImplementedError 86 raise NotImplementedError
79 sizer.Add(ctrl, 1, flag=wx.EXPAND) 87 sizer.Add(ctrl, 1, flag=wx.EXPAND)
80 #self.ctl_list[(name, category)] = ctrl 88 #self.ctl_list[(name, category)] = ctrl
81 panel.sizer.Add(sizer, flag=wx.EXPAND) 89 panel.sizer.Add(sizer, flag=wx.EXPAND)
82 90
83 submitButton = wx.Button(panel,wx.ID_OK, label="Submit")
84 cancelButton = wx.Button(panel,wx.ID_CANCEL)
85 dialogButtons = wx.StdDialogButtonSizer() 91 dialogButtons = wx.StdDialogButtonSizer()
92 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
86 dialogButtons.AddButton(submitButton) 93 dialogButtons.AddButton(submitButton)
87 dialogButtons.AddButton(cancelButton) 94 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
95 if not 'NO_CANCEL' in self.options:
96 cancelButton = wx.Button(panel,wx.ID_CANCEL)
97 dialogButtons.AddButton(cancelButton)
98 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
88 dialogButtons.Realize() 99 dialogButtons.Realize()
89 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
90 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
91 panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL) 100 panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
92 101
93 panel.SetSizer(panel.sizer) 102 panel.SetSizer(panel.sizer)
94 panel.SetAutoLayout(True) 103 panel.SetAutoLayout(True)
95 panel.sizer.Fit(self) 104 panel.sizer.Fit(self)
99 def onFormSubmitted(self, event): 108 def onFormSubmitted(self, event):
100 """Called when submit button is clicked""" 109 """Called when submit button is clicked"""
101 debug(_("Submitting form")) 110 debug(_("Submitting form"))
102 data = [] 111 data = []
103 for ctrl in self.ctl_list: 112 for ctrl in self.ctl_list:
104 data.append((ctrl["name"], ctrl["control"].GetValue())) 113 if isinstance(ctrl['control'], wx.ListBox):
105 id = self.host.bridge.gatewayRegister("SUBMIT",self.target, data) 114 data.append((ctrl['name'], ctrl['control'].GetStringSelection()))
106 self.host.current_action_ids.add(id) 115 else:
116 data.append((ctrl["name"], ctrl["control"].GetValue()))
117 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
118 id = self.misc['action_back']("SUBMIT",self.misc['target'], data)
119 self.host.current_action_ids.add(id)
120 elif self.misc.has_key('callback'):
121 self.misc['callback'](data)
122 else:
123 warning (_("The form data is not sent back, the type is not managed properly"))
107 self.MakeModal(False) 124 self.MakeModal(False)
108 self.Destroy() 125 self.Destroy()
109 126
110 def onFormCancelled(self, event): 127 def onFormCancelled(self, event):
111 """Called when cancel button is clicked""" 128 """Called when cancel button is clicked"""