From 0a17c54d85fba56e590b9e3f8b50c298aa3eb951 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Wed, 20 Nov 2024 13:05:24 +0100 Subject: [PATCH] Implemented a warning if the user tries to close a file that was not saved. --- file.py | 27 ++++++++++++++++++++++++--- main.py | 2 ++ utils.py | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/file.py b/file.py index 40882e6..bc3854c 100644 --- a/file.py +++ b/file.py @@ -27,17 +27,33 @@ class FileActions: file.write(data) file.close() + self.app.open_files[path].not_saved = False + def save_current_file(self): current_tab = self.app.main_window.openFileTabs.currentWidget() current_file_path = current_tab.objectName() self.save_file(current_file_path) + def close_file(self, path, tab_index): + self.app.main_window.openFileTabs.removeTab(tab_index) + del self.app.open_files[path] + def close_current_file(self): tab_index = self.app.main_window.openFileTabs.currentIndex() - self.app.main_window.openFileTabs.removeTab(tab_index) - current_file_path = self.app.main_window.openFileTabs.currentWidget().objectName() - del self.app.open_files[current_file_path] + current_file_path = self.app.main_window.openFileTabs.currentWidget().objectName()# + + if self.app.open_files[current_file_path].not_saved: + save_or_not = self.app.utils.unsaved_changes_popup() + + match save_or_not: + case "save": + self.save_file(current_file_path) + + case "cancel": + return + + self.close_file(current_file_path, tab_index) class File: @@ -46,6 +62,7 @@ class File: self.path = path self.name = name + self.not_saved = False file = open(path, "rb") file_content = file.read() @@ -60,6 +77,7 @@ class File: self.content_binary_input = QPlainTextEdit(self.content_widget) self.content_widget_layout.addWidget(self.content_binary_input) self.content_binary_input.setPlainText(self.app.utils.bstring_to_oz(file_content)) + self.content_binary_input.textChanged.connect(self.on_edit) self.content_widget.setLayout(self.content_widget_layout) @@ -67,3 +85,6 @@ class File: self.content_widget, self.name ) + + def on_edit(self): + self.not_saved = True diff --git a/main.py b/main.py index 9ca6e73..76c7419 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,8 @@ class BreadEditor: connect_gui(self) def run(self): + self.utils.popup_init() + self.QTMainWindow.show() sys.exit(self.qt_app.exec()) diff --git a/utils.py b/utils.py index 0a802f7..f89318d 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 from pathlib import Path +from PyQt6.QtWidgets import QMessageBox class Utils: @@ -9,6 +10,27 @@ class Utils: self.home_path = str(Path.home()) + def popup_init(self): + self.usc_popup = QMessageBox() # create a popup window that notifies the user that we have unsaved changes + self.usc_popup.setWindowTitle("Unsaved Changes!") # usc means unsaved changes + self.usc_popup.setText("The file you are trying to close has unsaved changes.") + self.usc_popup.setStandardButtons( + QMessageBox.StandardButton.Save | + QMessageBox.StandardButton.Discard | + QMessageBox.StandardButton.Cancel + ) + + def unsaved_changes_popup(self): + button = self.usc_popup.exec() + + match button: + case QMessageBox.StandardButton.Save: + return "save" + case QMessageBox.StandardButton.Discard: + return "discard" + case QMessageBox.StandardButton.Cancel: + return "cancel" + def bstring_to_oz(self, data): # convert binary data to a string of ones and zeros (oz) oz_bytes = []