-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontadorWidget.py
More file actions
44 lines (35 loc) · 1.59 KB
/
contadorWidget.py
File metadata and controls
44 lines (35 loc) · 1.59 KB
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
import re
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QWidget, QLabel, QHBoxLayout
class WordCounterWidget(QWidget):
conteoActualizado = Signal(int, int)
def __init__(self, wpm=200, mostrarPalabras=True, mostrarCaracteres=True, mostrarTiempoLectura=True, parent=None):
super().__init__(parent)
self.wpm = max(1, int(wpm))
self.mostrarPalabras = bool(mostrarPalabras)
self.mostrarCaracteres = bool(mostrarCaracteres)
self.mostrarTiempoLectura = bool(mostrarTiempoLectura)
self.lblP = QLabel("Palabras: 0")
self.lblC = QLabel("Caracteres: 0")
self.lblT = QLabel("Lectura: 0 min")
lay = QHBoxLayout(self)
lay.setContentsMargins(6, 2, 6, 2)
lay.setSpacing(12)
lay.addWidget(self.lblP)
lay.addWidget(self.lblC)
lay.addWidget(self.lblT)
lay.addStretch()
self._apply_visibility()
def _apply_visibility(self):
self.lblP.setVisible(self.mostrarPalabras)
self.lblC.setVisible(self.mostrarCaracteres)
self.lblT.setVisible(self.mostrarTiempoLectura)
def update_from_text(self, text: str):
text = text or ""
palabras = len(re.findall(r"\b\w+\b", text))
caracteres = len(text)
seg = int((palabras / self.wpm) * 60)
self.lblP.setText(f"Palabras: {palabras}")
self.lblC.setText(f"Caracteres: {caracteres}")
self.lblT.setText(f"Lectura: {seg}s" if seg < 60 else f"Lectura: {round(seg/60)} min")
self.conteoActualizado.emit(palabras, caracteres)