From 4529529a7fc40ba1b42d92dc89c934273af5a14a Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Thu, 21 Nov 2024 17:22:59 +0100 Subject: [PATCH] OOPed everything a little more. --- editor.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ file.py | 45 +++++++-------------------------------------- 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 editor.py diff --git a/editor.py b/editor.py new file mode 100644 index 0000000..659099a --- /dev/null +++ b/editor.py @@ -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 diff --git a/file.py b/file.py index 02b4249..557c304 100644 --- a/file.py +++ b/file.py @@ -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)