From 782ada62d56c63c85f5c834575ad0ebe4dc1e305 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Wed, 20 Nov 2024 13:45:49 +0100 Subject: [PATCH] Now the editor even warns when it gets closed and there are any unsaved files. --- connect_gui.py | 1 + file.py | 6 ++++++ utils.py | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/connect_gui.py b/connect_gui.py index 6d0bbab..4c21ab9 100644 --- a/connect_gui.py +++ b/connect_gui.py @@ -4,3 +4,4 @@ def connect_gui(app): app.main_window.openFile.triggered.connect(app.file_actions.open_files) app.main_window.saveFile.triggered.connect(app.file_actions.save_current_file) app.main_window.openFileTabs.tabCloseRequested.connect(app.file_actions.close_current_file) + app.QTMainWindow.closeEvent = app.utils.on_close diff --git a/file.py b/file.py index bc3854c..ad833cf 100644 --- a/file.py +++ b/file.py @@ -55,6 +55,12 @@ class FileActions: self.close_file(current_file_path, tab_index) + def save_all_files(self): + for file_path in self.app.open_files: + file = self.app.open_files[file_path] + + self.save_file(file.path) + class File: def __init__(self, app, path, name): diff --git a/utils.py b/utils.py index f89318d..1642356 100644 --- a/utils.py +++ b/utils.py @@ -52,3 +52,25 @@ class Utils: binary_string = bytes(bytes_int) return binary_string + + def on_close(self, event): + changes = False + + for file_path in self.app.open_files: + file = self.app.open_files[file_path] + + if file.not_saved: + changes = True + break + + if changes: + save_or_not = self.unsaved_changes_popup() + + match save_or_not: + case "save": + self.app.file_actions.save_all_files() + + case "cancel": + event.ignore() + + print("Bye!")