Source code for indicator.led

from PySide6.QtCore import QPointF, QSize, Qt, QTimer
from PySide6.QtGui import QBrush, QColor, QLinearGradient, QPainter, QPen
from PySide6.QtWidgets import QWidget


[docs] class LEDIndicator(QWidget): """LED-style indicator widget""" def __init__( self, parent: object | None = None, alignment_state: object | None = None ) -> None: """Initialize LED indicator Args: parent: Parent widget """ super().__init__(parent) # Set fixed size self.setFixedSize(16, 16) # Initialize state
[docs] self._state = False
# Create timer for blink
[docs] self._timer = QTimer(self)
self._timer.setSingleShot(True) self._timer.timeout.connect(self._reset_blink) # Define colors
[docs] self._on_color = QColor(0, 255, 0) # Green
[docs] self._off_color = QColor(50, 50, 50) # Dark gray
[docs] def set_active(self, active: bool) -> None: self._state = True self.update()
[docs] def sizeHint(self) -> QSize: """Get recommended size""" return QSize(16, 16)
[docs] def set_state(self, state: bool) -> None: """Set LED state Args: state: True for on, False for off """ self._state = state self._blink = False self.update()
[docs] def paintEvent(self, event: object) -> None: """Paint the LED indicator""" painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # Get current color if self._blink and self._blink_state: color = self._blink_color elif self._state: color = self._on_color else: color = self._off_color gradient = QLinearGradient(QPointF(0, 1), QPointF(0, 0)) # From bottom to top gradient.setCoordinateMode(QLinearGradient.ObjectBoundingMode) gradient.setColorAt(0.0, QColor(0, 255, 0, 0)) # Green at the bottom gradient.setColorAt(0.5, color) # Transparent at the top gradient.setColorAt(1.0, QColor(0, 255, 0, 0)) # Transparent at the top # Draw LED circle painter.setPen(QPen(Qt.black, 1)) painter.setBrush(gradient) painter.drawEllipse(2, 2, 12, 12) # Add highlight if self._state or (self._blink and self._blink_state): highlight = QColor(color) highlight.setAlpha(100) painter.setBrush(QBrush(highlight)) painter.drawEllipse(4, 4, 8, 8)