2024-11-21 17:22:59 +01:00
|
|
|
#!/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)
|
|
|
|
one_format = QTextCharFormat()
|
|
|
|
one_format.setBackground(QColor(200, 150, 100))
|
|
|
|
|
|
|
|
def __init__(self, app, file):
|
|
|
|
self.app = app
|
|
|
|
self.file = file
|
|
|
|
|
2024-11-21 19:47:15 +01:00
|
|
|
self.not_saved = False
|
|
|
|
|
2024-11-21 17:22:59 +01:00
|
|
|
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)
|
2024-11-21 19:47:15 +01:00
|
|
|
|
2024-11-21 17:22:59 +01:00
|
|
|
self.input.setPlainText(self.app.utils.bstring_to_oz(self.file.content))
|
2024-11-21 19:47:15 +01:00
|
|
|
|
2024-11-21 17:22:59 +01:00
|
|
|
self.input.textChanged.connect(self.on_edit)
|
|
|
|
|
|
|
|
self.bit_highlighter = Higlighter()
|
|
|
|
self.bit_highlighter.add_mapping("1", self.one_format)
|
2024-11-22 16:20:43 +01:00
|
|
|
|
|
|
|
self.update_style()
|
2024-11-21 17:22:59 +01:00
|
|
|
|
|
|
|
self.widget.setLayout(self.widget_layout)
|
|
|
|
|
2024-11-21 17:34:44 +01:00
|
|
|
self.tab_index = self.app.gui.main_window.openFileTabs.addTab( # add a tab for the file in the top files list
|
2024-11-21 17:22:59 +01:00
|
|
|
self.widget,
|
|
|
|
self.file.name
|
|
|
|
)
|
|
|
|
|
|
|
|
def on_edit(self):
|
|
|
|
self.not_saved = True
|
2024-11-22 16:20:43 +01:00
|
|
|
|
|
|
|
def update_style(self):
|
|
|
|
square = self.app.settings.square_bits
|
|
|
|
|
|
|
|
spacing = 200 if square else 100 # add spacing when setting is checked
|
|
|
|
|
|
|
|
self.font.setLetterSpacing(QFont.SpacingType.PercentageSpacing, spacing)
|
|
|
|
self.input.setFont(self.font)
|
|
|
|
|
|
|
|
highlight_ones = self.app.settings.highlight_ones
|
|
|
|
|
|
|
|
highlighter_document = self.input.document() if highlight_ones else None
|
|
|
|
|
|
|
|
self.bit_highlighter.setDocument(highlighter_document)
|