/** * @file DeviceCard.h * @brief 设备卡片组件定义 * @author CasualtySightPlus Team * @date 2024-12-01 * @version 1.0 * * 设备卡片组件,用于显示单个设备的详细信息,包括: * - 设备基本信息(名称、类型、IP地址) * - 设备状态显示(在线/离线、信号强度、电量) * - 设备位置信息(经纬度坐标) * - 设备操作按钮(详情、控制、定位) * * @note 依赖Qt GUI模块 * @since 1.0 */ #ifndef DEVICECARD_H #define DEVICECARD_H // Qt核心头文件 #include #include #include #include #include #include #include #include #include #include #include #include /** * @brief 设备状态枚举 */ enum class DeviceStatus { Online, ///< 在线状态 - 绿色 Warning, ///< 警告状态 - 黄色 Offline, ///< 离线状态 - 红色 Unknown ///< 未知状态 - 灰色 }; /** * @brief 设备信息结构体 * * 包含设备的完整信息,用于设备卡片显示和数据库交互 */ struct DeviceInfo { QString id; ///< 设备唯一标识符 QString name; ///< 设备名称 QString type; ///< 设备类型 ("uav" 或 "dog") QString ipAddress; ///< IP地址 int port; ///< 端口号 double longitude; ///< 经度坐标 double latitude; ///< 纬度坐标 DeviceStatus status; ///< 设备状态 int signalStrength; ///< 信号强度 (0-100) int batteryLevel; ///< 电量水平 (0-100) QDateTime lastHeartbeat; ///< 最后心跳时间 QDateTime createdAt; ///< 创建时间 QDateTime updatedAt; ///< 更新时间 /** * @brief 默认构造函数 */ DeviceInfo() : port(0) , longitude(0.0) , latitude(0.0) , status(DeviceStatus::Unknown) , signalStrength(0) , batteryLevel(0) , lastHeartbeat(QDateTime::currentDateTime()) , createdAt(QDateTime::currentDateTime()) , updatedAt(QDateTime::currentDateTime()) { } /** * @brief 检查设备是否在线 * @return 是否在线 */ bool isOnline() const { return status == DeviceStatus::Online || status == DeviceStatus::Warning; } /** * @brief 获取设备类型显示名称 * @return 设备类型中文名称 */ QString getTypeDisplayName() const { if (type == "uav") return "无人机"; else if (type == "dog") return "机器狗"; else return "未知设备"; } /** * @brief 获取设备类型图标路径 * @return 图标资源路径 */ QString getTypeIconPath() const { if (type == "uav") return ":/image/res/image/uav.png"; else if (type == "dog") return ":/image/res/image/robot.png"; else return ":/image/res/image/info.png"; } }; /** * @class DeviceCard * @brief 设备卡片组件 * * 显示单个设备的完整信息和操作控制的卡片组件。 * 采用现代化的卡片式设计,包含设备状态指示、信息显示和操作按钮。 * * 主要功能: * - 设备信息展示:名称、类型、网络地址、位置坐标 * - 实时状态显示:在线状态、信号强度、电量水平 * - 交互操作:设备详情查看、远程控制、位置定位 * - 视觉反馈:状态颜色编码、悬停效果、点击反馈 * * @note 遵循军用风格设计,使用深蓝灰色系配色 * @warning 需要定期更新设备状态以保持界面同步 * @see DeviceInfo, DeviceStatus * @since 1.0 */ class DeviceCard : public QWidget { Q_OBJECT public: /** * @brief 构造函数 * @param device 设备信息结构体 * @param parent 父窗口指针,默认为nullptr */ explicit DeviceCard(const DeviceInfo &device, QWidget *parent = nullptr); /** * @brief 析构函数 */ ~DeviceCard(); /** * @brief 获取设备ID * @return 设备唯一标识符 */ QString getDeviceId() const { return m_deviceInfo.id; } /** * @brief 获取设备信息 * @return 完整的设备信息结构体 */ const DeviceInfo& getDeviceInfo() const { return m_deviceInfo; } /** * @brief 更新设备基本信息 * @param device 新的设备信息 */ void updateDeviceInfo(const DeviceInfo &device); /** * @brief 更新设备状态 * @param status 新的设备状态 */ void updateDeviceStatus(DeviceStatus status); /** * @brief 更新位置坐标 * @param longitude 经度 * @param latitude 纬度 */ void updateLocation(double longitude, double latitude); /** * @brief 设置卡片选中状态 * @param selected 是否选中 */ void setSelected(bool selected); /** * @brief 获取卡片选中状态 * @return 是否选中 */ bool isSelected() const { return m_isSelected; } signals: /** * @brief 设备被选中信号 * @param deviceId 设备ID */ void deviceSelected(const QString &deviceId); /** * @brief 设备控制请求信号 * @param deviceId 设备ID */ void deviceControlRequested(const QString &deviceId); /** * @brief 设备定位请求信号 * @param deviceId 设备ID */ void deviceLocationRequested(const QString &deviceId); /** * @brief 设备详情请求信号 * @param deviceId 设备ID */ void deviceDetailsRequested(const QString &deviceId); /** * @brief 设备状态变化信号 * @param deviceId 设备ID * @param status 新状态 */ void deviceStatusChanged(const QString &deviceId, DeviceStatus status); public slots: /** * @brief 刷新设备状态 * * 从数据库重新加载设备状态信息并更新显示 */ void refreshStatus(); protected: /** * @brief 鼠标按下事件 * @param event 鼠标事件 */ void mousePressEvent(QMouseEvent *event) override; /** * @brief 进入事件(鼠标悬停) * @param event 进入事件 */ void enterEvent(QEvent *event) override; /** * @brief 离开事件(鼠标离开) * @param event 离开事件 */ void leaveEvent(QEvent *event) override; /** * @brief 绘制事件 * @param event 绘制事件 */ void paintEvent(QPaintEvent *event) override; private slots: /** * @brief 详情按钮点击槽函数 */ void onDetailsClicked(); /** * @brief 控制按钮点击槽函数 */ void onControlClicked(); /** * @brief 定位按钮点击槽函数 */ void onLocationClicked(); private: /** * @brief 初始化UI界面 */ void setupUI(); /** * @brief 连接信号和槽 */ void connectSignals(); /** * @brief 设置样式 */ void setupStyle(); /** * @brief 设置动画效果 */ void setupAnimations(); /** * @brief 状态变化动画 * @param oldStatus 旧状态 * @param newStatus 新状态 */ void animateStatusChange(DeviceStatus oldStatus, DeviceStatus newStatus); /** * @brief 更新状态颜色 */ void updateStatusColor(); /** * @brief 更新设备类型图标 */ void updateDeviceIcon(); /** * @brief 获取状态显示文本 * @param status 设备状态 * @return 状态文本 */ QString getStatusText(DeviceStatus status) const; /** * @brief 获取状态颜色 * @param status 设备状态 * @return 状态颜色字符串 */ QString getStatusColor(DeviceStatus status) const; /** * @brief 格式化坐标显示 * @param longitude 经度 * @param latitude 纬度 * @return 格式化的坐标字符串 */ QString formatCoordinates(double longitude, double latitude) const; /** * @brief 获取连接按钮文本 * @return 按钮文本("连接"或"断开") */ QString getConnectionButtonText() const; /** * @brief 获取连接按钮提示文本 * @return 按钮提示文本 */ QString getConnectionButtonTooltip() const; /** * @brief 更新数据库中的设备状态 * @param status 新的设备状态 * @return 是否更新成功 */ bool updateDeviceStatusInDatabase(DeviceStatus status); private: // 设备信息 DeviceInfo m_deviceInfo; ///< 设备信息 DeviceStatus m_currentStatus; ///< 当前设备状态 bool m_isSelected; ///< 是否被选中 bool m_isHovered; ///< 是否被悬停 // UI组件 - 头部区域 QLabel *m_deviceIconLabel; ///< 设备类型图标 QLabel *m_deviceNameLabel; ///< 设备名称标签 QLabel *m_statusLabel; ///< 状态指示标签 QLabel *m_statusIndicator; ///< 状态指示器(圆点) // UI组件 - 信息区域 QLabel *m_locationLabel; ///< 位置信息标签 QLabel *m_networkLabel; ///< 网络地址标签 // UI组件 - 操作按钮 QPushButton *m_detailsButton; ///< 详情按钮 QPushButton *m_controlButton; ///< 控制按钮 QPushButton *m_locationButton; ///< 定位按钮 // 布局管理器 QVBoxLayout *m_mainLayout; ///< 主布局 QHBoxLayout *m_headerLayout; ///< 头部布局 QGridLayout *m_infoLayout; ///< 信息区域布局 QVBoxLayout *m_buttonLayout; ///< 按钮区域布局(垂直排列) // 动画效果组件 QPropertyAnimation *m_hoverAnimation; ///< 悬停动画 QPropertyAnimation *m_scaleAnimation; ///< 缩放动画 QGraphicsDropShadowEffect *m_shadowEffect; ///< 阴影效果 // 样式和配置 - 优化的卡片尺寸 static const int CARD_WIDTH = 320; ///< 卡片宽度 (再次增加) static const int CARD_HEIGHT = 240; ///< 卡片高度 (增加以显示完整信息) static const int BORDER_RADIUS = 8; ///< 圆角半径 static const int PADDING = 15; ///< 内边距 (增加) static const int MARGIN = 8; ///< 外边距 (增加) }; #endif // DEVICECARD_H