diff --git a/bec_widgets/utils/entry_validator.py b/bec_widgets/utils/entry_validator.py index 98ee1fbf..dbfac89d 100644 --- a/bec_widgets/utils/entry_validator.py +++ b/bec_widgets/utils/entry_validator.py @@ -3,6 +3,15 @@ class EntryValidator: self.devices = devices def validate_signal(self, name: str, entry: str = None) -> str: + """ + Validate a signal entry for a given device. If the entry is not provided, the first signal entry will be used from the device hints. + Args: + name(str): Device name + entry(str): Signal entry + + Returns: + str: Signal entry + """ if name not in self.devices: raise ValueError(f"Device '{name}' not found in current BEC session") @@ -15,3 +24,17 @@ class EntryValidator: raise ValueError(f"Entry '{entry}' not found in device '{name}' signals") return entry + + def validate_monitor(self, monitor: str) -> str: + """ + Validate a monitor entry for a given device. + Args: + monitor(str): Monitor entry + + Returns: + str: Monitor entry + """ + if monitor not in self.devices: + raise ValueError(f"Device '{monitor}' not found in current BEC session") + + return monitor diff --git a/bec_widgets/widgets/plots/image.py b/bec_widgets/widgets/plots/image.py index 97bbfb82..6b39797e 100644 --- a/bec_widgets/widgets/plots/image.py +++ b/bec_widgets/widgets/plots/image.py @@ -12,7 +12,7 @@ from qtpy.QtCore import Signal as pyqtSignal from qtpy.QtCore import Slot as pyqtSlot from qtpy.QtWidgets import QWidget -from bec_widgets.utils import BECConnector, ConnectionConfig +from bec_widgets.utils import BECConnector, ConnectionConfig, EntryValidator from .plot_base import BECPlotBase, WidgetConfig @@ -335,7 +335,9 @@ class BECImageShow(BECPlotBase): super().__init__( parent=parent, parent_figure=parent_figure, config=config, client=client, gui_id=gui_id ) - + # Get bec shortcuts dev, scans, queue, scan_storage, dap + self.get_bec_shortcuts() + self.entry_validator = EntryValidator(self.dev) self._images = defaultdict(dict) self.apply_config(self.config) self.processor = ImageProcessor() @@ -507,6 +509,8 @@ class BECImageShow(BECPlotBase): f"Monitor with ID '{monitor}' already exists in widget '{self.gui_id}'." ) + monitor = self.entry_validator.validate_monitor(monitor) + image_config = ImageItemConfig( widget_class="BECImageItem", parent_id=self.gui_id, @@ -785,6 +789,22 @@ class BECImageShow(BECPlotBase): return True return False + def _validate_monitor(self, monitor: str, validate_bec: bool = True): + """ + Validate the monitor name. + Args: + monitor(str): The name of the monitor. + validate_bec(bool): Whether to validate the monitor name with BEC. + + Returns: + bool: True if the monitor name is valid, False otherwise. + """ + if not monitor or monitor == "": + return False + if validate_bec: + return monitor in self.dev + return True + def cleanup(self): """ Clean up the widget. diff --git a/tests/unit_tests/client_mocks.py b/tests/unit_tests/client_mocks.py index b1d8ca6e..69c2c3e0 100644 --- a/tests/unit_tests/client_mocks.py +++ b/tests/unit_tests/client_mocks.py @@ -91,6 +91,7 @@ DEVICES = [ FakeDevice("bpm4i"), FakeDevice("bpm3a"), FakeDevice("bpm3i"), + FakeDevice("eiger"), ]