Mercurial > libervia-backend
annotate frontends/src/wix/xmlui.py @ 610:c8c07c920e30
Primitivus: fixed xmlui/chat bad contents change
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Mar 2013 20:54:45 +0100 |
parents | 84a6e83157c2 |
children | 93bd868b8fb6 |
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 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
4 # wix: a SAT frontend |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
10 # (at your option) any later version. |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
15 # GNU Affero General Public License for more details. |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
587
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
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 import wx |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import pdb |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from xml.dom import minidom |
91 | 25 from logging import debug, info, warning, error |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
26 from sat.tools.jid import JID |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
103 | 29 class XMLUI(wx.Frame): |
30 """Create an user interface from a SàT xml""" | |
91 | 31 |
555
1d58758689e9
primitivus, wix: fixed bad defaults values for XMLUI class
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
32 def __init__(self, host, xml_data='', title="Form", options = None, misc = None): |
92 | 33 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 | 34 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
|
35 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 self.host = host |
555
1d58758689e9
primitivus, wix: fixed bad defaults values for XMLUI class
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
37 self.options = options or [] |
1d58758689e9
primitivus, wix: fixed bad defaults values for XMLUI class
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
38 self.misc = misc or {} |
107 | 39 self.ctrl_list = {} # usefull to access ctrl |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 self.sizer = wx.BoxSizer(wx.VERTICAL) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 self.SetSizer(self.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 self.SetAutoLayout(True) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
44 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 #events |
91 | 46 if not 'NO_CANCEL' in self.options: |
47 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
48 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.MakeModal() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
103 | 51 self.constructUI(xml_data) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.Show() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
104 | 55 def __parseElems(self, node, parent): |
56 """Parse elements inside a <layout> tags, and add them to the parent sizer""" | |
57 for elem in node.childNodes: | |
103 | 58 if elem.nodeName != "elem": |
59 message=_("Unmanaged tag") | |
60 error(message) | |
61 raise Exception(message) | |
62 _proportion = 0 | |
106 | 63 id = elem.getAttribute("id") |
103 | 64 name = elem.getAttribute("name") |
65 type = elem.getAttribute("type") | |
66 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' | |
67 if type=="empty": | |
68 ctrl = wx.Window(parent, -1) | |
69 elif type=="text": | |
70 try: | |
71 value = elem.childNodes[0].wholeText | |
149 | 72 except IndexError: |
103 | 73 warning (_("text node has no child !")) |
74 ctrl = wx.StaticText(parent, -1, value) | |
75 elif type=="label": | |
76 ctrl = wx.StaticText(parent, -1, value+": ") | |
77 elif type=="string": | |
78 ctrl = wx.TextCtrl(parent, -1, value) | |
107 | 79 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) |
103 | 80 _proportion = 1 |
81 elif type=="password": | |
82 ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_PASSWORD) | |
107 | 83 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
84 _proportion = 1 |
107 | 85 elif type=="textbox": |
86 ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_MULTILINE) | |
87 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
103 | 88 _proportion = 1 |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
89 elif type=="bool": |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
90 ctrl = wx.CheckBox(panel, -1, "", style = wx.CHK_2STATE) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
91 ctrl.SetValue(value=="true") |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
92 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
93 _proportion = 1 |
103 | 94 elif type=="list": |
107 | 95 style=wx.LB_MULTIPLE if elem.getAttribute("multi")=='yes' else wx.LB_SINGLE |
96 ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style) | |
97 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
103 | 98 _proportion = 1 |
105 | 99 elif type=="button": |
106 | 100 callback_id = elem.getAttribute("callback_id") |
101 ctrl = wx.Button(parent, -1, value) | |
107 | 102 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) |
106 | 103 parent.Bind(wx.EVT_BUTTON, self.onButtonClicked, ctrl) |
103 | 104 else: |
105 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! | |
106 raise NotImplementedError | |
104 | 107 parent.sizer.Add(ctrl, _proportion, flag=wx.EXPAND) |
103 | 108 |
104 | 109 def __parseChilds(self, parent, current_param, elem, wanted = ['layout']): |
110 """Recursively parse childNodes of an elemen | |
111 @param parent: parent wx.Window | |
112 @param current_param: current wx.Window (often wx.Panel) or None if we must create one | |
113 @param elem: element from which childs will be parsed | |
114 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant""" | |
115 for node in elem.childNodes: | |
116 if wanted and not node.nodeName in wanted: | |
117 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
118 if node.nodeName == "layout": | |
105 | 119 _proportion = 0 |
104 | 120 type = node.getAttribute('type') |
121 if type == "tabs": | |
122 current = wx.Notebook(parent, -1, style=wx.NB_LEFT if self.type=='param' else 0) | |
123 self.__parseChilds(current, None, node, ['category']) | |
105 | 124 _proportion = 1 |
104 | 125 else: |
126 if current_param == None: | |
127 current = wx.Panel(parent, -1) | |
128 else: | |
129 current = current_param | |
130 if type == "vertical": | |
131 current.sizer = wx.BoxSizer(wx.VERTICAL) | |
132 elif type == "pairs": | |
133 current.sizer = wx.FlexGridSizer(cols=2) | |
134 current.sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs | |
135 else: | |
136 warning(_("Unknown layout, using default one")) | |
137 current.sizer = wx.BoxSizer(wx.VERTICAL) | |
138 current.SetSizer(current.sizer) | |
139 self.__parseElems(node, current) | |
140 if parent: | |
105 | 141 parent.sizer.Add(current, _proportion, flag=wx.EXPAND) |
104 | 142 elif node.nodeName == "category": |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
143 name = node.getAttribute('name') |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
144 label = node.getAttribute('label') |
104 | 145 if not node.nodeName in wanted or not name or not isinstance(parent,wx.Notebook): |
146 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
147 notebook = parent | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
148 tab_panel = wx.Panel(notebook, -1) |
104 | 149 tab_panel.sizer = wx.BoxSizer(wx.VERTICAL) |
150 tab_panel.SetSizer(tab_panel.sizer) | |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
151 notebook.AddPage(tab_panel, label or name) |
104 | 152 self.__parseChilds(tab_panel, None, node, ['layout']) |
153 | |
154 else: | |
155 message=_("Unknown tag") | |
156 error(message) | |
157 raise Exception(message) #TODO: raise a custom exception here | |
103 | 158 |
159 | |
160 def constructUI(self, xml_data): | |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 panel=wx.Panel(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 panel.sizer = wx.BoxSizer(wx.VERTICAL) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
163 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 cat_dom = minidom.parseString(xml_data.encode('utf-8')) |
103 | 165 top= cat_dom.documentElement |
166 self.type = top.getAttribute("type") | |
107 | 167 self.title = top .getAttribute("title") |
168 if self.title: | |
169 self.SetTitle(self.title) | |
103 | 170 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: |
104 | 171 raise Exception("Invalid XMLUI") #TODO: make a custom exception |
103 | 172 |
104 | 173 self.__parseChilds(panel, None, cat_dom.documentElement) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
103 | 175 if self.type == 'form': |
176 dialogButtons = wx.StdDialogButtonSizer() | |
177 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit")) | |
178 dialogButtons.AddButton(submitButton) | |
179 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton) | |
180 if not 'NO_CANCEL' in self.options: | |
181 cancelButton = wx.Button(panel,wx.ID_CANCEL) | |
182 dialogButtons.AddButton(cancelButton) | |
183 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton) | |
184 dialogButtons.Realize() | |
185 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
|
186 |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 panel.SetSizer(panel.sizer) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 panel.SetAutoLayout(True) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 panel.sizer.Fit(self) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 self.sizer.Add(panel, 1, flag=wx.EXPAND) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 cat_dom.unlink() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 |
106 | 193 ###events |
194 | |
195 def onButtonClicked(self, event): | |
196 """Called when a button is pushed""" | |
107 | 197 callback_id, fields = event.GetEventObject().param_id |
106 | 198 data = {"callback_id":callback_id} |
107 | 199 for field in fields: |
200 ctrl = self.ctrl_list[field] | |
201 if isinstance(ctrl['control'], wx.ListBox): | |
202 data[field] = '\t'.join([ctrl['control'].GetString(idx) for idx in ctrl['control'].GetSelections()]) | |
203 else: | |
204 data[field] = ctrl['control'].GetValue() | |
205 | |
135
7452ac3818e7
Core, wix: added profile management for RegisterNewAccount method, and LaunchAction
Goffi <goffi@goffi.org>
parents:
107
diff
changeset
|
206 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile) |
106 | 207 self.host.current_action_ids.add(id) |
208 event.Skip() | |
209 | |
36 | 210 def onFormSubmitted(self, event): |
211 """Called when submit button is clicked""" | |
70 | 212 debug(_("Submitting form")) |
36 | 213 data = [] |
107 | 214 for ctrl_name in self.ctrl_list: |
215 ctrl = self.ctrl_list[ctrl_name] | |
91 | 216 if isinstance(ctrl['control'], wx.ListBox): |
107 | 217 data.append((ctrl_name, ctrl['control'].GetStringSelection())) |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
218 elif isinstance(ctrl['control'], wx.CheckBox): |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
149
diff
changeset
|
219 data.append((ctrl_name, "true" if ctrl['control'].GetValue() else "false")) |
91 | 220 else: |
107 | 221 data.append((ctrl_name, ctrl['control'].GetValue())) |
91 | 222 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned |
223 id = self.misc['action_back']("SUBMIT",self.misc['target'], data) | |
224 self.host.current_action_ids.add(id) | |
225 elif self.misc.has_key('callback'): | |
226 self.misc['callback'](data) | |
227 else: | |
228 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
|
229 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
230 self.Destroy() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
231 |
36 | 232 def onFormCancelled(self, event): |
233 """Called when cancel button is clicked""" | |
70 | 234 debug(_("Cancelling form")) |
37
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
235 self.MakeModal(False) |
a61beb21d16d
Gateway registration, unregistration & edition
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
236 self.Close() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
237 |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 def onClose(self, event): |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 """Close event: we have to send the form.""" |
70 | 240 debug(_("close")) |
35
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 self.MakeModal(False) |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 event.Skip() |
c45deebb40a5
Wix: Registration form management (not finished yet)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |