-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
72 lines (56 loc) · 2.04 KB
/
display.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from PySide6.QtWidgets import QLineEdit
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QKeyEvent
from variables import BIG_FONT_SIZE,TEXT_MARGIN, MINIMUM_WIDTH
from utils import isEmpty, isNumOrDot
class Display(QLineEdit):
eqPress = Signal()
delPress = Signal()
clearPress = Signal()
inputPress = Signal(str)
operatorPress = Signal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.configStyle()
def configStyle(self):
self.setStyleSheet(f'font-size:{BIG_FONT_SIZE}px;')
self.setMinimumHeight(BIG_FONT_SIZE * 2)
self.setAlignment(Qt.AlignmentFlag.AlignRight)
self.setTextMargins(*[TEXT_MARGIN for _ in range(4)])
self.setMinimumWidth(MINIMUM_WIDTH)
def keyPressEvent(self, event: QKeyEvent) -> None:
text = event.text().strip()
key = event.key()
KEYS = Qt.Key
isEnter = key in [KEYS.Key_Enter, KEYS.Key_Return, KEYS.Key_Equal]
isDelete = key in [KEYS.Key_Backspace,KEYS.Key_Delete]
isEsc = key in [KEYS.Key_Escape, KEYS.Key_Return]
isOperator = key in [
KEYS.Key_Plus,
KEYS.Key_Less,
KEYS.Key_multiply,
KEYS.Key_Minus,
KEYS.Key_Asterisk,
KEYS.Key_Slash,
KEYS.Key_P
]
if isEnter:
self.eqPress.emit()
return event.ignore()
elif isDelete:
self.delPress.emit()
return event.ignore()
elif isEsc:
self.clearPress.emit()
return event.ignore()
elif isOperator:
if text.lower().strip() == 'p':
text = '^'
self.operatorPress.emit(text)
return event.ignore()
if isEmpty(text):
event.ignore()
else:
if isNumOrDot(text):
self.inputPress.emit(text)
return event.ignore()