emsApplication/applications/EmsShower/customdisplaypanel.cpp

418 lines
15 KiB
C++
Raw Normal View History

2025-03-07 16:16:57 +08:00
#include "customdisplaypanel.h"
#include <QLabel>
#include <QTableWidget>
#include <QHeaderView>
#include <QGraphicsDropShadowEffect>
#include <QFontDatabase>
CustomDisplayPanel::CustomDisplayPanel(QWidget *parent)
: QWidget{parent}
,m_label_count{5},m_table_rows_count{10},m_table_rols_count{2},m_imagePath{":/icons/home.png"},m_pTableWidget(nullptr)
{
}
void CustomDisplayPanel::Build()
{
// **让 CustomWidget 背景透明,避免遮挡阴影**
this->setAttribute(Qt::WA_TranslucentBackground);
this->setStyleSheet("background: transparent;");
// **创建一个子容器 `containerWidget`,所有内容都放在这里**
QWidget *containerWidget = new QWidget(this);
containerWidget->setStyleSheet("background-color: white; border-radius: 10px;");
// **为 `containerWidget` 添加阴影**
QGraphicsDropShadowEffect *shadowMain = new QGraphicsDropShadowEffect(this);
shadowMain->setBlurRadius(10);
shadowMain->setOffset(5, 5);
shadowMain->setColor(QColor(0, 0, 0, 120)); // 半透明黑色
containerWidget->setGraphicsEffect(shadowMain);
// **主垂直布局**
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(containerWidget);
mainLayout->setContentsMargins(10, 10, 10, 10); // 预留空间显示阴影
this->setLayout(mainLayout);
// **容器内的布局**
QVBoxLayout *containerLayout = new QVBoxLayout(containerWidget);
// **上半部分(固定高度 200**
QWidget *topWidget = new QWidget(containerWidget);
topWidget->setFixedHeight(200);
topWidget->setStyleSheet("background-color: white; border-radius: 10px;");
// **应用阴影效果**
QGraphicsDropShadowEffect *shadowTop = new QGraphicsDropShadowEffect(this);
shadowTop->setBlurRadius(10);
shadowTop->setOffset(5, 5);
shadowTop->setColor(QColor(0, 0, 0, 100)); // 半透明黑色
topWidget->setGraphicsEffect(shadowTop);
QHBoxLayout *topLayout = new QHBoxLayout(topWidget);
// **左侧布局上下排列64x64 图片 + QLabel**
QVBoxLayout *leftLayout = new QVBoxLayout();
QLabel *imageLabel = new QLabel(topWidget);
QPixmap pixmap(m_imagePath); // 替换为实际图片路径
imageLabel->setPixmap(pixmap.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
int width = 120;
imageLabel->setFixedSize(width, 64);
imageLabel->setAlignment(Qt::AlignCenter);
QLabel *textLabel = new QLabel(m_mainLabel, topWidget);
textLabel->setAlignment(Qt::AlignCenter);
textLabel->setStyleSheet(
"QLabel {"
//" font-family: 'Alimama DongFangDaKai';" // 需确保字体已加载
" font-family: 'Arial';"
" font-size: 20px;"
" color: #13396E;"
" font-weight: bold;"
" font-style: black;"
"}"
);
textLabel->setFixedSize(width, 40);
leftLayout->addWidget(imageLabel);
leftLayout->addWidget(textLabel);
leftLayout->setAlignment(Qt::AlignCenter);
// **右侧布局:包含 5 个 QLabel**
// 加载字体
int fontId = QFontDatabase::addApplicationFont(":/fonts/Alimama_DongFangDaKai_Regular.ttf");
if (fontId == -1)
{
qDebug() << "字体加载失败";
}
QVBoxLayout *rightLayout = new QVBoxLayout();
for (int i = 0; i < m_label_count; ++i)
{
QLabel *label = new QLabel(m_lables[i], topWidget);
label->setStyleSheet(
"QLabel {"
//" font-family: 'Alimama DongFangDaKai';" // 需确保字体已加载
" font-family: 'Arial';"
" font-size: 20px;"
" color: #2E86C1;"
" font-weight: bold;"
"}"
);
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
rightLayout->addWidget(label);
m_txtLabels.push_back(label);
}
// **组装上半部分布局**
QWidget *leftWidget = new QWidget(topWidget);
leftWidget->setLayout(leftLayout);
QWidget *rightWidget = new QWidget(topWidget);
rightWidget->setLayout(rightLayout);
topLayout->addWidget(leftWidget, 1); // 1/3 宽度
topLayout->addWidget(rightWidget, 4); // 2/3 宽度
containerLayout->addWidget(topWidget,1);
// **下半部分2 列 5 行表格**
QTableWidget *tableWidget = new QTableWidget(m_table_rows_count, m_table_rols_count, containerWidget);
m_pTableWidget = tableWidget;
// tableWidget->setHorizontalHeaderLabels({"Signal", "Value"});
// 设置列宽策略
tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
tableWidget->setColumnWidth(0, 150);
tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
//tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 隐藏所有表头
tableWidget->horizontalHeader()->setVisible(false); // 隐藏水平表头[1](@ref)
tableWidget->verticalHeader()->setVisible(false); // 隐藏垂直表头[1](@ref)
// 禁止表格编辑
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 设置网格线样式
tableWidget->setShowGrid(true); // 默认显示网格线[3](@ref)
tableWidget->setStyleSheet("QTableWidget { gridline-color: #e0e0e0; background-color: white; border-radius: 10px;}");
// **应用阴影效果**
QGraphicsDropShadowEffect *shadowTable = new QGraphicsDropShadowEffect(this);
shadowTable->setBlurRadius(10);
shadowTable->setOffset(5, 5);
shadowTable->setColor(QColor(0, 0, 0, 100));
tableWidget->setGraphicsEffect(shadowTable);
containerLayout->addWidget(tableWidget,1);
m_pTableWidget->setRowCount(0);
}
void CustomDisplayPanel::UpdateData(OpenJson& json)
{
int panel_type = json["panel_type"].i32();
qDebug() << panel_type;
if (panel_type == CustomDisplayPanel::PANEL_TEMPERATURE)
{
UpdateTemperature(json);
}
else if (panel_type == CustomDisplayPanel::PANEL_ALARM)
{
UpdateAlarm(json);
}
}
void CustomDisplayPanel::UpdateTemperature(OpenJson &json)
{
auto& nodeLabel = json["text_panel"];
qDebug() << nodeLabel.size();
for(size_t i=0; i<nodeLabel.size(); i++)
{
auto& node = nodeLabel[i];
std::string title = node["title"].s();
std::string value = node["value"].s();
qDebug() << QString::fromStdString(title) << ":"<< QString::fromStdString(value);
QString disp;
if (i == 0)
{
if (value == "1")
disp = QString("Online");
else
disp = QString("Offline");
}
else
{
disp = QString("%1: %2").arg(QString::fromStdString(title)).arg(QString::fromStdString(value));
}
m_txtLabels[i]->setText(disp);
}
auto& nodeTable = json["table"];
qDebug() << nodeTable.size();
m_pTableWidget->clear();
m_pTableWidget->setRowCount(nodeTable.size());
// 加载字体
int fontId = QFontDatabase::addApplicationFont(":/fonts/Alimama_DongFangDaKai_Regular.ttf");
if (fontId == -1)
{
qDebug() << "字体加载失败";
}
for(size_t i=0; i<nodeTable.size(); i++)
{
auto& node = nodeTable[i];
std::string title = node["signal"].s();
std::string value = node["value"].s();
int col = 0;
// **填充表格数据**
QTableWidgetItem *item = new QTableWidgetItem(QString::fromStdString(title));
item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); // 右对齐+垂直居中
// 仅第一列设置特殊字体
QFont font;
//font.setFamily("Alimama DongFangDaKai");
font.setFamily("Arial");
font.setPixelSize(20);
font.setBold(true);
item->setFont(font);
//item->setForeground(QBrush(QColor("#2E86C1")));
item->setForeground(QColor(Qt::red));
m_pTableWidget->setItem(i, col++, item);
// **填充表格数据**
QTableWidgetItem *item2 = new QTableWidgetItem(QString::fromStdString(value));
item2->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); // 右对齐+垂直居中
QFont font2;
font2.setFamily("Arial");
font2.setPixelSize(20);
//font2.setBold(true);
item2->setFont(font2);
//item2->setForeground(QBrush(QColor("#2E86C1")));
item2->setForeground(QColor(Qt::red));
m_pTableWidget->setItem(i, col, item2);
}
}
void CustomDisplayPanel::UpdateAlarm(OpenJson &json)
{
qDebug() << "更新告警信息";
QFont font;
font.setFamily("Arial");
font.setPixelSize(16);
font.setBold(false);
auto& nodeTable = json["alarm"];
qDebug() << nodeTable.size();
for(size_t i=0; i<nodeTable.size(); i++)
{
m_pTableWidget->insertRow(0);
auto& node = nodeTable[i];
std::string s = node["time"].s();
std::string title = node["signal"].s();
std::string value = node["value"].s();
int col = 0;
QTableWidgetItem *item0 = new QTableWidgetItem(QString::fromStdString(s));
item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 右对齐+垂直居中
QTableWidgetItem *item1 = new QTableWidgetItem(QString::fromStdString(title));
QTableWidgetItem *item2 = new QTableWidgetItem(QString::fromStdString(value));
item0->setFont(font);
item1->setFont(font);
item2->setFont(font);
item0->setForeground(QBrush(QColor("#2E86C1")));
item1->setForeground(QBrush(QColor("#2E86C1")));
item2->setForeground(QBrush(QColor("#2E86C1")));
m_pTableWidget->setItem(0, 0, item0);
m_pTableWidget->setItem(0, 1, item1);
m_pTableWidget->setItem(0, 2, item2);
}
}
////////
/// \brief CustomWarningPanel::CustomWarningPanel
///
///
CustomWarningPanel::CustomWarningPanel(QWidget *parent)
: CustomDisplayPanel{parent}
{
}
void CustomWarningPanel::Build()
{
// **让 CustomWidget 背景透明,避免遮挡阴影**
this->setAttribute(Qt::WA_TranslucentBackground);
this->setStyleSheet("background: transparent;");
// **创建一个子容器 `containerWidget`,所有内容都放在这里**
QWidget *containerWidget = new QWidget(this);
containerWidget->setStyleSheet("background-color: white; border-radius: 10px;");
// **为 `containerWidget` 添加阴影**
QGraphicsDropShadowEffect *shadowMain = new QGraphicsDropShadowEffect(this);
shadowMain->setBlurRadius(10);
shadowMain->setOffset(5, 5);
shadowMain->setColor(QColor(0, 0, 0, 120)); // 半透明黑色
containerWidget->setGraphicsEffect(shadowMain);
// **主垂直布局**
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(containerWidget);
mainLayout->setContentsMargins(10, 10, 10, 10); // 预留空间显示阴影
this->setLayout(mainLayout);
// **容器内的布局**
QVBoxLayout *containerLayout = new QVBoxLayout(containerWidget);
// **上半部分(固定高度 200**
QWidget *topWidget = new QWidget(containerWidget);
topWidget->setFixedHeight(100);
topWidget->setStyleSheet("background-color: white; border-radius: 10px;");
// **应用阴影效果**
QGraphicsDropShadowEffect *shadowTop = new QGraphicsDropShadowEffect(this);
shadowTop->setBlurRadius(10);
shadowTop->setOffset(5, 5);
shadowTop->setColor(QColor(0, 0, 0, 100)); // 半透明黑色
topWidget->setGraphicsEffect(shadowTop);
QHBoxLayout *topLayout = new QHBoxLayout(topWidget);
// **左侧布局上下排列64x64 图片 + QLabel**
QVBoxLayout *leftLayout = new QVBoxLayout();
QLabel *imageLabel = new QLabel(topWidget);
QPixmap pixmap(m_imagePath); // 替换为实际图片路径
imageLabel->setPixmap(pixmap.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
int width = 80;
imageLabel->setFixedSize(width, 80);
imageLabel->setAlignment(Qt::AlignCenter);
leftLayout->addWidget(imageLabel);
leftLayout->setAlignment(Qt::AlignCenter);
// **右侧布局:包含 5 个 QLabel**
// 加载字体
int fontId = QFontDatabase::addApplicationFont(":/fonts/Alimama_DongFangDaKai_Regular.ttf");
if (fontId == -1)
{
qDebug() << "字体加载失败";
}
QVBoxLayout *rightLayout = new QVBoxLayout();
for (int i = 0; i < m_label_count; ++i)
{
QLabel *label = new QLabel(m_lables[i], topWidget);
label->setStyleSheet(
"QLabel {"
//" font-family: 'Alimama DongFangDaKai';" // 需确保字体已加载
" font-family: 'Arial';"
" font-size: 20px;"
" color: #2E86C1;"
" font-weight: bold;"
"}"
);
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
rightLayout->addWidget(label);
m_txtLabels.push_back(label);
}
// **组装上半部分布局**
QWidget *leftWidget = new QWidget(topWidget);
leftWidget->setLayout(leftLayout);
QWidget *rightWidget = new QWidget(topWidget);
rightWidget->setLayout(rightLayout);
topLayout->addWidget(leftWidget, 1); // 1/3 宽度
topLayout->addWidget(rightWidget, 4); // 2/3 宽度
containerLayout->addWidget(topWidget,1);
// **下半部分2 列 5 行表格**
QTableWidget *tableWidget = new QTableWidget(m_table_rows_count, m_table_rols_count, containerWidget);
m_pTableWidget = tableWidget;
// tableWidget->setHorizontalHeaderLabels({"Signal", "Value"});
// 设置列宽策略
tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
tableWidget->setColumnWidth(0, 150);
tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
//tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 隐藏所有表头
tableWidget->horizontalHeader()->setVisible(false); // 隐藏水平表头[1](@ref)
tableWidget->verticalHeader()->setVisible(false); // 隐藏垂直表头[1](@ref)
// 禁止表格编辑
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 设置网格线样式
tableWidget->setShowGrid(true); // 默认显示网格线[3](@ref)
tableWidget->setStyleSheet("QTableWidget { gridline-color: #e0e0e0; background-color: white; border-radius: 10px;}");
// **应用阴影效果**
QGraphicsDropShadowEffect *shadowTable = new QGraphicsDropShadowEffect(this);
shadowTable->setBlurRadius(10);
shadowTable->setOffset(5, 5);
shadowTable->setColor(QColor(0, 0, 0, 100));
tableWidget->setGraphicsEffect(shadowTable);
containerLayout->addWidget(tableWidget,1);
m_pTableWidget->setRowCount(0);
}