mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
refactor: scan_control.py clean up
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import sys
|
import msgpack
|
||||||
from PyQt5.QtWidgets import (
|
from PyQt5.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
QWidget,
|
QWidget,
|
||||||
@ -9,18 +9,13 @@ from PyQt5.QtWidgets import (
|
|||||||
QLabel,
|
QLabel,
|
||||||
QLineEdit,
|
QLineEdit,
|
||||||
QDoubleSpinBox,
|
QDoubleSpinBox,
|
||||||
QFormLayout,
|
|
||||||
QSpinBox,
|
QSpinBox,
|
||||||
QCheckBox,
|
QCheckBox,
|
||||||
QFrame,
|
QFrame,
|
||||||
QHBoxLayout,
|
QHBoxLayout,
|
||||||
QGridLayout,
|
|
||||||
QLayout,
|
QLayout,
|
||||||
)
|
)
|
||||||
from PyQt5.QtCore import Qt
|
|
||||||
import msgpack
|
|
||||||
|
|
||||||
from bec_widgets.bec_dispatcher import bec_dispatcher
|
|
||||||
from bec_lib.core import MessageEndpoints
|
from bec_lib.core import MessageEndpoints
|
||||||
|
|
||||||
|
|
||||||
@ -31,32 +26,6 @@ class ScanArgType:
|
|||||||
BOOL = "bool"
|
BOOL = "bool"
|
||||||
|
|
||||||
|
|
||||||
class BundleGroup(QWidget):
|
|
||||||
WIDGET_HANDLER = {
|
|
||||||
ScanArgType.DEVICE: QLineEdit,
|
|
||||||
ScanArgType.FLOAT: QDoubleSpinBox,
|
|
||||||
ScanArgType.INT: QSpinBox,
|
|
||||||
ScanArgType.BOOL: QCheckBox,
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, arg_input, parent=None):
|
|
||||||
super().__init__(parent)
|
|
||||||
self.arg_input = arg_input
|
|
||||||
self.init_ui()
|
|
||||||
|
|
||||||
def init_ui(self):
|
|
||||||
self.layout = QHBoxLayout(self)
|
|
||||||
for param, param_type in self.arg_input.items():
|
|
||||||
widget_class = self.WIDGET_HANDLER.get(param_type)
|
|
||||||
if widget_class:
|
|
||||||
widget = widget_class(self)
|
|
||||||
if isinstance(widget, QDoubleSpinBox):
|
|
||||||
widget.setAlignment(Qt.AlignLeft) # Align to left
|
|
||||||
self.layout.addWidget(widget)
|
|
||||||
else:
|
|
||||||
print(f"Unsupported annotation '{param_type}' for parameter '{param}'")
|
|
||||||
|
|
||||||
|
|
||||||
class ScanControl(QWidget):
|
class ScanControl(QWidget):
|
||||||
WIDGET_HANDLER = {
|
WIDGET_HANDLER = {
|
||||||
ScanArgType.DEVICE: QLineEdit,
|
ScanArgType.DEVICE: QLineEdit,
|
||||||
@ -82,15 +51,14 @@ class ScanControl(QWidget):
|
|||||||
def _init_UI(self):
|
def _init_UI(self):
|
||||||
self.verticalLayout = QVBoxLayout(self)
|
self.verticalLayout = QVBoxLayout(self)
|
||||||
|
|
||||||
|
# Scan selection group box
|
||||||
self.scan_selection_group = QGroupBox("Scan Selection", self)
|
self.scan_selection_group = QGroupBox("Scan Selection", self)
|
||||||
self.scan_selection_layout = QVBoxLayout(self.scan_selection_group)
|
self.scan_selection_layout = QVBoxLayout(self.scan_selection_group)
|
||||||
|
|
||||||
self.comboBox_scan_selection = QComboBox(self.scan_selection_group)
|
self.comboBox_scan_selection = QComboBox(self.scan_selection_group)
|
||||||
self.scan_selection_layout.addWidget(self.comboBox_scan_selection)
|
self.scan_selection_layout.addWidget(self.comboBox_scan_selection)
|
||||||
# self.scan_selection_layout.addWidget(QPushButton("Connect", self.scan_selection_group)) #TODO button probably not needed
|
|
||||||
|
|
||||||
self.verticalLayout.addWidget(self.scan_selection_group)
|
self.verticalLayout.addWidget(self.scan_selection_group)
|
||||||
|
|
||||||
|
# Scan control group box
|
||||||
self.scan_control_group = QGroupBox("Scan Control", self)
|
self.scan_control_group = QGroupBox("Scan Control", self)
|
||||||
self.scan_control_layout = QVBoxLayout(self.scan_control_group)
|
self.scan_control_layout = QVBoxLayout(self.scan_control_group)
|
||||||
self.verticalLayout.addWidget(self.scan_control_group)
|
self.verticalLayout.addWidget(self.scan_control_group)
|
||||||
@ -119,6 +87,7 @@ class ScanControl(QWidget):
|
|||||||
self.args_layout = QVBoxLayout()
|
self.args_layout = QVBoxLayout()
|
||||||
self.scan_control_layout.addLayout(self.args_layout)
|
self.scan_control_layout.addLayout(self.args_layout)
|
||||||
|
|
||||||
|
# Initialize scan selection
|
||||||
self.populate_scans()
|
self.populate_scans()
|
||||||
self.comboBox_scan_selection.currentIndexChanged.connect(self.on_scan_selected)
|
self.comboBox_scan_selection.currentIndexChanged.connect(self.on_scan_selected)
|
||||||
|
|
||||||
@ -150,7 +119,6 @@ class ScanControl(QWidget):
|
|||||||
# Generate kwargs input
|
# Generate kwargs input
|
||||||
self.generate_kwargs_input_fields(selected_scan_info)
|
self.generate_kwargs_input_fields(selected_scan_info)
|
||||||
|
|
||||||
# TODO until HERE!
|
|
||||||
# Args section
|
# Args section
|
||||||
self.arg_input = selected_scan_info.get("arg_input", {}) # Get arg_input from selected scan
|
self.arg_input = selected_scan_info.get("arg_input", {}) # Get arg_input from selected scan
|
||||||
self.add_labels(self.arg_input.keys(), self.args_layout) # Add labels
|
self.add_labels(self.arg_input.keys(), self.args_layout) # Add labels
|
||||||
@ -158,12 +126,6 @@ class ScanControl(QWidget):
|
|||||||
self.args_layout, self.arg_input.items()
|
self.args_layout, self.arg_input.items()
|
||||||
) # Add first row of widgets
|
) # Add first row of widgets
|
||||||
|
|
||||||
# self.generate_input_fields(selected_scan_info) #TODO probably not needed
|
|
||||||
|
|
||||||
print(10 * "#" + f"{selected_scan_name}" + 10 * "#")
|
|
||||||
print(10 * "#" + "selected_scan_info" + 10 * "#")
|
|
||||||
print(selected_scan_info)
|
|
||||||
|
|
||||||
def add_labels(self, labels: list, layout) -> None:
|
def add_labels(self, labels: list, layout) -> None:
|
||||||
"""
|
"""
|
||||||
Adds labels to the given layout in QHBox layout
|
Adds labels to the given layout in QHBox layout
|
||||||
@ -207,9 +169,6 @@ class ScanControl(QWidget):
|
|||||||
layout(QLayout): Layout to add the widgets to
|
layout(QLayout): Layout to add the widgets to
|
||||||
items(list): List of items to add
|
items(list): List of items to add
|
||||||
signature(dict): Dictionary containing signature information for kwargs
|
signature(dict): Dictionary containing signature information for kwargs
|
||||||
|
|
||||||
Returns:
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
item_type, item_name = None, None
|
item_type, item_name = None, None
|
||||||
widget_row_layout = QHBoxLayout()
|
widget_row_layout = QHBoxLayout()
|
||||||
@ -222,19 +181,14 @@ class ScanControl(QWidget):
|
|||||||
else:
|
else:
|
||||||
item_name, item_type = item
|
item_name, item_type = item
|
||||||
|
|
||||||
widget_class = self.WIDGET_HANDLER.get(
|
widget_class = self.WIDGET_HANDLER.get(item_type, None)
|
||||||
item_type, None
|
|
||||||
) # TODO fix crash when unsupported annotation
|
|
||||||
if widget_class is None:
|
if widget_class is None:
|
||||||
print(f"Unsupported annotation '{item_type}' for parameter '{item_name}'")
|
print(f"Unsupported annotation '{item_type}' for parameter '{item_name}'")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Generated widget by HANDLER
|
# Generated widget by HANDLER
|
||||||
widget = widget_class(self.scan_control_group)
|
widget = widget_class(self.scan_control_group)
|
||||||
widget_row_layout.addWidget(widget)
|
widget_row_layout.addWidget(widget)
|
||||||
print(
|
|
||||||
f"Added widget {widget} to layout {layout}"
|
|
||||||
) # TODO remove when app will be working correctly
|
|
||||||
|
|
||||||
layout.addLayout(widget_row_layout)
|
layout.addLayout(widget_row_layout)
|
||||||
|
|
||||||
def clear_layout(self, layout: QLayout) -> None: # TODO like this probably
|
def clear_layout(self, layout: QLayout) -> None: # TODO like this probably
|
||||||
@ -262,13 +216,6 @@ class ScanControl(QWidget):
|
|||||||
self.args_layout, self.arg_input.items()
|
self.args_layout, self.arg_input.items()
|
||||||
) # Add first row of widgets
|
) # Add first row of widgets
|
||||||
|
|
||||||
# def remove_bundle(self):
|
|
||||||
# # print layout children
|
|
||||||
# print("layout children:")
|
|
||||||
# for i in range(self.args_layout.count()):
|
|
||||||
# print(self.args_layout.itemAt(i).widget())
|
|
||||||
|
|
||||||
#
|
|
||||||
def remove_bundle(self) -> None:
|
def remove_bundle(self) -> None:
|
||||||
"""Removes the last bundle from the scan control layout"""
|
"""Removes the last bundle from the scan control layout"""
|
||||||
last_bundle_index = self.args_layout.count() - 1 # Index of the last bundle
|
last_bundle_index = self.args_layout.count() - 1 # Index of the last bundle
|
||||||
@ -282,9 +229,6 @@ class ScanControl(QWidget):
|
|||||||
|
|
||||||
self.window().resize(self.window().sizeHint()) # Resize window to fit contents
|
self.window().resize(self.window().sizeHint()) # Resize window to fit contents
|
||||||
|
|
||||||
# remove last row of widgets
|
|
||||||
# self.args_layout.itemAt(self.args_layout.count() - 1).widget().deleteLater()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from bec_widgets.bec_dispatcher import bec_dispatcher
|
from bec_widgets.bec_dispatcher import bec_dispatcher
|
||||||
|
Reference in New Issue
Block a user