Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
102:94011f553cd0 | 103:6be927a465ed |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 wix: a SAT frontend | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 | |
23 | |
24 import wx | |
25 import pdb | |
26 from xml.dom import minidom | |
27 from logging import debug, info, warning, error | |
28 from tools.jid import JID | |
29 | |
30 | |
31 class XMLUI(wx.Frame): | |
32 """Create an user interface from a SàT xml""" | |
33 | |
34 def __init__(self, host, xml_data='', title="Form", options=[], misc={}): | |
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 | |
36 super(XMLUI, self).__init__(None, title=title, style=style) | |
37 | |
38 self.host = host | |
39 self.options = options | |
40 self.misc = misc | |
41 self.ctl_list = [] # usefull to access ctrl | |
42 | |
43 self.sizer = wx.BoxSizer(wx.VERTICAL) | |
44 self.SetSizer(self.sizer) | |
45 self.SetAutoLayout(True) | |
46 | |
47 #events | |
48 if not 'NO_CANCEL' in self.options: | |
49 self.Bind(wx.EVT_CLOSE, self.onClose, self) | |
50 | |
51 self.MakeModal() | |
52 | |
53 self.constructUI(xml_data) | |
54 | |
55 self.Show() | |
56 | |
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): | |
98 panel=wx.Panel(self) | |
99 panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
100 | |
101 cat_dom = minidom.parseString(xml_data.encode('utf-8')) | |
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) | |
123 else: | |
124 message=_("Unknown tag") | |
125 error(message) | |
126 raise Exception(message) #TODO: raise a custom exception here | |
127 | |
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) | |
139 | |
140 panel.SetSizer(panel.sizer) | |
141 panel.SetAutoLayout(True) | |
142 panel.sizer.Fit(self) | |
143 self.sizer.Add(panel, 1, flag=wx.EXPAND) | |
144 cat_dom.unlink() | |
145 | |
146 def onFormSubmitted(self, event): | |
147 """Called when submit button is clicked""" | |
148 debug(_("Submitting form")) | |
149 data = [] | |
150 for ctrl in self.ctl_list: | |
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")) | |
162 self.MakeModal(False) | |
163 self.Destroy() | |
164 | |
165 def onFormCancelled(self, event): | |
166 """Called when cancel button is clicked""" | |
167 debug(_("Cancelling form")) | |
168 self.MakeModal(False) | |
169 self.Close() | |
170 | |
171 def onClose(self, event): | |
172 """Close event: we have to send the form.""" | |
173 debug(_("close")) | |
174 self.MakeModal(False) | |
175 event.Skip() | |
176 |