Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #237 from enthought/feature-slider-update
Browse files Browse the repository at this point in the history
Feature slider update
  • Loading branch information
sccolbert committed Jan 15, 2013
2 parents 8588e04 + ec38b5b commit 15a54a2
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 73 deletions.
6 changes: 4 additions & 2 deletions enaml/qt/qt_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def create(self, tree):
"""
super(QtSlider, self).create(tree)
self.set_value(tree['value'])
self.set_maximum(tree['maximum'])
# Initialize the value after the minimum and maximum to avoid
# the potential for premature internal clipping of the value.
self.set_minimum(tree['minimum'])
self.set_maximum(tree['maximum'])
self.set_value(tree['value'])
self.set_orientation(tree['orientation'])
self.set_page_step(tree['page_step'])
self.set_single_step(tree['single_step'])
Expand Down
229 changes: 229 additions & 0 deletions enaml/stdlib/slider_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
#------------------------------------------------------------------------------
# Copyright (c) 2013, Enthought, Inc.
# All rights reserved.
#------------------------------------------------------------------------------
""" Enaml Standard Library - Slider Transforms
This module contains declarative classes which can be used as children
of a `Slider` to transform the integer range of the slider into an
alternative data space.
"""
from traits.api import Any, Bool, Float, Range, on_trait_change

from enaml.core.declarative import Declarative


class SliderTransform(Declarative):
""" A base class for creating declarative slider transforms.
A SliderTransform must be subclassed to be useful. The abstract api
defined below must be implemented by the subclass.
When using a transform with a slider, the transform takes complete
ownership of the slider range. That is, no effort is made to listen
for outside changes to the slider range, so all changes should be
made on the transform.
"""
#: The data-space minimum for the transform. This may be redefined
#: by a subclass to enforce stronger typing.
minimum = Any

#: The data-space maximum for the transform. This may be redefined
#: by a subclass to enforce stronger typing.
maximum = Any

#: The data-space value for the transform. This may be redefined
#: by a subclass to enforce stronger typing.
value = Any

#: A boolean flag used to prevent loopback cycles.
_guard = Bool(False)

#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def pre_initialize(self):
""" A pre-initialization handler.
The parent slider values are initialized during the transform
initialization pass.
"""
self._apply_transform()

def _apply_transform(self, minimum=True, maximum=True, value=True):
""" Apply the current transform to the parent slider.
Parameters
----------
minimum : bool, optional
Whether or not to update the slider minimum. The default
is False.
maximum : bool, optional
Whether or not to update the slider maximum. The default
is False.
value : bool, optional
Whether or not to update the slider value. The default
is False.
"""
parent = self.parent
if parent is not None:
if minimum:
parent.minimum = self.get_minimum()
if maximum:
parent.maximum = self.get_maximum()
if value:
parent.value = self.get_value()

@on_trait_change('parent:value')
def _update_value(self, val):
""" Update the transformed value when slider changes.
"""
if self.is_active:
self._guard = True
try:
self.set_value(val)
finally:
self._guard = False

def _minimum_changed(self):
""" Update the slider minimum on transform minimum change.
"""
if self.is_active:
self._apply_transform(maximum=False, value=False)

def _maximum_changed(self):
""" Update the slider maximum on transform maximum change.
"""
if self.is_active:
self._apply_transform(minimum=False, value=False)

def _value_changed(self):
""" Update the slider value on transform value change.
"""
if self.is_active and not self._guard:
self._apply_transform(minimum=False, maximum=False)

#--------------------------------------------------------------------------
# Abstract API
#--------------------------------------------------------------------------
def get_minimum(self):
""" Get the minimum value of the transform as an int.
Returns
-------
result : int
The minimum value of the transform converted to an int.
"""
raise NotImplementedError

def get_maximum(self):
""" Get the maximum value of the transform as an int.
Returns
-------
result : int
The maximum value of the transform converted to an int.
"""
raise NotImplementedError

def get_value(self):
""" Get the value of the transform as an int.
Returns
-------
result : int
The value of the transform converted to an int.
"""
raise NotImplementedError

def set_value(self, value):
""" Set the value of the transform from an int.
Parameters
----------
value : int
The integer value of the slider.
"""
raise NotImplementedError


class FloatTransform(SliderTransform):
""" A concreted SliderTransform for floating point values.
"""
#: A redeclared parent class trait which enforces float values.
minimum = Float(0.0)

#: A redeclared parent class trait which enforces float values.
maximum = Float(1.0)

#: A redeclared parent class trait which enforces float values.
value = Float(0.0)

#: The number of stops to use between the minimum and maximum.
precision = Range(low=1, value=100)

#--------------------------------------------------------------------------
# Abstract API Implementation
#--------------------------------------------------------------------------
def get_minimum(self):
""" Get the minimum value of the transform as an int.
Returns
-------
result : int
The minimum value of the transform converted to an int.
"""
return 0

def get_maximum(self):
""" Get the maximum value of the transform as an int.
Returns
-------
result : int
The maximum value of the transform converted to an int.
"""
return self.precision

def get_value(self):
""" Get the value of the transform as an int.
Returns
-------
result : int
The value of the transform converted to an int.
"""
offset = self.value - self.minimum
delta = self.maximum - self.minimum
return int(offset * self.precision / delta)

def set_value(self, val):
""" Set the value of the transform from an int.
Parameters
----------
value : int
The integer value of the slider.
"""
delta = self.maximum - self.minimum
self.value = (val * delta / self.precision) + self.minimum

Loading

0 comments on commit 15a54a2

Please sign in to comment.