From e107d671b097d157d66015a3ce53727b742ef231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sat, 4 Mar 2023 07:35:02 -0300 Subject: [PATCH] Calculadora: capturando teclas ENTER, Backspace e ESC --- aula202-calculadora/buttons.py | 4 +++- aula202-calculadora/display.py | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/aula202-calculadora/buttons.py b/aula202-calculadora/buttons.py index 70f86ac..c626c77 100644 --- a/aula202-calculadora/buttons.py +++ b/aula202-calculadora/buttons.py @@ -63,7 +63,9 @@ class ButtonsGrid(QGridLayout): print('Signal recebido por "vouApagarVocê" em', type(self).__name__) def _makeGrid(self): - self.display.eqRequested.connect(self.vouApagarVocê) + self.display.eqPressed.connect(self.vouApagarVocê) + self.display.delPressed.connect(self.display.backspace) + self.display.clearPressed.connect(self.vouApagarVocê) for rowNumber, rowData in enumerate(self._gridMask): for colNumber, buttonText in enumerate(rowData): diff --git a/aula202-calculadora/display.py b/aula202-calculadora/display.py index c5bfcbf..d225525 100644 --- a/aula202-calculadora/display.py +++ b/aula202-calculadora/display.py @@ -1,11 +1,14 @@ from PySide6.QtCore import Qt, Signal from PySide6.QtGui import QKeyEvent from PySide6.QtWidgets import QLineEdit +from utils import isEmpty from variables import BIG_FONT_SIZE, MINIMUM_WIDTH, TEXT_MARGIN class Display(QLineEdit): - eqRequested = Signal() + eqPressed = Signal() + delPressed = Signal() + clearPressed = Signal() def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -20,12 +23,31 @@ class Display(QLineEdit): self.setTextMargins(*margins) 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] + isDelete = key in [KEYS.Key_Backspace, KEYS.Key_Delete] + isEsc = key in [KEYS.Key_Escape] if isEnter: print('Enter pressionado, sinal emitido', type(self).__name__) - self.eqRequested.emit() + self.eqPressed.emit() return event.ignore() + + if isDelete: + print('isDelete pressionado, sinal emitido', type(self).__name__) + self.delPressed.emit() + return event.ignore() + + if isEsc: + print('isEsc pressionado, sinal emitido', type(self).__name__) + self.clearPressed.emit() + return event.ignore() + + # Não passar daqui se não tiver texto + if isEmpty(text): + return event.ignore() + + print('Texto', text)