##// END OF EJS Templates
Renamed widgets......
Renamed widgets... TextWidget, TextareaWidget, CheckboxWidget, and SelectWidget

File last commit:

r14834:5ed93769
r14834:5ed93769
Show More
widget_selection.py
95 lines | 3.7 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleaned up Python widget code.
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
Support multiple types in selection widget.
r14698 from threading import Lock
Jonathan Frederic
s/Widget/DOMWidget s/BaseWidget/Widget
r14540 from .widget import DOMWidget
Jonathan Frederic
Support multiple types in selection widget.
r14698 from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict
Jonathan Frederic
Add selection widget
r14242
Jonathan Frederic
Cleaned up Python widget code.
r14283 #-----------------------------------------------------------------------------
# SelectionWidget
#-----------------------------------------------------------------------------
Jonathan Frederic
Create base widget classes
r14670 class _SelectionWidget(DOMWidget):
Jonathan Frederic
Support multiple types in selection widget.
r14698 value = Any(help="Selected value")
values = List(help="List of values the user can select")
Jonathan Frederic
Changed selection widget API to use labels list...
r14709 labels = List(help="""List of string representations for each value.
Jonathan Frederic
Support multiple types in selection widget.
r14698 These string representations are used to display the values in the
Jonathan Frederic
Changed selection widget API to use labels list...
r14709 front-end.""", sync=True) # Only synced to the back-end.
Jonathan Frederic
sync=True isntead of a keys list
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
1-to-1 widget / view mapping
r14592
Jonathan Frederic
Support multiple types in selection widget.
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
Changed selection widget API to use labels list...
r14709 DOMWidget.__init__(self, *pargs, **kwargs)
Jonathan Frederic
Support multiple types in selection widget.
r14698
Jonathan Frederic
Changed selection widget API to use labels list...
r14709 def _labels_changed(self, name=None, old=None, new=None):
Jonathan Frederic
Support multiple types in selection widget.
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
Changed selection widget API to use labels list...
r14709 if len(new) != len(self.values):
raise TypeError('Labels list must be the same size as the values list.')
Jonathan Frederic
Support multiple types in selection widget.
r14698
def _values_changed(self, name=None, old=None, new=None):
Jonathan Frederic
Changed selection widget API to use labels list...
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
Support multiple types in selection widget.
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
Changed selection widget API to use labels list...
r14709 self._value = self.labels[self.values.index(new)]
Jonathan Frederic
Support multiple types in selection widget.
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
Changed selection widget API to use labels list...
r14709 if new in self.labels:
self.value = self.values[self.labels.index(new)]
Jonathan Frederic
Support multiple types in selection widget.
r14698 else:
self.value = None
finally:
self.value_lock.release()
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Jonathan Frederic
Create base widget classes
r14670 class ToggleButtonsWidget(_SelectionWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('ToggleButtonsView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
class DropdownWidget(_SelectionWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('DropdownView', sync=True)
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Jonathan Frederic
Create base widget classes
r14670 class RadioButtonsWidget(_SelectionWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('RadioButtonsView', sync=True)
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Jonathan Frederic
Renamed widgets......
r14834 class SelectWidget(_SelectionWidget):
_view_name = Unicode('SelectView', sync=True)