Skip to content

Commit d1d333b

Browse files
committed
visibility of legend box synched with plotwidget visibility
1 parent f4b3f22 commit d1d333b

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

examples/plotLegendLists.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
plot = Plot2D()
1919
plot_layout.addWidget(plot)
20-
info_widget = LegendItemList()
21-
info_widget.binding(plot)
20+
info_widget = LegendItemList(parent=plot)
2221

2322
# --- Curve Demonstration ---
2423
x_curve = numpy.linspace(0, 10, 100)
@@ -46,14 +45,6 @@
4645
plot.addItem(curve2)
4746

4847

49-
# Add a custom button to highlight a curve to demonstrate the signal handling
50-
def highlight_curve1():
51-
curve1.setHighlighted(not curve1.isHighlighted())
52-
53-
54-
highlight_button = qt.QPushButton("Toggle Highlight on Sinusoidal Curve")
55-
plot_layout.addWidget(highlight_button)
5648
plot.resetZoom()
5749
plot_window.show()
58-
info_widget.show()
5950
app.exec_()

src/silx/gui/plot/LegendItem.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def __init__(self, parent, item: items):
9999
self._item = item
100100
self._itemRef = weakref.ref(item)
101101
self.setLayout(qt.QHBoxLayout())
102+
self.setFixedWidth(200)
103+
self.layout().setSpacing(20)
102104
self.layout().setContentsMargins(10, 0, 10, 0)
103105
item.sigItemChanged.connect(self._itemChanged)
104106

@@ -161,18 +163,24 @@ def _onLabelClicked(self, event):
161163
self._item.setVisible(not self._item.isVisible())
162164

163165

164-
class LegendItemList(qt.QMainWindow):
165-
def __init__(self, parent=None):
166+
class LegendItemList(qt.QWidget):
167+
def __init__(self, parent: PlotWidget):
166168
super().__init__(parent, qt.Qt.Window)
167-
self.setWindowTitle("Plot Info")
168-
self.resize(300, 150)
169-
self.central_widget = qt.QWidget()
170-
self.setCentralWidget(self.central_widget)
171-
self._layout = qt.QVBoxLayout(self.central_widget)
169+
self.setFixedWidth(200)
170+
self._layout = qt.QVBoxLayout(self)
172171
self._plot: PlotWidget | None = None
172+
self._binding(parent)
173+
if hasattr(parent, "sigVisibilityChanged"):
174+
parent.sigVisibilityChanged.connect(self._setVisibility)
173175

174-
def binding(self, plotWidget: PlotWidget):
175-
"""Binds this widget to the signals of a PlotWidget."""
176+
def _setVisibility(self, status: bool) -> None:
177+
if status:
178+
self.show()
179+
else:
180+
self.hide()
181+
182+
def _binding(self, plotWidget: PlotWidget):
183+
"""Binds this widget to the signals of a parent PlotWidget."""
176184
self._plot = plotWidget
177185
self._plot.sigActiveCurveChanged.connect(self._onActiveItemChanged)
178186
self._plot.sigActiveImageChanged.connect(self._onActiveItemChanged)
@@ -200,6 +208,17 @@ def _updateAllItemsList(self):
200208
_legendIcon = LegendItemWidget(None, item)
201209
self._layout.addWidget(_legendIcon)
202210

211+
self._updateHeigth()
212+
213+
def _updateHeigth(self):
214+
_totalHeight = (self._layout.count() - 1) * self._layout.spacing()
215+
_totalHeight += self._layout.contentsMargins().top()
216+
_totalHeight += self._layout.contentsMargins().bottom()
217+
if self._layout.count() > 0:
218+
item = self._layout.itemAt(0)
219+
_totalHeight += item.widget().sizeHint().height() * self._layout.count()
220+
self.setFixedHeight(_totalHeight)
221+
203222
def _onActiveItemChanged(self, previous, current):
204223
self._updateAllItemsList()
205224

0 commit comments

Comments
 (0)