Mercurial > libervia-backend
annotate frontends/wix/xmlui.py @ 103:6be927a465ed
XMLUI refactoring, step 1
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 23 Jun 2010 00:23:26 +0800 |
parents | frontends/wix/form.py@2503de7fb4c7 |
children | 5458ac1380cc |
rev | line source |
---|---|
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 wix: a SAT frontend |
57 | 6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import wx |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 import pdb |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from xml.dom import minidom |
91 | 27 from logging import debug, info, warning, error |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from tools.jid import JID |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
103 | 31 class XMLUI(wx.Frame): |
32 """Create an user interface from a SàT xml""" | |
91 | 33 |
34 def __init__(self, host, xml_data='', title="Form", options=[], misc={}): | |
92 | 35 style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in options else wx.DEFAULT_FRAME_STYLE #FIXME: gof: Q&D tmp hack |
103 | 36 super(XMLUI, self).__init__(None, title=title, style=style) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 self.host = host |
91 | 39 self.options = options |
40 self.misc = misc | |
36 | 41 self.ctl_list = [] # usefull to access ctrl |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 self.sizer = wx.BoxSizer(wx.VERTICAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 self.SetSizer(self.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 self.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 #events |
91 | 48 if not 'NO_CANCEL' in self.options: |
49 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.MakeModal() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
103 | 53 self.constructUI(xml_data) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.Show() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
103 | 57 def __parse_elems(self, childs, parent, sizer): |
58 """Parse elements inside a <layout> tags, and add them to the sizer""" | |
59 for elem in childs: | |
60 if elem.nodeName != "elem": | |
61 message=_("Unmanaged tag") | |
62 error(message) | |
63 raise Exception(message) | |
64 _proportion = 0 | |
65 name = elem.getAttribute("name") | |
66 type = elem.getAttribute("type") | |
67 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' | |
68 if type=="empty": | |
69 ctrl = wx.Window(parent, -1) | |
70 elif type=="text": | |
71 try: | |
72 value = elem.childNodes[0].wholeText | |
73 except KeyError: | |
74 warning (_("text node has no child !")) | |
75 ctrl = wx.StaticText(parent, -1, value) | |
76 elif type=="label": | |
77 ctrl = wx.StaticText(parent, -1, value+": ") | |
78 elif type=="string": | |
79 ctrl = wx.TextCtrl(parent, -1, value) | |
80 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
81 _proportion = 1 | |
82 elif type=="password": | |
83 ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_PASSWORD) | |
84 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
85 _proportion = 1 | |
86 elif type=="list": | |
87 ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE) | |
88 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
89 _proportion = 1 | |
90 else: | |
91 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! | |
92 raise NotImplementedError | |
93 sizer.Add(ctrl, _proportion, flag=wx.EXPAND) | |
94 | |
95 | |
96 | |
97 def constructUI(self, xml_data): | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 panel=wx.Panel(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 panel.sizer = wx.BoxSizer(wx.VERTICAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 cat_dom = minidom.parseString(xml_data.encode('utf-8')) |
103 | 102 top= cat_dom.documentElement |
103 self.type = top.getAttribute("type") | |
104 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: | |
105 message = _("XML UI received is invalid") | |
106 error(message) | |
107 raise Exception(message) | |
108 | |
109 for node in cat_dom.documentElement.childNodes: | |
110 if node.nodeName == "layout": | |
111 layout_panel = wx.Panel(panel, -1) | |
112 if node.getAttribute('type') == "vertical": | |
113 current_sizer = wx.BoxSizer(wx.VERTICAL) | |
114 elif node.getAttribute('type') == "pairs": | |
115 current_sizer = wx.FlexGridSizer(cols=2) | |
116 current_sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs | |
117 else: | |
118 warning(_("Unknown layout, using default one")) | |
119 current_sizer = wx.BoxSizer(wx.VERTICAL) | |
120 layout_panel.SetSizer(current_sizer) | |
121 self.__parse_elems(node.childNodes, layout_panel, current_sizer) | |
122 panel.sizer.Add(layout_panel, flag=wx.EXPAND) | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 else: |
103 | 124 message=_("Unknown tag") |
125 error(message) | |
126 raise Exception(message) #TODO: raise a custom exception here | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
103 | 128 if self.type == 'form': |
129 dialogButtons = wx.StdDialogButtonSizer() | |
130 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit")) | |
131 dialogButtons.AddButton(submitButton) | |
132 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton) | |
133 if not 'NO_CANCEL' in self.options: | |
134 cancelButton = wx.Button(panel,wx.ID_CANCEL) | |
135 dialogButtons.AddButton(cancelButton) | |
136 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton) | |
137 dialogButtons.Realize() | |
138 panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL) | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 panel.SetSizer(panel.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 panel.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 panel.sizer.Fit(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 self.sizer.Add(panel, 1, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 cat_dom.unlink() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
36 | 146 def onFormSubmitted(self, event): |
147 """Called when submit button is clicked""" | |
70 | 148 debug(_("Submitting form")) |
36 | 149 data = [] |
150 for ctrl in self.ctl_list: | |
91 | 151 if isinstance(ctrl['control'], wx.ListBox): |
152 data.append((ctrl['name'], ctrl['control'].GetStringSelection())) | |
153 else: | |
154 data.append((ctrl["name"], ctrl["control"].GetValue())) | |
155 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned | |
156 id = self.misc['action_back']("SUBMIT",self.misc['target'], data) | |
157 self.host.current_action_ids.add(id) | |
158 elif self.misc.has_key('callback'): | |
159 self.misc['callback'](data) | |
160 else: | |
161 warning (_("The form data is not sent back, the type is not managed properly")) | |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
162 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
163 self.Destroy() |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
36 | 165 def onFormCancelled(self, event): |
166 """Called when cancel button is clicked""" | |
70 | 167 debug(_("Cancelling form")) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
168 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
169 self.Close() |
36 | 170 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 def onClose(self, event): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 """Close event: we have to send the form.""" |
70 | 173 debug(_("close")) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 self.MakeModal(False) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 event.Skip() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 |