Mercurial > urwid-satext
comparison urwid_satext/sat_widgets.py @ 150:aa8f46b43a71
fix `single` style in GenericList:
multiple options could be selected even with `single` style. This patch fix it by
unselecting every other options when an option is set.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 08 Dec 2021 15:33:28 +0100 |
parents | bfab04d0a745 |
children | 6689aa54b20c |
comparison
equal
deleted
inserted
replaced
149:5158ff7370e5 | 150:aa8f46b43a71 |
---|---|
558 | 558 |
559 | 559 |
560 class GenericList(urwid.ListBox): | 560 class GenericList(urwid.ListBox): |
561 signals = ['click','change'] | 561 signals = ['click','change'] |
562 | 562 |
563 def __init__(self, options, style=None, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None): | 563 def __init__(self, options, style=None, align='left', option_type=SelectableText, on_click=None, on_change=None, user_data=None): |
564 """Widget managing list of string and their selection | 564 """Widget managing list of string and their selection |
565 | 565 |
566 @param options: list of strings used for options | 566 @param options: list of strings used for options |
567 @param style: list of string: | 567 @param style: list of string: |
568 - 'single' if only one must be selected | 568 - 'single' if only one must be selected |
653 for widget in self.content: | 653 for widget in self.content: |
654 if widget.getState(): | 654 if widget.getState(): |
655 result.append(widget.getValue()) | 655 result.append(widget.getValue()) |
656 return result | 656 return result |
657 | 657 |
658 def on_option_change(self, wid, *args, **kwargs): | |
659 if self.single: | |
660 for w in self.content: | |
661 if w is not wid: | |
662 w.setState(False, invisible=True) | |
663 | |
658 def changeValues(self, new_values): | 664 def changeValues(self, new_values): |
659 """Change all values in one shot""" | 665 """Change all values in one shot""" |
660 new_values = ListOption.fromOptions(new_values) | 666 new_values = ListOption.fromOptions(new_values) |
661 if not self.first_display: | 667 old_selected = self.getSelectedValues() if not self.first_display else [] |
662 old_selected = self.getSelectedValues() | |
663 widgets = [] | 668 widgets = [] |
664 for option in new_values: | 669 for option in new_values: |
665 widget = self.option_type(option, align=self.align) | 670 widget = self.option_type(option, align=self.align) |
671 urwid.connect_signal(widget, "change", self.on_option_change) | |
666 if not self.first_display and option in old_selected: | 672 if not self.first_display and option in old_selected: |
667 widget.setState(True) | 673 widget.setState(True) |
668 widgets.append(widget) | 674 widgets.append(widget) |
669 self.content[:] = widgets | 675 self.content[:] = widgets |
670 if self.first_display and self.single and new_values and not self.no_first_select: | 676 if self.first_display and self.single and new_values and not self.no_first_select: |