widget_selection.py
95 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r14283 | """SelectionWidget class. | ||
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 | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14698 | from threading import Lock | ||
Jonathan Frederic
|
r14540 | from .widget import DOMWidget | ||
Jonathan Frederic
|
r14698 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict | ||
Jonathan Frederic
|
r14242 | |||
Jonathan Frederic
|
r14283 | #----------------------------------------------------------------------------- | ||
# SelectionWidget | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r14670 | class _SelectionWidget(DOMWidget): | ||
Jonathan Frederic
|
r14698 | value = Any(help="Selected value") | ||
values = List(help="List of values the user can select") | ||||
Jonathan Frederic
|
r14709 | labels = List(help="""List of string representations for each value. | ||
Jonathan Frederic
|
r14698 | These string representations are used to display the values in the | ||
Jonathan Frederic
|
r14709 | front-end.""", sync=True) # Only synced to the back-end. | ||
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) | ||||
Jonathan Frederic
|
r14592 | |||
Jonathan Frederic
|
r14698 | _value = Unicode(sync=True) # Bi-directionally synced. | ||
def __init__(self, *pargs, **kwargs): | ||||
"""Constructor""" | ||||
self.value_lock = Lock() | ||||
self.on_trait_change(self._string_value_set, ['_value']) | ||||
Jonathan Frederic
|
r14709 | DOMWidget.__init__(self, *pargs, **kwargs) | ||
Jonathan Frederic
|
r14698 | |||
Jonathan Frederic
|
r14709 | def _labels_changed(self, name=None, old=None, new=None): | ||
Jonathan Frederic
|
r14698 | """Handles when the value_names Dict has been changed. | ||
This method sets the _reverse_value_names Dict to the inverse of the new | ||||
value for the value_names Dict.""" | ||||
Jonathan Frederic
|
r14709 | if len(new) != len(self.values): | ||
raise TypeError('Labels list must be the same size as the values list.') | ||||
Jonathan Frederic
|
r14698 | |||
def _values_changed(self, name=None, old=None, new=None): | ||||
Jonathan Frederic
|
r14709 | """Handles when the value_names Dict has been changed. | ||
This method sets the _reverse_value_names Dict to the inverse of the new | ||||
value for the value_names Dict.""" | ||||
if len(new) != len(self.labels): | ||||
self.labels = [(self.labels[i] if i < len(self.labels) else str(v)) for i, v in enumerate(new)] | ||||
Jonathan Frederic
|
r14698 | |||
def _value_changed(self, name, old, new): | ||||
"""Called when value has been changed""" | ||||
if self.value_lock.acquire(False): | ||||
try: | ||||
# Make sure the value is in the list of values. | ||||
if new in self.values: | ||||
# Set the string version of the value. | ||||
Jonathan Frederic
|
r14709 | self._value = self.labels[self.values.index(new)] | ||
Jonathan Frederic
|
r14698 | else: | ||
raise TypeError('Value must be a value in the values list.') | ||||
finally: | ||||
self.value_lock.release() | ||||
def _string_value_set(self, name, old, new): | ||||
"""Called when _value has been changed.""" | ||||
if self.value_lock.acquire(False): | ||||
try: | ||||
Jonathan Frederic
|
r14709 | if new in self.labels: | ||
self.value = self.values[self.labels.index(new)] | ||||
Jonathan Frederic
|
r14698 | else: | ||
self.value = None | ||||
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) | ||||