widget_selection.py
121 lines
| 4.6 KiB
| text/x-python
|
PythonLexer
MinRK
|
r15024 | """SelectionWidget classes. | ||
Jonathan Frederic
|
r14283 | |||
Represents an enumeration using a widget. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r15024 | |||
from collections import OrderedDict | ||||
Jonathan Frederic
|
r14698 | from threading import Lock | ||
Jonathan Frederic
|
r14540 | from .widget import DOMWidget | ||
MinRK
|
r15024 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError | ||
from IPython.utils.py3compat import unicode_type | ||||
Jonathan Frederic
|
r14242 | |||
Jonathan Frederic
|
r14283 | #----------------------------------------------------------------------------- | ||
# SelectionWidget | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14670 | class _SelectionWidget(DOMWidget): | ||
MinRK
|
r15024 | """Base class for Selection widgets | ||
``values`` can be specified as a list or dict. If given as a list, | ||||
it will be transformed to a dict of the form ``{str(value):value}``. | ||||
""" | ||||
value = Any(help="Selected value") | ||||
values = Dict(help="""Dictionary of {name: value} the user can select. | ||||
The keys of this dictionary are the strings that will be displayed in the UI, | ||||
representing the actual Python choices. | ||||
The keys of this dictionary are also available as value_names. | ||||
""") | ||||
value_name = Unicode(help="The name of the selected value", sync=True) | ||||
MinRK
|
r15025 | value_names = List(Unicode, help="""Read-only list of names for each value. | ||
MinRK
|
r15024 | |||
If values is specified as a list, this is the string representation of each element. | ||||
Otherwise, it is the keys of the values dictionary. | ||||
These strings are used to display the choices in the front-end.""", sync=True) | ||||
Jonathan Frederic
|
r14588 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | ||
description = Unicode(help="Description of the value this widget represents", sync=True) | ||||
MinRK
|
r15024 | |||
Jonathan Frederic
|
r14592 | |||
MinRK
|
r15024 | def __init__(self, *args, **kwargs): | ||
Jonathan Frederic
|
r14698 | self.value_lock = Lock() | ||
MinRK
|
r15025 | self._in_values_changed = False | ||
MinRK
|
r15024 | if 'values' in kwargs: | ||
values = kwargs['values'] | ||||
# convert list values to an dict of {str(v):v} | ||||
if isinstance(values, list): | ||||
# preserve list order with an OrderedDict | ||||
kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values) | ||||
DOMWidget.__init__(self, *args, **kwargs) | ||||
def _values_changed(self, name, old, new): | ||||
"""Handles when the values dict has been changed. | ||||
Jonathan Frederic
|
r14698 | |||
MinRK
|
r15024 | Setting values implies setting value names from the keys of the dict. | ||
""" | ||||
MinRK
|
r15025 | self._in_values_changed = True | ||
try: | ||||
self.value_names = list(new.keys()) | ||||
finally: | ||||
self._in_values_changed = False | ||||
MinRK
|
r15046 | |||
# ensure that the chosen value is one of the choices | ||||
if self.value not in new.values(): | ||||
self.value = next(iter(new.values())) | ||||
MinRK
|
r15024 | |||
def _value_names_changed(self, name, old, new): | ||||
MinRK
|
r15025 | if not self._in_values_changed: | ||
raise TraitError("value_names is a read-only proxy to values.keys(). Use the values dict instead.") | ||||
Jonathan Frederic
|
r14698 | |||
def _value_changed(self, name, old, new): | ||||
"""Called when value has been changed""" | ||||
if self.value_lock.acquire(False): | ||||
try: | ||||
MinRK
|
r15046 | # Reverse dictionary lookup for the value name | ||
MinRK
|
r15024 | for k,v in self.values.items(): | ||
if new == v: | ||||
# set the selected value name | ||||
self.value_name = k | ||||
return | ||||
MinRK
|
r15401 | # undo the change, and raise KeyError | ||
self.value = old | ||||
MinRK
|
r15046 | raise KeyError(new) | ||
Jonathan Frederic
|
r14698 | finally: | ||
self.value_lock.release() | ||||
MinRK
|
r15024 | def _value_name_changed(self, name, old, new): | ||
"""Called when the value name has been changed (typically by the frontend).""" | ||||
Jonathan Frederic
|
r14698 | if self.value_lock.acquire(False): | ||
try: | ||||
MinRK
|
r15046 | self.value = self.values[new] | ||
Jonathan Frederic
|
r14698 | finally: | ||
self.value_lock.release() | ||||
Jonathan Frederic
|
r14592 | |||
Jonathan Frederic
|
r14670 | class ToggleButtonsWidget(_SelectionWidget): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('ToggleButtonsView', sync=True) | ||
Jonathan Frederic
|
r14670 | |||
class DropdownWidget(_SelectionWidget): | ||||
Jonathan Frederic
|
r14701 | _view_name = Unicode('DropdownView', sync=True) | ||
Jonathan Frederic
|
r14592 | |||
Jonathan Frederic
|
r14670 | class RadioButtonsWidget(_SelectionWidget): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('RadioButtonsView', sync=True) | ||
Jonathan Frederic
|
r14592 | |||
Jonathan Frederic
|
r14834 | class SelectWidget(_SelectionWidget): | ||
_view_name = Unicode('SelectView', sync=True) | ||||