2024-11-18 18:28:26 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2024-11-19 17:52:08 +01:00
|
|
|
from PyQt6.QtWidgets import QWidget, QPlainTextEdit, QFileDialog, QVBoxLayout
|
2024-11-18 18:28:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FileActions:
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def open_files(self):
|
|
|
|
dialog = QFileDialog(self.app.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():
|
2024-11-19 18:13:41 +01:00
|
|
|
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-18 18:28:26 +01:00
|
|
|
|
2024-11-19 18:32:52 +01:00
|
|
|
def save_file(self, path):
|
|
|
|
oz_string = self.app.open_files[path].content_binary_input.toPlainText()
|
|
|
|
data = self.app.utils.oz_string_to_bstring(oz_string)
|
|
|
|
|
|
|
|
file = open(path, "wb")
|
|
|
|
file.write(data)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# the widget that contains all the file content
|
2024-11-19 18:32:52 +01:00
|
|
|
self.content_widget = QWidget(self.app.main_window.openFileTabs, objectName=path)
|
2024-11-19 17:52:08 +01:00
|
|
|
self.content_widget_layout = QVBoxLayout()
|
|
|
|
|
|
|
|
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_widget.setLayout(self.content_widget_layout)
|
2024-11-18 18:28:26 +01:00
|
|
|
|
|
|
|
self.app.main_window.openFileTabs.addTab( # add a tab for the file in the top files list
|
|
|
|
self.content_widget,
|
|
|
|
self.name
|
|
|
|
)
|