From e5273539741a1261e69b1bf76af78c7c1ab0d901 Mon Sep 17 00:00:00 2001 From: wyzula-jan <133381102+wyzula-jan@users.noreply.github.com> Date: Fri, 1 Sep 2023 09:38:32 +0200 Subject: [PATCH] feat: load and export configuration into .yaml from GUI --- bec_widgets/examples/extreme/extreme.py | 56 +++++++++++++++++++++++- bec_widgets/examples/extreme/extreme.ui | 58 +++++++++++++++---------- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/bec_widgets/examples/extreme/extreme.py b/bec_widgets/examples/extreme/extreme.py index b49154d3..85eab541 100644 --- a/bec_widgets/examples/extreme/extreme.py +++ b/bec_widgets/examples/extreme/extreme.py @@ -3,7 +3,7 @@ import os import numpy as np import pyqtgraph as pg from PyQt5.QtCore import pyqtSignal, pyqtSlot -from PyQt5.QtWidgets import QApplication, QWidget, QTableWidgetItem, QTableWidget +from PyQt5.QtWidgets import QApplication, QWidget, QTableWidgetItem, QTableWidget, QFileDialog from pyqtgraph import mkBrush, mkColor, mkPen from pyqtgraph.Qt import QtCore, uic @@ -75,10 +75,16 @@ class PlotApp(QWidget): # Initialize the UI self.init_ui(self.plot_settings["num_columns"]) - self.spinBox_N_columns.setValue(self.plot_settings["num_columns"]) + self.spinBox_N_columns.setValue( + self.plot_settings["num_columns"] + ) # TODO has to be checked if it will not setup more columns than plots self.spinBox_N_columns.setMaximum(len(self.plot_data)) self.splitter.setSizes([400, 100]) + # Buttons + self.pushButton_save.clicked.connect(self.save_settings_to_yaml) + self.pushButton_load.clicked.connect(self.load_settings_from_yaml) + # Connect the update signal to the update plot method self.proxy_update_plot = pg.SignalProxy( self.update_signal, rateLimit=25, slot=self.update_plot @@ -354,6 +360,52 @@ class PlotApp(QWidget): self.update_signal.emit() + def save_settings_to_yaml(self): + """Save the current settings to a .yaml file using a file dialog.""" + options = QFileDialog.Options() + options |= QFileDialog.DontUseNativeDialog + file_path, _ = QFileDialog.getSaveFileName( + self, "Save Settings", "", "YAML Files (*.yaml);;All Files (*)", options=options + ) + + if file_path: + try: + if not file_path.endswith(".yaml"): + file_path += ".yaml" + + with open(file_path, "w") as file: + yaml.dump( + {"plot_settings": self.plot_settings, "plot_data": self.plot_data}, file + ) + print(f"Settings saved to {file_path}") + except Exception as e: + print(f"An error occurred while saving the settings to {file_path}: {e}") + + def load_settings_from_yaml(self): + """Load settings from a .yaml file using a file dialog and update the current settings.""" + options = QFileDialog.Options() + options |= QFileDialog.DontUseNativeDialog + file_path, _ = QFileDialog.getOpenFileName( + self, "Load Settings", "", "YAML Files (*.yaml);;All Files (*)", options=options + ) + + if file_path: + try: + with open(file_path, "r") as file: + config = yaml.safe_load(file) + + self.plot_settings = config.get("plot_settings", {}) + self.plot_data = config.get("plot_data", {}) + # Reinitialize the UI and plots + # self.init_plot_background(self.plot_settings["background_color"]) #TODO implement + self.init_ui(self.plot_settings["num_columns"]) + self.init_curves() + print(f"Settings loaded from {file_path}") + except FileNotFoundError: + print(f"The file {file_path} was not found.") + except Exception as e: + print(f"An error occurred while loading the settings from {file_path}: {e}") + if __name__ == "__main__": import yaml diff --git a/bec_widgets/examples/extreme/extreme.ui b/bec_widgets/examples/extreme/extreme.ui index 51f6ab8a..e22291c4 100644 --- a/bec_widgets/examples/extreme/extreme.ui +++ b/bec_widgets/examples/extreme/extreme.ui @@ -22,27 +22,7 @@ - - - - Number of Columns: - - - - - - - 1 - - - 10 - - - 3 - - - - + Qt::Horizontal @@ -55,7 +35,7 @@ - + Cursor @@ -78,6 +58,40 @@ + + + + Number of Columns: + + + + + + + 1 + + + 10 + + + 3 + + + + + + + Load Config + + + + + + + Save Config + + +