203 lines
6.2 KiB
C++
203 lines
6.2 KiB
C++
#include "devstatus.h"
|
||
#include "ui_devstatus.h"
|
||
#include <QPainter>
|
||
#include <QBrush>
|
||
#include "VrLog.h"
|
||
|
||
devstatus::devstatus(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::devstatus)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
// 强制应用样式表 - 这是关键!
|
||
this->setAttribute(Qt::WA_StyledBackground, true);
|
||
|
||
// 初始化时设置样式
|
||
setItemStyle();
|
||
|
||
// 初始化设备状态(离线状态)
|
||
updateCamera1Status(false);
|
||
updateCamera2Status(false);
|
||
updateRobotStatus(false);
|
||
}
|
||
|
||
devstatus::~devstatus()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
// 设置devstatus的样式
|
||
void devstatus::setItemStyle()
|
||
{
|
||
this->setStyleSheet(
|
||
"devstatus { "
|
||
" border: 2px solid #191A1C; " // 添加边框作为格子间分隔线
|
||
" border-radius: 0px; " // 去掉圆角,让分隔线更清晰
|
||
"} "
|
||
);
|
||
}
|
||
|
||
// 设置相机状态图片的私有成员函数
|
||
void devstatus::setCameraStatusImage(QWidget* widget, bool isOnline) {
|
||
if (!widget) return;
|
||
|
||
QString imagePath = isOnline ? ":/resource/camera_online.png" : ":/resource/camera_offline.png";
|
||
|
||
widget->setStyleSheet(QString("image: url(%1);").arg(imagePath));
|
||
// widget->setFixedSize(32, 32); // 调整大小以适应图片
|
||
}
|
||
|
||
// 设置机械臂状态图片的私有成员函数
|
||
void devstatus::setRobotStatusImage(QWidget* widget, bool isOnline) {
|
||
if (!widget) return;
|
||
|
||
QString imagePath = isOnline ? ":/resource/robot_online.png" : ":/resource/robot_offline.png";
|
||
|
||
widget->setStyleSheet(QString("image: url(%1); ").arg(imagePath));
|
||
// widget->setFixedSize(32, 32); // 调整大小以适应图片
|
||
}
|
||
|
||
// 更新相机1状态
|
||
void devstatus::updateCamera1Status(bool isConnected)
|
||
{
|
||
setCameraStatusImage(ui->dev_camer_1_img, isConnected);
|
||
|
||
QString statusText = isConnected ? "相机1在线" : "相机1离线";
|
||
QString colorStyle = isConnected ? "color: rgb(140, 180, 60);" : "color: rgb(220, 60, 60);";
|
||
|
||
ui->dev_camera_1_txt->setText(statusText);
|
||
ui->dev_camera_1_txt->setStyleSheet(colorStyle);
|
||
}
|
||
|
||
// 更新相机2状态
|
||
void devstatus::updateCamera2Status(bool isConnected)
|
||
{
|
||
setCameraStatusImage(ui->dev_camer_2_img, isConnected);
|
||
|
||
QString statusText = isConnected ? "相机2在线" : "相机2离线";
|
||
QString colorStyle = isConnected ? "color: rgb(140, 180, 60);" : "color: rgb(220, 60, 60);";
|
||
|
||
ui->dev_camera_2_txt->setText(statusText);
|
||
ui->dev_camera_2_txt->setStyleSheet(colorStyle);
|
||
}
|
||
|
||
// 更新机械臂状态
|
||
void devstatus::updateRobotStatus(bool isConnected)
|
||
{
|
||
setRobotStatusImage(ui->dev_robot_img, isConnected);
|
||
|
||
QString statusText = isConnected ? "机械臂在线" : "机械臂离线";
|
||
QString colorStyle = isConnected ? "color: rgb(140, 180, 60);" : "color: rgb(220, 60, 60);";
|
||
|
||
ui->dev_robot_txt->setText(statusText);
|
||
ui->dev_robot_txt->setStyleSheet(colorStyle);
|
||
}
|
||
|
||
// 更新串口状态
|
||
void devstatus::updateSerialStatus(bool isConnected)
|
||
{
|
||
setRobotStatusImage(ui->dev_robot_img, isConnected);
|
||
QString statusText = isConnected ? "RS485在线" : "RS485离线";
|
||
QString colorStyle = isConnected ? "color: rgb(140, 180, 60);" : "color: rgb(220, 60, 60);";
|
||
|
||
ui->dev_robot_txt->setText(statusText);
|
||
ui->dev_robot_txt->setStyleSheet(colorStyle);
|
||
}
|
||
|
||
// 设置相机数量(用于控制相机二的显示和布局方向)
|
||
void devstatus::setCameraCount(int cameraCount)
|
||
{
|
||
// 如果相机数量小于2,隐藏相机二相关的UI元素
|
||
bool showCamera2 = (cameraCount >= 2);
|
||
|
||
LOG_DEBUG("setCameraCount: %d \n", cameraCount);
|
||
|
||
// 隐藏整个相机2的frame,而不是单独隐藏图片和文字
|
||
if (ui->frame_camera_2) {
|
||
ui->frame_camera_2->setVisible(showCamera2);
|
||
} else {
|
||
// 如果frame_camera_2不存在,则单独隐藏图片和文字控件
|
||
ui->dev_camer_2_img->setVisible(showCamera2);
|
||
ui->dev_camera_2_txt->setVisible(showCamera2);
|
||
}
|
||
|
||
// 根据相机数量选择布局方式
|
||
if (cameraCount >= 2) {
|
||
// 多相机:纵向排列
|
||
setVerticalLayout();
|
||
} else {
|
||
// 单相机:横向排列,相机和机械臂各占一半
|
||
setHorizontalLayoutForSingleCamera();
|
||
}
|
||
}
|
||
|
||
// 设置为纵向布局
|
||
void devstatus::setVerticalLayout()
|
||
{
|
||
// 调整主widget的尺寸以适应纵向布局
|
||
this->setMinimumSize(180, 198);
|
||
this->resize(180, 198);
|
||
|
||
// 通过调整每个frame的尺寸和位置来模拟垂直布局
|
||
adjustFramesForVerticalLayout();
|
||
|
||
LOG_DEBUG("Layout adjusted for vertical display\n");
|
||
}
|
||
|
||
// 设置为单相机横向布局(相机和机械臂各占一半)
|
||
void devstatus::setHorizontalLayoutForSingleCamera()
|
||
{
|
||
// 设置单相机模式的widget尺寸
|
||
this->setMinimumSize(180, 80);
|
||
this->resize(180, 80);
|
||
|
||
// 调整为单相机横向布局的frame尺寸和位置
|
||
adjustFramesForSingleCameraHorizontalLayout();
|
||
|
||
LOG_DEBUG("Layout adjusted for single camera horizontal display\n");
|
||
}
|
||
|
||
// 调整frames为纵向排列
|
||
void devstatus::adjustFramesForVerticalLayout()
|
||
{
|
||
int frameWidth = 180;
|
||
int frameHeight = 64;
|
||
int spacing = 2;
|
||
int startY = 0;
|
||
|
||
// 重新定位frame的位置以实现纵向排列效果
|
||
if (ui->frame_camera_1) {
|
||
ui->frame_camera_1->setGeometry(0, startY, frameWidth, frameHeight);
|
||
}
|
||
|
||
if (ui->frame_camera_2) {
|
||
ui->frame_camera_2->setGeometry(0, startY + frameHeight + spacing, frameWidth, frameHeight);
|
||
startY += frameHeight + spacing;
|
||
}
|
||
|
||
if (ui->frame_robot) {
|
||
ui->frame_robot->setGeometry(0, startY + frameHeight + spacing, frameWidth, frameHeight);
|
||
}
|
||
}
|
||
|
||
// 调整frames为单相机横向排列(相机和机械臂各占一半)
|
||
void devstatus::adjustFramesForSingleCameraHorizontalLayout()
|
||
{
|
||
int frameWidth = 275; // 每个frame占一半宽度 (180/2 - 5间距)
|
||
int frameHeight = 70;
|
||
int spacing = 6;
|
||
|
||
// 相机1占左半边
|
||
if (ui->frame_camera_1) {
|
||
ui->frame_camera_1->setGeometry(0, 0, frameWidth, frameHeight);
|
||
}
|
||
|
||
// 相机2隐藏(在setCameraCount中已处理)
|
||
|
||
// 机械臂占右半边
|
||
if (ui->frame_robot) {
|
||
ui->frame_robot->setGeometry(frameWidth + spacing, 0, frameWidth, frameHeight);
|
||
}
|
||
}
|