From 09f366f0bac03cc1c7ca86a254f8731063c83353 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 1 Jan 2024 11:25:40 +0100 Subject: [PATCH] more work --- bin/evaluate.py | 13 +++++++-- src/gui_qt/lib/backup.py | 55 ++++++++++++++++++++++++++++++----- src/gui_qt/main/mainwindow.py | 46 +++++++---------------------- 3 files changed, 68 insertions(+), 46 deletions(-) diff --git a/bin/evaluate.py b/bin/evaluate.py index a0870c3..a06eb99 100755 --- a/bin/evaluate.py +++ b/bin/evaluate.py @@ -17,24 +17,31 @@ from nmreval.lib.logger import handle_exception sys.excepthook = handle_exception from gui_qt import App +from gui_qt.Qt import QtCore app = App(['Team Rocket FTW!']) from gui_qt.main.mainwindow import NMRMainWindow -from gui_qt.lib.backup import Backup +from gui_qt.lib.backup import BackupManager -backuping = Backup() +backuping = BackupManager() pid = os.getpid() files = backuping.search_unsaved() print(files) bck_name = backuping.create_entry(pid) +mplQt = NMRMainWindow(bck_file=bck_name) + print(sys.executable, sys.argv) +timer = QtCore.QTimer() + +timer.timeout.connect(mplQt._autosave) +timer.timeout.connect(backuping.update_last_save) +timer.start(3 * 1000) app.aboutToQuit.connect(backuping.close) -mplQt = NMRMainWindow() mplQt.show() sys.exit(app.exec()) diff --git a/src/gui_qt/lib/backup.py b/src/gui_qt/lib/backup.py index 5fa19ae..2e6eb2c 100644 --- a/src/gui_qt/lib/backup.py +++ b/src/gui_qt/lib/backup.py @@ -1,12 +1,15 @@ import os import sqlite3 +from datetime import datetime +from pathlib import Path -from ..Qt import QtCore +from nmreval.configs import config_paths +from ..Qt import QtCore, QtWidgets DB_FILE = '/autohome/dominik/nmreval/nmreval.db' -class Backup(QtCore.QObject): +class BackupManager(QtCore.QObject): def __init__(self): super().__init__() self.create_table() @@ -15,18 +18,21 @@ class Backup(QtCore.QObject): def create_table(self): con = sqlite3.connect(DB_FILE) con.execute( - "CREATE TABLE IF NOT EXISTS sessions (pid INTEGER NOT NULL, backup_file TEXT NOT NULL );" + "CREATE TABLE IF NOT EXISTS sessions " + "(pid INTEGER NOT NULL, backup_file TEXT NOT NULL, last_save INTEGER);" ) def create_entry(self, pid: int): + backup_path = config_paths() / f'{datetime.now().strftime("%Y-%m-%d_%H%M%S")}.nmr' + con = sqlite3.connect(DB_FILE) - con.execute('INSERT INTO sessions VALUES(?, ?);', - (pid, f'/autohome/dominik/nmreval/tmp_{pid}.nmr')) + con.execute('INSERT INTO sessions VALUES(?, ?, ?);', + (pid, str(backup_path), None)) con.commit() con.close() self._pid = pid - return + return backup_path def remove_entry(self, pid: int = None): if pid is None: @@ -36,10 +42,15 @@ class Backup(QtCore.QObject): cursor = con.execute('DELETE FROM sessions WHERE pid = ?;', (pid,)) res = cursor.execute('SELECT COUNT(sessions.pid) FROM sessions GROUP BY sessions.pid;') con.commit() - print(res.fetchall()) - + remaining_processes = res.fetchone() con.close() + if remaining_processes is None: + self.delete_db() + + def delete_db(self): + Path(DB_FILE).unlink() + def close(self): self.remove_entry() @@ -61,3 +72,31 @@ class Backup(QtCore.QObject): return missing_processes + def update_last_save(self): + con = sqlite3.connect(DB_FILE) + con.execute( + 'UPDATE sessions SET last_save = ? WHERE pid = ?', + (datetime.now(), self._pid) + ) + con.commit() + con.close() + # + # def check_for_backup(self): + # backup_by_date = sorted(backups, key=lambda x: x[1]) + # msg = (QtWidgets.QMessageBox() + # , 'Backup found',) + # f'{len(backups)} backup files in directory {backup_by_date[-1][0].parent} found.\n\n' + # f'Latest backup date: {backup_by_date[-1][1]}\n\n' + # f'Press Ok to load, Cancel to delete backup, Close to do nothing.', + # QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel | QtWidgets.QMessageBox.Close + # ) + # + # if msg == QtWidgets.QMessageBox.Ok: + # self.management.load_files([str(backup_by_date[-1][0])]) + # backup_by_date[-1][0].unlink() + # elif msg == QtWidgets.QMessageBox.Cancel: + # backup_by_date[-1][0].unlink() + # else: + # pass + # + diff --git a/src/gui_qt/main/mainwindow.py b/src/gui_qt/main/mainwindow.py index 0d5f0c0..a35b9c2 100644 --- a/src/gui_qt/main/mainwindow.py +++ b/src/gui_qt/main/mainwindow.py @@ -22,7 +22,6 @@ from ..graphs.graphwindow import QGraphWindow from ..graphs.movedialog import QMover from ..io.fcbatchreader import QFCReader from ..io.filedialog import * -from ..lib.backup import Backup from ..lib.iconloading import make_action_icons, get_icon from ..lib.pg_objects import RegionItem from ..lib.starter import make_starter @@ -43,7 +42,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): save_ses_sig = QtCore.pyqtSignal(str) rest_ses_sig = QtCore.pyqtSignal(str) - def __init__(self, parents=None, path=None): + def __init__(self, parents=None, path=None, bck_file=None): super().__init__(parent=parents) if path is None: @@ -81,7 +80,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): if Updater.get_update_information(os.getenv('APPIMAGE'))[0]: self.look_for_update() - self.check_for_backup() + # 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' @@ -133,12 +132,15 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): self.fitregion = RegionItem() self._fit_plot_id = None - self.setGeometry(QtWidgets.QStyle.alignedRect(QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter, - self.size(), QtWidgets.qApp.desktop().availableGeometry())) + self.setGeometry(QtWidgets.QStyle.alignedRect( + QtCore.Qt.LayoutDirection.LeftToRight, + QtCore.Qt.AlignmentFlag.AlignCenter, + self.size(), + QtWidgets.qApp.desktop().availableGeometry()), + ) self.datawidget.management = self.management self.integralwidget.management = self.management - # self.drawingswidget.graphs = self.management.graphs self.ac_group = QtWidgets.QActionGroup(self) self.ac_group.addAction(self.action_lm_fit) @@ -992,7 +994,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): self.editor = QUsermodelEditor(config_paths() / 'usermodels.py', parent=self) self.editor.modelsChanged.connect(lambda: self.fit_dialog.read_and_load_functions()) - self.editor.setWindowModality(QtCore.Qt.ApplicationModal) + self.editor.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal) self.editor.show() @QtCore.pyqtSlot(list) @@ -1057,13 +1059,13 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): if self.sender() == self.actionLife: from ..lib.gol import QGameOfLife game = QGameOfLife(parent=self) - game.setWindowModality(QtCore.Qt.NonModal) + game.setWindowModality(QtCore.Qt.WindowModality.NonModal) game.show() elif self.sender() == self.actionMine: from ..lib.stuff import QMines game = QMines(parent=self) - game.setWindowModality(QtCore.Qt.NonModal) + game.setWindowModality(QtCore.Qt.WindowModality.NonModal) game.show() else: @@ -1128,32 +1130,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): self.status.setText('') - def check_for_backup(self): - backups = [] - for filename in config_paths().glob('*.nmr'): - try: - backups.append((filename, datetime.datetime.strptime(filename.stem, "%Y-%m-%d_%H%M%S"))) - except ValueError: - continue - - if backups: - backup_by_date = sorted(backups, key=lambda x: x[1]) - msg = QtWidgets.QMessageBox.information( - self, 'Backup found', - f'{len(backups)} backup files in directory {backup_by_date[-1][0].parent} found.\n\n' - f'Latest backup date: {backup_by_date[-1][1]}\n\n' - f'Press Ok to load, Cancel to delete backup, Close to do nothing.', - QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel | QtWidgets.QMessageBox.Close - ) - - if msg == QtWidgets.QMessageBox.Ok: - self.management.load_files([str(backup_by_date[-1][0])]) - backup_by_date[-1][0].unlink() - elif msg == QtWidgets.QMessageBox.Cancel: - backup_by_date[-1][0].unlink() - else: - pass - @QtCore.pyqtSlot(name='on_actionCreate_starter_triggered') def create_starter(self): make_starter(os.getenv('APPIMAGE'))