Implemented a warning if the user tries to close a file that was not saved.
This commit is contained in:
parent
56e16dfb8b
commit
0a17c54d85
3 changed files with 48 additions and 3 deletions
27
file.py
27
file.py
|
@ -27,17 +27,33 @@ class FileActions:
|
||||||
file.write(data)
|
file.write(data)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
self.app.open_files[path].not_saved = False
|
||||||
|
|
||||||
def save_current_file(self):
|
def save_current_file(self):
|
||||||
current_tab = self.app.main_window.openFileTabs.currentWidget()
|
current_tab = self.app.main_window.openFileTabs.currentWidget()
|
||||||
current_file_path = current_tab.objectName()
|
current_file_path = current_tab.objectName()
|
||||||
|
|
||||||
self.save_file(current_file_path)
|
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):
|
def close_current_file(self):
|
||||||
tab_index = self.app.main_window.openFileTabs.currentIndex()
|
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()#
|
||||||
current_file_path = self.app.main_window.openFileTabs.currentWidget().objectName()
|
|
||||||
del self.app.open_files[current_file_path]
|
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:
|
class File:
|
||||||
|
@ -46,6 +62,7 @@ class File:
|
||||||
|
|
||||||
self.path = path
|
self.path = path
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.not_saved = False
|
||||||
|
|
||||||
file = open(path, "rb")
|
file = open(path, "rb")
|
||||||
file_content = file.read()
|
file_content = file.read()
|
||||||
|
@ -60,6 +77,7 @@ class File:
|
||||||
self.content_binary_input = QPlainTextEdit(self.content_widget)
|
self.content_binary_input = QPlainTextEdit(self.content_widget)
|
||||||
self.content_widget_layout.addWidget(self.content_binary_input)
|
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.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)
|
self.content_widget.setLayout(self.content_widget_layout)
|
||||||
|
|
||||||
|
@ -67,3 +85,6 @@ class File:
|
||||||
self.content_widget,
|
self.content_widget,
|
||||||
self.name
|
self.name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def on_edit(self):
|
||||||
|
self.not_saved = True
|
||||||
|
|
2
main.py
2
main.py
|
@ -25,6 +25,8 @@ class BreadEditor:
|
||||||
connect_gui(self)
|
connect_gui(self)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
self.utils.popup_init()
|
||||||
|
|
||||||
self.QTMainWindow.show()
|
self.QTMainWindow.show()
|
||||||
sys.exit(self.qt_app.exec())
|
sys.exit(self.qt_app.exec())
|
||||||
|
|
||||||
|
|
22
utils.py
22
utils.py
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from PyQt6.QtWidgets import QMessageBox
|
||||||
|
|
||||||
|
|
||||||
class Utils:
|
class Utils:
|
||||||
|
@ -9,6 +10,27 @@ class Utils:
|
||||||
|
|
||||||
self.home_path = str(Path.home())
|
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)
|
def bstring_to_oz(self, data): # convert binary data to a string of ones and zeros (oz)
|
||||||
oz_bytes = []
|
oz_bytes = []
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue