Per-owner cache of Kind icons for models / delegates that paint them often.
Without an explicit colour the icons follow the application palette: the cache
is dropped when the palette changes (QPalette.cacheKey()). Explicit colours are
cached per (kind, colour); clear() drops everything (e.g. on a restyle).
Source code in src/cutepandas/utils/icons.py
| class KindIconCache:
"""Per-owner cache of `Kind` icons for models / delegates that paint them often.
Without an explicit colour the icons follow the application palette: the cache
is dropped when the palette changes (`QPalette.cacheKey()`). Explicit colours are
cached per (kind, colour); `clear()` drops everything (e.g. on a restyle).
"""
def __init__(self):
self._icons: dict[tuple[Kind, str | None], gui.QIcon] = {}
self._palette_key: int | None = None
def icon(self, kind: Kind, color: str | None = None) -> gui.QIcon:
if color is None:
from prettyqt import gui
key = (
gui.GuiApplication.palette().cacheKey()
if gui.GuiApplication.instance() is not None
else None
)
if key != self._palette_key:
self._icons.clear()
self._palette_key = key
elif len(self._icons) > len(ICON_FOR_KIND) * 4: # many colours: start over
self._icons.clear()
if (icon := self._icons.get((kind, color))) is None:
icon = self._icons[kind, color] = icon_for_kind(kind, color=color)
return icon
def clear(self):
self._icons.clear()
self._palette_key = None
|