caching for viewRect of LinearRegionItem to reduce CPU load (#1391)

* caching for boundaryRect of LinearRegionItem

* caching viewRect at GraphicsItem
This commit is contained in:
Julius Juodakis 2020-10-19 12:42:40 +13:00 committed by GitHub
parent 2c02d58134
commit 39f9c6a6aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,6 +36,7 @@ class GraphicsItem(object):
self._viewBox = None self._viewBox = None
self._connectedView = None self._connectedView = None
self._exportOpts = False ## If False, not currently exporting. Otherwise, contains dict of export options. self._exportOpts = False ## If False, not currently exporting. Otherwise, contains dict of export options.
self._cachedView = None
if register is not None and register: if register is not None and register:
warnings.warn( warnings.warn(
"'register' argument is deprecated and does nothing", "'register' argument is deprecated and does nothing",
@ -154,6 +155,10 @@ class GraphicsItem(object):
def viewRect(self): def viewRect(self):
"""Return the visible bounds of this item's ViewBox or GraphicsWidget, """Return the visible bounds of this item's ViewBox or GraphicsWidget,
in the local coordinate system of the item.""" in the local coordinate system of the item."""
if self._cachedView is not None:
return self._cachedView
# Note that in cases of early returns here, the view cache stays empty (None).
view = self.getViewBox() view = self.getViewBox()
if view is None: if view is None:
return None return None
@ -163,10 +168,12 @@ class GraphicsItem(object):
bounds = bounds.normalized() bounds = bounds.normalized()
self._cachedView = bounds
## nah. ## nah.
#for p in self.getBoundingParents(): #for p in self.getBoundingParents():
#bounds &= self.mapRectFromScene(p.sceneBoundingRect()) #bounds &= self.mapRectFromScene(p.sceneBoundingRect())
return bounds return bounds
@ -548,8 +555,9 @@ class GraphicsItem(object):
""" """
Called whenever the transformation matrix of the view has changed. Called whenever the transformation matrix of the view has changed.
(eg, the view range has changed or the view was resized) (eg, the view range has changed or the view was resized)
Invalidates the viewRect cache.
""" """
pass self._cachedView = None
#def prepareGeometryChange(self): #def prepareGeometryChange(self):
#self._qtBaseClass.prepareGeometryChange(self) #self._qtBaseClass.prepareGeometryChange(self)