comparison urwid_satext/sat_widgets.py @ 30:1aeb3540aa49

files reorganisation after project separation. new README, and COPYING files
author Goffi <goffi@goffi.org>
date Tue, 28 Dec 2010 11:53:18 +0100
parents frontends/primitivus/custom_widgets.py@654d31983f19
children 9fc778aab7f5
comparison
equal deleted inserted replaced
29:5d0e497f73a2 30:1aeb3540aa49
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 from urwid.escape import utf8decode
24 from logging import debug, info, warning, error
25
26 class Password(urwid.Edit):
27 """Edit box which doesn't show what is entered (show '*' or other char instead)"""
28
29 def __init__(self, *args, **kwargs):
30 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char'
31 @param hidden_char: char to show instead of what is actually entered: default '*'
32 """
33 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*'
34 self.__real_text=''
35 super(Password, self).__init__(*args, **kwargs)
36
37 def set_edit_text(self, text):
38 self.__real_text = text
39 hidden_txt = len(text)*'*'
40 super(Password, self).set_edit_text(hidden_txt)
41
42 def get_edit_text(self):
43 return self.__real_text
44
45 def insert_text(self, text):
46 self._edit_text = self.__real_text
47 super(Password,self).insert_text(text)
48
49 def render(self, size, focus=False):
50 return super(Password, self).render(size, focus)
51
52 class AdvancedEdit(urwid.Edit):
53 """Edit box with some custom improvments
54 new chars:
55 - C-a: like 'home'
56 - C-e: like 'end'
57 - C-k: remove everything on the right of the cursor
58 - C-w: remove the word on the back
59 new behaviour: emit a 'click' signal when enter is pressed"""
60 signals = urwid.Edit.signals + ['click']
61
62 def setCompletionMethod(self, callback):
63 """Define method called when completion is asked
64 @callback: method with 2 arguments:
65 - the text to complete
66 - if there was already a completion, a dict with
67 - 'completed':last completion
68 - 'completion_pos': cursor position where the completion starts
69 - 'position': last completion cursor position
70 this dict must be used (and can be filled) to find next completion)
71 and which return the full text completed"""
72 self.completion_cb = callback
73 self.completion_data = {}
74
75 def keypress(self, size, key):
76 #TODO: insert mode is not managed yet
77 if key == 'ctrl a':
78 key = 'home'
79 elif key == 'ctrl e':
80 key = 'end'
81 elif key == 'ctrl k':
82 self._delete_highlighted()
83 self.set_edit_text(self.edit_text[:self.edit_pos])
84 elif key == 'ctrl w':
85 before = self.edit_text[:self.edit_pos]
86 pos = before.rstrip().rfind(" ")+1
87 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:])
88 self.set_edit_pos(pos)
89 elif key == 'enter':
90 self._emit('click')
91 elif key == 'shift tab':
92 try:
93 before = self.edit_text[:self.edit_pos]
94 if self.completion_data:
95 if (not self.completion_data['completed']
96 or self.completion_data['position'] != self.edit_pos
97 or not before.endswith(self.completion_data['completed'])):
98 self.completion_data.clear()
99 else:
100 before = before[:-len(self.completion_data['completed'])]
101 complet = self.completion_cb(before, self.completion_data)
102 self.completion_data['completed'] = complet[len(before):]
103 self.set_edit_text(complet+self.edit_text[self.edit_pos:])
104 self.set_edit_pos(len(complet))
105 self.completion_data['position'] = self.edit_pos
106 return
107 except AttributeError:
108 #No completion method defined
109 pass
110 return super(AdvancedEdit, self).keypress(size, key)
111
112
113 class SurroundedText(urwid.FlowWidget):
114 """Text centered on a repeated character (like a Divider, but with a text in the center)"""
115
116 def __init__(self,text,car=utf8decode('─')):
117 self.text=text
118 self.car=car
119
120 def rows(self,size,focus=False):
121 return self.display_widget(size, focus).rows(size, focus)
122
123 def render(self, size, focus=False):
124 return self.display_widget(size, focus).render(size, focus)
125
126 def display_widget(self, size, focus):
127 (maxcol,) = size
128 middle = (maxcol-len(self.text))/2
129 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car
130 return urwid.Text(render_text)
131
132 class SelectableText(urwid.WidgetWrap):
133 """Text which can be selected with space"""
134 signals = ['change']
135
136 def __init__(self, text, align='left', header='', focus_attr='default_focus', selected_text=None, selected=False, data=None):
137 """@param text: same as urwid.Text's text parameter
138 @param align: same as urwid.Text's align parameter
139 @select_attr: attrbute to use when selected
140 @param selected: is the text selected ?"""
141 self.focus_attr = focus_attr
142 self.__selected = False
143 self.__was_focused = False
144 self.header = self.__valid_text(header)
145 self.default_txt = self.__valid_text(text)
146 urwid.WidgetWrap.__init__(self, urwid.Text("",align=align))
147 self.setSelectedText(selected_text)
148 self.setState(selected)
149
150 def __valid_text(self, text):
151 """Tmp method needed until dbus and urwid are more friends"""
152 if isinstance(text,basestring):
153 return unicode(text)
154 elif isinstance(text,tuple):
155 return (unicode(text[0]),text[1])
156 elif isinstance(text,list):
157 for idx in range(len(text)):
158 elem = text[idx]
159 if isinstance(elem,basestring):
160 text[idx] = unicode(elem)
161 if isinstance(elem,tuple):
162 text[idx] = (unicode(elem[0]),elem[1])
163 else:
164 warning (_('WARNING: unknown text type'))
165 return text
166
167 def getValue(self):
168 if isinstance(self.default_txt,basestring):
169 return self.default_txt
170 list_attr = self.default_txt if isinstance(self.default_txt, list) else [self.default_txt]
171 txt = ""
172 for attr in list_attr:
173 if isinstance(attr,tuple):
174 txt+=attr[1]
175 else:
176 txt+=attr
177 return txt
178
179 def get_text(self):
180 """for compatibility with urwid.Text"""
181 return self.getValue()
182
183 def set_text(self, text):
184 """/!\ set_text doesn't change self.selected_txt !"""
185 self.default_txt = self.__valid_text(text)
186 self.setState(self.__selected,invisible=True)
187
188 def setSelectedText(self, text=None):
189 """Text to display when selected
190 @text: text as in urwid.Text or None for default value"""
191 if text == None:
192 text = ('selected',self.getValue())
193 self.selected_txt = self.__valid_text(text)
194 if self.__selected:
195 self.setState(self.__selected)
196
197
198 def __set_txt(self):
199 txt_list = [self.header]
200 txt = self.selected_txt if self.__selected else self.default_txt
201 if isinstance(txt,list):
202 txt_list.extend(txt)
203 else:
204 txt_list.append(txt)
205 self._w.base_widget.set_text(txt_list)
206
207
208 def setState(self, selected, invisible=False):
209 """Change state
210 @param selected: boolean state value
211 @param invisible: don't emit change signal if True"""
212 assert(type(selected)==bool)
213 self.__selected=selected
214 self.__set_txt()
215 self.__was_focused = False
216 self._invalidate()
217 if not invisible:
218 self._emit("change", self.__selected)
219
220 def getState(self):
221 return self.__selected
222
223 def selectable(self):
224 return True
225
226 def keypress(self, size, key):
227 if key==' ' or key=='enter':
228 self.setState(not self.__selected)
229 else:
230 return key
231
232 def mouse_event(self, size, event, button, x, y, focus):
233 if urwid.is_mouse_press(event) and button == 1:
234 self.setState(not self.__selected)
235 return True
236
237 return False
238
239 def render(self, size, focus=False):
240 attr_list = self._w.base_widget._attrib
241 if not focus:
242 if self.__was_focused:
243 self.__set_txt()
244 self.__was_focused = False
245 else:
246 if not self.__was_focused:
247 if not attr_list:
248 attr_list.append((self.focus_attr,len(self._w.base_widget.text)))
249 else:
250 for idx in range(len(attr_list)):
251 attr,attr_len = attr_list[idx]
252 if attr == None:
253 attr = self.focus_attr
254 attr_list[idx] = (attr,attr_len)
255 else:
256 if not attr.endswith('_focus'):
257 attr+="_focus"
258 attr_list[idx] = (attr,attr_len)
259 self._w.base_widget._invalidate()
260 self.__was_focused = True #bloody ugly hack :)
261 return self._w.render(size, focus)
262
263 class ClickableText(SelectableText):
264 signals = SelectableText.signals + ['click']
265
266 def setState(self, selected, invisible=False):
267 super(ClickableText,self).setState(False,True)
268 if not invisible:
269 self._emit('click')
270
271 class CustomButton(ClickableText):
272
273 def __init__(self, label, on_press=None, user_data=None, left_border = "[ ", right_border = " ]"):
274 self.label = label
275 self.left_border = left_border
276 self.right_border = right_border
277 super(CustomButton, self).__init__([left_border, label, right_border])
278 self.size = len(self.get_text())
279 if on_press:
280 urwid.connect_signal(self, 'click', on_press, user_data)
281
282 def getSize(self):
283 """Return representation size of the button"""
284 return self.size
285
286 def get_label(self):
287 return self.label[1] if isinstance(self.label,tuple) else self.label
288
289 def set_label(self, label):
290 self.label = label
291 self.set_text([self.left_border, label, self.right_border])
292
293 class GenericList(urwid.WidgetWrap):
294 signals = ['click','change']
295
296 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
297 """
298 Widget managing list of string and their selection
299 @param options: list of strings used for options
300 @param style: list of string:
301 - 'single' if only one must be selected
302 - 'no_first_select' nothing selected when list is first displayed
303 - 'can_select_none' if we can select nothing
304 @param align: alignement of text inside the list
305 @param on_click: method called when click signal is emited
306 @param user_data: data sent to the callback for click signal
307 """
308 self.single = 'single' in style
309 self.no_first_select = 'no_first_select' in style
310 self.can_select_none = 'can_select_none' in style
311 self.align = align
312 self.option_type = option_type
313 self.first_display = True
314
315 if on_click:
316 urwid.connect_signal(self, 'click', on_click, user_data)
317
318 if on_change:
319 urwid.connect_signal(self, 'change', on_change, user_data)
320
321 self.content = urwid.SimpleListWalker([])
322 self.list_box = urwid.ListBox(self.content)
323 urwid.WidgetWrap.__init__(self, self.list_box)
324 self.changeValues(options)
325
326 def __onStateChange(self, widget, selected):
327 if self.single:
328 if not selected and not self.can_select_none:
329 #if in single mode, it's forbidden to unselect a value
330 widget.setState(True, invisible=True)
331 return
332 if selected:
333 self.unselectAll(invisible=True)
334 widget.setState(True, invisible=True)
335 self._emit("click")
336
337
338 def unselectAll(self, invisible=False):
339 for widget in self.content:
340 if widget.getState():
341 widget.setState(False, invisible)
342 widget._invalidate()
343
344 def deleteValue(self, value):
345 """Delete the first value equal to the param given"""
346 for widget in self.content:
347 if widget.getValue() == value:
348 self.content.remove(widget)
349 self._emit('change')
350 return
351 raise ValueError("%s ==> %s" % (str(value),str(self.content)))
352
353 def getSelectedValue(self):
354 """Convenience method to get the value selected as a string in single mode, or None"""
355 values = self.getSelectedValues()
356 return values[0] if values else None
357
358 def getAllValues(self):
359 """Return values of all items"""
360 return [widget.getValue() for widget in self.content]
361
362 def getSelectedValues(self):
363 """Return values of selected items"""
364 result = []
365 for widget in self.content:
366 if widget.getState():
367 result.append(widget.getValue())
368 return result
369
370 def getDisplayWidget(self):
371 return self.list_box
372
373 def changeValues(self, new_values):
374 """Change all value in one shot"""
375 if not self.first_display:
376 old_selected = self.getSelectedValues()
377 widgets = []
378 for option in new_values:
379 widget = self.option_type(option, self.align)
380 if not self.first_display and option in old_selected:
381 widget.setState(True)
382 widgets.append(widget)
383 try:
384 urwid.connect_signal(widget, 'change', self.__onStateChange)
385 except NameError:
386 pass #the widget given doesn't support 'change' signal
387 self.content[:] = widgets
388 if self.first_display and self.single and new_values and not self.no_first_select:
389 self.content[0].setState(True)
390 display_widget = self.getDisplayWidget()
391 self._set_w(display_widget)
392 self._emit('change')
393 self.first_display = False
394
395 def selectValue(self, value):
396 """Select the first item which has the given value"""
397 self.unselectAll()
398 idx = 0
399 for widget in self.content:
400 if widget.getValue() == value:
401 widget.setState(True)
402 self.list_box.set_focus(idx)
403 return
404 idx+=1
405
406 class List(urwid.FlowWidget):
407 """FlowWidget list, same arguments as GenericList, with an additional one 'max_height'"""
408 signals = ['click','change']
409
410 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
411 self.genericList = GenericList(options, style, align, option_type, on_click, on_change, user_data)
412 self.max_height = max_height
413
414 def selectable(self):
415 return True
416
417 def keypress(self, size, key):
418 return self.displayWidget(size,True).keypress(size, key)
419
420 def unselectAll(self, invisible=False):
421 return self.genericList.unselectAll(invisible)
422
423 def deleteValue(self, value):
424 return self.genericList.deleteValue(value)
425
426 def getSelectedValue(self):
427 return self.genericList.getSelectedValue()
428
429 def getAllValues(self):
430 return self.genericList.getAllValues()
431
432 def getSelectedValues(self):
433 return self.genericList.getSelectedValues()
434
435 def changeValues(self, new_values):
436 return self.genericList.changeValues(new_values)
437
438 def selectValue(self, value):
439 return self.genericList.selectValue(value)
440
441 def render(self, size, focus=False):
442 return self.displayWidget(size, focus).render(size, focus)
443
444 def rows(self, size, focus=False):
445 return self.displayWidget(size, focus).rows(size, focus)
446
447 def displayWidget(self, size, focus):
448 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content])
449 height = min(list_size,self.max_height) or 1
450 return urwid.BoxAdapter(self.genericList, height)
451
452 ## MISC ##
453
454 class NotificationBar(urwid.WidgetWrap):
455 """Bar used to show misc information to user"""
456 signals = ['change']
457
458 def __init__(self):
459 self.waitNotifs = urwid.Text('')
460 self.message = ClickableText('')
461 urwid.connect_signal(self.message, 'click', lambda wid: self.showNext())
462 self.progress = ClickableText('')
463 self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message,('fixed',4,self.progress)])
464 urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
465 self.notifs = []
466
467 def __modQueue(self):
468 """must be called each time the notifications queue is changed"""
469 self.waitNotifs.set_text(('notifs',"(%i)" % len(self.notifs) if self.notifs else ''))
470 self._emit('change')
471
472 def setProgress(self,percentage):
473 """Define the progression to show on the right side of the bar"""
474 if percentage == None:
475 self.progress.set_text('')
476 else:
477 self.progress.set_text(('notifs','%02i%%' % percentage))
478 self._emit('change')
479
480 def addPopUp(self, pop_up_widget):
481 """Add a popup to the waiting queue"""
482 self.notifs.append(('popup',pop_up_widget))
483 self.__modQueue()
484
485 def addMessage(self, message):
486 "Add a message to the notificatio bar"
487 if not self.message.get_text():
488 self.message.set_text(('notifs',message))
489 self._invalidate()
490 self._emit('change')
491 else:
492 self.notifs.append(('message',message))
493 self.__modQueue()
494
495 def showNext(self):
496 """Show next message if any, else delete current message"""
497 found = None
498 for notif in self.notifs:
499 if notif[0] == "message":
500 found = notif
501 break
502 if found:
503 self.notifs.remove(found)
504 self.message.set_text(('notifs',found[1]))
505 self.__modQueue()
506 else:
507 self.message.set_text('')
508 self._emit('change')
509
510 def getNextPopup(self):
511 """Return next pop-up and remove it from the queue
512 @return: pop-up or None if there is no more in the queue"""
513 ret = None
514 for notif in self.notifs:
515 if notif[0] == 'popup':
516 ret = notif[1]
517 break
518 if ret:
519 self.notifs.remove(notif)
520 self.__modQueue()
521 return ret
522
523 def isQueueEmpty(self):
524 return not bool(self.notifs)
525
526 def canHide(self):
527 """Return True if there is now important information to show"""
528 return self.isQueueEmpty() and not self.message.get_text() and not self.progress.get_text()
529
530
531 class MenuBox(urwid.WidgetWrap):
532 """Show menu items of a category in a box"""
533 signals = ['click']
534
535 def __init__(self,parent,items):
536 self.parent = parent
537 self.selected = None
538 content = urwid.SimpleListWalker([ClickableText(('menuitem',text)) for text in items])
539 for wid in content:
540 urwid.connect_signal(wid, 'click', self.onClick)
541
542 self.listBox = urwid.ListBox(content)
543 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items)))
544 urwid.WidgetWrap.__init__(self,menubox)
545
546 def getValue(self):
547 return self.selected
548
549 def keypress(self, size, key):
550 if key=='up':
551 if self.listBox.get_focus()[1] == 0:
552 self.parent.keypress(size, key)
553 elif key=='left' or key=='right':
554 self.parent.keypress(size,'up')
555 self.parent.keypress(size,key)
556 return super(MenuBox,self).keypress(size,key)
557
558 def mouse_event(self, size, event, button, x, y, focus):
559 if button == 3:
560 self.parent.keypress(size,'up')
561 return True
562 return super(MenuBox,self).mouse_event(size, event, button, x, y, focus)
563
564 def onClick(self, wid):
565 self.selected = wid.getValue()
566 self._emit('click')
567
568 class Menu(urwid.WidgetWrap):
569
570 def __init__(self,loop, x_orig=0):
571 """Menu widget
572 @param loop: main loop of urwid
573 @param x_orig: absolute start of the abscissa
574 """
575 self.loop = loop
576 self.menu_keys = []
577 self.menu = {}
578 self.x_orig = x_orig
579 self.shortcuts = {} #keyboard shortcuts
580 self.save_bottom = None
581 col_rol = ColumnsRoller()
582 urwid.WidgetWrap.__init__(self, urwid.AttrMap(col_rol,'menubar'))
583
584 def selectable(self):
585 return True
586
587 def getMenuSize(self):
588 """return the current number of categories in this menu"""
589 return len(self.menu_keys)
590
591 def setOrigX(self, orig_x):
592 self.x_orig = orig_x
593
594 def __buildOverlay(self,menu_key,columns):
595 """Build the overlay menu which show menuitems
596 @param menu_key: name of the category
597 @colums: column number where the menubox must be displayed"""
598 max_len = 0
599 for item in self.menu[menu_key]:
600 if len(item[0]) > max_len:
601 max_len = len(item[0])
602
603 self.save_bottom = self.loop.widget
604 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]])
605 urwid.connect_signal(menu_box, 'click', self.onItemClick)
606
607 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None)
608
609 def keypress(self, size, key):
610 if key == 'down':
611 key = 'enter'
612 elif key == 'up':
613 if self.save_bottom:
614 self.loop.widget = self.save_bottom
615 self.save_bottom = None
616
617 return self._w.base_widget.keypress(size, key)
618
619 def checkShortcuts(self, key):
620 for shortcut in self.shortcuts.keys():
621 if key == shortcut:
622 category, item, callback = self.shortcuts[shortcut]
623 callback((category, item))
624 return key
625
626 def addMenu(self, category, item, callback, shortcut=None):
627 """Add a menu item, create the category if new
628 @param category: category of the menu (e.g. File/Edit)
629 @param item: menu item (e.g. new/close/about)
630 @callback: method to call when item is selected"""
631 if not category in self.menu.keys():
632 self.menu_keys.append(category)
633 self.menu[category] = []
634 button = CustomButton(('menubar',category), self.onCategoryClick,
635 left_border = ('menubar',"[ "),
636 right_border = ('menubar'," ]"))
637 self._w.base_widget.addWidget(button,button.getSize())
638 self.menu[category].append((item, callback))
639 if shortcut:
640 assert(shortcut not in self.shortcuts.keys())
641 self.shortcuts[shortcut] = (category, item, callback)
642
643 def onItemClick(self, widget):
644 category = self._w.base_widget.getSelected().get_label()
645 item = widget.getValue()
646 callback = None
647 for menu_item in self.menu[category]:
648 if item == menu_item[0]:
649 callback = menu_item[1]
650 break
651 if callback:
652 self.keypress(None,'up')
653 callback((category, item))
654
655 def onCategoryClick(self, button):
656 self.__buildOverlay(button.get_label(),
657 self.x_orig + self._w.base_widget.getStartCol(button))
658
659
660 class MenuRoller(urwid.WidgetWrap):
661
662 def __init__(self,menus_list):
663 """Create a MenuRoller
664 @param menus_list: list of tuple with (name, Menu_instance), name can be None
665 """
666 assert (menus_list)
667 self.selected = 0
668 self.name_list = []
669 self.menus = {}
670
671 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')])
672 urwid.WidgetWrap.__init__(self, self.columns)
673
674 for menu_tuple in menus_list:
675 name,menu = menu_tuple
676 self.addMenu(name, menu)
677
678 def __showSelected(self):
679 """show menu selected"""
680 name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 '
681 current_name = ClickableText(name_txt)
682 name_len = len(name_txt)
683 current_menu = self.menus[self.name_list[self.selected]]
684 current_menu.setOrigX(name_len)
685 self.columns.widget_list[0] = current_name
686 self.columns.column_types[0]=('fixed', name_len)
687 self.columns.widget_list[1] = current_menu
688
689 def keypress(self, size, key):
690 if key=='up':
691 if self.columns.get_focus_column()==0 and self.selected > 0:
692 self.selected -= 1
693 self.__showSelected()
694 elif key=='down':
695 if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1:
696 self.selected += 1
697 self.__showSelected()
698 elif key=='right':
699 if self.columns.get_focus_column()==0 and \
700 (isinstance(self.columns.widget_list[1], urwid.Text) or \
701 self.menus[self.name_list[self.selected]].getMenuSize()==0):
702 return #if we have no menu or the menu is empty, we don't go the right column
703
704 return super(MenuRoller, self).keypress(size, key)
705
706 def addMenu(self, name_param, menu):
707 name = name_param or ''
708 if name not in self.name_list:
709 self.name_list.append(name)
710 self.menus[name] = menu
711 if self.name_list[self.selected] == name:
712 self.__showSelected() #if we are on the menu, we update it
713
714 def removeMenu(self, name):
715 if name in self.name_list:
716 self.name_list.remove(name)
717 if name in self.menus.keys():
718 del self.menus[name]
719 self.selected = 0
720 self.__showSelected()
721
722 def checkShortcuts(self, key):
723 for menu in self.name_list:
724 key = self.menus[menu].checkShortcuts(key)
725 return key
726
727
728 ## DIALOGS ##
729
730 class GenericDialog(urwid.WidgetWrap):
731
732 def __init__(self, widgets_lst, title, style=[], **kwargs):
733 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
734
735 buttons = None
736
737 if "OK/CANCEL" in style:
738 cancel_arg = [kwargs['cancel_value']] if kwargs.has_key('cancel_value') else []
739 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else []
740 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb'], *cancel_arg),
741 urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)]
742 elif "YES/NO" in style:
743 yes_arg = [kwargs['yes_value']] if kwargs.has_key('yes_value') else []
744 no_arg = [kwargs['no_value']] if kwargs.has_key('no_value') else []
745 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb'], *yes_arg),
746 urwid.Button(_("No"), kwargs['no_cb'], *no_arg)]
747 if "OK" in style:
748 ok_arg = [kwargs['ok_value']] if kwargs.has_key('ok_value') else []
749 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], *ok_arg)]
750 if buttons:
751 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
752 body_content = urwid.SimpleListWalker(widgets_lst)
753 frame_body = urwid.ListBox(body_content)
754 frame = FocusFrame(frame_body, frame_header, buttons_flow if buttons else None, 'footer' if buttons else 'body')
755 decorated_frame = urwid.LineBox(frame)
756 urwid.WidgetWrap.__init__(self, decorated_frame)
757
758
759
760 class InputDialog(GenericDialog):
761 """Dialog with an edit box"""
762
763 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs):
764 instr_wid = urwid.Text(instrucions+':')
765 edit_box = AdvancedEdit(edit_text=default_txt)
766 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs)
767 self._w.base_widget.set_focus('body')
768
769 class ConfirmDialog(GenericDialog):
770 """Dialog with buttons for confirm or cancel an action"""
771
772 def __init__(self, title, style=['YES/NO'], **kwargs):
773 GenericDialog.__init__(self, [], title, style, **kwargs)
774
775 class Alert(GenericDialog):
776 """Dialog with just a message and a OK button"""
777
778 def __init__(self, title, message, style=['OK'], **kwargs):
779 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
780
781 ## CONTAINERS ##
782
783 class ColumnsRoller(urwid.FlowWidget):
784
785 def __init__(self, widget_list = None, focus_column=0):
786 self.widget_list = widget_list or []
787 self.focus_column = focus_column
788 self.__start = 0
789 self.__next = False
790
791 def addWidget(self, widget, width):
792 self.widget_list.append((width,widget))
793 if len(self.widget_list) == 1:
794 self.set_focus(0)
795
796 def getStartCol(self, widget):
797 """Return the column of the left corner of the widget"""
798 start_col = 0
799 for wid in self.widget_list[self.__start:]:
800 if wid[1] == widget:
801 return start_col
802 start_col+=wid[0]
803 return None
804
805 def selectable(self):
806 try:
807 return self.widget_list[self.focus_column][1].selectable()
808 except IndexError:
809 return False
810
811 def keypress(self, size, key):
812 if key=='left':
813 if self.focus_column>0:
814 self.focus_column-=1
815 self._invalidate()
816 return
817 if key=='right':
818 if self.focus_column<len(self.widget_list)-1:
819 self.focus_column+=1
820 self._invalidate()
821 return
822 if self.focus_column<len(self.widget_list):
823 return self.widget_list[self.focus_column][1].keypress(size,key)
824 return key
825
826 def getSelected(self):
827 """Return selected widget"""
828 return self.widget_list[self.focus_column][1]
829
830 def set_focus(self, idx):
831 if idx>len(self.widget_list)-1:
832 idx = len(self.widget_list)-1
833 self.focus_column = idx
834
835 def rows(self,size,focus=False):
836 return 1
837
838 def __calculate_limits(self, size):
839 (maxcol,) = size
840 _prev = _next = False
841 start_wid = 0
842 end_wid = len(self.widget_list)-1
843
844 total_wid = sum([w[0] for w in self.widget_list])
845 while total_wid > maxcol:
846 if self.focus_column == end_wid:
847 if not _prev:
848 total_wid+=1
849 _prev = True
850 total_wid-=self.widget_list[start_wid][0]
851 start_wid+=1
852 else:
853 if not _next:
854 total_wid+=1
855 _next = True
856 total_wid-=self.widget_list[end_wid][0]
857 end_wid-=1
858
859 cols_left = maxcol - total_wid
860 self.__start = start_wid #we need to keep it for getStartCol
861 return _prev,_next,start_wid,end_wid,cols_left
862
863
864 def mouse_event(self, size, event, button, x, y, focus):
865 (maxcol,)=size
866
867 if urwid.is_mouse_press(event) and button == 1:
868 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size)
869 if x==0 and _prev:
870 self.keypress(size,'left')
871 return True
872 if x==maxcol-1 and _next:
873 self.keypress(size,'right')
874 return True
875
876 current_pos = 1 if _prev else 0
877 idx = 0
878 while current_pos<x and idx<len(self.widget_list):
879 width,widget = self.widget_list[idx]
880 if x<=current_pos+width:
881 self.focus_column = idx
882 self._invalidate()
883 if not hasattr(widget,'mouse_event'):
884 return False
885 return widget.mouse_event((width,0), event, button,
886 x-current_pos, 0, focus)
887
888 current_pos+=self.widget_list[idx][0]
889 idx+=1
890
891 return False
892
893 def render(self, size, focus=False):
894 if not self.widget_list:
895 return SolidCanvas(" ", size[0], 1)
896
897 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size)
898
899 idx=start_wid
900 render = []
901
902 for width,widget in self.widget_list[start_wid:end_wid+1]:
903 _focus = idx == self.focus_column and focus
904 render.append((widget.render((width,),_focus),False,_focus,width))
905 idx+=1
906 if _prev:
907 render.insert(0,(urwid.Text([u"◀"]).render((1,),False),False,False,1))
908 if _next:
909 render.append((urwid.Text([u"▶"],align='right').render((1+cols_left,),False),False,False,1+cols_left))
910 else:
911 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left))
912
913 return urwid.CanvasJoin(render)
914
915
916 class FocusFrame(urwid.Frame):
917 """Frame which manage 'tab' key"""
918
919 def keypress(self, size, key):
920 ret = urwid.Frame.keypress(self, size, key)
921 if not ret:
922 return
923
924 if key == 'tab':
925 focus_list = ('header','body','footer')
926 focus_idx = focus_list.index(self.focus_part)
927 for i in range(2):
928 focus_idx = (focus_idx + 1) % len(focus_list)
929 focus_name = focus_list[focus_idx]
930 widget = getattr(self,'_'+focus_name)
931 if widget!=None and widget.selectable():
932 self.set_focus(focus_name)
933
934 return ret
935
936 class TabsContainer(urwid.WidgetWrap):
937 signals = ['click']
938
939 def __init__(self):
940 self._current_tab = None
941 self._buttons_cont = ColumnsRoller()
942 self.tabs = []
943 self.__frame = FocusFrame(urwid.Filler(urwid.Text('')),urwid.Pile([self._buttons_cont,urwid.Divider(u"─")]))
944 urwid.WidgetWrap.__init__(self, self.__frame)
945
946 def keypress(self, size, key):
947 if key=='tab':
948 self._w.keypress(size,key)
949 return
950 return self._w.keypress(size,key)
951
952 def __buttonClicked(self, button, invisible=False):
953 """Called when a button on the tab is changed,
954 change the page
955 @param button: button clicked
956 @param invisible: emit signal only if False"""
957 tab_name = button.get_label()
958 for tab in self.tabs:
959 if tab[0] == tab_name:
960 break
961 if tab[0] != tab_name:
962 error(_("INTERNAL ERROR: Tab not found"))
963 assert(False)
964 self.__frame.body = tab[1]
965 button.set_label(('title',button.get_label()))
966 if self._current_tab:
967 self._current_tab.set_label(self._current_tab.get_label())
968 self._current_tab = button
969 if not invisible:
970 self._emit('click')
971
972 def __appendButton(self, name):
973 """Append a button to the frame header,
974 and link it to the page change method"""
975 button = CustomButton(name, self.__buttonClicked, left_border = '', right_border=' | ')
976 self._buttons_cont.addWidget(button, button.getSize())
977 if len(self._buttons_cont.widget_list) == 1:
978 #first button: we set the focus and the body
979 self._buttons_cont.set_focus(0)
980 self.__buttonClicked(button,True)
981
982 def addTab(self,name,content=[]):
983 """Add a page to the container
984 @param name: name of the page (what appear on the tab)
985 @param content: content of the page
986 @return: ListBox (content of the page)"""
987 listbox = urwid.ListBox(urwid.SimpleListWalker(content))
988 self.tabs.append([name,listbox])
989 self.__appendButton(name)
990 return listbox
991
992 def addFooter(self, widget):
993 """Add a widget on the bottom of the tab (will be displayed on all pages)
994 @param widget: FlowWidget"""
995 self._w.footer = widget
996
997
998 ## DECORATORS ##
999 class LabelLine(urwid.LineBox):
1000 """Like LineBox, but with a Label centered in the top line"""
1001
1002 def __init__(self, original_widget, label_widget):
1003 urwid.LineBox.__init__(self, original_widget)
1004 top_columns = self._w.widget_list[0]
1005 top_columns.widget_list[1] = label_widget
1006
1007 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap):
1008 def __init__(self, original_widget, left_char = u"│", right_char = ''):
1009 """Draw a separator on left and/or right of original_widget."""
1010
1011 widgets = [original_widget]
1012 if left_char:
1013 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char)))
1014 if right_char:
1015 widgets.append(('fixed', 1, urwid.SolidFill(right_char)))
1016 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1)
1017 urwid.WidgetDecoration.__init__(self, original_widget)
1018 urwid.WidgetWrap.__init__(self, columns)
1019
1020