-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.py
204 lines (157 loc) · 5.9 KB
/
buttons.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from PySide6.QtCore import Slot
from PySide6.QtWidgets import QPushButton, QGridLayout
from variables import MEDIUM_FONT_SIZE
from utils import isEmpty, isNumOrDot, isValidNumber, convertToIntOrFloat
from display import Display
from info import Info
import math
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from display import Display
from info import Info
from main_window import MainWindow
class Button(QPushButton):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.configStyle()
def configStyle(self):
font = self.font()
font.setPixelSize(MEDIUM_FONT_SIZE)
self.setFont(font)
self.setMinimumSize(75, 75)
class ButtonsGrid(QGridLayout):
def __init__(self, display: Display, info: Info,window: 'MainWindow', *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._grid_mask = [
["C", "◀", "^", "/"],
["7", "8", "9", "*"],
["4", "5", "6", "-"],
["1", "2", "3", "+"],
["N", "0", ".", "="],
]
self.display = display
self.info = info
self.window = window
self._equation = "Sua conta fica aqui"
self._left = None
self._right = None
self._op = None
self.equation = self._equation
self._makeGrid()
@property
def equation(self):
return self._equation
@equation.setter
def equation(self, value):
self._equation = value
self.info.setText(value)
def _makeGrid(self):
self.display.eqPress.connect(self._eq)
self.display.delPress.connect(self._backspace)
self.display.clearPress.connect(self._clear)
self.display.inputPress.connect(self._insertToDisplay)
self.display.operatorPress.connect(self._configLeftOp)
for row_number, row in enumerate(self._grid_mask):
for column_number, buttonText in enumerate(row):
button = Button(buttonText)
if not isNumOrDot(buttonText) and not isEmpty(buttonText):
button.setProperty("cssClass", "specialButton")
self._configSpecialButton(button)
self.addWidget(button, row_number, column_number)
slot = self._makeSlot(self._insertToDisplay, buttonText)
self._conectButtonClicked(button, slot)
def _conectButtonClicked(self, button: Button, slot):
button.clicked.connect(slot)
def _configSpecialButton(self, button: Button):
text = button.text()
if text == "C":
self._conectButtonClicked(button, self._clear)
if text == "N":
self._conectButtonClicked(button, self._invertNumber)
if text == '◀':
self._conectButtonClicked(button, self.display.backspace)
if text in "+-*/^":
self._conectButtonClicked(
button, self._makeSlot(self._configLeftOp, text)
)
if text == "=":
self._conectButtonClicked(button, self._eq)
def _makeSlot(self, func, *args, **kwargs):
@Slot(bool)
def realSlot(_):
func(*args, **kwargs)
return realSlot
@Slot()
def _invertNumber(self):
displayText = self.display.text()
if not isValidNumber(displayText):
return
newNumber = convertToIntOrFloat(displayText) *-1
self.display.setText(str(newNumber))
def _insertToDisplay(self, text):
newDisplay = self.display.text() + text
if not isValidNumber(newDisplay):
return
self.display.setText(newDisplay)
self.display.setFocus()
def _clear(self):
self.display.clear()
self.equation = ""
self._left = None
self._right = None
self._op = None
self.display.setFocus()
def _configLeftOp(self, text):
displayText = self.display.text()
self.display.clear()
if not isValidNumber:
self._showError('Você não digitou nada')
if self._left is None:
self._left = convertToIntOrFloat(displayText)
self._op = text
self.equation = f"{self._left} {self._op}"
self.display.setFocus()
@Slot()
def _eq(self):
displayText = self.display.text()
if not isValidNumber(displayText) or self._left is None:
self._showError('Conta Incompleta')
return
self._right = convertToIntOrFloat(displayText)
self.equation = f"{self._left} {self._op} {self._right}"
result: float| int | str = "error"
try:
if "^" in self.equation and isinstance(self._left, float|int):
result = math.pow(self._left, self._right)
result = convertToIntOrFloat(str(result))
else:
result = eval(self.equation)
except ZeroDivisionError:
self._showError('Divisão por Zero')
except OverflowError:
self._showError('Error: Numero Muito Grande')
self.display.clear()
self.info.setText(f"{self.equation} = {result}")
self._left = result
self._right = None
if result == "error":
self._left = None
self.display.setFocus()
def _makeDialog(self, text):
msgBox = self.window.makeMsgBox()
msgBox.setText(text)
return msgBox
@Slot()
def _backspace(self):
self.display.backspace()
self.display.setFocus()
def _showError(self, text):
msgBox = self._makeDialog(text)
msgBox.setIcon(msgBox.Icon.Critical)
msgBox.exec()
self.display.setFocus()
def _showInfo(self, text):
msgBox = self._makeDialog(text)
msgBox.setIcon(msgBox.Icon.Information)
msgBox.exec()
self.display.setFocus()