FemaLocalSoftware/sensorwidget.cpp

139 lines
5.3 KiB
C++
Raw Permalink Normal View History

2024-11-28 08:02:59 +00:00
#include "sensorwidget.h"
2024-12-16 08:38:04 +00:00
#include <QMouseEvent>
SensorWidget::SensorWidget(QWidget *parent, const Sensor &sensor)
2025-01-07 22:13:48 +00:00
: QFrame(parent)
, sensor(sensor)
, titleLabel(new QLabel(this))
, valueLabel(new QLabel(this))
, incidentLabel(new QLabel(this))
, layout(new QVBoxLayout(this))
2024-11-28 08:02:59 +00:00
{
setFrameStyle(QFrame::Box);
setLineWidth(1);
setFixedSize(250, 200);
layout->addWidget(titleLabel);
layout->addWidget(valueLabel);
layout->addWidget(incidentLabel);
2024-12-16 08:38:04 +00:00
setErrorState();
QString titleText = sensor.name;
if (titleText.length() > 40) {
titleText = titleText.left(37) + "...";
}
titleLabel->setText(titleText);
2024-11-28 08:02:59 +00:00
2024-12-16 08:38:04 +00:00
titleLabel->setWordWrap(true);
titleLabel->setMinimumHeight(100);
titleLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
2025-01-07 22:13:48 +00:00
if (sensor.isBooled) {
2024-12-16 08:38:04 +00:00
valueLabel->setText(sensor.lastValue != 0 ? "Да" : "Нет");
} else {
valueLabel->setText(QString::number(sensor.lastValue) + ' ' + sensor.unitName);
2025-01-07 22:13:48 +00:00
incidentLabel->setText(QString::number(sensor.alarmLowerBound) + "-"
+ QString::number(sensor.alarmUpperBound));
2024-12-16 08:38:04 +00:00
}
layout->setContentsMargins(10, 10, 10, 10);
layout->setSpacing(5);
2024-11-28 08:02:59 +00:00
setLayout(layout);
}
2025-01-07 22:13:48 +00:00
void SensorWidget::mousePressEvent(QMouseEvent *event)
{
emit clicked(this); // Генерация сигнала о клике
2024-12-16 08:38:04 +00:00
QFrame::mousePressEvent(event);
}
void SensorWidget::setErrorState()
2024-11-28 08:02:59 +00:00
{
2024-12-16 08:38:04 +00:00
if (sensor.isAlarmEnabled) {
2024-11-28 18:34:00 +00:00
setStyleSheet("QFrame { border: 2px solid #FF453A; border-radius: 10px; background: "
"rgba(255, 69, 58, 0.25);}");
2024-11-28 08:02:59 +00:00
titleLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: #13385F;"
"border: 0;"
"background: transparent;");
valueLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: #FF453A;"
"border: 0;"
"background: transparent;");
incidentLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: #13385F;"
"border: 0;"
"background: transparent;");
} else {
2024-11-28 18:34:00 +00:00
setStyleSheet("QFrame { border: 2px solid rgba(229, 231, 235, 0.70);; border-radius: 10px; "
2024-12-16 08:38:04 +00:00
"background: rgba(255, 255, 255, 0.70)}");
2024-11-28 08:02:59 +00:00
titleLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: #13385F;"
"border: 0;"
"background: transparent;");
valueLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: #13385F;"
"border: 0;"
"background: transparent;");
incidentLabel->setStyleSheet("font-family: Inter;"
"font-size: 24px;"
"font-weight: 400;"
"line-height: 29.05px;"
"text-align: left;"
"color: rgba(19, 56, 95, 0.5);"
"border: 0;"
"background: transparent;");
}
2024-12-16 08:38:04 +00:00
}
2025-01-07 22:13:48 +00:00
Sensor SensorWidget::getSensor() const
{
2024-12-16 08:38:04 +00:00
return sensor;
}
2024-11-28 08:02:59 +00:00
2025-01-07 22:13:48 +00:00
void SensorWidget::setSensor(const Sensor value)
{
2024-12-16 08:38:04 +00:00
sensor = value;
QString titleText = sensor.name;
if (titleText.length() > 40) {
titleText = titleText.left(37) + "...";
}
titleLabel->setText(titleText);
2025-01-07 22:13:48 +00:00
if (sensor.isBooled) {
2024-12-16 08:38:04 +00:00
valueLabel->setText(sensor.lastValue != 0 ? "Да" : "Нет");
2025-01-07 22:13:48 +00:00
incidentLabel->hide();
2024-12-16 08:38:04 +00:00
} else {
valueLabel->setText(QString::number(sensor.lastValue) + ' ' + sensor.unitName);
2025-01-07 22:13:48 +00:00
incidentLabel->show();
incidentLabel->setText(QString::number(sensor.alarmLowerBound) + "-"
+ QString::number(sensor.alarmUpperBound));
2024-12-16 08:38:04 +00:00
}
2024-11-28 08:02:59 +00:00
}