diff 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
line wrap: on
line diff
--- a/frontends/wix/form.py	Sun May 23 16:39:05 2010 +0930
+++ b/frontends/wix/form.py	Thu May 27 19:26:19 2010 +0930
@@ -24,16 +24,19 @@
 import wx
 import pdb
 from xml.dom import minidom
-from logging import debug, info, error
+from logging import debug, info, warning, error
 from tools.jid  import JID
 
 
 class Form(wx.Frame):
-    def __init__(self, host, target, xml_data='', title="Form", type="Form"):
+    """Create a form from a SàT xml"""
+
+    def __init__(self, host, xml_data='', title="Form", options=[], misc={}):
         super(Form, self).__init__(None, title=title)
 
         self.host = host
-        self.target = target
+        self.options = options
+        self.misc = misc
         self.ctl_list = []  # usefull to access ctrl
 
         self.sizer = wx.BoxSizer(wx.VERTICAL)
@@ -41,7 +44,8 @@
         self.SetAutoLayout(True)
         
         #events
-        self.Bind(wx.EVT_CLOSE, self.onClose, self)
+        if not 'NO_CANCEL' in self.options:
+            self.Bind(wx.EVT_CLOSE, self.onClose, self)
         
         self.MakeModal()
 
@@ -59,7 +63,7 @@
             name = elem.getAttribute("name")
             label = elem.getAttribute("label") if elem.hasAttribute('label') else name
             type = elem.getAttribute("type")
-            value = elem.firstChild.wholeText if elem.firstChild else u''  #TODO: must check if the first child is really the text value
+            value = elem.getAttribute("value") if elem.hasAttribute('value') else u''
             sizer = wx.BoxSizer(wx.HORIZONTAL)
             if type=="text":
                 ctrl = wx.StaticText(panel, -1, value)
@@ -73,6 +77,10 @@
                 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
@@ -80,14 +88,15 @@
             #self.ctl_list[(name, category)] = ctrl
             panel.sizer.Add(sizer, flag=wx.EXPAND)
 
-        submitButton = wx.Button(panel,wx.ID_OK, label="Submit")
-        cancelButton = wx.Button(panel,wx.ID_CANCEL)
         dialogButtons = wx.StdDialogButtonSizer()
+        submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
         dialogButtons.AddButton(submitButton)
-        dialogButtons.AddButton(cancelButton)
+        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.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
-        panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
         panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
 
         panel.SetSizer(panel.sizer)
@@ -101,9 +110,17 @@
         debug(_("Submitting form"))
         data = []
         for ctrl in self.ctl_list:
-            data.append((ctrl["name"], ctrl["control"].GetValue()))
-        id = self.host.bridge.gatewayRegister("SUBMIT",self.target, data)
-        self.host.current_action_ids.add(id)
+            if isinstance(ctrl['control'], wx.ListBox):
+                data.append((ctrl['name'], ctrl['control'].GetStringSelection()))
+            else:
+                data.append((ctrl["name"], ctrl["control"].GetValue()))
+        if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
+            id = self.misc['action_back']("SUBMIT",self.misc['target'], data)
+            self.host.current_action_ids.add(id)
+        elif self.misc.has_key('callback'):
+            self.misc['callback'](data)
+        else:
+            warning (_("The form data is not sent back, the type is not managed properly"))
         self.MakeModal(False)
         self.Destroy()