comparison frontends/primitivus/xmlui.py @ 166:b318d2b58887

Primitivus: parameters management via XMLUI
author Goffi <goffi@goffi.org>
date Mon, 09 Aug 2010 21:39:41 +0800
parents ae50b53ff868
children 6fd053c99421
comparison
equal deleted inserted replaced
165:8a2053de6f8c 166:b318d2b58887
98 ctrl = custom_widgets.CustomButton(value, on_press=self.onButtonPress) 98 ctrl = custom_widgets.CustomButton(value, on_press=self.onButtonPress)
99 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) 99 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
100 else: 100 else:
101 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME ! 101 error(_("FIXME FIXME FIXME: type [%s] is not implemented") % type) #FIXME !
102 raise NotImplementedError 102 raise NotImplementedError
103 if self.type == 'param':
104 if isinstance(ctrl,urwid.Edit):
105 urwid.connect_signal(ctrl,'change',self.onParamChange)
106 ctrl._param_category = self._current_category
107 ctrl._param_name = name
103 parent.append(ctrl) 108 parent.append(ctrl)
104 109
105 def __parseChilds(self, current, elem, wanted = ['layout'], data = None): 110 def __parseChilds(self, current, elem, wanted = ['layout'], data = None):
106 """Recursively parse childNodes of an elemen 111 """Recursively parse childNodes of an elemen
107 @param current: widget container with 'append' method 112 @param current: widget container with 'append' method
127 self.__parseElems(node, current) 132 self.__parseElems(node, current)
128 elif node.nodeName == "category": 133 elif node.nodeName == "category":
129 name = node.getAttribute('name') 134 name = node.getAttribute('name')
130 if not name or not isinstance(data,custom_widgets.TabsContainer): 135 if not name or not isinstance(data,custom_widgets.TabsContainer):
131 raise InvalidXMLUI 136 raise InvalidXMLUI
137 if self.type == 'param':
138 self._current_category = name #XXX: awful hack because params need category and we don't keep parent
132 tab_cont = data 139 tab_cont = data
133 listbox = tab_cont.addTab(name) 140 listbox = tab_cont.addTab(name)
134 self.__parseChilds(listbox.body, node, ['layout']) 141 self.__parseChilds(listbox.body, node, ['layout'])
135 else: 142 else:
136 message=_("Unknown tag") 143 message=_("Unknown tag")
137 error(message) 144 error(message)
138 raise NotImplementedError 145 raise NotImplementedError
139 146
140 def constructUI(self, xml_data): 147 def constructUI(self, xml_data):
141 148
142 list_box = urwid.ListBox(urwid.SimpleListWalker([])) 149 ret_wid = urwid.ListBox(urwid.SimpleListWalker([]))
143 150
144 cat_dom = minidom.parseString(xml_data.encode('utf-8')) 151 cat_dom = minidom.parseString(xml_data.encode('utf-8'))
145 top=cat_dom.documentElement 152 top=cat_dom.documentElement
146 self.type = top.getAttribute("type") 153 self.type = top.getAttribute("type")
147 if not self.title: 154 if not self.title:
148 self.title = top.getAttribute("title") #TODO: manage title 155 self.title = top.getAttribute("title") #TODO: manage title
149 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']: 156 if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
150 raise InvalidXMLUI 157 raise InvalidXMLUI
151 158
152 self.__parseChilds(list_box.body, cat_dom.documentElement) 159 if self.type == 'param':
153 160 self.param_changed = set()
154 assert list_box.body 161
155 if isinstance(list_box.body[0],custom_widgets.TabsContainer): 162 self.__parseChilds(ret_wid.body, cat_dom.documentElement)
156 return list_box.body[0] #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox 163
164 assert ret_wid.body
165
166 if isinstance(ret_wid.body[0],custom_widgets.TabsContainer):
167 ret_wid = urwid.Pile([ret_wid.body[0]]) #xxx: awfull hack cause TabsContainer is a BoxWidget, can't be inside a ListBox
168
157 169
158 if self.type == 'form': 170 if self.type == 'form':
159 buttons = [] 171 buttons = []
160 buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted)) 172 buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted))
161 if not 'NO_CANCEL' in self.options: 173 if not 'NO_CANCEL' in self.options:
162 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled)) 174 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled))
163 max_len = max([len(button.get_label()) for button in buttons]) 175 max_len = max([len(button.get_label()) for button in buttons])
164 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center') 176 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center')
165 list_box.body.append(grid_wid) 177 ret_wid.body.append(grid_wid)
166 178 elif self.type == 'param':
167 return list_box 179 assert(isinstance(ret_wid,urwid.Pile))
180 buttons = []
181 buttons.append(custom_widgets.CustomButton(_('Save'),self.onSaveParams))
182 buttons.append(custom_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow()))
183 max_len = max([button.getSize() for button in buttons])
184 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
185 ret_wid.widget_list.append(urwid.Filler(grid_wid))
186 ret_wid.item_types.append(('weight',1))
187
188 return ret_wid
168 189
169 def show(self): 190 def show(self):
170 """Show the constructed UI""" 191 """Show the constructed UI"""
171 decorated = custom_widgets.LabelLine(self, custom_widgets.SurroundedText(self.title or '')) 192 decorated = custom_widgets.LabelLine(self, custom_widgets.SurroundedText(self.title or ''))
172 self.host.showPopUp(decorated) 193 self.host.showPopUp(decorated)
185 else: 206 else:
186 data[field] = ctrl['control'].getValue() 207 data[field] = ctrl['control'].getValue()
187 208
188 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile) 209 id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
189 self.host.current_action_ids.add(id) 210 self.host.current_action_ids.add(id)
211
212 def onParamChange(self, widget, text):
213 """Called when type is param and a widget to save is modified"""
214 assert(self.type == "param")
215 self.param_changed.add(widget)
190 216
191 def onFormSubmitted(self, button): 217 def onFormSubmitted(self, button):
192 data = [] 218 data = []
193 for ctrl_name in self.ctrl_list: 219 for ctrl_name in self.ctrl_list:
194 ctrl = self.ctrl_list[ctrl_name] 220 ctrl = self.ctrl_list[ctrl_name]
205 warning (_("The form data is not sent back, the type is not managed properly")) 231 warning (_("The form data is not sent back, the type is not managed properly"))
206 self.host.removePopUp() 232 self.host.removePopUp()
207 233
208 def onFormCancelled(self, button): 234 def onFormCancelled(self, button):
209 self.host.removePopUp() 235 self.host.removePopUp()
236
237 def onSaveParams(self, button):
238 for ctrl in self.param_changed:
239 self.host.bridge.setParam(ctrl._param_name, ctrl.get_edit_text(), ctrl._param_category, profile_key = self.host.profile)
240 self.host.removeWindow()