@@ -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