mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
refactor: move add/remove bundle to scan group box
This commit is contained in:
@ -62,6 +62,8 @@ class ScanControl(BECWidget, QWidget):
|
||||
super().__init__(client=client, gui_id=gui_id, config=config)
|
||||
QWidget.__init__(self, parent=parent)
|
||||
|
||||
self._hide_add_remove_buttons = False
|
||||
|
||||
# Client from BEC + shortcuts to device manager and scans
|
||||
self.get_bec_shortcuts()
|
||||
|
||||
@ -100,21 +102,6 @@ class ScanControl(BECWidget, QWidget):
|
||||
self.comboBox_scan_selection.currentIndexChanged.connect(self.on_scan_selection_changed)
|
||||
self.button_run_scan.clicked.connect(self.run_scan)
|
||||
|
||||
# Add bundle button
|
||||
self.button_add_bundle = QPushButton("Add Bundle")
|
||||
self.button_add_bundle.setVisible(False)
|
||||
# Remove bundle button
|
||||
self.button_remove_bundle = QPushButton("Remove Bundle")
|
||||
self.button_remove_bundle.setVisible(False)
|
||||
|
||||
bundle_layout = QHBoxLayout()
|
||||
bundle_layout.addWidget(self.button_add_bundle)
|
||||
bundle_layout.addWidget(self.button_remove_bundle)
|
||||
self.layout.addLayout(bundle_layout)
|
||||
|
||||
self.button_add_bundle.clicked.connect(self.add_arg_bundle)
|
||||
self.button_remove_bundle.clicked.connect(self.remove_arg_bundle)
|
||||
|
||||
self.scan_selected.connect(self.scan_select)
|
||||
|
||||
# Initialize scan selection
|
||||
@ -366,11 +353,7 @@ class ScanControl(BECWidget, QWidget):
|
||||
self.arg_group = gui_config.get("arg_group", None)
|
||||
self.kwarg_groups = gui_config.get("kwarg_groups", None)
|
||||
|
||||
show_bundle_buttons = bool(self.arg_group["arg_inputs"])
|
||||
|
||||
self._show_bundle_buttons(show_bundle_buttons)
|
||||
|
||||
if show_bundle_buttons:
|
||||
if bool(self.arg_group["arg_inputs"]):
|
||||
self.add_arg_group(self.arg_group)
|
||||
if len(self.kwarg_groups) > 0:
|
||||
self.add_kwargs_boxes(self.kwarg_groups)
|
||||
@ -378,28 +361,21 @@ class ScanControl(BECWidget, QWidget):
|
||||
self.update()
|
||||
self.adjustSize()
|
||||
|
||||
def _show_bundle_buttons(self, show: bool):
|
||||
"""Shows or hides the bundle buttons based on the show argument.
|
||||
|
||||
Args:
|
||||
show(bool): Show or hide the bundle buttons.
|
||||
"""
|
||||
self.button_add_bundle.setVisible(show)
|
||||
self.button_remove_bundle.setVisible(show)
|
||||
|
||||
@Property(bool)
|
||||
def hide_bundle_buttons(self):
|
||||
"""Property to hide the bundle buttons."""
|
||||
return not self.button_add_bundle.isVisible()
|
||||
def hide_add_remove_buttons(self):
|
||||
"""Property to hide the add_remove buttons."""
|
||||
return self._hide_add_remove_buttons
|
||||
|
||||
@hide_bundle_buttons.setter
|
||||
def hide_bundle_buttons(self, hide: bool):
|
||||
"""Setter for the hide_bundle_buttons property.
|
||||
@hide_add_remove_buttons.setter
|
||||
def hide_add_remove_buttons(self, hide: bool):
|
||||
"""Setter for the hide_add_remove_buttons property.
|
||||
|
||||
Args:
|
||||
hide(bool): Hide or show the bundle buttons.
|
||||
hide(bool): Hide or show the add_remove buttons.
|
||||
"""
|
||||
self._show_bundle_buttons(not hide)
|
||||
self._hide_add_remove_buttons = hide
|
||||
if self.arg_box is not None:
|
||||
self.arg_box.hide_add_remove_buttons = hide
|
||||
|
||||
def add_kwargs_boxes(self, groups: list):
|
||||
"""
|
||||
@ -423,6 +399,7 @@ class ScanControl(BECWidget, QWidget):
|
||||
self.arg_box = ScanGroupBox(box_type="args", config=group)
|
||||
self.arg_box.device_selected.connect(self.emit_device_selected)
|
||||
self.arg_box.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
|
||||
self.arg_box.hide_add_remove_buttons = self._hide_add_remove_buttons
|
||||
self.layout.addWidget(self.arg_box)
|
||||
|
||||
@Slot(str)
|
||||
@ -435,16 +412,6 @@ class ScanControl(BECWidget, QWidget):
|
||||
self._selected_devices = dev_names
|
||||
self.device_selected.emit(dev_names)
|
||||
|
||||
@Slot()
|
||||
def add_arg_bundle(self):
|
||||
"""Adds a new argument bundle to the scan control layout."""
|
||||
self.arg_box.add_widget_bundle()
|
||||
|
||||
@Slot()
|
||||
def remove_arg_bundle(self):
|
||||
"""Removes the last argument bundle from the scan control layout."""
|
||||
self.arg_box.remove_widget_bundle()
|
||||
|
||||
def reset_layout(self):
|
||||
"""Clears the scan control layout from GuiGroups and ArgGroups boxes."""
|
||||
if self.arg_box is not None:
|
||||
|
@ -1,7 +1,8 @@
|
||||
from typing import Literal
|
||||
|
||||
from bec_lib.logger import bec_logger
|
||||
from qtpy.QtCore import Qt, Slot, Signal
|
||||
from bec_qthemes import material_icon
|
||||
from qtpy.QtCore import Property, Qt, Signal, Slot
|
||||
from qtpy.QtWidgets import (
|
||||
QCheckBox,
|
||||
QComboBox,
|
||||
@ -11,9 +12,12 @@ from qtpy.QtWidgets import (
|
||||
QFormLayout,
|
||||
QGridLayout,
|
||||
QGroupBox,
|
||||
QHBoxLayout,
|
||||
QLabel,
|
||||
QLineEdit,
|
||||
QPushButton,
|
||||
QSpinBox,
|
||||
QVBoxLayout,
|
||||
)
|
||||
|
||||
from bec_widgets.utils.widget_io import WidgetIO
|
||||
@ -146,14 +150,36 @@ class ScanGroupBox(QGroupBox):
|
||||
super().__init__(parent=parent, *args, **kwargs)
|
||||
self.config = config
|
||||
self.box_type = box_type
|
||||
self._hide_add_remove_buttons = False
|
||||
|
||||
vbox_layout = QVBoxLayout(self)
|
||||
hbox_layout = QHBoxLayout()
|
||||
vbox_layout.addLayout(hbox_layout)
|
||||
self.layout = QGridLayout(self)
|
||||
vbox_layout.addLayout(self.layout)
|
||||
|
||||
# Add bundle button
|
||||
self.button_add_bundle = QPushButton(self)
|
||||
self.button_add_bundle.setIcon(
|
||||
material_icon(icon_name="add", size=(15, 15), convert_to_pixmap=False)
|
||||
)
|
||||
# Remove bundle button
|
||||
self.button_remove_bundle = QPushButton(self)
|
||||
self.button_remove_bundle.setIcon(
|
||||
material_icon(icon_name="remove", size=(15, 15), convert_to_pixmap=False)
|
||||
)
|
||||
hbox_layout.addWidget(self.button_add_bundle)
|
||||
hbox_layout.addWidget(self.button_remove_bundle)
|
||||
|
||||
self.labels = []
|
||||
self.widgets = []
|
||||
self.selected_devices = {}
|
||||
|
||||
self.init_box(self.config)
|
||||
|
||||
self.button_add_bundle.clicked.connect(self.add_widget_bundle)
|
||||
self.button_remove_bundle.clicked.connect(self.remove_widget_bundle)
|
||||
|
||||
def init_box(self, config: dict):
|
||||
box_name = config.get("name", "ScanGroupBox")
|
||||
self.inputs = config.get("inputs", {})
|
||||
@ -169,6 +195,8 @@ class ScanGroupBox(QGroupBox):
|
||||
self.add_input_widgets(self.inputs, i)
|
||||
else:
|
||||
self.add_input_widgets(self.inputs, 1)
|
||||
self.button_add_bundle.setVisible(False)
|
||||
self.button_remove_bundle.setVisible(False)
|
||||
|
||||
def add_input_labels(self, group_inputs: dict, row: int) -> None:
|
||||
"""
|
||||
@ -223,8 +251,6 @@ class ScanGroupBox(QGroupBox):
|
||||
"""
|
||||
Adds a new row of widgets to the scan control layout. Only usable for arg_groups.
|
||||
"""
|
||||
if self.box_type != "args":
|
||||
return
|
||||
arg_max = self.config.get("max", None)
|
||||
row = self.layout.rowCount()
|
||||
if arg_max is not None and row >= arg_max:
|
||||
@ -236,17 +262,34 @@ class ScanGroupBox(QGroupBox):
|
||||
"""
|
||||
Removes the last row of widgets from the scan control layout. Only usable for arg_groups.
|
||||
"""
|
||||
if self.box_type != "args":
|
||||
return
|
||||
arg_min = self.config.get("min", None)
|
||||
row = self.count_arg_rows()
|
||||
if arg_min is not None and row <= arg_min:
|
||||
return
|
||||
|
||||
for widget in self.widgets[-len(self.inputs) :]:
|
||||
if isinstance(widget, DeviceLineEdit):
|
||||
self.selected_devices[widget] = ""
|
||||
widget.deleteLater()
|
||||
self.widgets = self.widgets[: -len(self.inputs)]
|
||||
|
||||
selected_devices_str = " ".join(self.selected_devices.values())
|
||||
self.device_selected.emit(selected_devices_str.strip())
|
||||
|
||||
@Property(bool)
|
||||
def hide_add_remove_buttons(self):
|
||||
return self._hide_add_remove_buttons
|
||||
|
||||
@hide_add_remove_buttons.setter
|
||||
def hide_add_remove_buttons(self, hide: bool):
|
||||
self._hide_add_remove_buttons = hide
|
||||
if not hide and self.box_type == "args":
|
||||
self.button_add_bundle.show()
|
||||
self.button_remove_bundle.show()
|
||||
return
|
||||
self.button_add_bundle.hide()
|
||||
self.button_remove_bundle.hide()
|
||||
|
||||
def get_parameters(self, device_object: bool = True):
|
||||
"""
|
||||
Returns the parameters from the widgets in the scan control layout formated to run scan from BEC.
|
||||
|
@ -355,14 +355,14 @@ def test_add_remove_bundle(scan_control, scan_name, qtbot):
|
||||
|
||||
assert initial_num_of_rows == expected_scan_info["arg_bundle_size"]["min"]
|
||||
|
||||
scan_control.button_add_bundle.click()
|
||||
scan_control.button_add_bundle.click()
|
||||
scan_control.arg_box.button_add_bundle.click()
|
||||
scan_control.arg_box.button_add_bundle.click()
|
||||
|
||||
if expected_scan_info["arg_bundle_size"]["max"] is None:
|
||||
assert scan_control.arg_box.count_arg_rows() == initial_num_of_rows + 2
|
||||
|
||||
# Remove one bundle
|
||||
scan_control.button_remove_bundle.click()
|
||||
scan_control.arg_box.button_remove_bundle.click()
|
||||
qtbot.wait(200)
|
||||
|
||||
assert scan_control.arg_box.count_arg_rows() == initial_num_of_rows + 1
|
||||
|
Reference in New Issue
Block a user