Mercurial > libervia-backend
annotate frontends/wix/xmlui.py @ 106:138d82f64b6f
plugin CS: friends parsing
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Jun 2010 15:33:16 +0800 |
parents | d2630fba8dfd |
children | 5ae370c71803 |
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 |
104 | 57 def __parseElems(self, node, parent): |
58 """Parse elements inside a <layout> tags, and add them to the parent sizer""" | |
59 for elem in node.childNodes: | |
103 | 60 if elem.nodeName != "elem": |
61 message=_("Unmanaged tag") | |
62 error(message) | |
63 raise Exception(message) | |
64 _proportion = 0 | |
106 | 65 id = elem.getAttribute("id") |
103 | 66 name = elem.getAttribute("name") |
67 type = elem.getAttribute("type") | |
68 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' | |
69 if type=="empty": | |
70 ctrl = wx.Window(parent, -1) | |
71 elif type=="text": | |
72 try: | |
73 value = elem.childNodes[0].wholeText | |
74 except KeyError: | |
75 warning (_("text node has no child !")) | |
76 ctrl = wx.StaticText(parent, -1, value) | |
77 elif type=="label": | |
78 ctrl = wx.StaticText(parent, -1, value+": ") | |
79 elif type=="string": | |
80 ctrl = wx.TextCtrl(parent, -1, value) | |
81 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
82 _proportion = 1 | |
83 elif type=="password": | |
84 ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_PASSWORD) | |
85 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
86 _proportion = 1 | |
87 elif type=="list": | |
88 ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=wx.LB_SINGLE) | |
89 self.ctl_list.append({'name':name, 'type':type, 'control':ctrl}) | |
90 _proportion = 1 | |
105 | 91 elif type=="button": |
106 | 92 callback_id = elem.getAttribute("callback_id") |
93 ctrl = wx.Button(parent, -1, value) | |
94 ctrl.param_id = callback_id | |
95 parent.Bind(wx.EVT_BUTTON, self.onButtonClicked, ctrl) | |
103 | 96 else: |
97 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! | |
98 raise NotImplementedError | |
104 | 99 parent.sizer.Add(ctrl, _proportion, flag=wx.EXPAND) |
103 | 100 |
104 | 101 def __parseChilds(self, parent, current_param, elem, wanted = ['layout']): |
102 """Recursively parse childNodes of an elemen | |
103 @param parent: parent wx.Window | |
104 @param current_param: current wx.Window (often wx.Panel) or None if we must create one | |
105 @param elem: element from which childs will be parsed | |
106 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant""" | |
107 for node in elem.childNodes: | |
108 if wanted and not node.nodeName in wanted: | |
109 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
110 if node.nodeName == "layout": | |
105 | 111 _proportion = 0 |
104 | 112 type = node.getAttribute('type') |
113 if type == "tabs": | |
114 current = wx.Notebook(parent, -1, style=wx.NB_LEFT if self.type=='param' else 0) | |
115 self.__parseChilds(current, None, node, ['category']) | |
105 | 116 _proportion = 1 |
104 | 117 else: |
118 if current_param == None: | |
119 current = wx.Panel(parent, -1) | |
120 else: | |
121 current = current_param | |
122 if type == "vertical": | |
123 current.sizer = wx.BoxSizer(wx.VERTICAL) | |
124 elif type == "pairs": | |
125 current.sizer = wx.FlexGridSizer(cols=2) | |
126 current.sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs | |
127 else: | |
128 warning(_("Unknown layout, using default one")) | |
129 current.sizer = wx.BoxSizer(wx.VERTICAL) | |
130 current.SetSizer(current.sizer) | |
131 self.__parseElems(node, current) | |
132 if parent: | |
105 | 133 parent.sizer.Add(current, _proportion, flag=wx.EXPAND) |
104 | 134 elif node.nodeName == "category": |
135 name = node.getAttribute('name') | |
136 if not node.nodeName in wanted or not name or not isinstance(parent,wx.Notebook): | |
137 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
138 notebook = parent | |
139 tab_panel = wx.Panel(notebook, -1) | |
140 tab_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
141 tab_panel.SetSizer(tab_panel.sizer) | |
142 notebook.AddPage(tab_panel, name) | |
143 self.__parseChilds(tab_panel, None, node, ['layout']) | |
144 | |
145 else: | |
146 message=_("Unknown tag") | |
147 error(message) | |
148 raise Exception(message) #TODO: raise a custom exception here | |
103 | 149 |
150 | |
151 def constructUI(self, xml_data): | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 panel=wx.Panel(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 panel.sizer = wx.BoxSizer(wx.VERTICAL) |
104 | 154 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 cat_dom = minidom.parseString(xml_data.encode('utf-8')) |
103 | 156 top= cat_dom.documentElement |
157 self.type = top.getAttribute("type") | |
158 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: | |
104 | 159 raise Exception("Invalid XMLUI") #TODO: make a custom exception |
103 | 160 |
104 | 161 self.__parseChilds(panel, None, cat_dom.documentElement) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 |
103 | 163 if self.type == 'form': |
164 dialogButtons = wx.StdDialogButtonSizer() | |
165 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit")) | |
166 dialogButtons.AddButton(submitButton) | |
167 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton) | |
168 if not 'NO_CANCEL' in self.options: | |
169 cancelButton = wx.Button(panel,wx.ID_CANCEL) | |
170 dialogButtons.AddButton(cancelButton) | |
171 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton) | |
172 dialogButtons.Realize() | |
173 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
|
174 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 panel.SetSizer(panel.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 panel.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 panel.sizer.Fit(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 self.sizer.Add(panel, 1, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 cat_dom.unlink() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 |
106 | 181 ###events |
182 | |
183 def onButtonClicked(self, event): | |
184 """Called when a button is pushed""" | |
185 callback_id = event.GetEventObject().param_id | |
186 data = {"callback_id":callback_id} | |
187 id = self.host.bridge.launchAction("button", data) | |
188 self.host.current_action_ids.add(id) | |
189 event.Skip() | |
190 | |
36 | 191 def onFormSubmitted(self, event): |
192 """Called when submit button is clicked""" | |
70 | 193 debug(_("Submitting form")) |
36 | 194 data = [] |
195 for ctrl in self.ctl_list: | |
91 | 196 if isinstance(ctrl['control'], wx.ListBox): |
197 data.append((ctrl['name'], ctrl['control'].GetStringSelection())) | |
198 else: | |
199 data.append((ctrl["name"], ctrl["control"].GetValue())) | |
200 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned | |
201 id = self.misc['action_back']("SUBMIT",self.misc['target'], data) | |
202 self.host.current_action_ids.add(id) | |
203 elif self.misc.has_key('callback'): | |
204 self.misc['callback'](data) | |
205 else: | |
206 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
|
207 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
208 self.Destroy() |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
36 | 210 def onFormCancelled(self, event): |
211 """Called when cancel button is clicked""" | |
70 | 212 debug(_("Cancelling form")) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
213 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
214 self.Close() |
36 | 215 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 def onClose(self, event): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 """Close event: we have to send the form.""" |
70 | 218 debug(_("close")) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 self.MakeModal(False) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 event.Skip() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |