Calculadora: os Signals de teclas digitadas aos Slots corretos
This commit is contained in:
@@ -59,19 +59,12 @@ class ButtonsGrid(QGridLayout):
|
|||||||
self._equation = value
|
self._equation = value
|
||||||
self.info.setText(value)
|
self.info.setText(value)
|
||||||
|
|
||||||
def vouApagarVocê(self, *args):
|
|
||||||
print(
|
|
||||||
'Signal recebido por "vouApagarVocê" em',
|
|
||||||
type(self).__name__,
|
|
||||||
args,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _makeGrid(self):
|
def _makeGrid(self):
|
||||||
self.display.eqPressed.connect(self.vouApagarVocê)
|
self.display.eqPressed.connect(self._eq)
|
||||||
self.display.delPressed.connect(self.display.backspace)
|
self.display.delPressed.connect(self.display.backspace)
|
||||||
self.display.clearPressed.connect(self.vouApagarVocê)
|
self.display.clearPressed.connect(self._clear)
|
||||||
self.display.inputPressed.connect(self.vouApagarVocê)
|
self.display.inputPressed.connect(self._insertToDisplay)
|
||||||
self.display.operatorPressed.connect(self.vouApagarVocê)
|
self.display.operatorPressed.connect(self._configLeftOp)
|
||||||
|
|
||||||
for rowNumber, rowData in enumerate(self._gridMask):
|
for rowNumber, rowData in enumerate(self._gridMask):
|
||||||
for colNumber, buttonText in enumerate(rowData):
|
for colNumber, buttonText in enumerate(rowData):
|
||||||
@@ -82,7 +75,7 @@ class ButtonsGrid(QGridLayout):
|
|||||||
self._configSpecialButton(button)
|
self._configSpecialButton(button)
|
||||||
|
|
||||||
self.addWidget(button, rowNumber, colNumber)
|
self.addWidget(button, rowNumber, colNumber)
|
||||||
slot = self._makeSlot(self._insertButtonTextToDisplay, button)
|
slot = self._makeSlot(self._insertToDisplay, buttonText)
|
||||||
self._connectButtonClicked(button, slot)
|
self._connectButtonClicked(button, slot)
|
||||||
|
|
||||||
def _connectButtonClicked(self, button, slot):
|
def _connectButtonClicked(self, button, slot):
|
||||||
@@ -100,27 +93,29 @@ class ButtonsGrid(QGridLayout):
|
|||||||
if text in '+-/*^':
|
if text in '+-/*^':
|
||||||
self._connectButtonClicked(
|
self._connectButtonClicked(
|
||||||
button,
|
button,
|
||||||
self._makeSlot(self._operatorClicked, button)
|
self._makeSlot(self._configLeftOp, button)
|
||||||
)
|
)
|
||||||
|
|
||||||
if text == '=':
|
if text == '=':
|
||||||
self._connectButtonClicked(button, self._eq)
|
self._connectButtonClicked(button, self._eq)
|
||||||
|
|
||||||
|
@Slot()
|
||||||
def _makeSlot(self, func, *args, **kwargs):
|
def _makeSlot(self, func, *args, **kwargs):
|
||||||
@ Slot(bool)
|
@ Slot(bool)
|
||||||
def realSlot(_):
|
def realSlot(_):
|
||||||
func(*args, **kwargs)
|
func(*args, **kwargs)
|
||||||
return realSlot
|
return realSlot
|
||||||
|
|
||||||
def _insertButtonTextToDisplay(self, button):
|
@Slot()
|
||||||
buttonText = button.text()
|
def _insertToDisplay(self, text):
|
||||||
newDisplayValue = self.display.text() + buttonText
|
newDisplayValue = self.display.text() + text
|
||||||
|
|
||||||
if not isValidNumber(newDisplayValue):
|
if not isValidNumber(newDisplayValue):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.display.insert(buttonText)
|
self.display.insert(text)
|
||||||
|
|
||||||
|
@Slot()
|
||||||
def _clear(self):
|
def _clear(self):
|
||||||
self._left = None
|
self._left = None
|
||||||
self._right = None
|
self._right = None
|
||||||
@@ -128,8 +123,8 @@ class ButtonsGrid(QGridLayout):
|
|||||||
self.equation = self._equationInitialValue
|
self.equation = self._equationInitialValue
|
||||||
self.display.clear()
|
self.display.clear()
|
||||||
|
|
||||||
def _operatorClicked(self, button):
|
@Slot()
|
||||||
buttonText = button.text() # +-/* (etc...)
|
def _configLeftOp(self, text):
|
||||||
displayText = self.display.text() # Deverá ser meu número _left
|
displayText = self.display.text() # Deverá ser meu número _left
|
||||||
self.display.clear() # Limpa o display
|
self.display.clear() # Limpa o display
|
||||||
|
|
||||||
@@ -144,9 +139,10 @@ class ButtonsGrid(QGridLayout):
|
|||||||
if self._left is None:
|
if self._left is None:
|
||||||
self._left = float(displayText)
|
self._left = float(displayText)
|
||||||
|
|
||||||
self._op = buttonText
|
self._op = text
|
||||||
self.equation = f'{self._left} {self._op} ??'
|
self.equation = f'{self._left} {self._op} ??'
|
||||||
|
|
||||||
|
@Slot()
|
||||||
def _eq(self):
|
def _eq(self):
|
||||||
displayText = self.display.text()
|
displayText = self.display.text()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user