2024-11-18 18:28:26 +01:00
|
|
|
#!/usr/bin/python3
|
2024-11-30 20:50:53 +01:00
|
|
|
import os.path
|
2024-11-18 18:28:26 +01:00
|
|
|
|
2024-11-30 22:32:18 +01:00
|
|
|
from PyQt6.QtWidgets import QFileDialog, QTabWidget
|
2024-11-21 17:22:59 +01:00
|
|
|
from editor import BitEditor
|
2024-11-18 18:28:26 +01:00
|
|
|
|
2024-11-30 20:50:53 +01:00
|
|
|
MAX_FILE_SIZE = 262144 # 2^18
|
|
|
|
|
2024-11-18 18:28:26 +01:00
|
|
|
|
|
|
|
class FileActions:
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def open_files(self):
|
2024-11-21 16:56:08 +01:00
|
|
|
dialog = QFileDialog(self.app.gui.QTMainWindow)
|
2024-11-30 21:47:29 +01:00
|
|
|
dialog.setWindowTitle("Open File")
|
2024-11-18 18:28:26 +01:00
|
|
|
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():
|
2024-11-19 18:13:41 +01:00
|
|
|
if not file_path in self.app.open_files: # dont open file twice
|
2024-11-30 20:50:53 +01:00
|
|
|
if os.path.getsize(file_path) > MAX_FILE_SIZE:
|
|
|
|
self.app.utils.ftb_popup.exec()
|
|
|
|
return
|
|
|
|
|
2024-11-19 18:13:41 +01:00
|
|
|
self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1])
|
2024-11-18 18:28:26 +01:00
|
|
|
|
2024-11-30 22:38:45 +01:00
|
|
|
def create_file(self, content: bin=b""):
|
2024-11-30 21:58:02 +01:00
|
|
|
file_path, extension = QFileDialog.getSaveFileName(
|
|
|
|
caption="New File",
|
|
|
|
directory=self.app.utils.home_path,
|
|
|
|
filter="Binary (*.bin);;Any (*)",
|
|
|
|
)
|
|
|
|
|
2024-11-30 22:38:45 +01:00
|
|
|
if file_path == "":
|
|
|
|
return
|
|
|
|
|
2024-11-30 21:58:02 +01:00
|
|
|
if "Binary" in extension:
|
|
|
|
file_path = file_path.split(".")[0] + ".bin" # make sure it has the right extension
|
|
|
|
|
|
|
|
file = open(file_path, "bw") # create new empty file
|
2024-11-30 22:20:49 +01:00
|
|
|
file.write(content)
|
2024-11-30 21:58:02 +01:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
self.app.open_files[file_path] = File(self.app, file_path, file_path.split("/")[-1]) # open file
|
|
|
|
|
2024-11-19 18:32:52 +01:00
|
|
|
def save_current_file(self):
|
2024-11-21 16:56:08 +01:00
|
|
|
current_tab = self.app.gui.main_window.openFileTabs.currentWidget()
|
2024-11-19 18:32:52 +01:00
|
|
|
current_file_path = current_tab.objectName()
|
|
|
|
|
2024-11-21 17:34:44 +01:00
|
|
|
self.app.open_files[current_file_path].save()
|
2024-11-20 13:05:24 +01:00
|
|
|
|
2024-11-30 22:20:49 +01:00
|
|
|
def save_current_file_as(self):
|
|
|
|
current_tab = self.app.gui.main_window.openFileTabs.currentWidget() # get currently open file
|
|
|
|
current_file_path = current_tab.objectName()
|
|
|
|
file = self.app.open_files[current_file_path]
|
|
|
|
|
|
|
|
oz_content = file.bit_editor.input.toPlainText() # convert user input to binary data
|
|
|
|
file_content = self.app.utils.oz_string_to_bstring(oz_content)
|
|
|
|
|
|
|
|
self.create_file(file_content)
|
|
|
|
|
2024-11-19 19:05:28 +01:00
|
|
|
def close_current_file(self):
|
2024-11-21 17:22:59 +01:00
|
|
|
current_file_path = self.app.gui.main_window.openFileTabs.currentWidget().objectName()
|
2024-11-20 13:05:24 +01:00
|
|
|
|
2024-11-21 17:22:59 +01:00
|
|
|
if self.app.open_files[current_file_path].bit_editor.not_saved:
|
2024-11-20 13:05:24 +01:00
|
|
|
save_or_not = self.app.utils.unsaved_changes_popup()
|
|
|
|
|
|
|
|
match save_or_not:
|
|
|
|
case "save":
|
2024-11-21 17:34:44 +01:00
|
|
|
self.app.open_files[current_file_path].save()
|
2024-11-20 13:05:24 +01:00
|
|
|
|
|
|
|
case "cancel":
|
|
|
|
return
|
|
|
|
|
2024-11-21 17:34:44 +01:00
|
|
|
self.app.open_files[current_file_path].close()
|
2024-11-19 19:05:28 +01:00
|
|
|
|
2024-11-20 13:45:49 +01:00
|
|
|
def save_all_files(self):
|
|
|
|
for file_path in self.app.open_files:
|
2024-11-21 17:34:44 +01:00
|
|
|
self.app.open_files[file_path].save()
|
2024-11-20 13:45:49 +01:00
|
|
|
|
2024-11-18 18:28:26 +01:00
|
|
|
|
|
|
|
class File:
|
|
|
|
def __init__(self, app, path, name):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
self.path = path
|
|
|
|
self.name = name
|
|
|
|
|
2024-11-19 17:52:08 +01:00
|
|
|
file = open(path, "rb")
|
2024-11-18 18:28:26 +01:00
|
|
|
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)
|
2024-11-21 17:34:44 +01:00
|
|
|
|
2024-11-21 19:47:15 +01:00
|
|
|
self.app.settings.last_opened_file = self.path
|
|
|
|
|
2024-11-30 22:32:18 +01:00
|
|
|
self.app.gui.main_window.openFileTabs.setCurrentIndex(self.app.gui.main_window.openFileTabs.count() - 1)
|
|
|
|
|
2024-11-21 17:34:44 +01:00
|
|
|
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):
|
2024-11-30 22:10:41 +01:00
|
|
|
oz_string = self.bit_editor.input.toPlainText()
|
2024-11-21 17:34:44 +01:00
|
|
|
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
|