Bread_Editor/binary_text_edit.py

21 lines
588 B
Python
Raw Normal View History

#!/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()