class SpinBox(QSpinBox): def __init__(self, parent=None): super().__init__(parent) regex = QRegExp(r"^M?M?M?(?:CM|CD|D?C?C?C?)" r"(?:XC|XL|L?X?X?X?)(?:IX|IV|V?I?I?I?)$") regex.setCaseSensitivity(Qt.CaseInsensitive) self.validator = QRegExpValidator(regex, self) self.setRange(1, 3999) self.lineEdit().textEdited.connect(self.fixCase) def fixCase(self, text): self.lineEdit().setText(text.upper()) def validate(self, text, pos): return self.validator.validate(text, pos) def valueFromText(self, text): try: return roman.fromRoman(text.upper()) except roman.RomanError: return 1 def textFromValue(self, value): return roman.toRoman(value)
class TestEngLangValidator(TestBaseClass): def setUp(self): self.validator = QRegExpValidator(QRegExp(utils.rxEng), None) def _check(self, s1, targetState): expectedState, s2, pos = self.validator.validate(s1, 0) self.assertEqual((s1, targetState), (s1, expectedState)) def testAcceptable(self): for word in ['abc', 'Abc DfZ', '123', 'vasia Petr', 'Salt-Shed rin']: self._check(word, QValidator.State.Acceptable) def testInvalid(self): for word in ['aз', 'Буки', 'Веди гл-а-голь', 'Вedi']: self._check(word, QValidator.State.Invalid)