From 191365dab4b65b36422b6561c8535cd43a95a554 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Tue, 25 Nov 2025 17:59:25 +0100 Subject: [PATCH] GUI: added message boxes that check the hutch shutter and the beam current --- gui/src/aaregui/widgets/message_box.py | 96 ++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 gui/src/aaregui/widgets/message_box.py diff --git a/gui/src/aaregui/widgets/message_box.py b/gui/src/aaregui/widgets/message_box.py new file mode 100644 index 00000000..a8134a20 --- /dev/null +++ b/gui/src/aaregui/widgets/message_box.py @@ -0,0 +1,96 @@ +from PySide6.QtCore import QTimer, QEventLoop +from PySide6.QtWidgets import QMessageBox +from aaredaqlib.logger_config import setup_logger + +logger = setup_logger("aareGUI") + +LOW_CURRENT_THRESHOLD = 100.0 + +def reply_box(parent, title: str = "Warning", msg: str = "Warning." ): + return QMessageBox.question( + parent, + title, + f"{msg} Do you wish to continue?", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No, + ) + +def timer_box(parent, title: str = "Warning", msg: str = "Warning.", condition_func=None) -> QMessageBox: + box = QMessageBox(parent) + box.setWindowTitle(title) + box.setText(f"{msg} Do you wish to continue?") + box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) + box.show() + + timer = QTimer(box) + timer.setInterval(300) + + def stop_timer(): + if timer.isActive(): + timer.stop() + + def check(): + if not box.isVisible(): + stop_timer() + return + try: + if condition_func and condition_func(): + # Auto-accept (as if user pressed Yes) + box.done(QMessageBox.StandardButton.Yes) + stop_timer() + except Exception as e: + # Never let errors in condition_func block UI + logger.error(f"Error in condition_func: {e}") + + box.accepted.connect(stop_timer) + box.rejected.connect(stop_timer) + timer.timeout.connect(check) + + timer.start() + return box + +def ring_current_low_check(parent, ring_current) -> bool: + if ring_current is None: + logger.debug("Ring current: unknown") + msg = "Ring current: unknown." + too_low = True + else: + too_low = ring_current < LOW_CURRENT_THRESHOLD + if too_low: + rc = round(ring_current, 2) + logger.debug(f"Ring current low {rc}") + msg = f"Ring current is low: {rc} mA." + else: + logger.debug(f"Ring current: {round(ring_current, 2)} mA") + return True + + # Ask user only when unknown or too low + reply = reply_box(parent, title="Ring current too low", msg=msg) + return reply == QMessageBox.StandardButton.Yes + +def experiment_hutch_shutter_check(parent, shutter_state) -> bool: + if shutter_state: + logger.debug("Experiment shutter open") + return True + else: + reply = reply_box( + parent, + title="Experiment shutter open", + msg=f"Experiment shutter is Closed." + ) + return reply == QMessageBox.StandardButton.Yes + +def ring_current_auto_check(parent, ring_current, check_func) -> bool: + msg = "Ring current: unknown." if ring_current is None else f"Ring current is low {round(ring_current, 2)} mA." + box = timer_box(parent, title="Ring current too low", msg=msg, condition_func=check_func) + loop = QEventLoop() + def finish(_=None): + if loop.isRunning(): + loop.quit() + box.finished.connect(finish) + + box.show() + loop.exec() # returns as soon as box finishes without blocking UI processing of its signals + + result = box.result() + return result == QMessageBox.StandardButton.Yes \ No newline at end of file