From 74b95d8cf9e52f1eae2e7ebcf0624c4f7bc2ad53 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Wed, 20 Nov 2024 15:52:10 +0100 Subject: [PATCH] Now the user is restricted to inputting only 0 and 1 and cant overwrite the separator character. --- binary_text_edit.py | 20 ++++++++++++++++++++ file.py | 8 ++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 binary_text_edit.py diff --git a/binary_text_edit.py b/binary_text_edit.py new file mode 100644 index 0000000..2d9abc1 --- /dev/null +++ b/binary_text_edit.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +from PyQt6.QtWidgets import QPlainTextEdit + + +class BinaryTextEdit(QPlainTextEdit): # rewrite QPlainTextEdit.keyPressEvent because it has no .setValidator() + def keyPressEvent(self, event): + allowed_keys = {"", "0", "1"} + + if event.text() in allowed_keys: + cursor = self.textCursor() + pos = cursor.position() + + if (pos + 1) % 9 == 0 and event.text() not in {'', None}: # dont overwrite the separator character + return + + super().keyPressEvent(event) + + else: + event.ignore() diff --git a/file.py b/file.py index ad833cf..a1ee6f8 100644 --- a/file.py +++ b/file.py @@ -1,6 +1,8 @@ #!/usr/bin/python3 -from PyQt6.QtWidgets import QWidget, QPlainTextEdit, QFileDialog, QVBoxLayout +from PyQt6.QtWidgets import QWidget, QFileDialog, QVBoxLayout +from PyQt6.QtGui import QFont +from binary_text_edit import BinaryTextEdit class FileActions: @@ -80,9 +82,11 @@ class File: self.content_widget = QWidget(self.app.main_window.openFileTabs, objectName=path) self.content_widget_layout = QVBoxLayout() - self.content_binary_input = QPlainTextEdit(self.content_widget) + 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.setFont(QFont("Ubuntu Mono")) self.content_binary_input.textChanged.connect(self.on_edit) self.content_widget.setLayout(self.content_widget_layout)