"""Half-circle arc gauge - native repaint of the SVG gauge in index.html (``, a 180° arc).""" from __future__ import annotations from PySide6.QtCore import QRectF, Qt from PySide6.QtGui import QColor, QFont, QPainter, QPen from PySide6.QtWidgets import QSizePolicy, QWidget class GaugeWidget(QWidget): def __init__(self, unit: str, caption: str, accent: str, parent=None) -> None: super().__init__(parent) self._unit = unit self._caption = caption self._accent = QColor(accent) self._track_color = QColor("#3a4453") self._text_color = QColor("#e7ecf3") self._dim_color = QColor("#8b97a8") self._fraction = 0.0 self._value_text = "0" self.setMinimumSize(150, 130) self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) def set_colors(self, track: str, text: str, dim: str) -> None: self._track_color = QColor(track) self._text_color = QColor(text) self._dim_color = QColor(dim) self.update() def set_value(self, fraction: float, value_text: str) -> None: self._fraction = max(0.0, min(1.0, fraction)) self._value_text = value_text self.update() def sizeHint(self): return self.minimumSize() def paintEvent(self, event) -> None: # noqa: N802 (Qt override) painter = QPainter(self) painter.setRenderHint(QPainter.RenderHint.Antialiasing) w = self.width() h = self.height() # Gauge art-board is 200x155 in the original SVG - scale to fit width, # keep it centered horizontally, anchored to the top. scale = min(w / 200.0, (h - 22) / 155.0) if h > 22 else w / 200.0 scale = max(scale, 0.1) painter.translate((w - 200 * scale) / 2.0, 0) painter.scale(scale, scale) rect = QRectF(20, 20, 160, 160) # bounding box of the r=80 circle at (100,100) pen_width = 14 track_pen = QPen(self._track_color, pen_width, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap) painter.setPen(track_pen) # Qt angles: 0 = 3 o'clock, positive = counter-clockwise, in 1/16th deg. # The arc goes from 180° (9 o'clock) to 0° (3 o'clock) along the top. painter.drawArc(rect, 180 * 16, -180 * 16) if self._fraction > 0.01: fill_pen = QPen(self._accent, pen_width, Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap) painter.setPen(fill_pen) span = -180 * self._fraction painter.drawArc(rect, 180 * 16, int(span * 16)) painter.setPen(self._text_color) value_font = QFont() value_font.setPointSizeF(22) value_font.setWeight(QFont.Weight.DemiBold) painter.setFont(value_font) painter.drawText(QRectF(0, 95, 200, 34), Qt.AlignmentFlag.AlignCenter, self._value_text) painter.setPen(self._dim_color) unit_font = QFont() unit_font.setPointSizeF(11) painter.setFont(unit_font) painter.drawText(QRectF(0, 128, 200, 20), Qt.AlignmentFlag.AlignCenter, self._unit) painter.resetTransform() painter.setPen(self._dim_color) cap_font = QFont() cap_font.setPointSizeF(11) painter.setFont(cap_font) painter.drawText( 0, int(h - 6), w, 18, Qt.AlignmentFlag.AlignHCenter, self._caption )