From aed2382e387eb11c9b6dfb07bc2121724f8e3664 Mon Sep 17 00:00:00 2001 From: lidstrom83 Date: Tue, 9 Feb 2021 13:58:15 -0800 Subject: [PATCH] Correct id-based keying of scatter plot pixmap cache (#1564) * Correct id-based keying of scatter plot pixmap cache Note: naively using the id function results in non-unique keys. Alternatively, we could serialize the Qt objects and use these in the key. This would provide protection from the user mutating these later, but at a cost to performance. * Make id attribute private * Access class variable instead of instance --- pyqtgraph/graphicsItems/ScatterPlotItem.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pyqtgraph/graphicsItems/ScatterPlotItem.py b/pyqtgraph/graphicsItems/ScatterPlotItem.py index 3b4e06be..9c235304 100644 --- a/pyqtgraph/graphicsItems/ScatterPlotItem.py +++ b/pyqtgraph/graphicsItems/ScatterPlotItem.py @@ -5,6 +5,7 @@ try: from itertools import imap except ImportError: imap = map +import itertools import numpy as np import weakref from ..Qt import QtGui, QtCore, QT_LIB @@ -161,6 +162,8 @@ class SymbolAtlas(object): pm = atlas.pixmap """ + _idGenerator = itertools.count() + def __init__(self): self._data = np.zeros((0, 0, 4), dtype=np.ubyte) # numpy array of atlas image self._coords = {} @@ -224,13 +227,16 @@ class SymbolAtlas(object): squareness=1.0 if n == 0 else 2 * w * h / (w**2 + h**2)) def _keys(self, styles): + def getId(obj): + try: + return obj._id + except AttributeError: + obj._id = next(SymbolAtlas._idGenerator) + return obj._id + return [ - ( - symbol if isinstance(symbol, (str, int)) else f"{symbol.boundingRect()} + {symbol.elementCount()} elements", - size, - (pen.style(), pen.capStyle(), pen.joinStyle()), - (brush.color().rgba(), brush.style()) - ) for symbol, size, pen, brush in styles + (symbol if isinstance(symbol, (str, int)) else getId(symbol), size, getId(pen), getId(brush)) + for symbol, size, pen, brush in styles ] def _itemData(self, keys):