From 9485f85c203db9d276942ecd88a8a1ceff6295df Mon Sep 17 00:00:00 2001 From: Adrian Peter Krone Date: Fri, 6 Dec 2024 16:39:30 +0100 Subject: [PATCH] Added context manager for signal blocking --- src/iminuit/qtwidget.py | 84 +++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/iminuit/qtwidget.py b/src/iminuit/qtwidget.py index 3682a7ac..bf409de3 100644 --- a/src/iminuit/qtwidget.py +++ b/src/iminuit/qtwidget.py @@ -5,6 +5,7 @@ from typing import Dict, Any, Callable import sys from functools import partial +from contextlib import contextmanager try: from PyQt6 import QtCore, QtGui, QtWidgets @@ -113,42 +114,38 @@ def on_val_changed(self, val): def on_min_changed(self): tmin = self.tmin.value() if tmin >= self.vmax: - self.tmin.blockSignals(True) - self.tmin.setValue(self.vmin) - self.tmin.blockSignals(False) + with block_signals(self.tmin): + self.tmin.setValue(self.vmin) return self.vmin = tmin - self.slider.blockSignals(True) - if tmin > self.val: - self.val = tmin - minuit.values[self.par] = tmin - self.slider.setValue(0) - self.value_label.setText(f"{self.val:.3g}") - self.callback() - else: - self.slider.setValue(self._float_to_int(self.val)) - self.slider.blockSignals(False) + with block_signals(self.slider): + if tmin > self.val: + self.val = tmin + minuit.values[self.par] = tmin + self.slider.setValue(0) + self.value_label.setText(f"{self.val:.3g}") + self.callback() + else: + self.slider.setValue(self._float_to_int(self.val)) lim = minuit.limits[self.par] minuit.limits[self.par] = (tmin, lim[1]) def on_max_changed(self): tmax = self.tmax.value() if tmax <= self.tmin.value(): - self.tmax.blockSignals(True) - self.tmax.setValue(self.vmax) - self.tmax.blockSignals(False) + with block_signals(self.tmax): + self.tmax.setValue(self.vmax) return self.vmax = tmax - self.slider.blockSignals(True) - if tmax < self.val: - self.val = tmax - minuit.values[self.par] = tmax - self.slider.setValue(int(1e8)) - self.value_label.setText(f"{self.val:.3g}") - self.callback() - else: - self.slider.setValue(self._float_to_int(self.val)) - self.slider.blockSignals(False) + with block_signals(self.slider): + if tmax < self.val: + self.val = tmax + minuit.values[self.par] = tmax + self.slider.setValue(int(1e8)) + self.value_label.setText(f"{self.val:.3g}") + self.callback() + else: + self.slider.setValue(self._float_to_int(self.val)) lim = minuit.limits[self.par] minuit.limits[self.par] = (lim[0], tmax) @@ -169,28 +166,22 @@ def reset(self, val, limits=None): step = _guess_initial_step(val, vmin, vmax) self.vmin = vmin if np.isfinite(vmin) else val - 100 * step self.vmax = vmax if np.isfinite(vmax) else val + 100 * step - self.tmin.blockSignals(True) - self.tmin.setValue(self.vmin) - self.tmin.blockSignals(False) - self.tmax.blockSignals(True) - self.tmax.setValue(self.vmax) - self.tmax.blockSignals(False) + with block_signals(self.tmin, self.tmax): + self.tmin.setValue(self.vmin) + self.tmax.setValue(self.vmax) self.val = val if self.val < self.vmin: self.vmin = self.val - self.tmin.blockSignals(True) - self.tmin.setValue(self.vmin) - self.tmin.blockSignals(False) + with block_signals(self.tmin): + self.tmin.setValue(self.vmin) elif self.val > self.vmax: self.vmax = self.val - self.tmax.blockSignals(True) - self.tmax.setValue(self.vmax) - self.tmax.blockSignals(False) + with block_signals(self.tmax): + self.tmax.setValue(self.vmax) - self.slider.blockSignals(True) - self.slider.setValue(self._float_to_int(self.val)) - self.slider.blockSignals(False) + with block_signals(self.slider): + self.slider.setValue(self._float_to_int(self.val)) self.value_label.setText(f"{self.val:.3g}") class MainWindow(QtWidgets.QMainWindow): @@ -400,6 +391,17 @@ def on_reset_button_clicked(self): return MainWindow() +@contextmanager +def block_signals(*widgets): + for w in widgets: + w.blockSignals(True) + try: + yield + finally: + for w in widgets: + w.blockSignals(False) + + def _make_finite(x: float) -> float: sign = -1 if x < 0 else 1 if abs(x) == np.inf: