Added saving and loading of measurements.

This commit is contained in:
jupfi 2023-12-16 09:55:30 +01:00
parent bfdbe796fb
commit 12b72fe263
3 changed files with 70 additions and 0 deletions

View file

@ -1,4 +1,5 @@
import logging
import json
from PyQt6.QtCore import pyqtSlot, pyqtSignal
from PyQt6.QtWidgets import QWidget
from nqrduck.module.module_controller import ModuleController
@ -100,3 +101,43 @@ class MeasurementController(ModuleController):
):
logger.debug("Received set averages failure.")
self.set_averages_failure.emit()
def save_measurement(self, file_name : str) -> None:
"""Save measurement to file.
Args:
file_name (str): Path to file.
"""
logger.debug("Saving measurement.")
if not self.module.model.measurements:
logger.debug("No measurement to save.")
return
measurement = self.module.model.measurements[-1].to_json()
with open(file_name, "w") as f:
json.dump(measurement, f)
def load_measurement(self, file_name: str) -> None:
"""Load measurement from file.
Args:
file_name (str): Path to file.
"""
logger.debug("Loading measurement.")
try:
with open(file_name, "r") as f:
measurement = Measurement.from_json(json.load(f))
self.module.model.add_measurement(measurement)
self.module.model.displayed_measurement = measurement
except FileNotFoundError:
logger.debug("File not found.")
self.module.nqrduck_signal.emit(
"notification", ["Error", "File not found."]
)
except (json.JSONDecodeError, KeyError):
logger.debug("File is not a valid measurement file.")
self.module.nqrduck_signal.emit(
"notification", ["Error", "File is not a valid measurement file."]
)

View file

@ -7,6 +7,7 @@ logger = logging.getLogger(__name__)
class MeasurementModel(ModuleModel):
FILE_EXTENSION = ".meas"
# This constants are used to determine which view is currently displayed.
FFT_VIEW = "fft"
TIME_VIEW = "time"

View file

@ -72,6 +72,11 @@ class MeasurementView(ModuleView):
self._ui_form.importButton.setIcon(Logos.Load16x16())
self._ui_form.importButton.setIconSize(self._ui_form.importButton.size())
# Connect measurement save and load buttons
self._ui_form.exportButton.clicked.connect(self.on_measurement_save_button_clicked)
self._ui_form.importButton.clicked.connect(self.on_measurement_load_button_clicked)
def init_plotter(self) -> None:
"""Initialize plotter with the according units for time domain."""
plotter = self._ui_form.plotter
@ -164,6 +169,27 @@ class MeasurementView(ModuleView):
logger.debug("Set averages failure.")
self._ui_form.averagesEdit.setStyleSheet("border: 1px solid red;")
@pyqtSlot()
def on_measurement_save_button_clicked(self) -> None:
"""Slot for when the measurement save button is clicked."""
logger.debug("Measurement save button clicked.")
file_manager = self.QFileManager(self.module.model.FILE_EXTENSION, parent=self.widget)
file_name = file_manager.saveFileDialog()
if file_name:
self.module.controller.save_measurement(file_name)
@pyqtSlot()
def on_measurement_load_button_clicked(self) -> None:
"""Slot for when the measurement load button is clicked."""
logger.debug("Measurement load button clicked.")
file_manager = self.QFileManager(self.module.model.FILE_EXTENSION, parent=self.widget)
file_name = file_manager.loadFileDialog()
if file_name:
self.module.controller.load_measurement(file_name)
class MeasurementDialog(QDialog):
""" This Dialog is shown when the measurement is started and therefore blocks the main window.
It shows the duck animation and a message."""
@ -195,3 +221,5 @@ class MeasurementView(ModuleView):
continue
self.spinner_movie.stop()
super().hide()