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

refactor: config_dialog.py refactored to accept new config formatting

This commit is contained in:
wyzula-jan
2024-01-08 16:22:39 +01:00
parent 404ca49821
commit 3982c5d498
2 changed files with 134 additions and 109 deletions

View File

@ -21,7 +21,7 @@ Tab_Ui_Form, Tab_BaseClass = uic.loadUiType(os.path.join(current_path, "tab_temp
# test configs for demonstration purpose # test configs for demonstration purpose
# Configuration for default mode when only devices are monitored # Configuration for default mode when only devices are monitored
config_default = { CONFIG_DEFAULT = {
"plot_settings": { "plot_settings": {
"background_color": "black", "background_color": "black",
"num_columns": 1, "num_columns": 1,
@ -31,35 +31,41 @@ config_default = {
"plot_data": [ "plot_data": [
{ {
"plot_name": "BPM4i plots vs samx", "plot_name": "BPM4i plots vs samx",
"x": { "x_label": "Motor Y",
"label": "Motor Y", "y_label": "bpm4i",
"signals": [{"name": "samx", "entry": "samx"}], "sources": [
}, {
"y": { "type": "scan_segment",
"label": "bpm4i", "signals": {
"signals": [{"name": "bpm4i", "entry": "bpm4i"}], "x": [{"name": "samx", "entry": "samx"}],
}, "y": [{"name": "bpm4i", "entry": "bpm4i"}],
},
}
],
}, },
{ {
"plot_name": "Gauss plots vs samx", "plot_name": "Gauss plots vs samx",
"x": { "x_label": "Motor X",
"label": "Motor X", "y_label": "Gauss",
"signals": [{"name": "samx", "entry": "samx"}], "sources": [
}, {
"y": { "type": "scan_segment",
"label": "Gauss", "signals": {
"signals": [ "x": [{"name": "samx", "entry": "samx"}],
{"name": "gauss_bpm", "entry": "gauss_bpm"}, "y": [
{"name": "gauss_acd1", "entry": "gauss_adc1"}, {"name": "gauss_bpm"},
{"name": "gauss_acd2", "entry": "gauss_adc2"}, {"name": "gauss_adc1"},
], {"name": "gauss_adc2"},
}, ],
},
}
],
}, },
], ],
} }
# Configuration which is dynamically changing depending on the scan type # Configuration which is dynamically changing depending on the scan type
config_scan = { CONFIG_SCAN_MODE = {
"plot_settings": { "plot_settings": {
"background_color": "white", "background_color": "white",
"num_columns": 3, "num_columns": 3,
@ -70,77 +76,89 @@ config_scan = {
"grid_scan": [ "grid_scan": [
{ {
"plot_name": "Grid plot 1", "plot_name": "Grid plot 1",
"x": {"label": "Motor X", "signals": [{"name": "samx", "entry": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "BPM",
"label": "BPM", "sources": [
"signals": [ {
{"name": "gauss_bpm", "entry": "gauss_bpm"}, "type": "scan_segment",
{"name": "gauss_adc1", "entry": "gauss_adc1"}, "signals": {
], "x": [{"name": "samx", "entry": "samx"}],
}, "y": [{"name": "gauss_bpm"}],
},
}
],
}, },
{ {
"plot_name": "Grid plot 2", "plot_name": "Grid plot 2",
"x": {"label": "Motor X", "signals": [{"name": "samx", "entry": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "BPM",
"label": "BPM", "sources": [
"signals": [ {
{"name": "gauss_bpm", "entry": "gauss_bpm"}, "type": "scan_segment",
{"name": "gauss_adc1", "entry": "gauss_adc1"}, "signals": {
], "x": [{"name": "samx", "entry": "samx"}],
}, "y": [{"name": "gauss_adc1"}],
},
}
],
}, },
{ {
"plot_name": "Grid plot 3", "plot_name": "Grid plot 3",
"x": {"label": "Motor Y", "signals": [{"name": "samx", "entry": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "BPM",
"label": "BPM", "sources": [
"signals": [{"name": "gauss_bpm", "entry": "gauss_bpm"}], {
}, "type": "scan_segment",
"signals": {
"x": [{"name": "samy"}],
"y": [{"name": "gauss_adc2"}],
},
}
],
}, },
{ {
"plot_name": "Grid plot 4", "plot_name": "Grid plot 4",
"x": {"label": "Motor Y", "signals": [{"name": "samx", "entry": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "BPM",
"label": "BPM", "sources": [
"signals": [{"name": "gauss_adc3", "entry": "gauss_adc3"}], {
}, "type": "scan_segment",
"signals": {
"x": [{"name": "samy", "entry": "samy"}],
"y": [{"name": "gauss_adc3"}],
},
}
],
}, },
], ],
"line_scan": [ "line_scan": [
{ {
"plot_name": "BPM plot", "plot_name": "BPM plots vs samx",
"x": {"label": "Motor X", "signals": [{"name": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "Gauss",
"label": "BPM", "sources": [
"signals": [ {
{"name": "gauss_bpm", "entry": "gauss_bpm"}, "type": "scan_segment",
{"name": "gauss_adc1", "entry": "gauss_adc1"}, "signals": {
{"name": "gauss_adc2", "entry": "gauss_adc2"}, "x": [{"name": "samx", "entry": "samx"}],
], "y": [{"name": "bpm4i"}],
}, },
}
],
}, },
{ {
"plot_name": "Multi", "plot_name": "Gauss plots vs samx",
"x": {"label": "Motor X", "signals": [{"name": "samx", "entry": "samx"}]}, "x_label": "Motor X",
"y": { "y_label": "Gauss",
"label": "Multi", "sources": [
"signals": [ {
{"name": "gauss_bpm", "entry": "gauss_bpm"}, "type": "scan_segment",
{"name": "samx", "entry": "samx"}, "signals": {
], "x": [{"name": "samx", "entry": "samx"}],
}, "y": [{"name": "gauss_bpm"}, {"name": "gauss_adc1"}],
}, },
{ }
"plot_name": "Multi", ],
"x": {"label": "Motor X", "signals": [{"name": "samx", "entry": "samx"}]},
"y": {
"label": "Multi",
"signals": [
{"name": "gauss_bpm", "entry": "gauss_bpm"},
{"name": "samx", "entry": "samx"},
],
},
}, },
], ],
}, },
@ -329,7 +347,15 @@ class ConfigDialog(QWidget, Ui_Form):
ui = plot_tab.ui ui = plot_tab.ui
table = ui.tableWidget_y_signals table = ui.tableWidget_y_signals
signals = [
x_signals = [
{
"name": self.safe_text(ui.lineEdit_x_name),
"entry": self.safe_text(ui.lineEdit_x_entry),
}
]
y_signals = [
{ {
"name": self.safe_text(table.item(row, 0)), "name": self.safe_text(table.item(row, 0)),
"entry": self.safe_text(table.item(row, 1)), "entry": self.safe_text(table.item(row, 1)),
@ -339,19 +365,17 @@ class ConfigDialog(QWidget, Ui_Form):
plot_data = { plot_data = {
"plot_name": self.safe_text(ui.lineEdit_plot_title), "plot_name": self.safe_text(ui.lineEdit_plot_title),
"x": { "x_label": self.safe_text(ui.lineEdit_x_label),
"label": self.safe_text(ui.lineEdit_x_label), "y_label": self.safe_text(ui.lineEdit_y_label),
"signals": [ "sources": [
{ {
"name": self.safe_text(ui.lineEdit_x_name), "type": "scan_segment",
"entry": self.safe_text(ui.lineEdit_x_entry), "signals": {
} "x": x_signals,
], "y": y_signals,
}, },
"y": { }
"label": self.safe_text(ui.lineEdit_y_label), ],
"signals": signals,
},
} }
return plot_data return plot_data
@ -446,15 +470,14 @@ class ConfigDialog(QWidget, Ui_Form):
plot (QWidget): plot tab widget plot (QWidget): plot tab widget
plot_config (dict): config for single plot tab plot_config (dict): config for single plot tab
""" """
x_config = plot_config.get("x", {}) sources = plot_config.get("sources", [{}])[0]
x_signals = x_config.get("signals", [{}])[0] # Assuming at least one x signal x_signals = sources.get("signals", {}).get("x", [{}])[0]
y_config = plot_config.get("y", {}) y_signals = sources.get("signals", {}).get("y", [])
y_signals = y_config.get("signals", [])
# LabelBox # LabelBox
plot.ui.lineEdit_plot_title.setText(plot_config.get("plot_name", "")) plot.ui.lineEdit_plot_title.setText(plot_config.get("plot_name", ""))
plot.ui.lineEdit_x_label.setText(x_config.get("label", "")) plot.ui.lineEdit_x_label.setText(plot_config.get("x_label", ""))
plot.ui.lineEdit_y_label.setText(y_config.get("label", "")) plot.ui.lineEdit_y_label.setText(plot_config.get("y_label", ""))
# X axis # X axis
plot.ui.lineEdit_x_name.setText(x_signals.get("name", "")) plot.ui.lineEdit_x_name.setText(x_signals.get("name", ""))
@ -507,4 +530,5 @@ if __name__ == "__main__": # pragma: no cover
app = QApplication([]) app = QApplication([])
main_app = ConfigDialog() main_app = ConfigDialog()
main_app.show() main_app.show()
main_app.load_config(CONFIG_SCAN_MODE)
app.exec() app.exec()

View File

@ -121,6 +121,7 @@ def test_add_new_plot_and_modify(config_dialog):
# Ensure the tab count is initially 1 and it is called "Default" # Ensure the tab count is initially 1 and it is called "Default"
assert config_dialog.tabWidget_scan_types.count() == 1 assert config_dialog.tabWidget_scan_types.count() == 1
assert config_dialog.tabWidget_scan_types.tabText(0) == "Default" assert config_dialog.tabWidget_scan_types.tabText(0) == "Default"
# Get the first tab (which should be a scan tab) # Get the first tab (which should be a scan tab)
scan_tab = config_dialog.tabWidget_scan_types.widget(0) scan_tab = config_dialog.tabWidget_scan_types.widget(0)
@ -149,8 +150,7 @@ def test_add_new_plot_and_modify(config_dialog):
new_plot_tab.ui.lineEdit_x_entry.setText("Modified X Entry") new_plot_tab.ui.lineEdit_x_entry.setText("Modified X Entry")
# Modify the table for signals # Modify the table for signals
# new_plot_tab.ui.pushButton_y_new.click() # Press button to add a new row config_dialog.add_new_signal(new_plot_tab.ui.tableWidget_y_signals)
config_dialog.add_new_signal(new_plot_tab.ui.tableWidget_y_signals) # TODO change to click?
table = new_plot_tab.ui.tableWidget_y_signals table = new_plot_tab.ui.tableWidget_y_signals
assert table.rowCount() == 1 # Ensure the new row is added assert table.rowCount() == 1 # Ensure the new row is added
@ -160,17 +160,18 @@ def test_add_new_plot_and_modify(config_dialog):
# Modify the first row # Modify the first row
table.setItem(row_position, 0, QTableWidgetItem("New Signal Name")) table.setItem(row_position, 0, QTableWidgetItem("New Signal Name"))
table.setItem(row_position, 1, QTableWidgetItem("New Signal Entry")) table.setItem(row_position, 1, QTableWidgetItem("New Signal Entry"))
# Apply the configuration # Apply the configuration
config = config_dialog.apply_config() config = config_dialog.apply_config()
# Check if the modifications are reflected in the configuration # Check if the modifications are reflected in the configuration
modified_plot_config = config["plot_data"][ modified_plot_config = config["plot_data"][1] # Access the second plot in the plot_data list
1 sources = modified_plot_config["sources"][0] # Access the first source in the sources list
] # Assuming the new plot is the second item in the plot_data list
assert modified_plot_config["plot_name"] == "Modified Plot Title" assert modified_plot_config["plot_name"] == "Modified Plot Title"
assert modified_plot_config["x"]["label"] == "Modified X Label" assert modified_plot_config["x_label"] == "Modified X Label"
assert modified_plot_config["y"]["label"] == "Modified Y Label" assert modified_plot_config["y_label"] == "Modified Y Label"
assert modified_plot_config["x"]["signals"][0]["name"] == "Modified X Name" assert sources["signals"]["x"][0]["name"] == "Modified X Name"
assert modified_plot_config["x"]["signals"][0]["entry"] == "Modified X Entry" assert sources["signals"]["x"][0]["entry"] == "Modified X Entry"
assert modified_plot_config["y"]["signals"][0]["name"] == "New Signal Name" assert sources["signals"]["y"][0]["name"] == "New Signal Name"
assert modified_plot_config["y"]["signals"][0]["entry"] == "New Signal Entry" assert sources["signals"]["y"][0]["entry"] == "New Signal Entry"