comparison frontends/src/wix/xmlui.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents frontends/wix/xmlui.py@9ee4a1d0d7fb
children fd9b7834d98a
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
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.ctrl_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 __parseElems(self, node, parent):
58 """Parse elements inside a <layout> tags, and add them to the parent sizer"""
59 for elem in node.childNodes:
60 if elem.nodeName != "elem":
61 message=_("Unmanaged tag")
62 error(message)
63 raise Exception(message)
64 _proportion = 0
65 id = elem.getAttribute("id")
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 IndexError:
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.ctrl_list[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.ctrl_list[name] = ({'type':type, 'control':ctrl})
86 _proportion = 1
87 elif type=="textbox":
88 ctrl = wx.TextCtrl(parent, -1, value, style=wx.TE_MULTILINE)
89 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
90 _proportion = 1
91 elif type=="bool":
92 ctrl = wx.CheckBox(panel, -1, "", style = wx.CHK_2STATE)
93 ctrl.SetValue(value=="true")
94 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
95 _proportion = 1
96 elif type=="list":
97 style=wx.LB_MULTIPLE if elem.getAttribute("multi")=='yes' else wx.LB_SINGLE
98 ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style)
99 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
100 _proportion = 1
101 elif type=="button":
102 callback_id = elem.getAttribute("callback_id")
103 ctrl = wx.Button(parent, -1, value)
104 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
105 parent.Bind(wx.EVT_BUTTON, self.onButtonClicked, ctrl)
106 else:
107 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
108 raise NotImplementedError
109 parent.sizer.Add(ctrl, _proportion, flag=wx.EXPAND)
110
111 def __parseChilds(self, parent, current_param, elem, wanted = ['layout']):
112 """Recursively parse childNodes of an elemen
113 @param parent: parent wx.Window
114 @param current_param: current wx.Window (often wx.Panel) or None if we must create one
115 @param elem: element from which childs will be parsed
116 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant"""
117 for node in elem.childNodes:
118 if wanted and not node.nodeName in wanted:
119 raise Exception("Invalid XMLUI") #TODO: make a custom exception
120 if node.nodeName == "layout":
121 _proportion = 0
122 type = node.getAttribute('type')
123 if type == "tabs":
124 current = wx.Notebook(parent, -1, style=wx.NB_LEFT if self.type=='param' else 0)
125 self.__parseChilds(current, None, node, ['category'])
126 _proportion = 1
127 else:
128 if current_param == None:
129 current = wx.Panel(parent, -1)
130 else:
131 current = current_param
132 if type == "vertical":
133 current.sizer = wx.BoxSizer(wx.VERTICAL)
134 elif type == "pairs":
135 current.sizer = wx.FlexGridSizer(cols=2)
136 current.sizer.AddGrowableCol(1) #The growable column need most of time to be the right one in pairs
137 else:
138 warning(_("Unknown layout, using default one"))
139 current.sizer = wx.BoxSizer(wx.VERTICAL)
140 current.SetSizer(current.sizer)
141 self.__parseElems(node, current)
142 if parent:
143 parent.sizer.Add(current, _proportion, flag=wx.EXPAND)
144 elif node.nodeName == "category":
145 name = node.getAttribute('name')
146 label = node.getAttribute('label')
147 if not node.nodeName in wanted or not name or not isinstance(parent,wx.Notebook):
148 raise Exception("Invalid XMLUI") #TODO: make a custom exception
149 notebook = parent
150 tab_panel = wx.Panel(notebook, -1)
151 tab_panel.sizer = wx.BoxSizer(wx.VERTICAL)
152 tab_panel.SetSizer(tab_panel.sizer)
153 notebook.AddPage(tab_panel, label or name)
154 self.__parseChilds(tab_panel, None, node, ['layout'])
155
156 else:
157 message=_("Unknown tag")
158 error(message)
159 raise Exception(message) #TODO: raise a custom exception here
160
161
162 def constructUI(self, xml_data):
163 panel=wx.Panel(self)
164 panel.sizer = wx.BoxSizer(wx.VERTICAL)
165
166 cat_dom = minidom.parseString(xml_data.encode('utf-8'))
167 top= cat_dom.documentElement
168 self.type = top.getAttribute("type")
169 self.title = top .getAttribute("title")
170 if self.title:
171 self.SetTitle(self.title)
172 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
173 raise Exception("Invalid XMLUI") #TODO: make a custom exception
174
175 self.__parseChilds(panel, None, cat_dom.documentElement)
176
177 if self.type == 'form':
178 dialogButtons = wx.StdDialogButtonSizer()
179 submitButton = wx.Button(panel,wx.ID_OK, label=_("Submit"))
180 dialogButtons.AddButton(submitButton)
181 panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
182 if not 'NO_CANCEL' in self.options:
183 cancelButton = wx.Button(panel,wx.ID_CANCEL)
184 dialogButtons.AddButton(cancelButton)
185 panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
186 dialogButtons.Realize()
187 panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)
188
189 panel.SetSizer(panel.sizer)
190 panel.SetAutoLayout(True)
191 panel.sizer.Fit(self)
192 self.sizer.Add(panel, 1, flag=wx.EXPAND)
193 cat_dom.unlink()
194
195 ###events
196
197 def onButtonClicked(self, event):
198 """Called when a button is pushed"""
199 callback_id, fields = event.GetEventObject().param_id
200 data = {"callback_id":callback_id}
201 for field in fields:
202 ctrl = self.ctrl_list[field]
203 if isinstance(ctrl['control'], wx.ListBox):
204 data[field] = '\t'.join([ctrl['control'].GetString(idx) for idx in ctrl['control'].GetSelections()])
205 else:
206 data[field] = ctrl['control'].GetValue()
207
208 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
209 self.host.current_action_ids.add(id)
210 event.Skip()
211
212 def onFormSubmitted(self, event):
213 """Called when submit button is clicked"""
214 debug(_("Submitting form"))
215 data = []
216 for ctrl_name in self.ctrl_list:
217 ctrl = self.ctrl_list[ctrl_name]
218 if isinstance(ctrl['control'], wx.ListBox):
219 data.append((ctrl_name, ctrl['control'].GetStringSelection()))
220 elif isinstance(ctrl['control'], wx.CheckBox):
221 data.append((ctrl_name, "true" if ctrl['control'].GetValue() else "false"))
222 else:
223 data.append((ctrl_name, ctrl['control'].GetValue()))
224 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
225 id = self.misc['action_back']("SUBMIT",self.misc['target'], data)
226 self.host.current_action_ids.add(id)
227 elif self.misc.has_key('callback'):
228 self.misc['callback'](data)
229 else:
230 warning (_("The form data is not sent back, the type is not managed properly"))
231 self.MakeModal(False)
232 self.Destroy()
233
234 def onFormCancelled(self, event):
235 """Called when cancel button is clicked"""
236 debug(_("Cancelling form"))
237 self.MakeModal(False)
238 self.Close()
239
240 def onClose(self, event):
241 """Close event: we have to send the form."""
242 debug(_("close"))
243 self.MakeModal(False)
244 event.Skip()
245