OOPed everything a little more.

This commit is contained in:
The Wobbler 2024-11-21 17:22:59 +01:00
parent 3762d67663
commit 4529529a7f
2 changed files with 53 additions and 38 deletions

46
editor.py Normal file
View file

@ -0,0 +1,46 @@
#!/usr/bin/python3
from PyQt6.QtWidgets import QWidget, QVBoxLayout
from PyQt6.QtGui import QFont, QTextCharFormat, QColor
from binary_text_edit import BinaryTextEdit
from highlighting import Higlighter
class BitEditor:
font = QFont("Ubuntu Mono", 12)
font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, 200)
one_format = QTextCharFormat()
one_format.setBackground(QColor(200, 150, 100))
def __init__(self, app, file):
self.app = app
self.file = file
self.setup_gui()
def setup_gui(self):
# widget that contains the text input
self.widget = QWidget(self.app.gui.main_window.openFileTabs, objectName=self.file.path)
self.widget_layout = QVBoxLayout()
self.input = BinaryTextEdit()
self.input.setOverwriteMode(True)
self.widget_layout.addWidget(self.input)
self.input.setPlainText(self.app.utils.bstring_to_oz(self.file.content))
self.input.textChanged.connect(self.on_edit)
self.input.setFont(self.font)
self.bit_highlighter = Higlighter()
self.bit_highlighter.add_mapping("1", self.one_format)
self.bit_highlighter.setDocument(self.input.document())
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.widget,
self.file.name
)
def on_edit(self):
self.not_saved = True

45
file.py
View file

@ -1,9 +1,7 @@
#!/usr/bin/python3
from PyQt6.QtWidgets import QWidget, QFileDialog, QVBoxLayout
from PyQt6.QtGui import QFont, QTextCharFormat, QColor
from binary_text_edit import BinaryTextEdit
from highlighting import Higlighter
from PyQt6.QtWidgets import QFileDialog
from editor import BitEditor
class FileActions:
@ -23,14 +21,14 @@ class FileActions:
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].content_binary_input.toPlainText()
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].not_saved = False
self.app.open_files[path].bit_editor.not_saved = False
def save_current_file(self):
current_tab = self.app.gui.main_window.openFileTabs.currentWidget()
@ -44,9 +42,9 @@ class FileActions:
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].not_saved:
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:
@ -71,7 +69,6 @@ class File:
self.path = path
self.name = name
self.not_saved = False
file = open(path, "rb")
file_content = file.read()
@ -79,32 +76,4 @@ class File:
self.content = file_content
# the widget that contains all the file content
self.content_widget = QWidget(self.app.gui.main_window.openFileTabs, objectName=path)
self.content_widget_layout = QVBoxLayout()
self.content_binary_input = BinaryTextEdit()
self.content_binary_input.setOverwriteMode(True)
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.textChanged.connect(self.on_edit)
self.binary_font = QFont("Ubuntu Mono", 12)
self.binary_font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, 200)
self.content_binary_input.setFont(self.binary_font)
self.bit_highlighter = Higlighter()
self.one_format = QTextCharFormat()
self.one_format.setBackground(QColor(200, 150, 100))
self.bit_highlighter.add_mapping("1", self.one_format)
self.bit_highlighter.setDocument(self.content_binary_input.document())
self.content_widget.setLayout(self.content_widget_layout)
self.app.gui.main_window.openFileTabs.addTab( # add a tab for the file in the top files list
self.content_widget,
self.name
)
def on_edit(self):
self.not_saved = True
self.bit_editor = BitEditor(self.app, self)