From 43285b4bd5f4b097b213ad9c231c7518d1016204 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Wed, 5 Apr 2023 17:50:06 +0000 Subject: [PATCH] added first version of a backup system (#37) Co-authored-by: Dominik Demuth Reviewed-on: https://gitea.pkm.physik.tu-darmstadt.de/IPKM/nmreval/pulls/37 --- src/gui_qt/graphs/graphwindow.py | 9 ++++++++- src/gui_qt/main/mainwindow.py | 15 +++++++++++++++ src/gui_qt/main/management.py | 5 ++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/gui_qt/graphs/graphwindow.py b/src/gui_qt/graphs/graphwindow.py index 029d9ff..b0dee37 100644 --- a/src/gui_qt/graphs/graphwindow.py +++ b/src/gui_qt/graphs/graphwindow.py @@ -7,6 +7,7 @@ import uuid from math import isnan from pathlib import Path +import numpy as np from numpy import errstate, floor, log10 from pyqtgraph import GraphicsObject, getConfigOption, mkColor @@ -137,7 +138,13 @@ class QGraphWindow(QtWidgets.QGraphicsView, Ui_GraphWindow): r = self.plotItem.getViewBox().viewRange() for i in [0, 1]: if self.log[i]: - r[i] = tuple([10**x for x in r[i]]) + tmp = [np.nan, np.nan] + for j, x in enumerate(r[i]): + try: + tmp[j] = 10**x + except OverflowError: + pass + r[i] = tuple(tmp) else: r[i] = tuple(r[i]) diff --git a/src/gui_qt/main/mainwindow.py b/src/gui_qt/main/mainwindow.py index 444fd1f..a6cc098 100644 --- a/src/gui_qt/main/mainwindow.py +++ b/src/gui_qt/main/mainwindow.py @@ -1,5 +1,6 @@ from __future__ import annotations +import datetime import os import pathlib import re @@ -9,6 +10,7 @@ from numpy import geomspace, linspace from pyqtgraph import ViewBox from nmreval.configs import * +from nmreval.io.sessionwriter import NMRWriter from .management import UpperManagement from ..Qt import QtGui, QtPrintSupport @@ -73,6 +75,11 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): if Updater.get_update_information(os.getenv('APPIMAGE'))[0]: self.look_for_update() + self.__timer = QtCore.QTimer() + self.__savepath = config_paths() / f'{datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")}.nmr' + self.__timer.start(0.2*60*1000) # every three minutese + self.__timer.timeout.connect(self._autosave) + def _init_gui(self): self.setupUi(self) make_action_icons(self) @@ -1020,6 +1027,9 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): def close(self): write_state({'History': {'recent path': str(self.path)}}) + # remove backup file when closing + self.__savepath.unlink(missing_ok=True) + super().close() def read_state(self): @@ -1043,3 +1053,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): QLog(parent=self).show() + def _autosave(self): + # TODO better separate thread may it takes some time to save + self.status.setText('Autosave...') + NMRWriter(self.management.graphs, self.management.data).export(self.__savepath) + self.status.setText('') diff --git a/src/gui_qt/main/management.py b/src/gui_qt/main/management.py index 51a2b10..3f2ccc0 100644 --- a/src/gui_qt/main/management.py +++ b/src/gui_qt/main/management.py @@ -180,7 +180,10 @@ class UpperManagement(QtCore.QObject): graph.active = active graph.listWidget.blockSignals(True) for i, l in enumerate(g['in_legend']): - graph.listWidget.item(i).setCheckState(l) + try: + graph.listWidget.item(i).setCheckState(QtCore.Qt.Checked if l else QtCore.Qt.Unchecked) + except AttributeError: + pass graph.listWidget.blockSignals(False) # set unchecked in tree and hide/show in plot