summaryrefslogtreecommitdiff
path: root/daq_system/ui/alarm_panel.py
blob: 590d78532a60b22a9176636a798f438dfd001fb0 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
ui/alarm_panel.py  —  Alarm event log panel.
"""

from datetime import datetime
from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QLabel,
    QScrollArea, QFrame, QPushButton
)
from PyQt6.QtCore import Qt


class AlarmEntry(QFrame):
    def __init__(self, device_id, channel_id, value, kind, timestamp):
        super().__init__()
        self.setObjectName("alarmEntry")
        color = "#f85149" if kind == "high" else "#d29922"
        self.setStyleSheet(f"border-left: 3px solid {color};")
        layout = QHBoxLayout(self)
        layout.setContentsMargins(8, 4, 8, 4)
        icon = "▲" if kind == "high" else "▼"
        msg = QLabel(f"{icon}  {device_id} / {channel_id}  =  {value:.3f}  [{kind.upper()}]")
        msg.setObjectName("alarmMsg")
        ts_lbl = QLabel(timestamp)
        ts_lbl.setObjectName("alarmTs")
        layout.addWidget(msg)
        layout.addStretch()
        layout.addWidget(ts_lbl)


class AlarmPanel(QWidget):
    def __init__(self):
        super().__init__()
        self._build_ui()

    def _build_ui(self):
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        hdr_row = QHBoxLayout()
        hdr = QLabel("  ALARMS")
        hdr.setObjectName("panelHeader")
        hdr_row.addWidget(hdr)
        hdr_row.addStretch()
        clr = QPushButton("Clear")
        clr.setObjectName("clearAlarmsBtn")
        clr.clicked.connect(self.clear_alarms)
        hdr_row.addWidget(clr)
        hdr_widget = QWidget()
        hdr_widget.setLayout(hdr_row)
        hdr_widget.setObjectName("alarmHeaderWidget")
        layout.addWidget(hdr_widget)

        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        self._container = QWidget()
        self._inner = QVBoxLayout(self._container)
        self._inner.setContentsMargins(4, 4, 4, 4)
        self._inner.setSpacing(3)
        self._inner.addStretch()
        scroll.setWidget(self._container)
        layout.addWidget(scroll)

        self._scroll = scroll
        self._count = 0

    def add_alarm(self, device_id, channel_id, value, kind):
        ts = datetime.now().strftime("%H:%M:%S")
        entry = AlarmEntry(device_id, channel_id, value, kind, ts)
        self._inner.insertWidget(0, entry)
        self._count += 1
        # Keep last 200
        if self._count > 200:
            item = self._inner.takeAt(self._inner.count() - 2)
            if item and item.widget():
                item.widget().deleteLater()
            self._count -= 1

    def clear_alarms(self):
        while self._inner.count() > 1:
            item = self._inner.takeAt(0)
            if item and item.widget():
                item.widget().deleteLater()
        self._count = 0