|
|
|
@ -18,6 +18,13 @@
|
|
|
|
|
#include <QFontMetrics>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
|
#include <QGraphicsDropShadowEffect>
|
|
|
|
|
#include <QEasingCurve>
|
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
#include <QSqlError>
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
|
|
DeviceCard::DeviceCard(const DeviceInfo &device, QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
|
|
|
@ -38,9 +45,13 @@ DeviceCard::DeviceCard(const DeviceInfo &device, QWidget *parent)
|
|
|
|
|
, m_headerLayout(nullptr)
|
|
|
|
|
, m_infoLayout(nullptr)
|
|
|
|
|
, m_buttonLayout(nullptr)
|
|
|
|
|
, m_hoverAnimation(nullptr)
|
|
|
|
|
, m_scaleAnimation(nullptr)
|
|
|
|
|
, m_shadowEffect(nullptr)
|
|
|
|
|
{
|
|
|
|
|
setupUI();
|
|
|
|
|
setupStyle();
|
|
|
|
|
setupAnimations(); // 添加动画初始化
|
|
|
|
|
connectSignals();
|
|
|
|
|
|
|
|
|
|
// 设置卡片基本属性
|
|
|
|
@ -68,10 +79,11 @@ void DeviceCard::setupUI()
|
|
|
|
|
m_headerLayout->setSpacing(8); // 减少间距,让名称更靠左
|
|
|
|
|
m_headerLayout->setContentsMargins(0, 0, 0, 0); // 移除边距
|
|
|
|
|
|
|
|
|
|
// 设备图标 - 适当调整尺寸
|
|
|
|
|
// 设备图标 - 增大尺寸提高区分度
|
|
|
|
|
m_deviceIconLabel = new QLabel();
|
|
|
|
|
m_deviceIconLabel->setFixedSize(32, 32); // 稍微减小图标
|
|
|
|
|
m_deviceIconLabel->setFixedSize(40, 40); // 增大图标尺寸
|
|
|
|
|
m_deviceIconLabel->setScaledContents(true);
|
|
|
|
|
m_deviceIconLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
|
updateDeviceIcon();
|
|
|
|
|
|
|
|
|
|
// 设备名称 - 调整字体和位置
|
|
|
|
@ -84,11 +96,11 @@ void DeviceCard::setupUI()
|
|
|
|
|
m_deviceNameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 左对齐,垂直居中
|
|
|
|
|
m_deviceNameLabel->setContentsMargins(0, 0, 0, 0); // 恢复正常边距
|
|
|
|
|
|
|
|
|
|
// 状态指示器(圆点)- 适当调整尺寸
|
|
|
|
|
// 状态指示器(圆点)- 增大尺寸提高可见性
|
|
|
|
|
m_statusIndicator = new QLabel("●");
|
|
|
|
|
m_statusIndicator->setFixedSize(20, 20);
|
|
|
|
|
m_statusIndicator->setFixedSize(28, 28); // 增大状态指示器
|
|
|
|
|
m_statusIndicator->setAlignment(Qt::AlignCenter);
|
|
|
|
|
m_statusIndicator->setFont(QFont("Arial", 16));
|
|
|
|
|
m_statusIndicator->setFont(QFont("Arial", 22)); // 增大字体
|
|
|
|
|
|
|
|
|
|
// 状态文本 - 适当调整字体
|
|
|
|
|
m_statusLabel = new QLabel(getStatusText(m_currentStatus));
|
|
|
|
@ -143,9 +155,9 @@ void DeviceCard::setupUI()
|
|
|
|
|
m_detailsButton->setToolTip("设备详情");
|
|
|
|
|
m_detailsButton->setFont(QFont("Arial", 12, QFont::Bold));
|
|
|
|
|
|
|
|
|
|
m_controlButton = new QPushButton("控制");
|
|
|
|
|
m_controlButton = new QPushButton(getConnectionButtonText());
|
|
|
|
|
m_controlButton->setFixedSize(60, 32);
|
|
|
|
|
m_controlButton->setToolTip("设备控制");
|
|
|
|
|
m_controlButton->setToolTip(getConnectionButtonTooltip());
|
|
|
|
|
m_controlButton->setFont(QFont("Arial", 12, QFont::Bold));
|
|
|
|
|
|
|
|
|
|
m_locationButton = new QPushButton("定位");
|
|
|
|
@ -181,7 +193,7 @@ void DeviceCard::setupUI()
|
|
|
|
|
|
|
|
|
|
void DeviceCard::setupStyle()
|
|
|
|
|
{
|
|
|
|
|
// 基础卡片样式
|
|
|
|
|
// 基础卡片样式(移除CSS悬停样式,使用动画实现)
|
|
|
|
|
QString cardStyle = QString(
|
|
|
|
|
"DeviceCard {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
@ -190,12 +202,6 @@ void DeviceCard::setupStyle()
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 0.4);"
|
|
|
|
|
" border-radius: %1px;"
|
|
|
|
|
"}"
|
|
|
|
|
"DeviceCard:hover {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
|
" stop:0 rgba(82, 194, 242, 0.3),"
|
|
|
|
|
" stop:1 rgba(45, 120, 180, 0.3));"
|
|
|
|
|
" border-color: rgba(82, 194, 242, 0.8);"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(BORDER_RADIUS);
|
|
|
|
|
|
|
|
|
|
setStyleSheet(cardStyle);
|
|
|
|
@ -266,6 +272,95 @@ void DeviceCard::setupStyle()
|
|
|
|
|
updateStatusColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceCard::setupAnimations()
|
|
|
|
|
{
|
|
|
|
|
// 创建阴影效果
|
|
|
|
|
m_shadowEffect = new QGraphicsDropShadowEffect(this);
|
|
|
|
|
m_shadowEffect->setBlurRadius(15);
|
|
|
|
|
m_shadowEffect->setColor(QColor(82, 194, 242, 100));
|
|
|
|
|
m_shadowEffect->setOffset(0, 3);
|
|
|
|
|
setGraphicsEffect(m_shadowEffect);
|
|
|
|
|
|
|
|
|
|
// 创建悬停动画(阴影模糊半径)
|
|
|
|
|
m_hoverAnimation = new QPropertyAnimation(m_shadowEffect, "blurRadius", this);
|
|
|
|
|
m_hoverAnimation->setDuration(200);
|
|
|
|
|
m_hoverAnimation->setEasingCurve(QEasingCurve::OutCubic);
|
|
|
|
|
|
|
|
|
|
// 创建缩放动画(会在需要时手动创建)
|
|
|
|
|
m_scaleAnimation = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceCard::animateStatusChange(DeviceStatus oldStatus, DeviceStatus newStatus)
|
|
|
|
|
{
|
|
|
|
|
// 状态指示器闪烁动画
|
|
|
|
|
QPropertyAnimation *flashAnimation = new QPropertyAnimation(m_statusIndicator, "windowOpacity", this);
|
|
|
|
|
flashAnimation->setDuration(300);
|
|
|
|
|
flashAnimation->setKeyValueAt(0, 1.0);
|
|
|
|
|
flashAnimation->setKeyValueAt(0.5, 0.3);
|
|
|
|
|
flashAnimation->setKeyValueAt(1, 1.0);
|
|
|
|
|
flashAnimation->setEasingCurve(QEasingCurve::InOutQuad);
|
|
|
|
|
|
|
|
|
|
// 卡片边框闪烁效果
|
|
|
|
|
QPropertyAnimation *borderAnimation = new QPropertyAnimation(this);
|
|
|
|
|
borderAnimation->setDuration(500);
|
|
|
|
|
|
|
|
|
|
// 根据状态变化类型选择不同的动画效果
|
|
|
|
|
QString notificationColor;
|
|
|
|
|
if (newStatus == DeviceStatus::Online && oldStatus != DeviceStatus::Online) {
|
|
|
|
|
// 设备上线 - 绿色闪烁
|
|
|
|
|
notificationColor = "rgba(0, 255, 127, 0.8)";
|
|
|
|
|
} else if (newStatus == DeviceStatus::Offline) {
|
|
|
|
|
// 设备离线 - 红色闪烁
|
|
|
|
|
notificationColor = "rgba(255, 68, 68, 0.8)";
|
|
|
|
|
} else if (newStatus == DeviceStatus::Warning) {
|
|
|
|
|
// 设备警告 - 黄色闪烁
|
|
|
|
|
notificationColor = "rgba(255, 215, 0, 0.8)";
|
|
|
|
|
} else {
|
|
|
|
|
// 其他状态变化 - 蓝色闪烁
|
|
|
|
|
notificationColor = "rgba(82, 194, 242, 0.8)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置临时的闪烁样式
|
|
|
|
|
connect(flashAnimation, &QPropertyAnimation::valueChanged, this, [this, notificationColor](const QVariant &value) {
|
|
|
|
|
QString flashStyle = QString(
|
|
|
|
|
"DeviceCard {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
|
" stop:0 rgba(45, 65, 95, 0.9),"
|
|
|
|
|
" stop:1 rgba(25, 40, 65, 0.9));"
|
|
|
|
|
" border: 3px solid %1;"
|
|
|
|
|
" border-radius: %2px;"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(notificationColor).arg(BORDER_RADIUS);
|
|
|
|
|
|
|
|
|
|
setStyleSheet(flashStyle);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 动画结束后恢复正常样式并更新状态颜色
|
|
|
|
|
connect(flashAnimation, &QPropertyAnimation::finished, this, [this, flashAnimation]() {
|
|
|
|
|
// 恢复默认样式
|
|
|
|
|
QString defaultStyle = QString(
|
|
|
|
|
"DeviceCard {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
|
" stop:0 rgba(45, 65, 95, 0.9),"
|
|
|
|
|
" stop:1 rgba(25, 40, 65, 0.9));"
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 0.4);"
|
|
|
|
|
" border-radius: %1px;"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(BORDER_RADIUS);
|
|
|
|
|
|
|
|
|
|
setStyleSheet(defaultStyle);
|
|
|
|
|
|
|
|
|
|
// 更新状态颜色
|
|
|
|
|
updateStatusColor();
|
|
|
|
|
|
|
|
|
|
// 清理动画对象
|
|
|
|
|
flashAnimation->deleteLater();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 启动动画
|
|
|
|
|
flashAnimation->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceCard::connectSignals()
|
|
|
|
|
{
|
|
|
|
|
connect(m_detailsButton, &QPushButton::clicked, this, &DeviceCard::onDetailsClicked);
|
|
|
|
@ -290,11 +385,21 @@ void DeviceCard::updateDeviceInfo(const DeviceInfo &device)
|
|
|
|
|
|
|
|
|
|
void DeviceCard::updateDeviceStatus(DeviceStatus status)
|
|
|
|
|
{
|
|
|
|
|
m_currentStatus = status;
|
|
|
|
|
m_statusLabel->setText(getStatusText(status));
|
|
|
|
|
updateStatusColor();
|
|
|
|
|
|
|
|
|
|
qDebug() << "Device status updated:" << m_deviceInfo.name << "status:" << static_cast<int>(status);
|
|
|
|
|
if (m_currentStatus != status) {
|
|
|
|
|
DeviceStatus oldStatus = m_currentStatus;
|
|
|
|
|
m_currentStatus = status;
|
|
|
|
|
m_statusLabel->setText(getStatusText(status));
|
|
|
|
|
|
|
|
|
|
// 更新连接按钮文本和提示
|
|
|
|
|
m_controlButton->setText(getConnectionButtonText());
|
|
|
|
|
m_controlButton->setToolTip(getConnectionButtonTooltip());
|
|
|
|
|
|
|
|
|
|
// 添加状态变化动画效果
|
|
|
|
|
animateStatusChange(oldStatus, status);
|
|
|
|
|
|
|
|
|
|
qDebug() << "Device status updated with animation:" << m_deviceInfo.name
|
|
|
|
|
<< "from" << static_cast<int>(oldStatus) << "to" << static_cast<int>(status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -340,6 +445,30 @@ void DeviceCard::mousePressEvent(QMouseEvent *event)
|
|
|
|
|
void DeviceCard::enterEvent(QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
m_isHovered = true;
|
|
|
|
|
|
|
|
|
|
// 启动悬停动画效果
|
|
|
|
|
if (m_hoverAnimation && m_shadowEffect) {
|
|
|
|
|
m_hoverAnimation->setStartValue(15);
|
|
|
|
|
m_hoverAnimation->setEndValue(25);
|
|
|
|
|
m_hoverAnimation->start();
|
|
|
|
|
|
|
|
|
|
// 更新阴影颜色为更亮的蓝色
|
|
|
|
|
m_shadowEffect->setColor(QColor(82, 194, 242, 150));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新卡片背景以实现更明显的悬停效果
|
|
|
|
|
QString enhancedHoverStyle = QString(
|
|
|
|
|
"DeviceCard {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
|
" stop:0 rgba(82, 194, 242, 0.4),"
|
|
|
|
|
" stop:1 rgba(45, 120, 180, 0.4));"
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 1.0);"
|
|
|
|
|
" border-radius: %1px;"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(BORDER_RADIUS);
|
|
|
|
|
|
|
|
|
|
setStyleSheet(enhancedHoverStyle);
|
|
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
QWidget::enterEvent(event);
|
|
|
|
|
}
|
|
|
|
@ -347,6 +476,30 @@ void DeviceCard::enterEvent(QEvent *event)
|
|
|
|
|
void DeviceCard::leaveEvent(QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
m_isHovered = false;
|
|
|
|
|
|
|
|
|
|
// 停止悬停动画效果
|
|
|
|
|
if (m_hoverAnimation && m_shadowEffect) {
|
|
|
|
|
m_hoverAnimation->setStartValue(25);
|
|
|
|
|
m_hoverAnimation->setEndValue(15);
|
|
|
|
|
m_hoverAnimation->start();
|
|
|
|
|
|
|
|
|
|
// 恢复阴影颜色
|
|
|
|
|
m_shadowEffect->setColor(QColor(82, 194, 242, 100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 恢复卡片默认背景
|
|
|
|
|
QString defaultStyle = QString(
|
|
|
|
|
"DeviceCard {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
|
|
|
|
|
" stop:0 rgba(45, 65, 95, 0.9),"
|
|
|
|
|
" stop:1 rgba(25, 40, 65, 0.9));"
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 0.4);"
|
|
|
|
|
" border-radius: %1px;"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(BORDER_RADIUS);
|
|
|
|
|
|
|
|
|
|
setStyleSheet(defaultStyle);
|
|
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
QWidget::leaveEvent(event);
|
|
|
|
|
}
|
|
|
|
@ -375,8 +528,39 @@ void DeviceCard::onDetailsClicked()
|
|
|
|
|
|
|
|
|
|
void DeviceCard::onControlClicked()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "Control clicked for device:" << m_deviceInfo.name;
|
|
|
|
|
emit deviceControlRequested(m_deviceInfo.id);
|
|
|
|
|
qDebug() << "Connection toggle clicked for device:" << m_deviceInfo.name;
|
|
|
|
|
|
|
|
|
|
// 切换设备连接状态
|
|
|
|
|
DeviceStatus newStatus;
|
|
|
|
|
if (m_currentStatus == DeviceStatus::Online) {
|
|
|
|
|
// 当前在线,切换为离线
|
|
|
|
|
newStatus = DeviceStatus::Offline;
|
|
|
|
|
qDebug() << "Disconnecting device:" << m_deviceInfo.name;
|
|
|
|
|
} else {
|
|
|
|
|
// 当前离线,切换为在线
|
|
|
|
|
newStatus = DeviceStatus::Online;
|
|
|
|
|
qDebug() << "Connecting device:" << m_deviceInfo.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新数据库中的状态
|
|
|
|
|
if (updateDeviceStatusInDatabase(newStatus)) {
|
|
|
|
|
// 数据库更新成功,更新UI
|
|
|
|
|
updateDeviceStatus(newStatus);
|
|
|
|
|
m_deviceInfo.status = newStatus;
|
|
|
|
|
|
|
|
|
|
// 更新按钮文本和提示
|
|
|
|
|
m_controlButton->setText(getConnectionButtonText());
|
|
|
|
|
m_controlButton->setToolTip(getConnectionButtonTooltip());
|
|
|
|
|
|
|
|
|
|
// 发送状态变化信号,通知父组件更新统计信息
|
|
|
|
|
emit deviceStatusChanged(m_deviceInfo.id, newStatus);
|
|
|
|
|
|
|
|
|
|
qDebug() << "Device status successfully updated:" << m_deviceInfo.name
|
|
|
|
|
<< "to" << (newStatus == DeviceStatus::Online ? "Online" : "Offline");
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "Failed to update device status in database for:" << m_deviceInfo.name;
|
|
|
|
|
// 可以显示错误提示给用户
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceCard::onLocationClicked()
|
|
|
|
@ -388,7 +572,34 @@ void DeviceCard::onLocationClicked()
|
|
|
|
|
void DeviceCard::updateStatusColor()
|
|
|
|
|
{
|
|
|
|
|
QString color = getStatusColor(m_currentStatus);
|
|
|
|
|
m_statusIndicator->setStyleSheet(QString("QLabel { color: %1; background: transparent; border: none; }").arg(color));
|
|
|
|
|
|
|
|
|
|
// 为状态指示器添加发光效果和阴影
|
|
|
|
|
QString shadowColor;
|
|
|
|
|
switch (m_currentStatus) {
|
|
|
|
|
case DeviceStatus::Online:
|
|
|
|
|
shadowColor = "rgba(0, 255, 127, 0.6)"; // 绿色发光
|
|
|
|
|
break;
|
|
|
|
|
case DeviceStatus::Warning:
|
|
|
|
|
shadowColor = "rgba(255, 215, 0, 0.6)"; // 黄色发光
|
|
|
|
|
break;
|
|
|
|
|
case DeviceStatus::Offline:
|
|
|
|
|
shadowColor = "rgba(255, 68, 68, 0.6)"; // 红色发光
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
shadowColor = "rgba(136, 136, 136, 0.6)"; // 灰色发光
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_statusIndicator->setStyleSheet(QString(
|
|
|
|
|
"QLabel {"
|
|
|
|
|
" color: %1;"
|
|
|
|
|
" background: qradialgradient(cx:0.5, cy:0.5, radius:0.8,"
|
|
|
|
|
" stop:0 %2, stop:1 transparent);"
|
|
|
|
|
" border-radius: 14px;"
|
|
|
|
|
" border: 2px solid %1;"
|
|
|
|
|
" font-weight: bold;"
|
|
|
|
|
"}"
|
|
|
|
|
).arg(color).arg(shadowColor));
|
|
|
|
|
|
|
|
|
|
// 更新卡片边框颜色
|
|
|
|
|
QString borderColor;
|
|
|
|
@ -415,10 +626,42 @@ void DeviceCard::updateDeviceIcon()
|
|
|
|
|
QString iconPath = m_deviceInfo.getTypeIconPath();
|
|
|
|
|
QPixmap pixmap(iconPath);
|
|
|
|
|
if (!pixmap.isNull()) {
|
|
|
|
|
m_deviceIconLabel->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|
|
|
|
m_deviceIconLabel->setPixmap(pixmap.scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|
|
|
|
// 添加图标背景效果
|
|
|
|
|
m_deviceIconLabel->setStyleSheet(
|
|
|
|
|
"QLabel {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
|
|
|
|
|
" stop:0 rgba(82, 194, 242, 0.15),"
|
|
|
|
|
" stop:1 rgba(45, 120, 180, 0.15));"
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 0.3);"
|
|
|
|
|
" border-radius: 20px;"
|
|
|
|
|
" padding: 2px;"
|
|
|
|
|
"}"
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
qWarning() << "Failed to load device icon:" << iconPath;
|
|
|
|
|
m_deviceIconLabel->setText("📱"); // 备用图标
|
|
|
|
|
// 设置默认设备图标,根据设备类型选择
|
|
|
|
|
if (m_deviceInfo.type == "uav") {
|
|
|
|
|
m_deviceIconLabel->setText("🚁"); // 无人机图标
|
|
|
|
|
m_deviceIconLabel->setFont(QFont("Arial", 20));
|
|
|
|
|
} else if (m_deviceInfo.type == "dog") {
|
|
|
|
|
m_deviceIconLabel->setText("🐕"); // 机器狗图标
|
|
|
|
|
m_deviceIconLabel->setFont(QFont("Arial", 20));
|
|
|
|
|
} else {
|
|
|
|
|
m_deviceIconLabel->setText("📱"); // 备用图标
|
|
|
|
|
m_deviceIconLabel->setFont(QFont("Arial", 18));
|
|
|
|
|
}
|
|
|
|
|
// 添加图标背景效果
|
|
|
|
|
m_deviceIconLabel->setStyleSheet(
|
|
|
|
|
"QLabel {"
|
|
|
|
|
" background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
|
|
|
|
|
" stop:0 rgba(82, 194, 242, 0.15),"
|
|
|
|
|
" stop:1 rgba(45, 120, 180, 0.15));"
|
|
|
|
|
" border: 2px solid rgba(82, 194, 242, 0.3);"
|
|
|
|
|
" border-radius: 20px;"
|
|
|
|
|
" padding: 2px;"
|
|
|
|
|
"}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -435,10 +678,10 @@ QString DeviceCard::getStatusText(DeviceStatus status) const
|
|
|
|
|
QString DeviceCard::getStatusColor(DeviceStatus status) const
|
|
|
|
|
{
|
|
|
|
|
switch (status) {
|
|
|
|
|
case DeviceStatus::Online: return "#00FF7F"; // 绿色
|
|
|
|
|
case DeviceStatus::Warning: return "#FFD700"; // 黄色
|
|
|
|
|
case DeviceStatus::Offline: return "#FF4444"; // 红色
|
|
|
|
|
default: return "#888888"; // 灰色
|
|
|
|
|
case DeviceStatus::Online: return "#00FF7F"; // 明亮绿色
|
|
|
|
|
case DeviceStatus::Warning: return "#FFD700"; // 明亮黄色
|
|
|
|
|
case DeviceStatus::Offline: return "#FF4444"; // 明亮红色
|
|
|
|
|
default: return "#A0A0A0"; // 明亮灰色
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -447,4 +690,61 @@ QString DeviceCard::formatCoordinates(double longitude, double latitude) const
|
|
|
|
|
return QString("N%1, E%2")
|
|
|
|
|
.arg(latitude, 0, 'f', 2)
|
|
|
|
|
.arg(longitude, 0, 'f', 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DeviceCard::getConnectionButtonText() const
|
|
|
|
|
{
|
|
|
|
|
if (m_currentStatus == DeviceStatus::Online) {
|
|
|
|
|
return "断开";
|
|
|
|
|
} else {
|
|
|
|
|
return "连接";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DeviceCard::getConnectionButtonTooltip() const
|
|
|
|
|
{
|
|
|
|
|
if (m_currentStatus == DeviceStatus::Online) {
|
|
|
|
|
return "断开设备连接";
|
|
|
|
|
} else {
|
|
|
|
|
return "连接到设备";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceCard::updateDeviceStatusInDatabase(DeviceStatus status)
|
|
|
|
|
{
|
|
|
|
|
// 创建唯一的数据库连接名称(包含时间戳避免重复)
|
|
|
|
|
QString connectionName = QString("DeviceCard_%1_%2").arg(m_deviceInfo.id).arg(QDateTime::currentMSecsSinceEpoch());
|
|
|
|
|
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", connectionName);
|
|
|
|
|
db.setHostName("localhost");
|
|
|
|
|
db.setPort(3306);
|
|
|
|
|
db.setDatabaseName("Client");
|
|
|
|
|
db.setUserName("root");
|
|
|
|
|
db.setPassword("hzk200407140238");
|
|
|
|
|
|
|
|
|
|
if (!db.open()) {
|
|
|
|
|
qWarning() << "Failed to connect to database for device status update:" << db.lastError().text();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSqlQuery query(db);
|
|
|
|
|
|
|
|
|
|
// 将DeviceStatus转换为数据库中的state值 (0=离线, 1=在线)
|
|
|
|
|
int stateValue = (status == DeviceStatus::Online) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
// 更新设备状态
|
|
|
|
|
QString sql = "UPDATE devices SET state = ? WHERE id = ?";
|
|
|
|
|
query.prepare(sql);
|
|
|
|
|
query.addBindValue(stateValue);
|
|
|
|
|
query.addBindValue(m_deviceInfo.id);
|
|
|
|
|
|
|
|
|
|
bool success = query.exec();
|
|
|
|
|
if (!success) {
|
|
|
|
|
qWarning() << "Failed to update device status in database:" << query.lastError().text();
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "Successfully updated device status in database:" << m_deviceInfo.id
|
|
|
|
|
<< "state:" << stateValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.close();
|
|
|
|
|
return success;
|
|
|
|
|
}
|