Fix autosize not taking to correct range with TextItem in view (#1435)

* Removing first call to updateMatrix in resizeEvent
This commit is contained in:
Ogi Moore 2020-11-21 00:02:32 -08:00 committed by GitHub
parent 0e816f1dbe
commit 71a92a6fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 49 deletions

View File

@ -148,15 +148,9 @@ class TextItem(GraphicsObject):
offset = (br - tl) * self.anchor offset = (br - tl) * self.anchor
self.textItem.setPos(-offset) self.textItem.setPos(-offset)
### Needed to maintain font size when rendering to image with increased resolution
#self.textItem.resetTransform()
##self.textItem.rotate(self.angle)
#if self._exportOpts is not False and 'resolutionScale' in self._exportOpts:
#s = self._exportOpts['resolutionScale']
#self.textItem.scale(s, s)
def boundingRect(self): def boundingRect(self):
return self.textItem.mapToParent(self.textItem.boundingRect()).boundingRect() return self.textItem.mapRectToParent(self.textItem.boundingRect())
def viewTransformChanged(self): def viewTransformChanged(self):
# called whenever view transform has changed. # called whenever view transform has changed.
@ -217,9 +211,6 @@ class TextItem(GraphicsObject):
a = np.arctan2(d.y(), d.x()) * 180 / np.pi a = np.arctan2(d.y(), d.x()) * 180 / np.pi
angle += a angle += a
t.rotate(angle) t.rotate(angle)
self.setTransform(t) self.setTransform(t)
self._lastTransform = pt self._lastTransform = pt
self.updateTextPos() self.updateTextPos()

View File

@ -294,10 +294,6 @@ class ViewBox(GraphicsWidget):
#scene.sigPrepareForPaint.connect(self.prepareForPaint) #scene.sigPrepareForPaint.connect(self.prepareForPaint)
#return ret #return ret
def update(self, *args, **kwargs):
self.prepareForPaint()
GraphicsWidget.update(self, *args, **kwargs)
def prepareForPaint(self): def prepareForPaint(self):
#autoRangeEnabled = (self.state['autoRange'][0] is not False) or (self.state['autoRange'][1] is not False) #autoRangeEnabled = (self.state['autoRange'][0] is not False) or (self.state['autoRange'][1] is not False)
# don't check whether auto range is enabled here--only check when setting dirty flag. # don't check whether auto range is enabled here--only check when setting dirty flag.
@ -440,8 +436,9 @@ class ViewBox(GraphicsWidget):
ch.setParentItem(None) ch.setParentItem(None)
def resizeEvent(self, ev): def resizeEvent(self, ev):
if ev.oldSize() != ev.newSize():
self._matrixNeedsUpdate = True self._matrixNeedsUpdate = True
self.updateMatrix() self.childGroup.prepareGeometryChange()
self.linkedXChanged() self.linkedXChanged()
self.linkedYChanged() self.linkedYChanged()
@ -449,15 +446,14 @@ class ViewBox(GraphicsWidget):
self.updateAutoRange() self.updateAutoRange()
self.updateViewRange() self.updateViewRange()
self._matrixNeedsUpdate = True # self._matrixNeedsUpdate = True
self.updateMatrix()
self.background.setRect(self.rect()) self.background.setRect(self.rect())
self.borderRect.setRect(self.rect()) self.borderRect.setRect(self.rect())
self.sigStateChanged.emit(self) self.sigStateChanged.emit(self)
self.sigResized.emit(self) self.sigResized.emit(self)
self.childGroup.prepareGeometryChange()
def viewRange(self): def viewRange(self):
"""Return a the view's visible range as a list: [[xmin, xmax], [ymin, ymax]]""" """Return a the view's visible range as a list: [[xmin, xmax], [ymin, ymax]]"""
@ -517,9 +513,6 @@ class ViewBox(GraphicsWidget):
================== ===================================================================== ================== =====================================================================
""" """
#print self.name, "ViewBox.setRange", rect, xRange, yRange, padding
#import traceback
#traceback.print_stack()
changes = {} # axes changes = {} # axes
setRequested = [False, False] setRequested = [False, False]
@ -611,7 +604,6 @@ class ViewBox(GraphicsWidget):
mx = lmx mx = lmx
mn += delta mn += delta
# Set target range # Set target range
if self.state['targetRange'][ax] != [mn, mx]: if self.state['targetRange'][ax] != [mn, mx]:
self.state['targetRange'][ax] = [mn, mx] self.state['targetRange'][ax] = [mn, mx]
@ -674,7 +666,6 @@ class ViewBox(GraphicsWidget):
if item is None: if item is None:
bounds = self.childrenBoundingRect(items=items) bounds = self.childrenBoundingRect(items=items)
else: else:
print("Warning: ViewBox.autoRange(item=__) is deprecated. Use 'items' argument instead.")
bounds = self.mapFromItemToView(item, item.boundingRect()).boundingRect() bounds = self.mapFromItemToView(item, item.boundingRect()).boundingRect()
if bounds is not None: if bounds is not None:
@ -877,7 +868,6 @@ class ViewBox(GraphicsWidget):
## to a view change. ## to a view change.
if self._updatingRange: if self._updatingRange:
return return
self._updatingRange = True self._updatingRange = True
try: try:
targetRect = self.viewRange() targetRect = self.viewRange()
@ -903,11 +893,9 @@ class ViewBox(GraphicsWidget):
oRange = [None, None] oRange = [None, None]
oRange[ax] = targetRect[1-ax] oRange[ax] = targetRect[1-ax]
childRange = self.childrenBounds(frac=fractionVisible, orthoRange=oRange) childRange = self.childrenBounds(frac=fractionVisible, orthoRange=oRange)
else: else:
if childRange is None: if childRange is None:
childRange = self.childrenBounds(frac=fractionVisible) childRange = self.childrenBounds(frac=fractionVisible)
## Make corrections to range ## Make corrections to range
xr = childRange[ax] xr = childRange[ax]
if xr is not None: if xr is not None:
@ -932,9 +920,8 @@ class ViewBox(GraphicsWidget):
if len(args) == 0: if len(args) == 0:
return return
args['padding'] = 0 args['padding'] = 0.0
args['disableAutoRange'] = False args['disableAutoRange'] = False
self.setRange(**args) self.setRange(**args)
finally: finally:
self._autoRangeNeedsUpdate = False self._autoRangeNeedsUpdate = False
@ -1085,7 +1072,6 @@ class ViewBox(GraphicsWidget):
if (self.state['autoRange'][0] is not False) or (self.state['autoRange'][1] is not False): if (self.state['autoRange'][0] is not False) or (self.state['autoRange'][1] is not False):
self._autoRangeNeedsUpdate = True self._autoRangeNeedsUpdate = True
self.update() self.update()
#self.updateAutoRange()
def _invertAxis(self, ax, inv): def _invertAxis(self, ax, inv):
key = 'xy'[ax] + 'Inverted' key = 'xy'[ax] + 'Inverted'
@ -1193,7 +1179,6 @@ class ViewBox(GraphicsWidget):
"""Maps *obj* from the local coordinate system of *item* to the view coordinates""" """Maps *obj* from the local coordinate system of *item* to the view coordinates"""
self.updateMatrix() self.updateMatrix()
return self.childGroup.mapFromItem(item, obj) return self.childGroup.mapFromItem(item, obj)
#return self.mapSceneToView(item.mapToScene(obj))
def mapFromViewToItem(self, item, obj): def mapFromViewToItem(self, item, obj):
"""Maps *obj* from view coordinates to the local coordinate system of *item*.""" """Maps *obj* from view coordinates to the local coordinate system of *item*."""
@ -1422,9 +1407,7 @@ class ViewBox(GraphicsWidget):
else: else:
if int(item.flags() & item.ItemHasNoContents) > 0: if int(item.flags() & item.ItemHasNoContents) > 0:
continue continue
else: bounds = self.mapFromItemToView(item, item.boundingRect()).boundingRect()
bounds = item.boundingRect()
bounds = self.mapFromItemToView(item, bounds).boundingRect()
itemBounds.append((bounds, True, True, 0)) itemBounds.append((bounds, True, True, 0))
## determine tentative new range ## determine tentative new range
@ -1461,7 +1444,6 @@ class ViewBox(GraphicsWidget):
continue continue
range[1][0] = min(range[1][0], bounds.top() - px*pxSize) range[1][0] = min(range[1][0], bounds.top() - px*pxSize)
range[1][1] = max(range[1][1], bounds.bottom() + px*pxSize) range[1][1] = max(range[1][1], bounds.bottom() + px*pxSize)
return range return range
def childrenBoundingRect(self, *args, **kwds): def childrenBoundingRect(self, *args, **kwds):
@ -1606,6 +1588,7 @@ class ViewBox(GraphicsWidget):
self.sigTransformChanged.emit(self) ## segfaults here: 1 self.sigTransformChanged.emit(self) ## segfaults here: 1
def paint(self, p, opt, widget): def paint(self, p, opt, widget):
self.prepareForPaint()
if self.border is not None: if self.border is not None:
bounds = self.shape() bounds = self.shape()
p.setPen(self.border) p.setPen(self.border)

View File

@ -17,7 +17,7 @@ def init_viewbox():
""" """
global win, vb global win, vb
win = pg.GraphicsWindow() win = pg.GraphicsLayoutWidget()
win.ci.layout.setContentsMargins(0,0,0,0) win.ci.layout.setContentsMargins(0,0,0,0)
win.resize(200, 200) win.resize(200, 200)
win.show() win.show()
@ -82,6 +82,7 @@ def test_ViewBox_setMenuEnabled():
assert vb.menu is None assert vb.menu is None
skipreason = "Skipping this test until someone has time to fix it." skipreason = "Skipping this test until someone has time to fix it."
@pytest.mark.skipif(True, reason=skipreason) @pytest.mark.skipif(True, reason=skipreason)
def test_limits_and_resize(): def test_limits_and_resize():

View File

@ -82,12 +82,9 @@ def test_clipping():
w = pg.PlotWidget(autoRange=True, downsample=5) w = pg.PlotWidget(autoRange=True, downsample=5)
c = pg.PlotDataItem(x, y) c = pg.PlotDataItem(x, y)
w.addItem(c) w.addItem(c)
w.show()
c.setClipToView(True) c.setClipToView(True)
w.setXRange(200, 600) w.setXRange(200, 600)
for x_min in range(100, 2**10 - 100, 100): for x_min in range(100, 2**10 - 100, 100):
w.setXRange(x_min, x_min + 100) w.setXRange(x_min, x_min + 100)