ViewBox will now correctly auto-range when an item's position or transform changes.

This commit is contained in:
Luke Campagnola 2012-10-31 01:57:00 -04:00
parent 679de86509
commit 2c679dfbcc
3 changed files with 96 additions and 66 deletions

View File

@ -434,3 +434,17 @@ class GraphicsItem(object):
Called whenever the transformation matrix of the view has changed. Called whenever the transformation matrix of the view has changed.
""" """
pass pass
#def prepareGeometryChange(self):
#self._qtBaseClass.prepareGeometryChange(self)
#self.informViewBoundsChanged()
def informViewBoundsChanged(self):
"""
Inform this item's container ViewBox that the bounds of this item have changed.
This is used by ViewBox to react if auto-range is enabled.
"""
view = self.getViewBox()
if view is not None and hasattr(view, 'implements') and view.implements('ViewBox'):
view.itemBoundsChanged(self) ## inform view so it can update its range if it wants

View File

@ -11,10 +11,13 @@ class GraphicsObject(GraphicsItem, QtGui.QGraphicsObject):
_qtBaseClass = QtGui.QGraphicsObject _qtBaseClass = QtGui.QGraphicsObject
def __init__(self, *args): def __init__(self, *args):
QtGui.QGraphicsObject.__init__(self, *args) QtGui.QGraphicsObject.__init__(self, *args)
self.setFlag(self.ItemSendsGeometryChanges)
GraphicsItem.__init__(self) GraphicsItem.__init__(self)
def itemChange(self, change, value): def itemChange(self, change, value):
ret = QtGui.QGraphicsObject.itemChange(self, change, value) ret = QtGui.QGraphicsObject.itemChange(self, change, value)
if change in [self.ItemParentHasChanged, self.ItemSceneHasChanged]: if change in [self.ItemParentHasChanged, self.ItemSceneHasChanged]:
self._updateView() self._updateView()
if change in [self.ItemPositionHasChanged, self.ItemTransformHasChanged]:
self.informViewBoundsChanged()
return ret return ret

View File

@ -109,6 +109,7 @@ class ViewBox(GraphicsWidget):
'background': None, 'background': None,
} }
self._updatingRange = False ## Used to break recursive loops. See updateAutoRange.
self.setFlag(self.ItemClipsChildrenToShape) self.setFlag(self.ItemClipsChildrenToShape)
@ -340,6 +341,7 @@ class ViewBox(GraphicsWidget):
============= ===================================================================== ============= =====================================================================
""" """
changes = {} changes = {}
if rect is not None: if rect is not None:
@ -471,6 +473,7 @@ class ViewBox(GraphicsWidget):
#if not enable: #if not enable:
#import traceback #import traceback
#traceback.print_stack() #traceback.print_stack()
if enable is True: if enable is True:
enable = 1.0 enable = 1.0
@ -520,6 +523,14 @@ class ViewBox(GraphicsWidget):
self.updateAutoRange() self.updateAutoRange()
def updateAutoRange(self): def updateAutoRange(self):
## Break recursive loops when auto-ranging.
## This is needed because some items change their size in response
## to a view change.
if self._updatingRange:
return
self._updatingRange = True
try:
targetRect = self.viewRange() targetRect = self.viewRange()
if not any(self.state['autoRange']): if not any(self.state['autoRange']):
return return
@ -588,6 +599,8 @@ class ViewBox(GraphicsWidget):
args['disableAutoRange'] = False args['disableAutoRange'] = False
#self.setRange(xRange=targetRect[0], yRange=targetRect[1], padding=0, disableAutoRange=False) #self.setRange(xRange=targetRect[0], yRange=targetRect[1], padding=0, disableAutoRange=False)
self.setRange(**args) self.setRange(**args)
finally:
self._updatingRange = False
def setXLink(self, view): def setXLink(self, view):
"""Link this view's X axis to another view. (see LinkView)""" """Link this view's X axis to another view. (see LinkView)"""