Moved some functions from FileActions to File to make the code more OO

This commit is contained in:
The Wobbler 2024-11-21 17:34:44 +01:00
parent 4529529a7f
commit 4a1ec42656
2 changed files with 19 additions and 22 deletions

View file

@ -37,7 +37,7 @@ class BitEditor:
self.widget.setLayout(self.widget_layout) self.widget.setLayout(self.widget_layout)
self.app.gui.main_window.openFileTabs.addTab( # add a tab for the file in the top files list self.tab_index = self.app.gui.main_window.openFileTabs.addTab( # add a tab for the file in the top files list
self.widget, self.widget,
self.file.name self.file.name
) )

39
file.py
View file

@ -20,28 +20,13 @@ class FileActions:
if not file_path in self.app.open_files: # dont open file twice if not file_path in self.app.open_files: # dont open file twice
self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1]) self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1])
def save_file(self, path):
oz_string = self.app.open_files[path].bit_editor.input.toPlainText()
data = self.app.utils.oz_string_to_bstring(oz_string)
file = open(path, "wb")
file.write(data)
file.close()
self.app.open_files[path].bit_editor.not_saved = False
def save_current_file(self): def save_current_file(self):
current_tab = self.app.gui.main_window.openFileTabs.currentWidget() current_tab = self.app.gui.main_window.openFileTabs.currentWidget()
current_file_path = current_tab.objectName() current_file_path = current_tab.objectName()
self.save_file(current_file_path) self.app.open_files[current_file_path].save()
def close_file(self, path, tab_index):
self.app.gui.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.gui.main_window.openFileTabs.currentIndex()
current_file_path = self.app.gui.main_window.openFileTabs.currentWidget().objectName() current_file_path = self.app.gui.main_window.openFileTabs.currentWidget().objectName()
if self.app.open_files[current_file_path].bit_editor.not_saved: if self.app.open_files[current_file_path].bit_editor.not_saved:
@ -49,18 +34,16 @@ class FileActions:
match save_or_not: match save_or_not:
case "save": case "save":
self.save_file(current_file_path) self.app.open_files[current_file_path].save()
case "cancel": case "cancel":
return return
self.close_file(current_file_path, tab_index) self.app.open_files[current_file_path].close()
def save_all_files(self): def save_all_files(self):
for file_path in self.app.open_files: for file_path in self.app.open_files:
file = self.app.open_files[file_path] self.app.open_files[file_path].save()
self.save_file(file.path)
class File: class File:
@ -77,3 +60,17 @@ class File:
self.content = file_content self.content = file_content
self.bit_editor = BitEditor(self.app, self) self.bit_editor = BitEditor(self.app, self)
def close(self):
self.app.gui.main_window.openFileTabs.removeTab(self.bit_editor.tab_index)
del self.app.open_files[self.path]
def save(self):
oz_string = self.app.open_files[self.path].bit_editor.input.toPlainText()
data = self.app.utils.oz_string_to_bstring(oz_string)
file = open(self.path, "wb")
file.write(data)
file.close()
self.app.open_files[self.path].bit_editor.not_saved = False