5
5
from pathlib import Path
6
6
from typing import TYPE_CHECKING , Any , cast
7
7
8
+ import psygnal
8
9
from qtpy .QtCore import QSize , Qt , Signal
9
10
from qtpy .QtGui import QMovie
10
11
from qtpy .QtWidgets import (
@@ -177,12 +178,15 @@ def __init__(self, parent: QWidget | None = None) -> None:
177
178
178
179
179
180
class QLutView (LutView ):
181
+ # NB: In practice this will be a ChannelKey but Unions not allowed here.
182
+ histogramRequested = psygnal .Signal (object )
183
+
180
184
def __init__ (self , channel : ChannelKey = None ) -> None :
181
185
super ().__init__ ()
182
186
self ._qwidget = _QLUTWidget ()
183
187
self ._channel = channel
184
- self ._histogram : QWidget | None = None
185
188
# TODO: use emit_fast
189
+ self ._qwidget .histogram_btn .toggled .connect (self ._on_q_histogram_toggled )
186
190
self ._qwidget .visible .toggled .connect (self ._on_q_visibility_changed )
187
191
self ._qwidget .cmap .currentColormapChanged .connect (self ._on_q_cmap_changed )
188
192
self ._qwidget .clims .valueChanged .connect (self ._on_q_clims_changed )
@@ -237,6 +241,16 @@ def _on_q_auto_changed(self, autoscale: bool) -> None:
237
241
clims = self ._qwidget .clims .value ()
238
242
self ._model .clims = ClimsManual (min = clims [0 ], max = clims [1 ])
239
243
244
+ def _on_q_histogram_toggled (self , toggled : bool ) -> None :
245
+ container = self ._qwidget ._histogram_container
246
+ if count := container .count ():
247
+ for i in range (count ):
248
+ item = container .itemAt (i ).widget ()
249
+ if isinstance (item , QWidget ):
250
+ item .setVisible (toggled )
251
+ elif toggled :
252
+ self .histogramRequested .emit (self ._channel )
253
+
240
254
241
255
class _QDimsSliders (QWidget ):
242
256
currentIndexChanged = Signal ()
@@ -370,10 +384,6 @@ def __init__(self, canvas_widget: QWidget, parent: QWidget | None = None):
370
384
set_range_icon = QIconifyIcon ("fluent:full-screen-maximize-24-filled" )
371
385
self .set_range_btn = QPushButton (set_range_icon , "" , self )
372
386
373
- # # button to add a histogram
374
- # add_histogram_icon = QIconifyIcon("foundation:graph-bar")
375
- # self.histogram_btn = QPushButton(add_histogram_icon, "", self)
376
-
377
387
self .luts = _UpCollapsible (
378
388
"LUTs" ,
379
389
parent = self ,
@@ -389,7 +399,6 @@ def __init__(self, canvas_widget: QWidget, parent: QWidget | None = None):
389
399
390
400
self ._btn_layout .addWidget (self .channel_mode_combo )
391
401
self ._btn_layout .addWidget (self .ndims_btn )
392
- # self._btn_layout.addWidget(self.histogram_btn)
393
402
self ._btn_layout .addWidget (self .set_range_btn )
394
403
# self._btns.addWidget(self._add_roi_btn)
395
404
@@ -456,14 +465,7 @@ def add_lut_view(self, channel: ChannelKey) -> QLutView:
456
465
view = QLutView (channel )
457
466
self ._luts [channel ] = view
458
467
459
- def _on_histogram_requested (toggled : bool ) -> None :
460
- if view ._histogram :
461
- view ._histogram .setVisible (toggled )
462
- else :
463
- self .histogramRequested .emit (view ._channel )
464
-
465
- # TODO: This ugly
466
- view ._qwidget .histogram_btn .toggled .connect (_on_histogram_requested )
468
+ view .histogramRequested .connect (self .histogramRequested )
467
469
self ._qwidget .luts .addWidget (view .frontend_widget ())
468
470
return view
469
471
@@ -485,20 +487,12 @@ def _on_add_histogram_clicked(self, channel: int) -> None:
485
487
486
488
def add_histogram (self , channel : ChannelKey , widget : QWidget ) -> None :
487
489
if lut := self ._luts .get (channel , None ):
488
- # FIXME: Yuck
490
+ # # FIXME: Yuck
489
491
lut ._qwidget .resize (
490
492
QSize (lut ._qwidget .width (), lut ._qwidget .height () + 100 )
491
493
)
492
494
widget .resize (QSize (lut ._qwidget .width (), 100 ))
493
495
lut ._qwidget ._histogram_container .addWidget (widget )
494
- lut ._histogram = widget
495
-
496
- # self._qwidget.luts.childAt
497
- # if hasattr(self, "_hist"):
498
- # raise RuntimeError("Only one histogram can be added at a time")
499
- # self._hist = widget
500
- # self._qwidget.splitter.addWidget(widget)
501
- # self._qwidget.splitter.setSizes([self._qwidget.height() - 100, 100])
502
496
503
497
def remove_histogram (self , widget : QWidget ) -> None :
504
498
widget .setParent (None )
0 commit comments