Mercurial > libervia-backend
comparison frontends/primitivus/xmlui.py @ 144:80661755ea8d
Primitivus: Tarot card game implementation
- quick frontend: card_game added
- wix: card_game splitted with quick frontend
- tools: new game library
- primitivus: new card_game widget (not finished yet)
- primitivus: SàT XML UI management: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 26 Jul 2010 19:43:44 +0800 |
parents | |
children | 3c3f70c01333 |
comparison
equal
deleted
inserted
replaced
143:119f45746fde | 144:80661755ea8d |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: 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 import urwid | |
23 import custom_widgets | |
24 from xml.dom import minidom | |
25 | |
26 class Pairs(urwid.WidgetWrap): | |
27 | |
28 def __init__(self, weight_0='1', weight_1='1'): | |
29 self.idx = 0 | |
30 self.weight_0 = weight_0 | |
31 self.weight_1 = weight_1 | |
32 columns = urwid.Columns([urwid.Text(''), urwid.Text('')]) | |
33 #XXX: empty Text hack needed because Pile doesn't support empty list | |
34 urwid.WidgetWrap.__init__(self,columns) | |
35 | |
36 def append(self, widget): | |
37 pile = self._w.widget_list[self.idx] | |
38 if pile.__class__ == urwid.Text: | |
39 self._w.widget_list[self.idx] = urwid.Pile([widget]) | |
40 else: | |
41 pile.widget_list.append(widget) | |
42 pile.item_types.append(('weight',getattr(self,'weight_'+str(self.idx)))) | |
43 self.idx = (self.idx + 1) % 2 | |
44 | |
45 | |
46 class XMLUI(urwid.WidgetWrap): | |
47 | |
48 def __init__(self, host, xml_data, title = None, options = [], misc={}): | |
49 self.host = host | |
50 self.title = title | |
51 self.options = options | |
52 self.misc = misc | |
53 self.ctrl_list = {} # usefull to access ctrl | |
54 widget = self.constructUI(xml_data) | |
55 urwid.WidgetWrap.__init__(self,widget) | |
56 | |
57 def __parseElems(self, node, parent): | |
58 """Parse elements inside a <layout> tags, and add them to the parent""" | |
59 for elem in node.childNodes: | |
60 if elem.nodeName != "elem": | |
61 message=_("Unmanaged tag") | |
62 error(message) | |
63 raise Exception(message) | |
64 id = elem.getAttribute("id") | |
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 = urwid.Text('') | |
70 elif type=="text": | |
71 try: | |
72 value = elem.childNodes[0].wholeText | |
73 except KeyError: | |
74 warning (_("text node has no child !")) | |
75 ctrl = urwid.Text(value) | |
76 elif type=="label": | |
77 ctrl = urwid.Text(value+": ") | |
78 elif type=="string": | |
79 ctrl = urwid.Edit(edit_text = value) | |
80 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
81 elif type=="password": | |
82 ctrl = custom_widgets.Password(edit_text = value) | |
83 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
84 elif type=="textbox": | |
85 ctrl = urwid.Edit(edit_text = value, multiline=True) | |
86 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
87 elif type=="list": | |
88 style=[] if elem.getAttribute("multi")=='yes' else ['single'] | |
89 ctrl = custom_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style) | |
90 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) | |
91 elif type=="button": | |
92 callback_id = elem.getAttribute("callback_id") | |
93 ctrl = urwid.Button(value, on_press=self.onButtonPress) | |
94 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) | |
95 else: | |
96 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! | |
97 raise NotImplementedError | |
98 parent.append(ctrl) | |
99 | |
100 def __parseChilds(self, current, elem, wanted = ['layout']): | |
101 """Recursively parse childNodes of an elemen | |
102 @param current: widget container with 'append' method | |
103 @param elem: element from which childs will be parsed | |
104 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant""" | |
105 for node in elem.childNodes: | |
106 if wanted and not node.nodeName in wanted: | |
107 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
108 if node.nodeName == "layout": | |
109 type = node.getAttribute('type') | |
110 if type == "tabs": | |
111 raise NotImplementedError | |
112 self.__parseChilds(current, None, node, ['category']) | |
113 else: | |
114 if type == "vertical": | |
115 pass | |
116 elif type == "pairs": | |
117 pairs = Pairs() | |
118 current.append(pairs) | |
119 current = pairs | |
120 else: | |
121 warning(_("Unknown layout, using default one")) | |
122 self.__parseElems(node, current) | |
123 elif node.nodeName == "category": | |
124 raise NotImplementedError | |
125 """name = node.getAttribute('name') | |
126 if not node.nodeName in wanted or not name or not isinstance(parent,wx.Notebook): | |
127 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
128 notebook = parent | |
129 tab_panel = wx.Panel(notebook, -1) | |
130 tab_panel.sizer = wx.BoxSizer(wx.VERTICAL) | |
131 tab_panel.SetSizer(tab_panel.sizer) | |
132 notebook.AddPage(tab_panel, name) | |
133 self.__parseChilds(tab_panel, None, node, ['layout'])""" | |
134 | |
135 else: | |
136 message=_("Unknown tag") | |
137 error(message) | |
138 raise Exception(message) #TODO: raise a custom exception here | |
139 | |
140 def constructUI(self, xml_data): | |
141 | |
142 list_box = urwid.ListBox(urwid.SimpleListWalker([])) | |
143 | |
144 cat_dom = minidom.parseString(xml_data.encode('utf-8')) | |
145 top= cat_dom.documentElement | |
146 self.type = top.getAttribute("type") | |
147 if not self.title: | |
148 self.title = top.getAttribute("title") #TODO: manage title | |
149 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: | |
150 raise Exception("Invalid XMLUI") #TODO: make a custom exception | |
151 | |
152 self.__parseChilds(list_box.body, cat_dom.documentElement) | |
153 | |
154 if self.type == 'form': | |
155 buttons = [] | |
156 buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted)) | |
157 if not 'NO_CANCEL' in self.options: | |
158 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled)) | |
159 max_len = max([len(button.get_label()) for button in buttons]) | |
160 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center') | |
161 list_box.body.append(grid_wid) | |
162 | |
163 | |
164 return list_box | |
165 | |
166 def show(self): | |
167 """Show the constructed UI""" | |
168 decorated = custom_widgets.LabelLine(self, custom_widgets.SurroundedText(self.title or '')) | |
169 self.host.showPopUp(decorated) | |
170 self.host.redraw() | |
171 | |
172 | |
173 ##EVENTS## | |
174 | |
175 def onButtonPress(self, button): | |
176 self.host.debug() | |
177 | |
178 def onFormSubmitted(self, button): | |
179 data = [] | |
180 for ctrl_name in self.ctrl_list: | |
181 ctrl = self.ctrl_list[ctrl_name] | |
182 if isinstance(ctrl['control'], custom_widgets.List): | |
183 data.append((ctrl_name, ctrl['control'].getSelectedValue())) | |
184 else: | |
185 data.append((ctrl_name, ctrl['control'].get_edit_text())) | |
186 if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned | |
187 raise NotImplementedError | |
188 self.host.debug() | |
189 elif self.misc.has_key('callback'): | |
190 self.misc['callback'](data) | |
191 else: | |
192 warning (_("The form data is not sent back, the type is not managed properly")) | |
193 self.host.removePopUp() | |
194 | |
195 def onFormCancelled(self, button): | |
196 self.host.removePopUp() |