예제 #1
0
class Selector(WidgetWrap):
    """A widget that allows the user to chose between options by popping
       up a list of options.

    (A bit like <select> in an HTML form).
    """

    signals = ['select']

    def __init__(self, opts, index=0):
        self._icon = SelectableIcon("", 0)
        self._padding = UrwidPadding(
            AttrWrap(
                Columns([
                    (1, Text('[')),
                    self._icon,
                    (3, Text('\N{BLACK DOWN-POINTING SMALL TRIANGLE} ]')),
                ],
                        dividechars=1), 'menu_button', 'menu_button focus'))

        options = []
        for opt in opts:
            options.append(Option(opt))

        self.options = options
        self._set_index(index)
        super().__init__(_Launcher(self, self._padding))

    def get_natural_width(self):
        return self._padding.width

    def keypress(self, size, key):
        if self._command_map[key] != ACTIVATE:
            return key
        self.open_pop_up()

    def _set_index(self, val):
        self._icon.set_text(self._options[val].label)
        self._index = val

    @property
    def index(self):
        return self._index

    @index.setter
    def index(self, val):
        self._emit('select', self._options[val].value)
        self._set_index(val)

    @property
    def options(self):
        return self._options[:]

    @options.setter
    def options(self, val):
        self._options = val
        self._padding.width = max([len(o.label) for o in self._options]) + 6

    def option_by_label(self, label):
        for opt in self._options:
            if opt.label == label:
                return opt

    def option_by_value(self, value):
        for opt in self._options:
            if opt.value == value:
                return opt

    def option_by_index(self, index):
        return self._options[index]

    @property
    def value(self):
        return self._options[self._index].value

    @value.setter
    def value(self, val):
        for i, opt in enumerate(self._options):
            if opt.value == val:
                self._set_index(i)
                return
        raise AttributeError("cannot set value to %r", val)

    def create_pop_up(self):
        return _PopUpSelectDialog(self, self.index)

    def get_pop_up_parameters(self):
        # line on left, space, line on right
        return {
            'left': 0,
            'top': -self.index - 1,
            'overlay_width': self._padding.width,
            'overlay_height': len(self._options) + 2
        }

    def open_pop_up(self):
        self._w.open_pop_up()

    def close_pop_up(self):
        self._w.close_pop_up()
예제 #2
0
class Selector(PopUpLauncher):
    """A widget that allows the user to chose between options by popping up this list of options.

    (A bit like <select> in an HTML form).
    """

    _prefix = "(+) "

    signals = ['select']

    def __init__(self, opts, index=0):
        self._options = []
        for opt in opts:
            if not isinstance(opt, tuple):
                if not isinstance(opt, str):
                    raise SelectorError("invalid option %r", opt)
                opt = (opt, True, opt)
            elif len(opt) == 1:
                opt = (opt[0], True, opt[0])
            elif len(opt) == 2:
                opt = (opt[0], opt[1], opt[0])
            elif len(opt) != 3:
                raise SelectorError("invalid option %r", opt)
            self._options.append(opt)
        self._button = SelectableIcon(self._prefix, len(self._prefix))
        self._set_index(index)
        super().__init__(self._button)

    def keypress(self, size, key):
        if self._command_map[key] != ACTIVATE:
            return key
        self.open_pop_up()

    def _set_index(self, val):
        self._button.set_text(self._prefix + self._options[val][0])
        self._index = val

    @property
    def index(self):
        return self._index

    @index.setter
    def index(self, val):
        self._emit('select', self._options[val][2])
        self._set_index(val)

    @property
    def value(self):
        return self._options[self._index][2]

    @value.setter
    def value(self, val):
        for i, (label, enabled, value) in enumerate(self._options):
            if value == val:
                self.index = i
                return
        raise AttributeError("cannot set value to %r", val)

    def create_pop_up(self):
        return _PopUpSelectDialog(self, self.index)

    def get_pop_up_parameters(self):
        width = max([len(o[0]) for o in self._options]) \
          + len(self._prefix) +  3 # line on left, space, line on right
        return {'left':-1, 'top':-self.index-1, 'overlay_width':width, 'overlay_height':len(self._options) + 2}
예제 #3
0
class Selector(PopUpLauncher):
    """A widget that allows the user to chose between options by popping
       up a list of options.

    (A bit like <select> in an HTML form).
    """

    _prefix = "(+) "

    signals = ['select']

    def __init__(self, opts, index=0):
        self._options = []
        for opt in opts:
            if not isinstance(opt, Option):
                opt = Option(opt)
            self._options.append(opt)
        self._button = SelectableIcon(self._prefix, len(self._prefix))
        self._set_index(index)
        super().__init__(self._button)

    def keypress(self, size, key):
        if self._command_map[key] != ACTIVATE:
            return key
        self.open_pop_up()

    def _set_index(self, val):
        self._button.set_text(self._prefix + self._options[val].label)
        self._index = val

    @property
    def index(self):
        return self._index

    @index.setter
    def index(self, val):
        self._emit('select', self._options[val].value)
        self._set_index(val)

    def option_by_label(self, label):
        for opt in self._options:
            if opt.label == label:
                return opt

    def option_by_value(self, value):
        for opt in self._options:
            if opt.value == value:
                return opt

    def option_by_index(self, index):
        return self._options[index]

    @property
    def value(self):
        return self._options[self._index].value

    @value.setter
    def value(self, val):
        for i, opt in enumerate(self._options):
            if opt.value == val:
                self.index = i
                return
        raise AttributeError("cannot set value to %r", val)

    def create_pop_up(self):
        return _PopUpSelectDialog(self, self.index)

    def get_pop_up_parameters(self):
        # line on left, space, line on right
        width = (max([len(o.label) for o in self._options]) +
                 len(self._prefix) + 3)
        return {'left': -1, 'top': -self.index - 1,
                'overlay_width': width,
                'overlay_height': len(self._options) + 2}