2014-07-19 03:47:33 +08:00
|
|
|
"""Int class.
|
2013-10-26 02:31:48 +08:00
|
|
|
|
|
|
|
Represents an unbounded int 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
|
|
|
|
#-----------------------------------------------------------------------------
|
2014-09-18 05:04:03 +08:00
|
|
|
from .widget import DOMWidget, register
|
2014-08-27 08:26:05 +08:00
|
|
|
from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple
|
2014-07-19 03:47:33 +08:00
|
|
|
from IPython.utils.warn import DeprecatedClass
|
2013-10-19 04:38:26 +08:00
|
|
|
|
2013-10-26 02:31:48 +08:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Classes
|
|
|
|
#-----------------------------------------------------------------------------
|
2014-07-19 03:47:33 +08:00
|
|
|
class _Int(DOMWidget):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Base class used to create widgets that represent an int."""
|
2015-01-28 04:48:47 +08:00
|
|
|
value = CInt(0, help="Int value", sync=True)
|
2014-01-14 23:15:19 +08:00
|
|
|
disabled = Bool(False, help="Enable or disable user changes", sync=True)
|
|
|
|
description = Unicode(help="Description of the value this widget represents", sync=True)
|
2014-01-22 08:44:57 +08:00
|
|
|
|
2014-09-28 03:14:21 +08:00
|
|
|
def __init__(self, value=None, **kwargs):
|
|
|
|
if value is not None:
|
|
|
|
kwargs['value'] = value
|
|
|
|
super(_Int, self).__init__(**kwargs)
|
2014-01-22 08:44:57 +08:00
|
|
|
|
2014-07-19 03:47:33 +08:00
|
|
|
class _BoundedInt(_Int):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Base class used to create widgets that represent a int that is bounded
|
|
|
|
by a minium and maximum."""
|
2014-01-22 08:44:57 +08:00
|
|
|
step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
|
|
|
|
max = CInt(100, help="Max value", sync=True)
|
|
|
|
min = CInt(0, help="Min value", sync=True)
|
|
|
|
|
|
|
|
def __init__(self, *pargs, **kwargs):
|
|
|
|
"""Constructor"""
|
2014-09-28 04:07:42 +08:00
|
|
|
super(_BoundedInt, self).__init__(*pargs, **kwargs)
|
2015-01-28 04:48:47 +08:00
|
|
|
self._handle_value_changed('value', None, self.value)
|
|
|
|
self._handle_max_changed('max', None, self.max)
|
|
|
|
self._handle_min_changed('min', None, self.min)
|
|
|
|
self.on_trait_change(self._handle_value_changed, 'value')
|
|
|
|
self.on_trait_change(self._handle_max_changed, 'max')
|
|
|
|
self.on_trait_change(self._handle_min_changed, 'min')
|
2014-01-22 08:44:57 +08:00
|
|
|
|
2015-01-28 04:48:47 +08:00
|
|
|
def _handle_value_changed(self, name, old, new):
|
2014-09-16 13:24:49 +08:00
|
|
|
"""Validate value."""
|
2014-01-22 08:44:57 +08:00
|
|
|
if self.min > new or new > self.max:
|
|
|
|
self.value = min(max(new, self.min), self.max)
|
|
|
|
|
2015-01-28 04:48:47 +08:00
|
|
|
def _handle_max_changed(self, name, old, new):
|
2014-09-16 13:24:49 +08:00
|
|
|
"""Make sure the min is always <= the max."""
|
2014-09-24 03:25:02 +08:00
|
|
|
if new < self.min:
|
|
|
|
raise ValueError("setting max < min")
|
2015-01-27 07:23:09 +08:00
|
|
|
if new < self.value:
|
|
|
|
self.value = new
|
2014-09-16 13:24:49 +08:00
|
|
|
|
2015-01-28 04:48:47 +08:00
|
|
|
def _handle_min_changed(self, name, old, new):
|
2014-09-16 13:24:49 +08:00
|
|
|
"""Make sure the max is always >= the min."""
|
2014-09-24 03:25:02 +08:00
|
|
|
if new > self.max:
|
|
|
|
raise ValueError("setting min > max")
|
2015-01-27 07:23:09 +08:00
|
|
|
if new > self.value:
|
|
|
|
self.value = new
|
2014-01-22 08:44:57 +08:00
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.IntText')
|
2014-07-19 03:47:33 +08:00
|
|
|
class IntText(_Int):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Textbox widget that represents a int."""
|
2014-01-23 08:21:00 +08:00
|
|
|
_view_name = Unicode('IntTextView', sync=True)
|
2014-01-22 08:44:57 +08:00
|
|
|
|
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.BoundedIntText')
|
2014-07-19 03:47:33 +08:00
|
|
|
class BoundedIntText(_BoundedInt):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Textbox widget that represents a int bounded by a minimum and maximum value."""
|
2014-01-23 08:21:00 +08:00
|
|
|
_view_name = Unicode('IntTextView', sync=True)
|
2014-01-22 08:44:57 +08:00
|
|
|
|
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.IntSlider')
|
2014-07-19 03:47:33 +08:00
|
|
|
class IntSlider(_BoundedInt):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Slider widget that represents a int bounded by a minimum and maximum value."""
|
2014-01-23 08:21:00 +08:00
|
|
|
_view_name = Unicode('IntSliderView', sync=True)
|
2014-08-27 08:26:05 +08:00
|
|
|
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
|
|
|
|
default_value='horizontal', allow_none=False,
|
2014-01-22 08:44:57 +08:00
|
|
|
help="Vertical or horizontal.", sync=True)
|
2014-08-24 17:48:19 +08:00
|
|
|
_range = Bool(False, help="Display a range selector", sync=True)
|
2014-02-07 09:09:09 +08:00
|
|
|
readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
|
2014-08-26 04:00:29 +08:00
|
|
|
slider_color = Unicode(sync=True)
|
2014-01-22 08:44:57 +08:00
|
|
|
|
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.IntProgress')
|
2014-07-19 03:47:33 +08:00
|
|
|
class IntProgress(_BoundedInt):
|
2014-07-22 02:37:42 +08:00
|
|
|
"""Progress bar that represents a int bounded by a minimum and maximum value."""
|
2014-01-23 08:21:00 +08:00
|
|
|
_view_name = Unicode('ProgressView', sync=True)
|
2014-06-25 23:51:55 +08:00
|
|
|
|
2014-08-27 08:26:05 +08:00
|
|
|
bar_style = CaselessStrEnum(
|
|
|
|
values=['success', 'info', 'warning', 'danger', ''],
|
|
|
|
default_value='', allow_none=True, sync=True, help="""Use a
|
|
|
|
predefined styling for the progess bar.""")
|
|
|
|
|
2014-08-22 16:19:03 +08:00
|
|
|
class _IntRange(_Int):
|
2014-08-20 22:25:33 +08:00
|
|
|
value = Tuple(CInt, CInt, default_value=(0, 1), help="Tuple of (lower, upper) bounds", sync=True)
|
|
|
|
lower = CInt(0, help="Lower bound", sync=False)
|
|
|
|
upper = CInt(1, help="Upper bound", sync=False)
|
|
|
|
|
|
|
|
def __init__(self, *pargs, **kwargs):
|
|
|
|
value_given = 'value' in kwargs
|
2014-08-24 18:26:56 +08:00
|
|
|
lower_given = 'lower' in kwargs
|
|
|
|
upper_given = 'upper' in kwargs
|
|
|
|
if value_given and (lower_given or upper_given):
|
|
|
|
raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
|
|
|
|
if lower_given != upper_given:
|
|
|
|
raise ValueError("Must specify both 'lower' and 'upper' for range widget")
|
2014-08-20 22:25:33 +08:00
|
|
|
|
2014-12-18 04:47:04 +08:00
|
|
|
super(_IntRange, self).__init__(*pargs, **kwargs)
|
2014-08-20 22:25:33 +08:00
|
|
|
|
|
|
|
# ensure the traits match, preferring whichever (if any) was given in kwargs
|
|
|
|
if value_given:
|
|
|
|
self.lower, self.upper = self.value
|
|
|
|
else:
|
|
|
|
self.value = (self.lower, self.upper)
|
|
|
|
|
|
|
|
self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
|
|
|
|
|
|
|
|
def _validate(self, name, old, new):
|
|
|
|
if name == 'value':
|
|
|
|
self.lower, self.upper = min(new), max(new)
|
|
|
|
elif name == 'lower':
|
|
|
|
self.value = (new, self.value[1])
|
|
|
|
elif name == 'upper':
|
|
|
|
self.value = (self.value[0], new)
|
2014-06-25 23:51:55 +08:00
|
|
|
|
2014-08-22 16:19:03 +08:00
|
|
|
class _BoundedIntRange(_IntRange):
|
2014-06-25 23:51:55 +08:00
|
|
|
step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
|
|
|
|
max = CInt(100, help="Max value", sync=True)
|
|
|
|
min = CInt(0, help="Min value", sync=True)
|
|
|
|
|
|
|
|
def __init__(self, *pargs, **kwargs):
|
2014-08-20 22:25:33 +08:00
|
|
|
any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs
|
2014-08-22 16:19:03 +08:00
|
|
|
_IntRange.__init__(self, *pargs, **kwargs)
|
2014-08-20 22:25:33 +08:00
|
|
|
|
|
|
|
# ensure a minimal amount of sanity
|
|
|
|
if self.min > self.max:
|
|
|
|
raise ValueError("min must be <= max")
|
|
|
|
|
2014-08-24 18:26:56 +08:00
|
|
|
if any_value_given:
|
|
|
|
# if a value was given, clamp it within (min, max)
|
|
|
|
self._validate("value", None, self.value)
|
|
|
|
else:
|
|
|
|
# otherwise, set it to 25-75% to avoid the handles overlapping
|
2014-08-08 03:32:25 +08:00
|
|
|
self.value = (0.75*self.min + 0.25*self.max,
|
|
|
|
0.25*self.min + 0.75*self.max)
|
2014-08-20 22:25:33 +08:00
|
|
|
# callback already set for 'value', 'lower', 'upper'
|
|
|
|
self.on_trait_change(self._validate, ['min', 'max'])
|
2014-06-25 23:51:55 +08:00
|
|
|
|
|
|
|
def _validate(self, name, old, new):
|
2014-08-20 22:25:33 +08:00
|
|
|
if name == "min":
|
|
|
|
if new > self.max:
|
|
|
|
raise ValueError("setting min > max")
|
|
|
|
elif name == "max":
|
|
|
|
if new < self.min:
|
|
|
|
raise ValueError("setting max < min")
|
|
|
|
|
|
|
|
low, high = self.value
|
2014-06-25 23:51:55 +08:00
|
|
|
if name == "value":
|
2014-08-20 22:25:33 +08:00
|
|
|
low, high = min(new), max(new)
|
|
|
|
elif name == "upper":
|
|
|
|
if new < self.lower:
|
|
|
|
raise ValueError("setting upper < lower")
|
|
|
|
high = new
|
|
|
|
elif name == "lower":
|
|
|
|
if new > self.upper:
|
|
|
|
raise ValueError("setting lower > upper")
|
|
|
|
low = new
|
|
|
|
|
|
|
|
low = max(self.min, min(low, self.max))
|
|
|
|
high = min(self.max, max(high, self.min))
|
|
|
|
|
|
|
|
# determine the order in which we should update the
|
|
|
|
# lower, upper traits to avoid a temporary inverted overlap
|
|
|
|
lower_first = high < self.lower
|
|
|
|
|
|
|
|
self.value = (low, high)
|
|
|
|
if lower_first:
|
|
|
|
self.lower = low
|
|
|
|
self.upper = high
|
2014-07-03 18:05:49 +08:00
|
|
|
else:
|
2014-08-20 22:25:33 +08:00
|
|
|
self.upper = high
|
|
|
|
self.lower = low
|
2014-06-25 23:51:55 +08:00
|
|
|
|
2014-10-14 09:45:16 +08:00
|
|
|
@register('IPython.IntRangeSlider')
|
2014-08-22 16:19:03 +08:00
|
|
|
class IntRangeSlider(_BoundedIntRange):
|
2015-01-16 10:21:33 +08:00
|
|
|
"""Slider widget that represents a pair of ints between a minimum and maximum value."""
|
2014-06-25 23:51:55 +08:00
|
|
|
_view_name = Unicode('IntSliderView', sync=True)
|
2014-08-27 23:15:11 +08:00
|
|
|
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
|
|
|
|
default_value='horizontal', allow_none=False,
|
2014-06-25 23:51:55 +08:00
|
|
|
help="Vertical or horizontal.", sync=True)
|
2014-08-24 17:48:19 +08:00
|
|
|
_range = Bool(True, help="Display a range selector", sync=True)
|
2014-06-25 23:51:55 +08:00
|
|
|
readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
|
2014-09-24 06:18:00 +08:00
|
|
|
slider_color = Unicode(sync=True)
|
2014-07-22 02:37:42 +08:00
|
|
|
|
|
|
|
# Remove in IPython 4.0
|
2014-07-19 03:47:33 +08:00
|
|
|
IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget')
|
|
|
|
BoundedIntTextWidget = DeprecatedClass(BoundedIntText, 'BoundedIntTextWidget')
|
|
|
|
IntSliderWidget = DeprecatedClass(IntSlider, 'IntSliderWidget')
|
|
|
|
IntProgressWidget = DeprecatedClass(IntProgress, 'IntProgressWidget')
|