Now the user is restricted to inputting only 0 and 1 and cant overwrite the separator character.

This commit is contained in:
The Wobbler 2024-11-20 15:52:10 +01:00
parent 782ada62d5
commit 74b95d8cf9
2 changed files with 26 additions and 2 deletions

20
binary_text_edit.py Normal file
View file

@ -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()

View file

@ -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)