189 lines
6.3 KiB
C++
189 lines
6.3 KiB
C++
#include "DeviceStatusWidget.h"
|
||
#include <QHBoxLayout>
|
||
|
||
DeviceStatusWidget::DeviceStatusWidget(QWidget* parent)
|
||
: QWidget(parent)
|
||
{
|
||
m_gridLayout = new QGridLayout(this);
|
||
m_gridLayout->setSpacing(10);
|
||
m_gridLayout->setContentsMargins(10, 10, 10, 10);
|
||
}
|
||
|
||
DeviceStatusWidget::~DeviceStatusWidget()
|
||
{
|
||
removeDeviceLabels();
|
||
}
|
||
|
||
void DeviceStatusWidget::setDevices(const QList<WheelDeviceDisplayInfo>& devices)
|
||
{
|
||
m_devices = devices;
|
||
removeDeviceLabels();
|
||
createDeviceLabels(devices.size());
|
||
|
||
for (int i = 0; i < m_devices.size(); ++i) {
|
||
updateDeviceLabel(i);
|
||
}
|
||
}
|
||
|
||
void DeviceStatusWidget::updateDeviceStatus(const QString& deviceName, DeviceStatus status)
|
||
{
|
||
for (int i = 0; i < m_devices.size(); ++i) {
|
||
if (m_devices[i].name == deviceName || m_devices[i].alias == deviceName) {
|
||
m_devices[i].status = status;
|
||
updateDeviceLabel(i);
|
||
emit deviceStatusChanged(deviceName, status);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
void DeviceStatusWidget::updateDeviceTemperature(const QString& deviceName, float temperature)
|
||
{
|
||
for (int i = 0; i < m_devices.size(); ++i) {
|
||
if (m_devices[i].name == deviceName || m_devices[i].alias == deviceName) {
|
||
m_devices[i].temperature = temperature;
|
||
m_devices[i].hasTemperature = true;
|
||
updateDeviceLabel(i);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
void DeviceStatusWidget::createDeviceLabels(int count)
|
||
{
|
||
for (int i = 0; i < count; ++i) {
|
||
QWidget* deviceWidget = new QWidget(this);
|
||
deviceWidget->setStyleSheet("background-color: rgb(47, 48, 52); border-radius: 5px;");
|
||
deviceWidget->setCursor(Qt::PointingHandCursor);
|
||
deviceWidget->installEventFilter(this);
|
||
|
||
// 改为垂直布局:上图标下名称
|
||
QVBoxLayout* deviceLayout = new QVBoxLayout(deviceWidget);
|
||
deviceLayout->setSpacing(4);
|
||
deviceLayout->setContentsMargins(8, 10, 8, 10);
|
||
deviceLayout->setAlignment(Qt::AlignCenter);
|
||
|
||
// 状态图标(放在上方,居中)
|
||
QLabel* statusLabel = new QLabel(deviceWidget);
|
||
statusLabel->setFixedSize(32, 32);
|
||
statusLabel->setStyleSheet("border: none;");
|
||
statusLabel->setAlignment(Qt::AlignCenter);
|
||
statusLabel->setScaledContents(true);
|
||
deviceLayout->addWidget(statusLabel, 0, Qt::AlignCenter);
|
||
m_statusLabels.append(statusLabel);
|
||
|
||
// 设备名称(放在下方,居中)
|
||
QLabel* nameLabel = new QLabel(deviceWidget);
|
||
nameLabel->setStyleSheet("color: rgb(221, 225, 233); font-size: 12px; border: none;");
|
||
nameLabel->setAlignment(Qt::AlignCenter);
|
||
deviceLayout->addWidget(nameLabel, 0, Qt::AlignCenter);
|
||
m_nameLabels.append(nameLabel);
|
||
|
||
// 温度标签(放在最下方,居中)
|
||
QLabel* tempLabel = new QLabel(deviceWidget);
|
||
tempLabel->setStyleSheet("color: rgb(150, 150, 150); font-size: 10px; border: none;");
|
||
tempLabel->setAlignment(Qt::AlignCenter);
|
||
tempLabel->hide();
|
||
deviceLayout->addWidget(tempLabel, 0, Qt::AlignCenter);
|
||
m_temperatureLabels.append(tempLabel);
|
||
|
||
deviceWidget->setProperty("deviceIndex", i);
|
||
m_deviceLabels.append(deviceWidget);
|
||
|
||
// 计算行列位置 (每行4个)
|
||
int row = i / DEVICES_PER_ROW;
|
||
int col = i % DEVICES_PER_ROW;
|
||
m_gridLayout->addWidget(deviceWidget, row, col);
|
||
}
|
||
|
||
// 设置列拉伸因子,使设备项平均分布
|
||
for (int col = 0; col < DEVICES_PER_ROW; ++col) {
|
||
m_gridLayout->setColumnStretch(col, 1);
|
||
}
|
||
}
|
||
|
||
void DeviceStatusWidget::removeDeviceLabels()
|
||
{
|
||
for (auto widget : m_deviceLabels) {
|
||
m_gridLayout->removeWidget(widget);
|
||
delete widget;
|
||
}
|
||
m_deviceLabels.clear();
|
||
m_statusLabels.clear();
|
||
m_nameLabels.clear();
|
||
m_temperatureLabels.clear();
|
||
}
|
||
|
||
void DeviceStatusWidget::updateDeviceLabel(int index)
|
||
{
|
||
if (index < 0 || index >= m_devices.size()) {
|
||
return;
|
||
}
|
||
|
||
const WheelDeviceDisplayInfo& device = m_devices[index];
|
||
|
||
// 更新状态图标(使用 QPixmap)
|
||
if (index < m_statusLabels.size()) {
|
||
QString imagePath = getStatusImage(device.status);
|
||
QPixmap pixmap(imagePath);
|
||
if (!pixmap.isNull()) {
|
||
m_statusLabels[index]->setPixmap(pixmap.scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||
}
|
||
}
|
||
|
||
// 更新名称
|
||
if (index < m_nameLabels.size()) {
|
||
m_nameLabels[index]->setText(device.name);
|
||
}
|
||
|
||
// 更新温度
|
||
if (index < m_temperatureLabels.size()) {
|
||
if (device.hasTemperature) {
|
||
m_temperatureLabels[index]->setText(QString("%1°C").arg(device.temperature, 0, 'f', 1));
|
||
m_temperatureLabels[index]->show();
|
||
} else {
|
||
m_temperatureLabels[index]->hide();
|
||
}
|
||
}
|
||
}
|
||
|
||
QString DeviceStatusWidget::getStatusImage(DeviceStatus status) const
|
||
{
|
||
switch (status) {
|
||
case DeviceStatus::Online:
|
||
return ":/common/resource/camera_online.png";
|
||
case DeviceStatus::Offline:
|
||
return ":/common/resource/camera_offline.png";
|
||
case DeviceStatus::Error:
|
||
return ":/common/resource/camera_offline.png";
|
||
default:
|
||
return ":/common/resource/camera_offline.png";
|
||
}
|
||
}
|
||
|
||
bool DeviceStatusWidget::eventFilter(QObject* obj, QEvent* event)
|
||
{
|
||
if (event->type() == QEvent::MouseButtonPress) {
|
||
QWidget* widget = qobject_cast<QWidget*>(obj);
|
||
if (widget && m_deviceLabels.contains(widget)) {
|
||
int index = widget->property("deviceIndex").toInt();
|
||
if (index >= 0 && index < m_devices.size()) {
|
||
emit deviceClicked(m_devices[index].alias.isEmpty() ? m_devices[index].name : m_devices[index].alias);
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
return QWidget::eventFilter(obj, event);
|
||
}
|
||
|
||
void DeviceStatusWidget::onDeviceLabelClicked()
|
||
{
|
||
QWidget* widget = qobject_cast<QWidget*>(sender());
|
||
if (widget) {
|
||
int index = widget->property("deviceIndex").toInt();
|
||
if (index >= 0 && index < m_devices.size()) {
|
||
emit deviceClicked(m_devices[index].alias.isEmpty() ? m_devices[index].name : m_devices[index].alias);
|
||
}
|
||
}
|
||
}
|