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