Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions enable/qt4/scrollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,46 @@ def _create_control(self, window, enable_range, value):

def _update_control(self, enable_range, value):
minimum, maximum, page_size, line_size = enable_range
self._control.setMinimum(minimum)
# The maximum value of a QScrollBar is the maximum position of the
# scroll bar, not the document length. We need to subtract the length
# of the scroll bar itself.
self._control.setMaximum(maximum-page_size)
max_value = maximum-page_size
# invert values for vertical ranges because of coordinate system issues
value = self._correct_value(value, max_value)

self._control.setMinimum(minimum)
self._control.setMaximum(max_value)
self._control.setValue(value)
self._control.setPageStep(page_size)
self._control.setSingleStep(line_size)

def _correct_value(self, value, max_value):
""" Correct vertical position values for Qt and Enable conventions

Enable expects vertical scroll_position to be measured with origin at
the bottom and positive going upwards, while Qt scrollbar values are
measured with origin at the top and positive going down.

Parameters
----------
value : float
The position value in either Enable or Qt conventions.
max_value : float
The maximum value that the Qt scrollbar can be set to (height of
the scrolled component, less the page size).
"""
if self.orientation != 'vertical':
return value
return max_value - value


#------------------------------------------------------------------------
# Qt Event handlers
#------------------------------------------------------------------------

def _update_enable_pos(self, value):
# invert values for vertical ranges because of coordinate system issues
value = self._correct_value(value, self.high-self.page_size)
self.scroll_position = value

def _on_slider_pressed(self):
Expand Down Expand Up @@ -285,4 +311,3 @@ def _set_line_size(self, line_size):
low, high, page_size, ignore = self.range
self._clean = False
self.range =(low, high, page_size, line_size)

39 changes: 24 additions & 15 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 @@ -258,8 +268,8 @@ def _view_position_items_changed_for_viewport_component(self):
self.update_from_viewport()
return

def _component_bounds_items_handler(self, object, new):
if new.added != new.removed:
def _component_bounds_items_handler(self, object, event):
if event.added != event.removed:
self.update_bounds()

def _component_bounds_handler(self, object, name, old, new):
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.component = self.component
self.viewport_component.view_position = [0,0]
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._initialize_position()
self.add(self.viewport_component)

def _alternate_vsb_changed(self, old, new):
Expand All @@ -323,12 +337,10 @@ def _component_update(self, old, new):
def _bounds_changed ( self, old, new ):
Component._bounds_changed( self, old, new )
self.update_bounds()
return

def _bounds_items_changed(self, event):
Component._bounds_items_changed(self, event)
self.update_bounds()
return


#---------------------------------------------------------------------------
Expand Down Expand Up @@ -400,6 +412,7 @@ def _do_layout ( self ):
range=range_x,
enabled=False,
)
self._hsb.scroll_position = self.viewport_component.view_position[0]
self._hsb.on_trait_change(self._handle_horizontal_scroll,
'scroll_position')
self._hsb.on_trait_change(self._mouse_thumb_changed,
Expand All @@ -411,8 +424,6 @@ def _do_layout ( self ):
self._hsb.position = hsb_position
elif self._hsb is not None:
self._hsb = self._release_sb(self._hsb)
if not hasattr(self.component, "bounds_offset"):
self.viewport_component.view_position[0] = 0
else:
# We don't need to render the horizontal scrollbar, and we don't
# have one to update, either.
Expand All @@ -432,9 +443,9 @@ def _do_layout ( self ):
self._vsb = NativeScrollBar(orientation = 'vertical',
bounds=bounds,
position=vsb_position,
range=range_y
range=range_y,
)

self._vsb.scroll_position = self.viewport_component.view_position[1]
self._vsb.on_trait_change(self._handle_vertical_scroll,
'scroll_position')
self._vsb.on_trait_change(self._mouse_thumb_changed,
Expand All @@ -446,8 +457,6 @@ def _do_layout ( self ):
self._vsb.range = range_y
elif self._vsb:
self._vsb = self._release_sb(self._vsb)
if not hasattr(self.component, "bounds_offset"):
self.viewport_component.view_position[1] = 0
else:
# We don't need to render the vertical scrollbar, and we don't
# have one to update, either.
Expand Down
Loading