more work

This commit is contained in:
Dominik Demuth 2024-01-01 12:08:17 +01:00
parent 09f366f0ba
commit c55a983b54
3 changed files with 35 additions and 31 deletions

View File

@ -24,20 +24,27 @@ app = App(['Team Rocket FTW!'])
from gui_qt.main.mainwindow import NMRMainWindow from gui_qt.main.mainwindow import NMRMainWindow
from gui_qt.lib.backup import BackupManager from gui_qt.lib.backup import BackupManager
def do_autosave():
success = mplQt.autosave()
if success:
backuping.update_last_save()
backuping = BackupManager() backuping = BackupManager()
pid = os.getpid()
files = backuping.search_unsaved() files = backuping.search_unsaved()
print(files) print(files)
print(sys.executable, sys.argv)
pid = os.getpid()
bck_name = backuping.create_entry(pid) bck_name = backuping.create_entry(pid)
mplQt = NMRMainWindow(bck_file=bck_name) mplQt = NMRMainWindow(bck_file=bck_name)
print(sys.executable, sys.argv) do_autosave()
timer = QtCore.QTimer()
timer.timeout.connect(mplQt._autosave) timer = QtCore.QTimer()
timer.timeout.connect(backuping.update_last_save) timer.timeout.connect(do_autosave)
timer.start(3 * 1000) timer.start(3 * 1000)
app.aboutToQuit.connect(backuping.close) app.aboutToQuit.connect(backuping.close)

View File

@ -39,20 +39,28 @@ class BackupManager(QtCore.QObject):
pid = self._pid pid = self._pid
con = sqlite3.connect(DB_FILE) con = sqlite3.connect(DB_FILE)
cursor = con.execute('DELETE FROM sessions WHERE pid = ?;', (pid,)) cursor = con.cursor()
res = cursor.execute('DELETE FROM sessions WHERE pid = ?;', (pid,))
con.commit()
self.delete_db_if_empty()
def delete_db_if_empty(self):
con = sqlite3.connect(DB_FILE)
cursor = con.cursor()
res = cursor.execute('SELECT COUNT(sessions.pid) FROM sessions GROUP BY sessions.pid;') res = cursor.execute('SELECT COUNT(sessions.pid) FROM sessions GROUP BY sessions.pid;')
con.commit() con.commit()
remaining_processes = res.fetchone() remaining_processes = res.fetchone()
con.close() con.close()
if remaining_processes is None: if remaining_processes is None:
self.delete_db() Path(DB_FILE).unlink()
def delete_db(self):
Path(DB_FILE).unlink()
def close(self): def close(self):
self.remove_entry() self.remove_entry()
self.delete_db_if_empty()
def search_unsaved(self): def search_unsaved(self):
con = sqlite3.connect(DB_FILE) con = sqlite3.connect(DB_FILE)
@ -70,7 +78,9 @@ class BackupManager(QtCore.QObject):
except ProcessLookupError: except ProcessLookupError:
missing_processes.append((pid, fname)) missing_processes.append((pid, fname))
return missing_processes if missing_processes:
msg = QtWidgets.QMessageBox()
msg.exec()
def update_last_save(self): def update_last_save(self):
con = sqlite3.connect(DB_FILE) con = sqlite3.connect(DB_FILE)

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import datetime import datetime
import os import os
import pathlib
import re import re
from pathlib import Path from pathlib import Path
@ -54,7 +55,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self.management = UpperManagement(self) self.management = UpperManagement(self)
self.fitlimitvalues = [None, None] self.fitlimitvalues = [None, None]
self.fitpreview = [] self.fitpreview = []
self._fit_plot_id = None self._fit_plot_id = None
@ -76,23 +76,14 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self._init_gui() self._init_gui()
self._init_signals() self._init_signals()
if os.getenv('APPIMAGE') is not None:
if Updater.get_update_information(os.getenv('APPIMAGE'))[0]:
self.look_for_update()
# self.check_for_backup()
self.__timer = QtCore.QTimer()
self.__backup_path = config_paths() / f'{datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")}.nmr'
self.__timer.start(3*60*1000) # every three minutes
self.__timer.timeout.connect(self._autosave)
self.fit_timer = QtCore.QTimer() self.fit_timer = QtCore.QTimer()
self.fit_timer.setInterval(500) self.fit_timer.setInterval(500)
self.fit_timer.timeout.connect( self.fit_timer.timeout.connect(
lambda: self.status.setText(f'Fit running... ({self.management.fitter.step} evaluations)') lambda: self.status.setText(f'Fit running... ({self.management.fitter.step} evaluations)')
) )
self.__backup_path = pathlib.Path(bck_file)
def _init_gui(self): def _init_gui(self):
self.setupUi(self) self.setupUi(self)
make_action_icons(self) make_action_icons(self)
@ -110,8 +101,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self.fitlim_button.setIcon(get_icon('fit_region')) self.fitlim_button.setIcon(get_icon('fit_region'))
self.toolBar_fit.addWidget(self.fitlim_button) self.toolBar_fit.addWidget(self.fitlim_button)
# self.area.dragEnterEvent = self.dragEnterEvent
while self.tabWidget.count() > 2: while self.tabWidget.count() > 2:
self.tabWidget.removeTab(self.tabWidget.count()-1) self.tabWidget.removeTab(self.tabWidget.count()-1)
@ -166,6 +155,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self.menuData.insertAction(self.actionRedo, self.actionUndo) self.menuData.insertAction(self.actionRedo, self.actionUndo)
self.action_save_fit_parameter.triggered.connect(self.save_fit_parameter) self.action_save_fit_parameter.triggered.connect(self.save_fit_parameter)
# noinspection PyUnresolvedReferences
self.ac_group2.triggered.connect(self.change_fit_limits) self.ac_group2.triggered.connect(self.change_fit_limits)
self.t1action.triggered.connect(lambda: self._show_tab('t1_temp')) self.t1action.triggered.connect(lambda: self._show_tab('t1_temp'))
@ -1093,11 +1083,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
def close(self): def close(self):
write_state({'History': {'recent path': str(self.path)}}) write_state({'History': {'recent path': str(self.path)}})
# remove backup file when closing
self.__backup_path.unlink(missing_ok=True)
print('Close me', self.sender())
super().close() super().close()
def read_state(self): def read_state(self):
@ -1121,7 +1106,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
QLog(parent=self).show() QLog(parent=self).show()
def _autosave(self): def autosave(self) -> bool:
# TODO better separate thread may it takes some time to save # TODO better separate thread may it takes some time to save
self.status.setText('Autosave...') self.status.setText('Autosave...')
success = NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path.with_suffix('.nmr.0')) success = NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path.with_suffix('.nmr.0'))
@ -1130,6 +1115,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self.status.setText('') self.status.setText('')
return success
@QtCore.pyqtSlot(name='on_actionCreate_starter_triggered') @QtCore.pyqtSlot(name='on_actionCreate_starter_triggered')
def create_starter(self): def create_starter(self):
make_starter(os.getenv('APPIMAGE')) make_starter(os.getenv('APPIMAGE'))