diff --git a/bec_widgets/widgets/control/device_input/signal_combobox/signal_combobox.py b/bec_widgets/widgets/control/device_input/signal_combobox/signal_combobox.py index bf47cf18..b00a104f 100644 --- a/bec_widgets/widgets/control/device_input/signal_combobox/signal_combobox.py +++ b/bec_widgets/widgets/control/device_input/signal_combobox/signal_combobox.py @@ -90,6 +90,36 @@ class SignalComboBox(DeviceSignalInputBase, QComboBox): self.insertItem(0, "Hinted Signals") self.model().item(0).setEnabled(False) + def set_to_obj_name(self, obj_name: str) -> bool: + """ + Set the combobox to the object name of the signal. + + Args: + obj_name (str): Object name of the signal. + + Returns: + bool: True if the object name was found and set, False otherwise. + """ + for i in range(self.count()): + signal_data = self.itemData(i) + if signal_data and signal_data.get("obj_name") == obj_name: + self.setCurrentIndex(i) + return True + return False + + def set_to_first_enabled(self) -> bool: + """ + Set the combobox to the first enabled item. + + Returns: + bool: True if an enabled item was found and set, False otherwise. + """ + for i in range(self.count()): + if self.model().item(i).isEnabled(): + self.setCurrentIndex(i) + return True + return False + @SafeSlot() def reset_selection(self): """Reset the selection of the combobox.""" diff --git a/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_setting.py b/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_setting.py index a4b4f417..a5a0e0f7 100644 --- a/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_setting.py +++ b/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_setting.py @@ -102,19 +102,10 @@ class CurveSetting(SettingWidget): self.device_x.setCurrentIndex(item if item != -1 else 0) signal_x = self.target_widget.x_axis_mode.get("entry", "") if signal_x: - for i in range(self.signal_x.count()): - signal_data = self.signal_x.itemData(i) - if signal_data and signal_data.get("obj_name") == signal_x: - self.signal_x.setCurrentIndex(i) - break + self.signal_x.set_to_obj_name(signal_x) else: # If no match is found, set to the first enabled item - for i in range(self.signal_x.count()): - model = self.signal_x.model() - if model.flags(model.index(i, 0)) & Qt.ItemIsEnabled: - self.signal_x.setCurrentIndex(i) - break - else: + if not self.signal_x.set_to_first_enabled(): # If no enabled item is found, set to the first item self.signal_x.setCurrentIndex(0) else: diff --git a/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_tree.py b/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_tree.py index 1c41348c..61066d80 100644 --- a/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_tree.py +++ b/bec_widgets/widgets/plots/waveform/settings/curve_settings/curve_tree.py @@ -142,20 +142,10 @@ class CurveRow(QTreeWidgetItem): # If the device name is not found, set the first enabled item self.device_edit.setCurrentIndex(0) - for i in range(self.entry_edit.count()): - entry_data = self.entry_edit.itemData(i) - if entry_data and entry_data.get("obj_name") == self.config.signal.entry: - # If the device name matches an object name, set it - self.entry_edit.setCurrentIndex(i) - break - else: - # If no match found, set the first enabled item - for i in range(self.entry_edit.count()): - model = self.entry_edit.model() - if model.flags(model.index(i, 0)) & Qt.ItemIsEnabled: - self.entry_edit.setCurrentIndex(i) - break - else: + if not self.entry_edit.set_to_obj_name(self.config.signal.entry): + # If the entry is not found, try to set it to the first enabled item + if not self.entry_edit.set_to_first_enabled(): + # If no enabled item is found, set to the first item self.entry_edit.setCurrentIndex(0) self.tree.setItemWidget(self, 1, self.device_edit)