Files
QT_wuhan_YT/widgets/datacard.cpp
T
2026-10-14 09:59:42 +08:00

109 lines
3.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "datacard.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include "global.h"
#include <QStyle>
DataCard::DataCard(const QString& title, QWidget* parent)
: QWidget(parent)
{
setProperty("card", true);
QVBoxLayout* lay = new QVBoxLayout(this);
lay->setContentsMargins(24, 0, 24, 20);
lay->setSpacing(16);
// 标题栏
m_title = new QLabel(title, this);
m_title->setFixedHeight(60);
m_title->setProperty("cardTitle", true);
m_title->setStyleSheet(QString("color:%1;font-size:42px;font-weight:700;border-bottom:1px solid %2;")
.arg(C::TEXT, C::BORDER));
lay->addWidget(m_title);
// 字段行容器
lay->addStretch(1);
}
void DataCard::setTitle(const QString& t)
{
m_title->setText(t);
}
int DataCard::addField(const QString& label, const QString& value, bool big)
{
int index = m_values.size();
QHBoxLayout* row = new QHBoxLayout();
row->setSpacing(16);
QLabel* lab = new QLabel(label + ":", this);
lab->setProperty("sub", true);
lab->setStyleSheet(QString("color:%1;font-size:32px;").arg(C::TEXT_SUB));
QLabel* val = new QLabel(value, this);
val->setStyleSheet(QString("color:%1;%2").arg(C::TEXT,
big ? "font-size:50px;font-weight:700;" : "font-size:42px;font-weight:700;"));
row->addWidget(lab);
row->addWidget(val, 1);
// 插入到标题栏之后(stretch 之前)
QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(layout());
lay->insertLayout(lay->count() - 1, row);
m_values.append(val);
m_labels.append(label);
return index;
}
void DataCard::setField(int index, const QString& value)
{
if (index < 0 || index >= m_values.size())
return;
m_values[index]->setText(value);
}
void DataCard::setAlarm(int index, bool on)
{
if (index < 0 || index >= m_values.size())
return;
QLabel* val = m_values[index];
if (on) {
val->setProperty("alert", true);
startBlink(val, "blink");
} else {
stopBlink(val, "blink");
val->setProperty("alert", false);
val->style()->unpolish(val);
val->style()->polish(val);
}
}
void DataCard::setHighlight(bool on)
{
if (on)
setStyleSheet(QString("QWidget[card=\"true\"]{background-color:%1;border:3px solid %2;border-radius:12px;}")
.arg(C::PANEL, C::PRIMARY));
else
setStyleSheet(QString());
}
// 字段行文字从左缘缩进到「中间偏左」:行首弹性 2 : 数值行尾弹性 3
void DataCard::indentRows()
{
QVBoxLayout* lay = qobject_cast<QVBoxLayout*>(layout());
if (!lay)
return;
for (int i = 0; i < lay->count(); ++i) {
QHBoxLayout* row = qobject_cast<QHBoxLayout*>(lay->itemAt(i)->layout());
if (!row || row->count() != 2) // 字段行是 [标签][数值];标题与末尾 stretch 不是子布局
continue;
if (row->itemAt(0)->spacerItem()) // 已缩进过,避免重复插入
continue;
row->insertStretch(0, 2);
row->setStretch(2, 3); // 插入后 [0]=行首弹性 [1]=标签 [2]=数值
}
}