From f50631521cd43f3f81141d911d8e88e2f70cd7a4 Mon Sep 17 00:00:00 2001 From: CPD Date: Wed, 12 Mar 2025 17:35:18 +0100 Subject: [PATCH] Add load from csv --- cpdctrl_gui/ui/main_window.py | 32 ++++++++++++++++++++++++++++++++ cpdctrl_gui/ui/widgets/plot.py | 13 +++++++++++++ 2 files changed, 45 insertions(+) diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py index ed438f6..9dd0144 100644 --- a/cpdctrl_gui/ui/main_window.py +++ b/cpdctrl_gui/ui/main_window.py @@ -1,4 +1,6 @@ ''' cpdctrl_gui/ui/main_window.py ''' +import os.path + from PyQt6.QtCore import Qt, QTimer from PyQt6.QtWidgets import QMainWindow, QWidget, QHBoxLayout, QLabel, QStatusBar, QFileDialog, \ QVBoxLayout @@ -134,6 +136,7 @@ class MainWindow(QMainWindow): self.topbar.add_button("meas_start", "Start", QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart), self.measure_start) self.topbar.add_button("meas_stop", "Stop", QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStop), self.measure_stop) self.topbar.add_button("meas_save", "Save", QIcon.fromTheme(QIcon.ThemeIcon.DocumentSaveAs), self.measurement_save) + self.topbar.add_button("meas_load", "Load", QIcon.fromTheme(QIcon.ThemeIcon.DocumentOpen), self.measurement_load_dialog) self.topbar.add_separator() self.topbar.add_button("app_settings", "Settings", QIcon.fromTheme(QIcon.ThemeIcon.DocumentProperties), self.app_open_settings) self.topbar.addAction(self.a_open_help) @@ -237,6 +240,7 @@ class MainWindow(QMainWindow): self.topbar.disable_button("connect_vmdev") self.topbar.disable_button("connect_leddev") self.topbar.disable_button("meas_save") + self.topbar.disable_button("meas_load") self.topbar.enable_button("meas_stop") self.w_plot.clear_data() @@ -304,6 +308,7 @@ class MainWindow(QMainWindow): self.topbar.enable_button("connect_vmdev") self.topbar.enable_button("connect_leddev") self.topbar.enable_button("meas_save") + self.topbar.enable_button("meas_load") self.topbar.disable_button("meas_stop") def measure_update(self): @@ -341,6 +346,33 @@ class MainWindow(QMainWindow): else: self.set_status(f"Aborted saving data, no file selected") + def measurement_load(self, filepath): + if self.measurement_is_running(): + QMessageBox.critical(self, "Measurement running", "Can not load data while measurement is running") + return + + + if os.path.isfile(filepath): + data, mdata = DataCollector.load_data_from_csv(filepath) + else: + raise FileNotFoundError(f"No such file or directory {filepath} not found") + self.w_plot.set_data(data[:,1], data[:,2], data[:,3]) + + def measurement_load_dialog(self): + if self.measurement_is_running(): + QMessageBox.critical(self, "Measurement running", "Can not load data while measurement is running") + return + + last_dir = AppConfig.MAIN_CFG.get_or("tmp_last_measurement_load_dir", "") + file_path, _ = QFileDialog.getOpenFileName(self, "Load File", last_dir, "CSV Files (*.csv)") + if file_path: + dir_name = os.path.dirname(file_path) + AppConfig.MAIN_CFG.set("tmp_last_measurement_load_dir", dir_name) + self.measurement_load(file_path) + else: + self.set_status(f"Aborted loading data, no file selected") + self.measurement_load(file_path) + def led_script_load(self): script = self.w_measurement_settings.get_value("led_script") try: diff --git a/cpdctrl_gui/ui/widgets/plot.py b/cpdctrl_gui/ui/widgets/plot.py index 26b6369..472ec7c 100644 --- a/cpdctrl_gui/ui/widgets/plot.py +++ b/cpdctrl_gui/ui/widgets/plot.py @@ -117,6 +117,19 @@ class Plot(pg.GraphicsLayoutWidget): self.n_data_total += 1 + def set_data(self, data_t, data_v, data_l): + """ + Set the data to be plotted, without updating the internal arrays + """ + # self.data_t = data_t + # self.data_v = data_v + # self.data_l = data_l + # self.n_data_array = len(data_t) + # self.n_data_total = len(data_t) + # self.n_data_take_every_nth = 1 + self.v_line.setData(data_t, data_v) + self.l_line.setData(data_t, data_l) + def clear_data(self): """ Clear the lines and data