Skip to content

Commit 88760cf

Browse files
committed
Add HistogramWidget._bin_widgets attribute for storing bin widgets
1 parent b8ffdb7 commit 88760cf

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/napari_matplotlib/histogram.py

+31-15
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def __init__(
4242

4343
# Create widgets for setting bin parameters
4444
bins_start = QDoubleSpinBox()
45-
bins_start.setObjectName("bins start")
4645
bins_start.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
4746
bins_start.setRange(-1e10, 1e10)
4847
bins_start.setValue(0)
@@ -51,7 +50,6 @@ def __init__(
5150
bins_start.setDecimals(2)
5251

5352
bins_stop = QDoubleSpinBox()
54-
bins_stop.setObjectName("bins stop")
5553
bins_stop.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
5654
bins_stop.setRange(-1e10, 1e10)
5755
bins_stop.setValue(100)
@@ -60,7 +58,6 @@ def __init__(
6058
bins_stop.setDecimals(2)
6159

6260
bins_num = QSpinBox()
63-
bins_num.setObjectName("bins num")
6461
bins_num.setRange(1, 100_000)
6562
bins_num.setValue(101)
6663
bins_num.setWrapping(False)
@@ -84,6 +81,13 @@ def __init__(
8481
bins_stop.valueChanged.connect(self._draw)
8582
bins_num.valueChanged.connect(self._draw)
8683

84+
# Store widgets for later usage
85+
self._bin_widgets = {
86+
"start": bins_start,
87+
"stop": bins_stop,
88+
"num": bins_num,
89+
}
90+
8791
self._update_layers(None)
8892
self.viewer.events.theme.connect(self._on_napari_theme_changed)
8993

@@ -108,12 +112,17 @@ def on_update_layers(self) -> None:
108112
is_unsigned = layer_data.dtype.kind == "u"
109113
minimum_value = 0 if is_unsigned else -1e10
110114

111-
bins_start = self.findChild(QDoubleSpinBox, name="bins start")
112-
bins_stop = self.findChild(QDoubleSpinBox, name="bins stop")
113-
bins_start.setDecimals(n_decimals)
114-
bins_stop.setDecimals(n_decimals)
115-
bins_start.setMinimum(minimum_value)
116-
bins_stop.setMinimum(minimum_value)
115+
# Disable callbacks whilst widget values might change
116+
for widget in self._bin_widgets.values():
117+
widget.blockSignals(True)
118+
119+
self._bin_widgets["start"].setDecimals(n_decimals)
120+
self._bin_widgets["stop"].setDecimals(n_decimals)
121+
self._bin_widgets["start"].setMinimum(minimum_value)
122+
self._bin_widgets["stop"].setMinimum(minimum_value)
123+
124+
for widget in self._bin_widgets.values():
125+
widget.blockSignals(False)
117126

118127
def _update_contrast_lims(self) -> None:
119128
for lim, line in zip(
@@ -138,39 +147,46 @@ def autoset_widget_bins(self, data: npt.NDArray[Any]) -> None:
138147
else:
139148
bins = np.linspace(np.min(data), np.max(data), 100)
140149

150+
# Disable callbacks whilst setting widget values
151+
for widget in self._bin_widgets.values():
152+
widget.blockSignals(True)
153+
141154
self.bins_start = bins[0]
142155
self.bins_stop = bins[-1]
143156
self.bins_num = bins.size
144157

158+
for widget in self._bin_widgets.values():
159+
widget.blockSignals(False)
160+
145161
@property
146162
def bins_start(self) -> float:
147163
"""Minimum bin edge"""
148-
return self.findChild(QDoubleSpinBox, name="bins start").value()
164+
return self._bin_widgets["start"].value()
149165

150166
@bins_start.setter
151167
def bins_start(self, start: Union[int, float]) -> None:
152168
"""Set the minimum bin edge"""
153-
self.findChild(QDoubleSpinBox, name="bins start").setValue(start)
169+
self._bin_widgets["start"].setValue(start)
154170

155171
@property
156172
def bins_stop(self) -> float:
157173
"""Maximum bin edge"""
158-
return self.findChild(QDoubleSpinBox, name="bins stop").value()
174+
return self._bin_widgets["stop"].value()
159175

160176
@bins_stop.setter
161177
def bins_stop(self, stop: Union[int, float]) -> None:
162178
"""Set the maximum bin edge"""
163-
self.findChild(QDoubleSpinBox, name="bins stop").setValue(stop)
179+
self._bin_widgets["stop"].setValue(stop)
164180

165181
@property
166182
def bins_num(self) -> int:
167183
"""Number of bins to use"""
168-
return self.findChild(QSpinBox, name="bins num").value()
184+
return self._bin_widgets["num"].value()
169185

170186
@bins_num.setter
171187
def bins_num(self, num: int) -> None:
172188
"""Set the number of bins to use"""
173-
self.findChild(QSpinBox, name="bins num").setValue(num)
189+
self._bin_widgets["num"].setValue(num)
174190

175191
def _get_layer_data(self, layer: napari.layers.Layer) -> npt.NDArray[Any]:
176192
"""Get the data associated with a given layer"""

0 commit comments

Comments
 (0)