comparison frontends/primitivus/xmlui.py @ 222:3198bfd66daa

primitivus: refactoring to use urwid-satext which is now a separate project
author Goffi <goffi@goffi.org>
date Tue, 28 Dec 2010 23:10:13 +0100
parents 879beacb8e16
children
comparison
equal deleted inserted replaced
221:96186f36d8cb 222:3198bfd66daa
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 import urwid 22 import urwid
23 from logging import debug, info, warning, error 23 from logging import debug, info, warning, error
24 import custom_widgets 24 from urwid_satext import sat_widgets
25 from xml.dom import minidom 25 from xml.dom import minidom
26 26
27 class Pairs(urwid.WidgetWrap): 27 class Pairs(urwid.WidgetWrap):
28 28
29 def __init__(self, weight_0='1', weight_1='1'): 29 def __init__(self, weight_0='1', weight_1='1'):
80 warning (_("text node has no child !")) 80 warning (_("text node has no child !"))
81 ctrl = urwid.Text(value) 81 ctrl = urwid.Text(value)
82 elif type=="label": 82 elif type=="label":
83 ctrl = urwid.Text(value+": ") 83 ctrl = urwid.Text(value+": ")
84 elif type=="string": 84 elif type=="string":
85 ctrl = custom_widgets.AdvancedEdit(edit_text = value) 85 ctrl = sat_widgets.AdvancedEdit(edit_text = value)
86 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 86 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
87 elif type=="password": 87 elif type=="password":
88 ctrl = custom_widgets.Password(edit_text = value) 88 ctrl = sat_widgets.Password(edit_text = value)
89 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 89 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
90 elif type=="textbox": 90 elif type=="textbox":
91 ctrl = custom_widgets.AdvancedEdit(edit_text = value, multiline=True) 91 ctrl = sat_widgets.AdvancedEdit(edit_text = value, multiline=True)
92 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 92 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
93 elif type=="bool": 93 elif type=="bool":
94 ctrl = urwid.CheckBox('', state = value=="true") 94 ctrl = urwid.CheckBox('', state = value=="true")
95 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 95 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
96 elif type=="list": 96 elif type=="list":
97 style=[] if elem.getAttribute("multi")=='yes' else ['single'] 97 style=[] if elem.getAttribute("multi")=='yes' else ['single']
98 ctrl = custom_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style) 98 ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style)
99 self.ctrl_list[name] = ({'type':type, 'control':ctrl}) 99 self.ctrl_list[name] = ({'type':type, 'control':ctrl})
100 elif type=="button": 100 elif type=="button":
101 callback_id = elem.getAttribute("callback_id") 101 callback_id = elem.getAttribute("callback_id")
102 ctrl = custom_widgets.CustomButton(value, on_press=self.onButtonPress) 102 ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress)
103 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) 103 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
104 else: 104 else:
105 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! 105 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
106 raise NotImplementedError 106 raise NotImplementedError
107 if self.type == 'param': 107 if self.type == 'param':
120 if wanted and not node.nodeName in wanted: 120 if wanted and not node.nodeName in wanted:
121 raise InvalidXMLUI 121 raise InvalidXMLUI
122 if node.nodeName == "layout": 122 if node.nodeName == "layout":
123 type = node.getAttribute('type') 123 type = node.getAttribute('type')
124 if type == "tabs": 124 if type == "tabs":
125 tab_cont = custom_widgets.TabsContainer() 125 tab_cont = sat_widgets.TabsContainer()
126 self.__parseChilds(current, node, ['category'], tab_cont) 126 self.__parseChilds(current, node, ['category'], tab_cont)
127 current.append(tab_cont) 127 current.append(tab_cont)
128 elif type == "vertical": 128 elif type == "vertical":
129 self.__parseElems(node, current) 129 self.__parseElems(node, current)
130 elif type == "pairs": 130 elif type == "pairs":
135 warning(_("Unknown layout, using default one")) 135 warning(_("Unknown layout, using default one"))
136 self.__parseElems(node, current) 136 self.__parseElems(node, current)
137 elif node.nodeName == "category": 137 elif node.nodeName == "category":
138 name = node.getAttribute('name') 138 name = node.getAttribute('name')
139 label = node.getAttribute('label') 139 label = node.getAttribute('label')
140 if not name or not isinstance(data,custom_widgets.TabsContainer): 140 if not name or not isinstance(data,sat_widgets.TabsContainer):
141 raise InvalidXMLUI 141 raise InvalidXMLUI
142 if self.type == 'param': 142 if self.type == 'param':
143 self._current_category = name #XXX: awful hack because params need category and we don't keep parent 143 self._current_category = name #XXX: awful hack because params need category and we don't keep parent
144 tab_cont = data 144 tab_cont = data
145 listbox = tab_cont.addTab(label or name) 145 listbox = tab_cont.addTab(label or name)
165 165
166 self.__parseChilds(ret_wid.body, cat_dom.documentElement) 166 self.__parseChilds(ret_wid.body, cat_dom.documentElement)
167 167
168 assert ret_wid.body 168 assert ret_wid.body
169 169
170 if isinstance(ret_wid.body[0],custom_widgets.TabsContainer): 170 if isinstance(ret_wid.body[0],sat_widgets.TabsContainer):
171 ret_wid = ret_wid.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox 171 ret_wid = ret_wid.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox
172 172
173 173
174 if self.type == 'form': 174 if self.type == 'form':
175 buttons = [] 175 buttons = []
178 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled)) 178 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled))
179 max_len = max([len(button.get_label()) for button in buttons]) 179 max_len = max([len(button.get_label()) for button in buttons])
180 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center') 180 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center')
181 ret_wid.body.append(grid_wid) 181 ret_wid.body.append(grid_wid)
182 elif self.type == 'param': 182 elif self.type == 'param':
183 assert(isinstance(ret_wid,custom_widgets.TabsContainer)) 183 assert(isinstance(ret_wid,sat_widgets.TabsContainer))
184 buttons = [] 184 buttons = []
185 buttons.append(custom_widgets.CustomButton(_('Save'),self.onSaveParams)) 185 buttons.append(sat_widgets.CustomButton(_('Save'),self.onSaveParams))
186 buttons.append(custom_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow())) 186 buttons.append(sat_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow()))
187 max_len = max([button.getSize() for button in buttons]) 187 max_len = max([button.getSize() for button in buttons])
188 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center') 188 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
189 ret_wid.addFooter(grid_wid) 189 ret_wid.addFooter(grid_wid)
190 return ret_wid 190 return ret_wid
191 191
193 """Show the constructed UI 193 """Show the constructed UI
194 @param show_type: how to show the UI: 194 @param show_type: how to show the UI:
195 - popup 195 - popup
196 - window""" 196 - window"""
197 self.__dest = "popup" 197 self.__dest = "popup"
198 decorated = custom_widgets.LabelLine(self, custom_widgets.SurroundedText(self.title or '')) 198 decorated = sat_widgets.LabelLine(self, sat_widgets.SurroundedText(self.title or ''))
199 if show_type == 'popup': 199 if show_type == 'popup':
200 self.host.showPopUp(decorated) 200 self.host.showPopUp(decorated)
201 elif show_type == 'window': 201 elif show_type == 'window':
202 self.host.addWindow(decorated) 202 self.host.addWindow(decorated)
203 else: 203 else:
211 def onButtonPress(self, button): 211 def onButtonPress(self, button):
212 callback_id, fields = button.param_id 212 callback_id, fields = button.param_id
213 data = {"callback_id":callback_id} 213 data = {"callback_id":callback_id}
214 for field in fields: 214 for field in fields:
215 ctrl = self.ctrl_list[field] 215 ctrl = self.ctrl_list[field]
216 if isinstance(ctrl['control'],custom_widgets.List): 216 if isinstance(ctrl['control'],sat_widgets.List):
217 data[field] = '\t'.join(ctrl['control'].getSelectedValues()) 217 data[field] = '\t'.join(ctrl['control'].getSelectedValues())
218 else: 218 else:
219 data[field] = ctrl['control'].getValue() 219 data[field] = ctrl['control'].getValue()
220 220
221 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile) 221 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
228 228
229 def onFormSubmitted(self, button): 229 def onFormSubmitted(self, button):
230 data = [] 230 data = []
231 for ctrl_name in self.ctrl_list: 231 for ctrl_name in self.ctrl_list:
232 ctrl = self.ctrl_list[ctrl_name] 232 ctrl = self.ctrl_list[ctrl_name]
233 if isinstance(ctrl['control'], custom_widgets.List): 233 if isinstance(ctrl['control'], sat_widgets.List):
234 data.append((ctrl_name, ctrl['control'].getSelectedValue())) 234 data.append((ctrl_name, ctrl['control'].getSelectedValue()))
235 elif isinstance(ctrl['control'], urwid.CheckBox): 235 elif isinstance(ctrl['control'], urwid.CheckBox):
236 data.append((ctrl_name, "true" if ctrl['control'].get_state() else "false")) 236 data.append((ctrl_name, "true" if ctrl['control'].get_state() else "false"))
237 else: 237 else:
238 data.append((ctrl_name, ctrl['control'].get_edit_text())) 238 data.append((ctrl_name, ctrl['control'].get_edit_text()))