comparison frontends/src/primitivus/xmlui.py @ 976:68faf7d77a42

frontends (xmlui): add setter methods + fixes: - add _xmluiSetValue for all widgets + _xmluiAddValues for ListWidget - fix popup dialog OK button + _xmluiOnClick for ButtonWidget
author souliane <souliane@mailoo.org>
date Thu, 03 Apr 2014 14:49:05 +0200
parents b37b1d183ac3
children 73a0b7f94674
comparison
equal deleted inserted replaced
975:b37b1d183ac3 976:68faf7d77a42
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 import urwid 21 import urwid
22 import copy
22 from urwid_satext import sat_widgets 23 from urwid_satext import sat_widgets
23 from logging import debug, info, warning, error 24 from logging import debug, info, warning, error
24 from xml.dom import minidom 25 from xml.dom import minidom
25 from sat_frontends.tools import xmlui 26 from sat_frontends.tools import xmlui
26 27
76 warning(_("Unknown div_char")) 77 warning(_("Unknown div_char"))
77 div_char = u'─' 78 div_char = u'─'
78 79
79 urwid.Divider.__init__(self, div_char) 80 urwid.Divider.__init__(self, div_char)
80 81
82
81 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents): 83 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
82 84
83 def __init__(self, parent, value): 85 def __init__(self, parent, value):
84 sat_widgets.AdvancedEdit.__init__(self, edit_text=value) 86 sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
87
88 def _xmluiSetValue(self, value):
89 self.set_edit_text(value)
85 90
86 def _xmluiGetValue(self): 91 def _xmluiGetValue(self):
87 return self.get_edit_text() 92 return self.get_edit_text()
88 93
89 94
90 class PrimitivusPasswordWidget(xmlui.PasswordWidget, sat_widgets.Password, PrimitivusEvents): 95 class PrimitivusPasswordWidget(xmlui.PasswordWidget, sat_widgets.Password, PrimitivusEvents):
91 96
92 def __init__(self, parent, value): 97 def __init__(self, parent, value):
93 sat_widgets.Password.__init__(self, edit_text=value) 98 sat_widgets.Password.__init__(self, edit_text=value)
94 99
100 def _xmluiSetValue(self, value):
101 self.set_edit_text(value)
102
95 def _xmluiGetValue(self): 103 def _xmluiGetValue(self):
96 return self.get_edit_text() 104 return self.get_edit_text()
97 105
98 106
99 class PrimitivusTextBoxWidget(xmlui.TextBoxWidget, sat_widgets.AdvancedEdit, PrimitivusEvents): 107 class PrimitivusTextBoxWidget(xmlui.TextBoxWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
100 108
101 def __init__(self, parent, value): 109 def __init__(self, parent, value):
102 sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True) 110 sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True)
103 111
112 def _xmluiSetValue(self, value):
113 self.set_edit_text(value)
114
104 def _xmluiGetValue(self): 115 def _xmluiGetValue(self):
105 return self.get_edit_text() 116 return self.get_edit_text()
106 117
107 118
108 class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents): 119 class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents):
109 120
110 def __init__(self, parent, state): 121 def __init__(self, parent, state):
111 urwid.CheckBox.__init__(self, '', state=state) 122 urwid.CheckBox.__init__(self, '', state=state)
112 123
124 def _xmluiSetValue(self, value):
125 self.set_state(value == "true")
126
113 def _xmluiGetValue(self): 127 def _xmluiGetValue(self):
114 return "true" if self.get_state() else "false" 128 return "true" if self.get_state() else "false"
115 129
116 130
117 class PrimitivusButtonWidget(xmlui.ButtonWidget, sat_widgets.CustomButton, PrimitivusEvents): 131 class PrimitivusButtonWidget(xmlui.ButtonWidget, sat_widgets.CustomButton, PrimitivusEvents):
118 132
119 def __init__(self, parent, value, click_callback): 133 def __init__(self, parent, value, click_callback):
120 sat_widgets.CustomButton.__init__(self, value, on_press=click_callback) 134 sat_widgets.CustomButton.__init__(self, value, on_press=click_callback)
135
136 def _xmluiOnClick(self, callback):
137 urwid.connect_signal(self, 'click', callback)
121 138
122 139
123 class PrimitivusListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents): 140 class PrimitivusListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
124 141
125 def __init__(self, parent, options, selected, flags): 142 def __init__(self, parent, options, selected, flags):
132 def _xmluiSelectValues(self, values): 149 def _xmluiSelectValues(self, values):
133 return self.selectValues(values) 150 return self.selectValues(values)
134 151
135 def _xmluiGetSelectedValues(self): 152 def _xmluiGetSelectedValues(self):
136 return [option.value for option in self.getSelectedValues()] 153 return [option.value for option in self.getSelectedValues()]
154
155 def _xmluiAddValues(self, values, select=True):
156 current_values = self.getAllValues()
157 new_values = copy.deepcopy(current_values)
158 for value in values:
159 if value not in current_values:
160 new_values.append(value)
161 if select:
162 selected = self._xmluiGetSelectedValues()
163 self.changeValues(new_values)
164 if select:
165 for value in values:
166 if value not in selected:
167 selected.append(value)
168 self._xmluiSelectValues(selected)
137 169
138 170
139 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents): 171 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents):
140 172
141 def __init__(self, parent, columns, selectable='no'): 173 def __init__(self, parent, columns, selectable='no'):
157 return self.getSelectedIndex() 189 return self.getSelectedIndex()
158 190
159 def _xmluiOnSelect(self, callback): 191 def _xmluiOnSelect(self, callback):
160 """ Call callback with widget as only argument """ 192 """ Call callback with widget as only argument """
161 urwid.connect_signal(self, 'click', self._event_callback, callback) 193 urwid.connect_signal(self, 'click', self._event_callback, callback)
194
162 195
163 class PrimitivusPairsContainer(xmlui.PairsContainer, sat_widgets.TableContainer): 196 class PrimitivusPairsContainer(xmlui.PairsContainer, sat_widgets.TableContainer):
164 197
165 def __init__(self, parent): 198 def __init__(self, parent):
166 options = {'ADAPT':(0,), 'HIGHLIGHT':(0,)} 199 options = {'ADAPT':(0,), 'HIGHLIGHT':(0,)}
231 264
232 def constructUI(self, xml_data): 265 def constructUI(self, xml_data):
233 def postTreat(): 266 def postTreat():
234 assert self.main_cont.body 267 assert self.main_cont.body
235 268
236 if self.type == 'form': 269 if self.type in ('form', 'popup'):
237 buttons = [] 270 buttons = []
238 buttons.append(urwid.Button(_('Submit'),self.onFormSubmitted)) 271 if self.type == 'form':
239 if not 'NO_CANCEL' in self.flags: 272 buttons.append(urwid.Button(_('Submit'), self.onFormSubmitted))
240 buttons.append(urwid.Button(_('Cancel'),self.onFormCancelled)) 273 if not 'NO_CANCEL' in self.flags:
274 buttons.append(urwid.Button(_('Cancel'), self.onFormCancelled))
275 else:
276 buttons.append(urwid.Button(_('OK'), lambda dummy: self._xmluiClose()))
241 max_len = max([len(button.get_label()) for button in buttons]) 277 max_len = max([len(button.get_label()) for button in buttons])
242 grid_wid = urwid.GridFlow(buttons,max_len+4,1,0,'center') 278 grid_wid = urwid.GridFlow(buttons, max_len + 4, 1, 0, 'center')
243 self.main_cont.body.append(grid_wid) 279 self.main_cont.body.append(grid_wid)
244 elif self.type == 'param': 280 elif self.type == 'param':
245 tabs_cont = self.main_cont.body[0].base_widget 281 tabs_cont = self.main_cont.body[0].base_widget
246 assert(isinstance(tabs_cont,sat_widgets.TabsContainer)) 282 assert(isinstance(tabs_cont,sat_widgets.TabsContainer))
247 buttons = [] 283 buttons = []