From 12dacb73b3195d79bf4db150de9c0ac758644434 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Tue, 16 May 2023 20:05:55 +0200 Subject: [PATCH] add QMessageBox to display existing backup files --- src/gui_qt/main/mainwindow.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/gui_qt/main/mainwindow.py b/src/gui_qt/main/mainwindow.py index 8f0ce3f..4a6f92a 100644 --- a/src/gui_qt/main/mainwindow.py +++ b/src/gui_qt/main/mainwindow.py @@ -75,6 +75,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): 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 @@ -1082,6 +1084,33 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path) 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'))