22
33import napari
44import numpy as np
5- from qtpy .QtWidgets import QComboBox , QHBoxLayout , QSpinBox
5+ from qtpy .QtWidgets import QComboBox , QHBoxLayout , QLabel , QSpinBox
66
77from napari_matplotlib .base import NapariMPLWidget
88
99__all__ = ["SliceWidget" ]
1010
11+ _dims_sel = ["x" , "y" ]
1112_dims = ["x" , "y" , "z" ]
1213
1314
@@ -19,21 +20,30 @@ class SliceWidget(NapariMPLWidget):
1920 n_layers_input = 1
2021
2122 def __init__ (self , napari_viewer : napari .viewer .Viewer ):
23+ # Setup figure/axes
2224 super ().__init__ (napari_viewer )
2325 self .axes = self .canvas .figure .subplots ()
2426
2527 button_layout = QHBoxLayout ()
2628 self .layout ().addLayout (button_layout )
2729
2830 self .dim_selector = QComboBox ()
31+ button_layout .addWidget (QLabel ("Slice axis:" ))
2932 button_layout .addWidget (self .dim_selector )
3033 self .dim_selector .addItems (_dims )
3134
3235 self .slice_selectors = {}
33- for d in _dims :
36+ for d in _dims_sel :
3437 self .slice_selectors [d ] = QSpinBox ()
38+ button_layout .addWidget (QLabel (f"{ d } :" ))
3539 button_layout .addWidget (self .slice_selectors [d ])
3640
41+ # Setup callbacks
42+ # Re-draw when any of the combon/spin boxes are updated
43+ self .dim_selector .currentTextChanged .connect (self ._draw )
44+ for d in _dims_sel :
45+ self .slice_selectors [d ].textChanged .connect (self ._draw )
46+
3747 self .update_layers (None )
3848
3949 @property
@@ -58,32 +68,34 @@ def current_dim_index(self) -> int:
5868
5969 @property
6070 def selector_values (self ) -> Dict [str , int ]:
61- return {d : self .slice_selectors [d ].value () for d in _dims }
71+ return {d : self .slice_selectors [d ].value () for d in _dims_sel }
6272
6373 def update_slice_selectors (self ) -> None :
6474 """
6575 Update range and enabled status of the slice selectors, and the value
6676 of the z slice selector.
6777 """
6878 # Update min/max
69- for i , dim in enumerate (_dims ):
79+ for i , dim in enumerate (_dims_sel ):
7080 self .slice_selectors [dim ].setRange (0 , self .layer .data .shape [i ])
7181
72- # The z dimension is always set by current z in the viewer
73- self .slice_selectors ["z" ].setValue (self .current_z )
74- self .slice_selectors [self .current_dim ].setEnabled (False )
75-
7682 def get_xy (self ) -> Tuple [np .ndarray , np .ndarray ]:
83+ """
84+ Get data for plotting.
85+ """
7786 x = np .arange (self .layer .data .shape [self .current_dim_index ])
7887
88+ vals = self .selector_values
89+ vals .update ({"z" : self .current_z })
90+
7991 slices = []
8092 for d in _dims :
8193 if d == self .current_dim :
8294 # Select all data along this axis
8395 slices .append (slice (None ))
8496 else :
8597 # Select specific index
86- val = self . selector_values [d ]
98+ val = vals [d ]
8799 slices .append (slice (val , val + 1 ))
88100
89101 # Reverse since z is the first axis in napari
@@ -99,7 +111,6 @@ def draw(self) -> None:
99111 """
100112 Clear axes and draw a 1D plot.
101113 """
102- self .update_slice_selectors ()
103114 x , y = self .get_xy ()
104115
105116 self .axes .plot (x , y )
0 commit comments