0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

refactor: scan_control.py refactor to use WidgetIO

This commit is contained in:
wyzula-jan
2023-11-08 14:35:43 +01:00
parent 18a702572f
commit 3be9c974b5

View File

@ -1,5 +1,4 @@
import msgpack import msgpack
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QApplication, QApplication,
QWidget, QWidget,
@ -18,12 +17,11 @@ from PyQt5.QtWidgets import (
QGridLayout, QGridLayout,
QTableWidget, QTableWidget,
QTableWidgetItem, QTableWidgetItem,
QHeaderView,
) )
from bec_lib.core import MessageEndpoints from bec_lib.core import MessageEndpoints
from bec_widgets.qt_utils.widget_hierarchy import print_widget_hierarchy from bec_widgets.qt_utils.widget_io import WidgetIO
# from bec_widgets.qt_utils.layout_tools import remove_empty_cells
class ScanArgType: class ScanArgType:
@ -94,6 +92,8 @@ class ScanControl(QWidget):
# Initialize the QTableWidget for args # Initialize the QTableWidget for args
self.args_table = QTableWidget() self.args_table = QTableWidget()
self.args_table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed)
self.scan_control_layout.addWidget(self.args_table) self.scan_control_layout.addWidget(self.args_table)
# Connect signals # Connect signals
@ -126,6 +126,7 @@ class ScanControl(QWidget):
self.comboBox_scan_selection.addItems(allowed_scans) self.comboBox_scan_selection.addItems(allowed_scans)
def on_scan_selected(self): def on_scan_selected(self):
"""Callback for scan selection combo box"""
selected_scan_name = self.comboBox_scan_selection.currentText() selected_scan_name = self.comboBox_scan_selection.currentText()
selected_scan_info = self.available_scans.get(selected_scan_name, {}) selected_scan_info = self.available_scans.get(selected_scan_name, {})
@ -154,10 +155,21 @@ class ScanControl(QWidget):
grid_layout.addWidget(label, row_index, column_index) grid_layout.addWidget(label, row_index, column_index)
def add_labels_to_table(self, labels: list, table: QTableWidget) -> None: def add_labels_to_table(self, labels: list, table: QTableWidget) -> None:
"""
Adds labels to the given table widget as a header row.
Args:
labels(list): List of label names to add.
table(QTableWidget): The table widget to which labels will be added.
"""
table.setColumnCount(len(labels)) table.setColumnCount(len(labels))
table.setHorizontalHeaderLabels(labels) table.setHorizontalHeaderLabels(labels)
def generate_args_input_fields(self, scan_info: dict) -> None: def generate_args_input_fields(self, scan_info: dict) -> None:
"""
Generates input fields for args
Args:
scan_info(dict): Scan signature dictionary from BEC.
"""
# Get arg_input from selected scan # Get arg_input from selected scan
self.arg_input = scan_info.get("arg_input", {}) self.arg_input = scan_info.get("arg_input", {})
@ -171,7 +183,7 @@ class ScanControl(QWidget):
""" """
Generates input fields for kwargs Generates input fields for kwargs
Args: Args:
scan_info(dict): Dictionary containing scan information scan_info(dict): Scan signature dictionary from BEC.
""" """
# Get signature # Get signature
signature = scan_info.get("signature", []) signature = scan_info.get("signature", [])
@ -187,6 +199,15 @@ class ScanControl(QWidget):
self.add_widgets_row_to_layout(self.kwargs_layout, widgets) self.add_widgets_row_to_layout(self.kwargs_layout, widgets)
def generate_widgets_from_signature(self, items: list, signature: dict = None) -> list: def generate_widgets_from_signature(self, items: list, signature: dict = None) -> list:
"""
Generates widgets from the given list of items.
Args:
items(list): List of items to create widgets for.
signature(dict, optional): Scan signature dictionary from BEC.
Returns:
"""
widgets = [] # Initialize an empty list to hold the widgets widgets = [] # Initialize an empty list to hold the widgets
for item in items: for item in items:
@ -324,17 +345,8 @@ class ScanControl(QWidget):
value_item = grid_layout.itemAtPosition(row + 1, column) value_item = grid_layout.itemAtPosition(row + 1, column)
if value_item is not None: if value_item is not None:
value_widget = value_item.widget() value_widget = value_item.widget()
# Extract the value from the widget # Use WidgetIO.get_value to extract the value
if isinstance(value_widget, QLineEdit): value = WidgetIO.get_value(value_widget)
value = value_widget.text()
elif isinstance(value_widget, QSpinBox) or isinstance(
value_widget, QDoubleSpinBox
):
value = value_widget.value()
elif isinstance(value_widget, QCheckBox):
value = value_widget.isChecked()
else:
value = None # You can decide how to handle other widget types
kwargs[key] = value kwargs[key] = value
return kwargs return kwargs
@ -344,23 +356,20 @@ class ScanControl(QWidget):
Args: Args:
table(QTableWidget): Table widget from which to extract the arguments table(QTableWidget): Table widget from which to extract the arguments
""" """
# Extract args from the table
args = [] args = []
for row in range(table.rowCount()): for row in range(table.rowCount()):
row_args = [] row_args = []
for column in range(table.columnCount()): for column in range(table.columnCount()):
widget = table.cellWidget(row, column) widget = table.cellWidget(row, column)
if widget: if widget:
# Extract the value from the widget if isinstance(widget, QLineEdit): # special case for QLineEdit for Devices
if isinstance(widget, QLineEdit):
value = widget.text().lower() value = widget.text().lower()
# If the value corresponds to a device, retrieve the device object
if value in self.dev: if value in self.dev:
value = getattr(self.dev, value) value = getattr(self.dev, value)
elif isinstance(widget, QSpinBox) or isinstance(widget, QDoubleSpinBox): else:
value = widget.value() raise ValueError(f"The device '{value}' is not recognized.")
else: else:
value = None # You can decide how to handle other widget types value = WidgetIO.get_value(widget)
row_args.append(value) row_args.append(value)
args.extend(row_args) args.extend(row_args)
return args return args
@ -373,24 +382,16 @@ class ScanControl(QWidget):
} }
# Extract args from the table # Extract args from the table
args = self.extract_args_from_table() args = self.extract_args_from_table(self.args_table)
# Convert args to lowercase if they are strings # Convert args to lowercase if they are strings
args = [arg.lower() if isinstance(arg, str) else arg for arg in args] args = [arg.lower() if isinstance(arg, str) else arg for arg in args]
# TODO remove when function exec works
print(f"Args: {args}")
print(f"Kwargs: {kwargs}")
# Execute the scan # Execute the scan
scan_function = getattr(self.scans, self.comboBox_scan_selection.currentText()) scan_function = getattr(self.scans, self.comboBox_scan_selection.currentText())
if callable(scan_function): if callable(scan_function):
scan_function(*args, **kwargs) scan_function(*args, **kwargs)
def get_widget_value(self, widget):
if isinstance(widget, QLabel):
return widget.text()
if __name__ == "__main__": if __name__ == "__main__":
from bec_widgets.bec_dispatcher import bec_dispatcher from bec_widgets.bec_dispatcher import bec_dispatcher