comparison sat_frontends/primitivus/xmlui.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/primitivus/xmlui.py@0046283a285d
children 1209a5d83082
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # Primitivus: a SAT frontend
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 from sat.core.i18n import _
21 import urwid
22 import copy
23 from sat.core import exceptions
24 from urwid_satext import sat_widgets
25 from urwid_satext import files_management
26 from sat.core.log import getLogger
27 log = getLogger(__name__)
28 from sat_frontends.primitivus.constants import Const as C
29 from sat_frontends.primitivus.widget import PrimitivusWidget
30 from sat_frontends.tools import xmlui
31
32
33 class PrimitivusEvents(object):
34 """ Used to manage change event of Primitivus widgets """
35
36 def _event_callback(self, ctrl, *args, **kwargs):
37 """" Call xmlui callback and ignore any extra argument """
38 args[-1](ctrl)
39
40 def _xmluiOnChange(self, callback):
41 """ Call callback with widget as only argument """
42 urwid.connect_signal(self, 'change', self._event_callback, callback)
43
44
45 class PrimitivusEmptyWidget(xmlui.EmptyWidget, urwid.Text):
46
47 def __init__(self, _xmlui_parent):
48 urwid.Text.__init__(self, '')
49
50
51 class PrimitivusTextWidget(xmlui.TextWidget, urwid.Text):
52
53 def __init__(self, _xmlui_parent, value, read_only=False):
54 urwid.Text.__init__(self, value)
55
56
57 class PrimitivusLabelWidget(xmlui.LabelWidget, PrimitivusTextWidget):
58
59 def __init__(self, _xmlui_parent, value):
60 super(PrimitivusLabelWidget, self).__init__(_xmlui_parent, value+": ")
61
62
63 class PrimitivusJidWidget(xmlui.JidWidget, PrimitivusTextWidget):
64 pass
65
66
67 class PrimitivusDividerWidget(xmlui.DividerWidget, urwid.Divider):
68
69 def __init__(self, _xmlui_parent, style='line'):
70 if style == 'line':
71 div_char = u'─'
72 elif style == 'dot':
73 div_char = u'·'
74 elif style == 'dash':
75 div_char = u'-'
76 elif style == 'plain':
77 div_char = u'█'
78 elif style == 'blank':
79 div_char = ' '
80 else:
81 log.warning(_("Unknown div_char"))
82 div_char = u'─'
83
84 urwid.Divider.__init__(self, div_char)
85
86
87 class PrimitivusStringWidget(xmlui.StringWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
88
89 def __init__(self, _xmlui_parent, value, read_only=False):
90 sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
91 self.read_only = read_only
92
93 def selectable(self):
94 if self.read_only:
95 return False
96 return super(PrimitivusStringWidget, self).selectable()
97
98 def _xmluiSetValue(self, value):
99 self.set_edit_text(value)
100
101 def _xmluiGetValue(self):
102 return self.get_edit_text()
103
104
105 class PrimitivusJidInputWidget(xmlui.JidInputWidget, PrimitivusStringWidget):
106 pass
107
108
109 class PrimitivusPasswordWidget(xmlui.PasswordWidget, sat_widgets.Password, PrimitivusEvents):
110
111 def __init__(self, _xmlui_parent, value, read_only=False):
112 sat_widgets.Password.__init__(self, edit_text=value)
113 self.read_only = read_only
114
115 def selectable(self):
116 if self.read_only:
117 return False
118 return super(PrimitivusPasswordWidget, self).selectable()
119
120 def _xmluiSetValue(self, value):
121 self.set_edit_text(value)
122
123 def _xmluiGetValue(self):
124 return self.get_edit_text()
125
126
127 class PrimitivusTextBoxWidget(xmlui.TextBoxWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
128
129 def __init__(self, _xmlui_parent, value, read_only=False):
130 sat_widgets.AdvancedEdit.__init__(self, edit_text=value, multiline=True)
131 self.read_only = read_only
132
133 def selectable(self):
134 if self.read_only:
135 return False
136 return super(PrimitivusTextBoxWidget, self).selectable()
137
138 def _xmluiSetValue(self, value):
139 self.set_edit_text(value)
140
141 def _xmluiGetValue(self):
142 return self.get_edit_text()
143
144
145 class PrimitivusBoolWidget(xmlui.BoolWidget, urwid.CheckBox, PrimitivusEvents):
146
147 def __init__(self, _xmlui_parent, state, read_only=False):
148 urwid.CheckBox.__init__(self, '', state=state)
149 self.read_only = read_only
150
151 def selectable(self):
152 if self.read_only:
153 return False
154 return super(PrimitivusBoolWidget, self).selectable()
155
156 def _xmluiSetValue(self, value):
157 self.set_state(value == "true")
158
159 def _xmluiGetValue(self):
160 return C.BOOL_TRUE if self.get_state() else C.BOOL_FALSE
161
162
163 class PrimitivusIntWidget(xmlui.IntWidget, sat_widgets.AdvancedEdit, PrimitivusEvents):
164
165 def __init__(self, _xmlui_parent, value, read_only=False):
166 sat_widgets.AdvancedEdit.__init__(self, edit_text=value)
167 self.read_only = read_only
168
169 def selectable(self):
170 if self.read_only:
171 return False
172 return super(PrimitivusIntWidget, self).selectable()
173
174 def _xmluiSetValue(self, value):
175 self.set_edit_text(value)
176
177 def _xmluiGetValue(self):
178 return self.get_edit_text()
179
180
181 class PrimitivusButtonWidget(xmlui.ButtonWidget, sat_widgets.CustomButton, PrimitivusEvents):
182
183 def __init__(self, _xmlui_parent, value, click_callback):
184 sat_widgets.CustomButton.__init__(self, value, on_press=click_callback)
185
186 def _xmluiOnClick(self, callback):
187 urwid.connect_signal(self, 'click', callback)
188
189
190 class PrimitivusListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
191
192 def __init__(self, _xmlui_parent, options, selected, flags):
193 sat_widgets.List.__init__(self, options=options, style=flags)
194 self._xmluiSelectValues(selected)
195
196 def _xmluiSelectValue(self, value):
197 return self.selectValue(value)
198
199 def _xmluiSelectValues(self, values):
200 return self.selectValues(values)
201
202 def _xmluiGetSelectedValues(self):
203 return [option.value for option in self.getSelectedValues()]
204
205 def _xmluiAddValues(self, values, select=True):
206 current_values = self.getAllValues()
207 new_values = copy.deepcopy(current_values)
208 for value in values:
209 if value not in current_values:
210 new_values.append(value)
211 if select:
212 selected = self._xmluiGetSelectedValues()
213 self.changeValues(new_values)
214 if select:
215 for value in values:
216 if value not in selected:
217 selected.append(value)
218 self._xmluiSelectValues(selected)
219
220 class PrimitivusJidsListWidget(xmlui.ListWidget, sat_widgets.List, PrimitivusEvents):
221
222 def __init__(self, _xmlui_parent, jids, styles):
223 sat_widgets.List.__init__(self, options=jids+[''], # the empty field is here to add new jids if needed
224 option_type=lambda txt, align: sat_widgets.AdvancedEdit(edit_text=txt, align=align),
225 on_change=self._onChange)
226 self.delete=0
227
228 def _onChange(self, list_widget, jid_widget=None, text=None):
229 if jid_widget is not None:
230 if jid_widget != list_widget.contents[-1] and not text:
231 # if a field is empty, we delete the line (except for the last line)
232 list_widget.contents.remove(jid_widget)
233 elif jid_widget == list_widget.contents[-1] and text:
234 # we always want an empty field as last value to be able to add jids
235 list_widget.contents.append(sat_widgets.AdvancedEdit())
236
237 def _xmluiGetSelectedValues(self):
238 # XXX: there is not selection in this list, so we return all non empty values
239 return [jid_ for jid_ in self.getAllValues() if jid_]
240
241
242 class PrimitivusAdvancedListContainer(xmlui.AdvancedListContainer, sat_widgets.TableContainer, PrimitivusEvents):
243
244 def __init__(self, _xmlui_parent, columns, selectable='no'):
245 options = {'ADAPT':()}
246 if selectable != 'no':
247 options['HIGHLIGHT'] = ()
248 sat_widgets.TableContainer.__init__(self, columns=columns, options=options, row_selectable = selectable!='no')
249
250 def _xmluiAppend(self, widget):
251 self.addWidget(widget)
252
253 def _xmluiAddRow(self, idx):
254 self.setRowIndex(idx)
255
256 def _xmluiGetSelectedWidgets(self):
257 return self.getSelectedWidgets()
258
259 def _xmluiGetSelectedIndex(self):
260 return self.getSelectedIndex()
261
262 def _xmluiOnSelect(self, callback):
263 """ Call callback with widget as only argument """
264 urwid.connect_signal(self, 'click', self._event_callback, callback)
265
266
267 class PrimitivusPairsContainer(xmlui.PairsContainer, sat_widgets.TableContainer):
268
269 def __init__(self, _xmlui_parent):
270 options = {'ADAPT':(0,), 'HIGHLIGHT':(0,)}
271 if self._xmlui_main.type == 'param':
272 options['FOCUS_ATTR'] = 'param_selected'
273 sat_widgets.TableContainer.__init__(self, columns=2, options=options)
274
275 def _xmluiAppend(self, widget):
276 if isinstance(widget, PrimitivusEmptyWidget):
277 # we don't want highlight on empty widgets
278 widget = urwid.AttrMap(widget, 'default')
279 self.addWidget(widget)
280
281
282 class PrimitivusLabelContainer(PrimitivusPairsContainer, xmlui.LabelContainer):
283 pass
284
285
286 class PrimitivusTabsContainer(xmlui.TabsContainer, sat_widgets.TabsContainer):
287
288 def __init__(self, _xmlui_parent):
289 sat_widgets.TabsContainer.__init__(self)
290
291 def _xmluiAppend(self, widget):
292 self.body.append(widget)
293
294 def _xmluiAddTab(self, label, selected):
295 tab = PrimitivusVerticalContainer(None)
296 self.addTab(label, tab, selected)
297 return tab
298
299
300 class PrimitivusVerticalContainer(xmlui.VerticalContainer, urwid.ListBox):
301 BOX_HEIGHT = 5
302
303 def __init__(self, _xmlui_parent):
304 urwid.ListBox.__init__(self, urwid.SimpleListWalker([]))
305 self._last_size = None
306
307 def _xmluiAppend(self, widget):
308 if 'flow' not in widget.sizing():
309 widget = urwid.BoxAdapter(widget, self.BOX_HEIGHT)
310 self.body.append(widget)
311
312 def render(self, size, focus=False):
313 if size != self._last_size:
314 (maxcol, maxrow) = size
315 if self.body:
316 widget = self.body[0]
317 if isinstance(widget, urwid.BoxAdapter):
318 widget.height = maxrow
319 self._last_size = size
320 return super(PrimitivusVerticalContainer, self).render(size, focus)
321
322
323 ### Dialogs ###
324
325
326 class PrimitivusDialog(object):
327
328 def __init__(self, _xmlui_parent):
329 self.host = _xmlui_parent.host
330
331 def _xmluiShow(self):
332 self.host.showPopUp(self)
333
334 def _xmluiClose(self):
335 self.host.removePopUp(self)
336
337
338 class PrimitivusMessageDialog(PrimitivusDialog, xmlui.MessageDialog, sat_widgets.Alert):
339
340 def __init__(self, _xmlui_parent, title, message, level):
341 PrimitivusDialog.__init__(self, _xmlui_parent)
342 xmlui.MessageDialog.__init__(self, _xmlui_parent)
343 sat_widgets.Alert.__init__(self, title, message, ok_cb=lambda dummy: self._xmluiClose())
344
345
346 class PrimitivusNoteDialog(xmlui.NoteDialog, PrimitivusMessageDialog):
347 # TODO: separate NoteDialog
348 pass
349
350
351 class PrimitivusConfirmDialog(PrimitivusDialog, xmlui.ConfirmDialog, sat_widgets.ConfirmDialog):
352
353 def __init__(self, _xmlui_parent, title, message, level, buttons_set):
354 PrimitivusDialog.__init__(self, _xmlui_parent)
355 xmlui.ConfirmDialog.__init__(self, _xmlui_parent)
356 sat_widgets.ConfirmDialog.__init__(self, title, message, no_cb=lambda dummy: self._xmluiCancelled(), yes_cb=lambda dummy: self._xmluiValidated())
357
358
359 class PrimitivusFileDialog(PrimitivusDialog, xmlui.FileDialog, files_management.FileDialog):
360
361 def __init__(self, _xmlui_parent, title, message, level, filetype):
362 # TODO: message is not managed yet
363 PrimitivusDialog.__init__(self, _xmlui_parent)
364 xmlui.FileDialog.__init__(self, _xmlui_parent)
365 style = []
366 if filetype == C.XMLUI_DATA_FILETYPE_DIR:
367 style.append('dir')
368 files_management.FileDialog.__init__(self,
369 ok_cb=lambda path: self._xmluiValidated({'path': path}),
370 cancel_cb=lambda dummy: self._xmluiCancelled(),
371 message=message,
372 title=title,
373 style=style)
374
375
376 class GenericFactory(object):
377
378 def __getattr__(self, attr):
379 if attr.startswith("create"):
380 cls = globals()["Primitivus" + attr[6:]] # XXX: we prefix with "Primitivus" to work around an Urwid bug, WidgetMeta in Urwid don't manage multiple inheritance with same names
381 return cls
382
383
384 class WidgetFactory(GenericFactory):
385
386 def __getattr__(self, attr):
387 if attr.startswith("create"):
388 cls = GenericFactory.__getattr__(self, attr)
389 cls._xmlui_main = self._xmlui_main
390 return cls
391
392
393 class XMLUIPanel(xmlui.XMLUIPanel, PrimitivusWidget):
394 widget_factory = WidgetFactory()
395
396 def __init__(self, host, parsed_xml, title=None, flags=None, callback=None, ignore=None, profile=C.PROF_KEY_NONE):
397 self.widget_factory._xmlui_main = self
398 self._dest = None
399 xmlui.XMLUIPanel.__init__(self,
400 host,
401 parsed_xml,
402 title = title,
403 flags = flags,
404 callback = callback,
405 ignore = ignore,
406 profile = profile)
407 PrimitivusWidget.__init__(self, self.main_cont, self.xmlui_title)
408
409 def constructUI(self, parsed_dom):
410 def postTreat():
411 assert self.main_cont.body
412
413 if self.type in ('form', 'popup'):
414 buttons = []
415 if self.type == 'form':
416 buttons.append(urwid.Button(_('Submit'), self.onFormSubmitted))
417 if not 'NO_CANCEL' in self.flags:
418 buttons.append(urwid.Button(_('Cancel'), self.onFormCancelled))
419 else:
420 buttons.append(urwid.Button(_('OK'), on_press=lambda dummy: self._xmluiClose()))
421 max_len = max([len(button.get_label()) for button in buttons])
422 grid_wid = urwid.GridFlow(buttons, max_len + 4, 1, 0, 'center')
423 self.main_cont.body.append(grid_wid)
424 elif self.type == 'param':
425 tabs_cont = self.main_cont.body[0].base_widget
426 assert isinstance(tabs_cont,sat_widgets.TabsContainer)
427 buttons = []
428 buttons.append(sat_widgets.CustomButton(_('Save'),self.onSaveParams))
429 buttons.append(sat_widgets.CustomButton(_('Cancel'),lambda x:self.host.removeWindow()))
430 max_len = max([button.getSize() for button in buttons])
431 grid_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
432 tabs_cont.addFooter(grid_wid)
433
434 xmlui.XMLUIPanel.constructUI(self, parsed_dom, postTreat)
435 urwid.WidgetWrap.__init__(self, self.main_cont)
436
437 def show(self, show_type=None, valign='middle'):
438 """Show the constructed UI
439 @param show_type: how to show the UI:
440 - None (follow XMLUI's recommendation)
441 - 'popup'
442 - 'window'
443 @param valign: vertical alignment when show_type is 'popup'.
444 Ignored when show_type is 'window'.
445
446 """
447 if show_type is None:
448 if self.type in ('window', 'param'):
449 show_type = 'window'
450 elif self.type in ('popup', 'form'):
451 show_type = 'popup'
452
453 if show_type not in ('popup', 'window'):
454 raise ValueError('Invalid show_type [%s]' % show_type)
455
456 self._dest = show_type
457 if show_type == 'popup':
458 self.host.showPopUp(self, valign=valign)
459 elif show_type == 'window':
460 self.host.newWidget(self)
461 else:
462 assert False
463 self.host.redraw()
464
465 def _xmluiClose(self):
466 if self._dest == 'window':
467 self.host.removeWindow()
468 elif self._dest == 'popup':
469 self.host.removePopUp(self)
470 else:
471 raise exceptions.InternalError("self._dest unknown, are you sure you have called XMLUI.show ?")
472
473
474 class XMLUIDialog(xmlui.XMLUIDialog):
475 dialog_factory = GenericFactory()
476
477
478 xmlui.registerClass(xmlui.CLASS_PANEL, XMLUIPanel)
479 xmlui.registerClass(xmlui.CLASS_DIALOG, XMLUIDialog)
480 create = xmlui.create