Skip to content

Commit

Permalink
Fixes based on getting tests to work right.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Aug 21, 2015
1 parent f531cb6 commit 2bbf7de
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 23 deletions.
22 changes: 18 additions & 4 deletions enable/scrolled.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import with_statement

# Enthought library imports
from traits.api import Bool, Instance, Int, Any, Float
from traits.api import Any, Bool, DelegatesTo, Float, Instance, Int

# Local, relative imports
from base import intersect_bounds, empty_rectangle
Expand All @@ -23,7 +23,17 @@ class Scrolled(Container):
component = Instance(Component)

# The viewport onto our component
viewport_component = Instance(Viewport)
viewport_component = Instance(Viewport, ())

# Whether or not the viewport should stay constrained to the bounds
# of the viewed component
stay_inside = DelegatesTo('viewport_component')

# Where to anchor vertically on resizes
vertical_anchor = DelegatesTo('viewport_component')

# Where to anchor vertically on resizes
horizontal_anchor = DelegatesTo('viewport_component')

# Inside padding is a background drawn area between the edges or scrollbars
# and the scrolled area/left component.
Expand Down Expand Up @@ -298,10 +308,14 @@ def _inside_padding_width_changed(self):

def _viewport_component_changed(self):
if self.viewport_component is None:
self.viewport_component = Viewport()
self.viewport_component = Viewport(
stay_inside=self.stay_inside,
vertical_anchor=self.vertical_anchor,
horizontal_anchor=self.horizontal_anchor,
)
self.viewport_component.view_bounds = self.bounds
self.viewport_component.component = self.component
#self.viewport_component.view_position = [0, 0]
self.viewport_component._initialize_position()
self.add(self.viewport_component)

def _alternate_vsb_changed(self, old, new):
Expand Down
261 changes: 260 additions & 1 deletion enable/tests/viewport_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def test_basic_viewport(self):
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])

self.assertEqual(view.view_position, [10, 10])
print view.components_at(0.0, 0.0), view.view_position
self.assert_(view.components_at(0.0, 0.0)[0] == component)
self.assert_(view.components_at(44.9, 0.0)[0] == component)
self.assert_(view.components_at(0.0, 44.9)[0] == component)
Expand All @@ -23,7 +26,263 @@ def test_basic_viewport(self):
self.assert_(view.components_at(46.0, 0.0) == [])
self.assert_(view.components_at(45.0, 46.0) == [])
self.assert_(view.components_at(0.0, 46.0) == [])
return

def test_initial_position(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])
self.assertEqual(view.view_position, [0, 0])

def test_initial_position_vanchor_top(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
vertical_anchor='top',
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])
self.assertEqual(view.view_position, [0, 50])

def test_initial_position_vanchor_center(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
vertical_anchor='center',
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])
self.assertEqual(view.view_position, [0, 25])

def test_initial_position_hanchor_right(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
horizontal_anchor='right',
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])
self.assertEqual(view.view_position, [50, 0])

def test_initial_position_hanchor_center(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
horizontal_anchor='center',
view_bounds=[50.0, 50.0],
position=[0,0],
bounds=[50,50])
self.assertEqual(view.view_position, [25, 0])

def test_adjust_bounds(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[10,10],
position=[0,0],
bounds=[50,50])

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [10, 10])

def test_adjust_bounds_vanchor_top(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
vertical_anchor='top')

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [20, 10.0])

# resize beyond bottom
view.bounds = [80, 80]
self.assertEqual(view.view_position, [20, -10.0])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [20, -50.0])

def test_adjust_bounds_vanchor_top_stay_inside(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
vertical_anchor='top',
stay_inside=True)

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [20, 10.0])

# resize beyond bottom
view.bounds = [80, 80]
self.assertEqual(view.view_position, [20, 0.0])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [0, -20.0])

def test_adjust_bounds_vanchor_center(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
vertical_anchor='center')

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [20, 15.0])

# resize beyond bottom
view.bounds = [95, 95]
self.assertEqual(view.view_position, [20, -2.5])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [20, -15.0])

def test_adjust_bounds_vanchor_center_stay_inside(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
vertical_anchor='center',
stay_inside=True)

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [20, 15.0])

# resize beyond bottom
view.bounds = [95, 95]
self.assertEqual(view.view_position, [5, 0.0])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [0, -10.0])

def test_adjust_bounds_hanchor_top(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
horizontal_anchor='right')

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [10, 20.0])

# resize beyond bottom
view.bounds = [80, 80]
self.assertEqual(view.view_position, [-10, 20.0])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [-50, 20.0])

def test_adjust_bounds_hanchor_top_stay_inside(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
horizontal_anchor='right',
stay_inside=True)

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [10, 20.0])

# resize beyond bottom
view.bounds = [80, 80]
self.assertEqual(view.view_position, [0, 20.0])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [-20.0, 0])

def test_adjust_bounds_hanchor_center(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
horizontal_anchor='center')

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [15.0, 20])

# resize beyond bottom
view.bounds = [95, 95]
self.assertEqual(view.view_position, [-2.5, 20])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [-15.0, 20])

def test_adjust_bounds_hanchor_center_stay_inside(self):
container = Container(bounds=[100.0, 100.0])
component = Component(bounds=[50.0, 50.0], position=[5.0, 5.0])
container.add(component)
view = Viewport(component=container,
view_bounds=[50.0, 50.0],
view_position=[20, 20],
position=[0,0],
bounds=[50,50],
horizontal_anchor='center',
stay_inside=True)

# simple resize
view.bounds = [60, 60]
self.assertEqual(view.view_position, [15.0, 20])

# resize beyond bottom
view.bounds = [95, 95]
self.assertEqual(view.view_position, [0.0, 5])

# resize bigger than view
view.bounds = [120, 120]
self.assertEqual(view.view_position, [-10.0, 0])



if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 2bbf7de

Please sign in to comment.