From 3ea9273ffe1b7468649e7bbfa5013e82036c75a1 Mon Sep 17 00:00:00 2001
From: CPD <CPD@TUZEWSI-2LN203M.ads.mwn.de>
Date: Mon, 17 Mar 2025 12:16:17 +0100
Subject: [PATCH] Add drag-n-drop load data from directory and pkl

---
 cpdctrl_gui/ui/main_window.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py
index 30835c4..b6116f0 100644
--- a/cpdctrl_gui/ui/main_window.py
+++ b/cpdctrl_gui/ui/main_window.py
@@ -19,6 +19,7 @@ from .widgets.about import MarkdownView
 from .widgets.led_script import LedScriptViewer
 # from .widgets.treeview import TreeView
 
+import time
 import pickle
 import logging
 log = logging.getLogger(__name__)
@@ -112,6 +113,7 @@ class MainWindow(QMainWindow):
 
         # LED SCRIPT
         self.w_led_script = LedScriptViewer(LedScript(0))
+        self.w_led_script.model.dataChanged.connect(self._led_script_updated)
         self.w_lefttab.addTab(self.w_led_script, "LED Script")
         self.w_measurement_settings.w_led_script.script_changed.connect(self.led_script_load)
 
@@ -336,7 +338,6 @@ class MainWindow(QMainWindow):
         self.set_status("Ready")
 
     def measure_update(self):
-        import time
         self.w_led_script.update_time(time.time())
         if self.proc_measure.is_alive():
             while not self.data_queue.empty():
@@ -426,6 +427,7 @@ class MainWindow(QMainWindow):
         script = self.w_measurement_settings.get_value("led_script")
         script_type = self.w_measurement_settings.get_value("led_script_type")
         auto_update = AppConfig.MAIN_CFG.get_or("led_script_watch_file", False)
+        log.info(f"Loading led script of type '{script_type}' with auto-update {auto_update}")
         try:
             self.led_script = LedScript(script=script, auto_update=False, verbose=True)
         except ValueError as e:
@@ -433,13 +435,14 @@ class MainWindow(QMainWindow):
             QMessageBox.critical(self, "LED script error", str(e))
             return
         if auto_update and script_type == "file":
-            # can not use "integrated" auto update funciton because we cant receive qtsignals
-            # from the watchdog thread -> use Qfilesystemwatcher
+            # can not use "integrated" auto update function because we cant receive qt signals
+            # from the watchdog thread -> use QFileSystemwatcher
             self.led_script_watcher = QFileSystemWatcher()
             self.led_script_watcher.addPath(script)
             self.led_script_watcher.fileChanged.connect(self._led_script_update_from_file)
         else:
             self.led_script_watcher = None
+        self.w_led_script.set_script(self.led_script)
         self._led_script_updated()
 
     def _led_script_update_from_file(self):
@@ -453,12 +456,11 @@ class MainWindow(QMainWindow):
         """
         Send the new led script to the measurement thread and update the gui widget.
         """
+        # update gui
+        self.w_led_script.update_time_predictions()
         # update the measurement led script
         if self.measurement_is_running():
             self.command_queue.put(("led_script", self.led_script.copy()))
-        self.w_led_script.set_script(self.led_script)
-        # update gui
-        self.w_led_script.update_time_predictions()
 
     def app_exit(self) -> None:
         """