comparison urwid_satext/sat_widgets.py @ 68:5c28bb50ae0d

new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:01:09 +0100
parents c270867a80f9
children b39c81cdd863
comparison
equal deleted inserted replaced
67:c270867a80f9 68:5c28bb50ae0d
111 def render(self, size, focus=False): 111 def render(self, size, focus=False):
112 return super(Password, self).render(size, focus) 112 return super(Password, self).render(size, focus)
113 113
114 class ModalEdit(AdvancedEdit): 114 class ModalEdit(AdvancedEdit):
115 """AdvancedEdit with vi-like mode management 115 """AdvancedEdit with vi-like mode management
116 - there is a new 'mode' property wich can be changed with properties 116 - there is a new 'mode' property which can be changed with properties
117 specified during init 117 specified during init
118 - completion callback received a new 'mode' argument 118 - completion callback received a new 'mode' argument
119 """ 119 """
120 120
121 def __init__(self, modes, *args, **kwargs): 121 def __init__(self, modes, *args, **kwargs):
198 self.setSelectedText(selected_text) 198 self.setSelectedText(selected_text)
199 self.setState(selected) 199 self.setState(selected)
200 200
201 def __valid_text(self, text): 201 def __valid_text(self, text):
202 """Tmp method needed until dbus and urwid are more friends""" 202 """Tmp method needed until dbus and urwid are more friends"""
203 if isinstance(text, ListOption):
204 return text
203 if isinstance(text,basestring): 205 if isinstance(text,basestring):
204 return unicode(text) 206 return unicode(text)
205 elif isinstance(text,tuple): 207 elif isinstance(text,tuple):
206 return (unicode(text[0]),text[1]) 208 return (unicode(text[0]),text[1])
207 elif isinstance(text,list): 209 elif isinstance(text,list):
339 341
340 def set_label(self, label): 342 def set_label(self, label):
341 self.label = label 343 self.label = label
342 self.set_text([self.left_border, label, self.right_border]) 344 self.set_text([self.left_border, label, self.right_border])
343 345
346 class ListOption(unicode):
347 """ Class similar to unicode, but which make the difference between value and label
348 label is show when use as unicode, the .value attribute contain the actual value
349 Can be initialised with:
350 - basestring (label = value = given string)
351 - a tuple with (value, label)
352 XXX: comparaison is made again value, not the label which is the one displayed
353
354 """
355
356 def __new__(cls, option):
357 if (isinstance(option, cls)):
358 return option
359 elif isinstance(option, basestring):
360 value = label = option
361 elif (isinstance(option, tuple) and len(option) == 2):
362 value, label = option
363 else:
364 raise NotImplementedError
365 if not value:
366 raise ValueError("value can't be empty")
367 if not label:
368 label = value
369 instance = super(ListOption, cls).__new__(cls, label)
370 instance._value = value
371 return instance
372
373 def __eq__(self, other):
374 # XXX: try to compare values, if other has no value
375 # (e.g. unicode string) try to compare to other itself
376 try:
377 return self._value == other._value
378 except AttributeError:
379 return self._value == other
380
381 def __ne__(self, other):
382 # XXX: see __eq__
383 try:
384 return self._value != other._value
385 except AttributeError:
386 return self._value != other
387
388 @property
389 def value(self):
390 """ return option value """
391 return self._value
392
393 @value.setter
394 def value(self, value):
395 self._value = value
396
397 @staticmethod
398 def fromOptions(options):
399 """ convert a list of string/tuple options to a list of listOption
400 @param options: list of managed option type (basestring, tuple)
401 return: list of ListOption
402 """
403 return [(ListOption(option)) for option in options]
404
405
344 class GenericList(urwid.WidgetWrap): 406 class GenericList(urwid.WidgetWrap):
345 signals = ['click','change'] 407 signals = ['click','change']
346 408
347 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): 409 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
348 """ 410 """
422 484
423 def getDisplayWidget(self): 485 def getDisplayWidget(self):
424 return self.list_box 486 return self.list_box
425 487
426 def changeValues(self, new_values): 488 def changeValues(self, new_values):
427 """Change all value in one shot""" 489 """Change all values in one shot"""
490 new_values = ListOption.fromOptions(new_values)
428 if not self.first_display: 491 if not self.first_display:
429 old_selected = self.getSelectedValues() 492 old_selected = self.getSelectedValues()
430 widgets = [] 493 widgets = []
431 for option in new_values: 494 for option in new_values:
432 widget = self.option_type(option, self.align) 495 widget = self.option_type(option, self.align)