From 77054a676c6874e44fd03bfe4fdf5db6ff208ca0 Mon Sep 17 00:00:00 2001 From: CPD Date: Wed, 12 Mar 2025 12:55:21 +0100 Subject: [PATCH] Add rudimentary time left display --- cpdctrl_gui/ui/main_window.py | 8 +++- cpdctrl_gui/ui/widgets/led_script.py | 60 +++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/cpdctrl_gui/ui/main_window.py b/cpdctrl_gui/ui/main_window.py index 46140f3..f367b33 100644 --- a/cpdctrl_gui/ui/main_window.py +++ b/cpdctrl_gui/ui/main_window.py @@ -21,6 +21,8 @@ from .widgets.led_script import LedScriptViewer import logging log = logging.getLogger(__name__) +import time + import multiprocessing as mp import threading as mt @@ -82,7 +84,7 @@ class MainWindow(QMainWindow): # Right: Tabs: Script, Plot self.w_right_tab = QTabWidget() layout.addWidget(self.w_right_tab) - max_data_points = AppConfig.MAIN_CFG.get_or("plot_max_data_points", 3000) + max_data_points = AppConfig.MAIN_CFG.get_or("plot_max_data_points", 40000) self.w_plot = Plot(max_data_points=max_data_points) self.w_right_tab.addTab(self.w_plot, "Plot") @@ -251,6 +253,8 @@ class MainWindow(QMainWindow): log.info(f"Starting measurement with:\n\tinterval = {interval}\n\tflush_after = {flush_after}\n\tuse_buffer = {use_buffer}\n\tmax_measurements = {max_measurements}\n\tstop_on_script_end = {stop_on_script_end}") + self.w_led_script.w_time_left.set_end_time(time.time() + led_script.script["dtsum"][-1]) + self.led_script = LedScript(script=script, auto_update=True, verbose=True) self.data_collector = DataCollector(metadata=metadata, data_path=AppConfig.MAIN_CFG.get("datadir"), data_name=measurement_name) # data_collector.clear() @@ -297,6 +301,8 @@ class MainWindow(QMainWindow): self.topbar.disable_button("meas_stop") 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(): # print(data_queue.qsize(), "\n\n") diff --git a/cpdctrl_gui/ui/widgets/led_script.py b/cpdctrl_gui/ui/widgets/led_script.py index 10d2469..98a941c 100644 --- a/cpdctrl_gui/ui/widgets/led_script.py +++ b/cpdctrl_gui/ui/widgets/led_script.py @@ -4,6 +4,56 @@ from PyQt6.QtCore import QAbstractTableModel, QModelIndex, Qt import numpy as np from cpdctrl.led_script import LedScript +import time +import datetime +class TimeLeft(QWidget): + def __init__(self, parent=None): + super().__init__(parent) + self.t_end = None + self.setLayout(QVBoxLayout()) + self.layout().addWidget(QLabel("Time left")) + self.w_time_left = QLabel("N.A.") + self.layout().addWidget(self.w_time_left) + self.w_end_time = QLabel("N.A.") + self.layout().addWidget(self.w_end_time) + + def set_end_time(self, t_end: float): + """ + Set the end time + Parameters + ---------- + t_end + The end time in seconds since epoch + """ + self.t_end = t_end + self.w_end_time.setText( + datetime.datetime.fromtimestamp(t_end).strftime("%Y-%m-%d %H:%M:%S") + ) + + def update_time(self, t_now: float): + """ + Update the time left display + Parameters + ---------- + t_now + The current time in seconds since epoch + """ + if self.t_end is None: raise RuntimeError("Update called before end time was set") + # convert to format d, h, m, s + print(self.t_end, t_now, self.t_end - t_now) + sign = 1 if self.t_end > t_now else -1 + time_left = abs(int(self.t_end - t_now)) + days = sign * int(time_left / (24 * 3600)) + time_left %= (24 * 3600) + hours = sign * int(time_left / 3600) + time_left %= 3600 + minutes = sign * int(time_left / 60) + time_left %= 60 + seconds = sign * int(time_left) + self.w_time_left.setText(f"{days}d, {hours:02}h, {minutes:02}m, {seconds:02}s") + + + class LedScriptTableModel(QAbstractTableModel): """ A table model for the led script. @@ -84,6 +134,9 @@ class LedScriptViewer(QWidget): self.w_scroll.setWidgetResizable(True) self.w_scroll.setFixedHeight(200) + + self.w_time_left = TimeLeft(self) + self.l_vbox.addWidget(self.w_time_left) self.l_vbox.addStretch(1) def set_script(self, led_script: LedScript): @@ -91,5 +144,10 @@ class LedScriptViewer(QWidget): self.w_table.setModel(self.model) self.w_table.show() - def set_relative_time(self, time: float): + def set_relative_time(self, t: float): pass + raise NotImplementedError() + + def update_time(self, t_now: float): + self.w_time_left.update_time(t_now) +