"""Small reusable building blocks used across every panel.""" from __future__ import annotations from PySide6.QtCore import Signal from PySide6.QtWidgets import ( QFrame, QHBoxLayout, QLabel, QProgressBar, QPushButton, QSizePolicy, QVBoxLayout, QWidget, ) def style_role(widget: QWidget, role: str) -> QWidget: """Sets the `role` dynamic property the app.qss selectors key off of.""" widget.setProperty("role", role) return widget def clear_layout(layout) -> None: """Empties a layout so it can be rebuilt from scratch on the next poll tick. Removing a widget from a layout with ``takeAt()`` does NOT hide it - it keeps painting at its last geometry until ``deleteLater()``'s deferred deletion actually runs on a later event-loop pass. Since every dynamic list in this app (fill units, implements, storage cards, farm rows, forecast days, ...) gets cleared and rebuilt on every ~500ms poll tick, that lag would show up as visible flicker/ghosting. Hiding the widget immediately (synchronous, cheap) avoids that; deleteLater() still runs afterwards to actually free it. """ while layout.count(): item = layout.takeAt(0) w = item.widget() if w is not None: w.hide() w.deleteLater() class StatChip(QFrame): """One "stat-chip": small label on top, bold value below.""" def __init__(self, label: str, parent=None) -> None: super().__init__(parent) style_role(self, "stat-chip") layout = QVBoxLayout(self) layout.setContentsMargins(8, 5, 8, 5) layout.setSpacing(1) self.label = QLabel(label) style_role(self.label, "stat-label") layout.addWidget(self.label) self.value = QLabel("–") style_role(self.value, "stat-value") self.value.setWordWrap(True) layout.addWidget(self.value) def set_value(self, text: str) -> None: self.value.setText(text) class LabeledBar(QWidget): """Label row (name + value text) + a colored progress bar underneath.""" def __init__(self, label: str, color: str, parent=None) -> None: super().__init__(parent) self._color = color layout = QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(3) row = QHBoxLayout() row.setContentsMargins(0, 0, 0, 0) self.name_label = QLabel(label) style_role(self.name_label, "stat-label") row.addWidget(self.name_label) row.addStretch(1) self.value_label = QLabel("–") style_role(self.value_label, "stat-label") row.addWidget(self.value_label) layout.addLayout(row) self.bar = QProgressBar() self.bar.setRange(0, 1000) self.bar.setValue(0) self.bar.setTextVisible(False) self.bar.setFixedHeight(10) layout.addWidget(self.bar) self._apply_color(color) def _apply_color(self, color: str) -> None: self.bar.setStyleSheet(f"QProgressBar::chunk {{ background: {color}; }}") def set_label(self, label: str) -> None: self.name_label.setText(label) def set_value(self, text: str, pct: float, color: str | None = None) -> None: self.value_label.setText(text) self.bar.setValue(int(max(0.0, min(100.0, pct)) * 10)) if color and color != self._color: self._color = color self._apply_color(color) class PanelHeader(QFrame): """Header bar of a collapsible panel: title, optional subtitle/extra widgets, a collapse toggle.""" toggled = Signal(bool) def __init__(self, title: str, parent=None) -> None: super().__init__(parent) style_role(self, "panel-header") self.layout = QHBoxLayout(self) self.layout.setContentsMargins(10, 6, 8, 6) self.layout.setSpacing(8) self.title_label = QLabel(title) style_role(self.title_label, "panel-title") self.layout.addWidget(self.title_label) self.sub_label = QLabel("") style_role(self.sub_label, "panel-sub") self.layout.addWidget(self.sub_label) self.layout.addStretch(1) self._collapsed = False self.toggle_btn = QPushButton("▾") self.toggle_btn.setProperty("flat", "true") self.toggle_btn.setFixedWidth(24) self.toggle_btn.clicked.connect(self._on_toggle_clicked) self.layout.addWidget(self.toggle_btn) def insert_extra(self, widget: QWidget) -> None: # inserted just before the stretch/toggle so it sits next to the title self.layout.insertWidget(self.layout.count() - 2, widget) def _on_toggle_clicked(self) -> None: self.set_collapsed(not self._collapsed) self.toggled.emit(self._collapsed) def set_collapsed(self, collapsed: bool) -> None: self._collapsed = collapsed self.toggle_btn.setText("▸" if collapsed else "▾") class CollapsiblePanel(QFrame): """A QGroupBox-like panel with a header (title + collapse toggle) and a body widget, matching the `.panel[data-panel-id]` behaviour in the web version (collapsed state persisted via QSettings by the caller).""" collapsed_changed = Signal(bool) def __init__(self, panel_id: str, title: str, parent=None) -> None: super().__init__(parent) self.panel_id = panel_id style_role(self, "panel") self.setObjectName("panel_" + panel_id) outer = QVBoxLayout(self) outer.setContentsMargins(0, 0, 0, 0) outer.setSpacing(0) self.header = PanelHeader(title, self) self.header.toggled.connect(self._on_header_toggled) outer.addWidget(self.header) self.body = QWidget(self) self.body_layout = QVBoxLayout(self.body) self.body_layout.setContentsMargins(12, 10, 12, 12) self.body_layout.setSpacing(8) outer.addWidget(self.body) self.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Maximum) def set_collapsed(self, collapsed: bool) -> None: self.header.set_collapsed(collapsed) self.body.setVisible(not collapsed) def _on_header_toggled(self, collapsed: bool) -> None: self.body.setVisible(not collapsed) self.collapsed_changed.emit(collapsed)