From 5bb3800adc095c46f5d81af55bccece16ef74435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=B6ries?= <43136580+dgoeries@users.noreply.github.com> Date: Fri, 29 Jan 2021 06:14:41 +0100 Subject: [PATCH] ItemSample: Allow toggle of visibility via mouse click in LegendItem (#1497) * Legend toggle directly on ItemSample * Add invisible Eye icon * Include package data and remove username from svg * Allow svg and png in the setup.py and cleanup sg --- pyqtgraph/graphicsItems/LegendItem.py | 18 ++++++++++++- pyqtgraph/icons/__init__.py | 38 +++++++++++++++++++++++++++ pyqtgraph/icons/invisibleEye.svg | 35 ++++++++++++++++++++++++ setup.py | 4 ++- 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 pyqtgraph/icons/__init__.py create mode 100644 pyqtgraph/icons/invisibleEye.svg diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index 16e0e9e1..f5c5a559 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -3,6 +3,7 @@ from .GraphicsWidget import GraphicsWidget from .LabelItem import LabelItem from ..Qt import QtGui, QtCore from .. import functions as fn +from ..icons import invisibleEye from ..Point import Point from .ScatterPlotItem import ScatterPlotItem, drawSymbol from .PlotDataItem import PlotDataItem @@ -339,10 +340,15 @@ class ItemSample(GraphicsWidget): def paint(self, p, *args): opts = self.item.opts - if opts.get('antialias'): p.setRenderHint(p.Antialiasing) + visible = self.item.isVisible() + if not visible: + icon = invisibleEye.qicon + p.drawPixmap(QtCore.QPoint(1, 1), icon.pixmap(18, 18)) + return + if not isinstance(self.item, ScatterPlotItem): p.setPen(fn.mkPen(opts['pen'])) p.drawLine(0, 11, 20, 11) @@ -366,3 +372,13 @@ class ItemSample(GraphicsWidget): if isinstance(self.item, BarGraphItem): p.setBrush(fn.mkBrush(opts['brush'])) p.drawRect(QtCore.QRectF(2, 2, 18, 18)) + + def mouseClickEvent(self, event): + """Use the mouseClick event to toggle the visibility of the plotItem + """ + if event.button() == QtCore.Qt.LeftButton: + visible = self.item.isVisible() + self.item.setVisible(not visible) + + event.accept() + self.update() diff --git a/pyqtgraph/icons/__init__.py b/pyqtgraph/icons/__init__.py new file mode 100644 index 00000000..7842d8a9 --- /dev/null +++ b/pyqtgraph/icons/__init__.py @@ -0,0 +1,38 @@ +import os.path as op + +from ..Qt import QtGui + +__all__ = ['getGraphIcon'] + +_ICON_REGISTRY = {} + + +class GraphIcon: + """An icon place holder for lazy loading of QIcons""" + + def __init__(self, path): + self._path = path + self._icon = None + name = path.split('.')[0] + _ICON_REGISTRY[name] = self + + @property + def qicon(self): + if self._icon is None: + self._icon = QtGui.QIcon(op.join(op.dirname(__file__), self._path)) + + return self._icon + + +def getGraphIcon(name): + """Return a `PyQtGraph` icon from the registry by `name`""" + icon = _ICON_REGISTRY[name] + if isinstance(icon, GraphIcon): + icon = icon.qicon + _ICON_REGISTRY[name] = icon + + return icon + + +# Note: List all graph icons here ... +invisibleEye = GraphIcon("invisibleEye.svg") diff --git a/pyqtgraph/icons/invisibleEye.svg b/pyqtgraph/icons/invisibleEye.svg new file mode 100644 index 00000000..5a678328 --- /dev/null +++ b/pyqtgraph/icons/invisibleEye.svg @@ -0,0 +1,35 @@ + + + + + + + + + diff --git a/setup.py b/setup.py index eaaf4272..8f328f9a 100644 --- a/setup.py +++ b/setup.py @@ -138,7 +138,9 @@ setup( packages=allPackages, python_requires=">=3.7", package_dir={'pyqtgraph.examples': 'examples'}, ## install examples along with the rest of the source - package_data={'pyqtgraph.examples': ['optics/*.gz', 'relativity/presets/*.cfg']}, + package_data={'pyqtgraph.examples': ['optics/*.gz', 'relativity/presets/*.cfg'], + "pyqtgraph.icons": ["*.svg", "*.png"], + }, install_requires = [ 'numpy>=1.17.0', ],