From 39f9c6a6aa3327ffee8bbf353ac5248237bf2ebe Mon Sep 17 00:00:00 2001 From: Julius Juodakis Date: Mon, 19 Oct 2020 12:42:40 +1300 Subject: [PATCH] caching for viewRect of LinearRegionItem to reduce CPU load (#1391) * caching for boundaryRect of LinearRegionItem * caching viewRect at GraphicsItem --- pyqtgraph/graphicsItems/GraphicsItem.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pyqtgraph/graphicsItems/GraphicsItem.py b/pyqtgraph/graphicsItems/GraphicsItem.py index 9daf2a92..5c365f1a 100644 --- a/pyqtgraph/graphicsItems/GraphicsItem.py +++ b/pyqtgraph/graphicsItems/GraphicsItem.py @@ -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)