Bread_Editor/file.py

80 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2024-11-21 17:22:59 +01:00
from PyQt6.QtWidgets import QFileDialog
from editor import BitEditor
class FileActions:
def __init__(self, app):
self.app = app
def open_files(self):
dialog = QFileDialog(self.app.gui.QTMainWindow)
dialog.setDirectory(self.app.utils.home_path)
dialog.setFileMode(QFileDialog.FileMode.ExistingFiles)
dialog.setNameFilters(["Binary (*.bin)", "Any (*)"])
dialog.setViewMode(QFileDialog.ViewMode.List)
if dialog.exec():
for file_path in dialog.selectedFiles():
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])
2024-11-19 18:32:52 +01:00
def save_file(self, path):
2024-11-21 17:22:59 +01:00
oz_string = self.app.open_files[path].bit_editor.input.toPlainText()
2024-11-19 18:32:52 +01:00
data = self.app.utils.oz_string_to_bstring(oz_string)
file = open(path, "wb")
file.write(data)
file.close()
2024-11-21 17:22:59 +01:00
self.app.open_files[path].bit_editor.not_saved = False
2024-11-19 18:32:52 +01:00
def save_current_file(self):
current_tab = self.app.gui.main_window.openFileTabs.currentWidget()
2024-11-19 18:32:52 +01:00
current_file_path = current_tab.objectName()
self.save_file(current_file_path)
def close_file(self, path, tab_index):
self.app.gui.main_window.openFileTabs.removeTab(tab_index)
del self.app.open_files[path]
2024-11-19 19:05:28 +01:00
def close_current_file(self):
tab_index = self.app.gui.main_window.openFileTabs.currentIndex()
2024-11-21 17:22:59 +01:00
current_file_path = self.app.gui.main_window.openFileTabs.currentWidget().objectName()
2024-11-21 17:22:59 +01:00
if self.app.open_files[current_file_path].bit_editor.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)
2024-11-19 19:05:28 +01:00
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):
self.app = app
self.path = path
self.name = name
file = open(path, "rb")
file_content = file.read()
file.close()
self.content = file_content
2024-11-21 17:22:59 +01:00
self.bit_editor = BitEditor(self.app, self)