GrabBag/AppUtils/UICommon/Src/CommonDialogCamera.cpp

216 lines
6.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "CommonDialogCamera.h"
#include "ui_CommonDialogCamera.h"
#include <QMessageBox>
#include <QAbstractItemView>
#include "VrLog.h"
#include "StyledMessageBox.h"
CommonDialogCamera::CommonDialogCamera(const std::vector<std::pair<std::string, IVrEyeDevice*>>& deviceList,
QWidget *parent) :
QDialog(parent),
ui(new Ui::CommonDialogCamera),
m_deviceList(deviceList)
{
ui->setupUi(this);
// 隐藏标题栏
// setWindowFlags(Qt::FramelessWindowHint);
// 检查设备列表是否有效
if (m_deviceList.empty()) {
return;
}
// 初始化相机选择下拉框
InitCameraComboBox();
// 修复下拉列表文字颜色Windows 原生样式下 .ui 中的子选择器不生效)
ui->combo_camera->view()->setStyleSheet(
"QAbstractItemView { color: rgb(221, 225, 233); background-color: rgb(47, 48, 52); selection-background-color: rgb(60, 120, 180); }");
// 设置默认选择第一个相机
m_currentCameraIndex = 0;
m_currentDevice = m_deviceList[0].second;
// 连接ComboBox的信号槽
connect(ui->combo_camera, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &CommonDialogCamera::on_camera_selection_changed);
// 初始化界面数据
InitCameraParameters();
}
CommonDialogCamera::~CommonDialogCamera()
{
delete ui;
}
void CommonDialogCamera::InitCameraComboBox()
{
// 清空ComboBox
ui->combo_camera->clear();
// 添加相机选项
for (size_t i = 0; i < m_deviceList.size(); ++i) {
ui->combo_camera->addItem(QString::fromStdString(m_deviceList[i].first));
}
// 默认选择第一个
if (!m_deviceList.empty()) {
ui->combo_camera->setCurrentIndex(0);
}
}
void CommonDialogCamera::on_camera_selection_changed(int index)
{
if (index >= 0 && index < static_cast<int>(m_deviceList.size())) {
m_currentCameraIndex = index;
m_currentDevice = m_deviceList[index].second;
// 重新加载当前选择相机的参数
InitCameraParameters();
LOG_DEBUG("Camera selection changed to index: %d\n", index);
}
}
void CommonDialogCamera::on_btn_camer_ok_clicked()
{
if (!m_currentDevice) {
StyledMessageBox::warning(this, "错误", "设备未初始化");
return;
}
// 应用参数配置
QString errorMsg;
if (ApplyCameraParameters(errorMsg)) {
StyledMessageBox::information(this, "成功", "相机参数配置成功!");
accept(); // 关闭对话框并返回Accepted
} else {
StyledMessageBox::warning(this, "失败",
QString("相机参数配置失败:\n%1").arg(errorMsg));
}
}
void CommonDialogCamera::on_btn_camer_cancel_clicked()
{
// 直接关闭窗口,不保存任何更改
reject();
}
void CommonDialogCamera::InitCameraParameters()
{
if (!m_currentDevice) return;
try {
// 获取曝光时间
unsigned int exposeTime = 0;
if (m_currentDevice->GetEyeExpose(exposeTime) == 0) {
ui->lineEdit_export->setText(QString::number(exposeTime));
LOG_DEBUG("Current expose time: %u\n", exposeTime);
}
// 获取增益
unsigned int gain = 0;
if (m_currentDevice->GetEyeGain(gain) == 0) {
ui->lineEdit_gain->setText(QString::number(gain));
LOG_DEBUG("Current gain: %u\n", gain);
}
// 获取帧率
int frame = 0;
if (m_currentDevice->GetFrame(frame) == 0) {
ui->lineEdit_frame->setText(QString::number(frame));
LOG_DEBUG("Current frame rate: %d\n", frame);
}
// 获取摆动速度
float swingSpeed = 0.0f;
if (m_currentDevice->GetSwingSpeed(swingSpeed) == 0) {
ui->lineEdit_swing_speed->setText(QString::number(swingSpeed));
LOG_DEBUG("Swing speed: %.3f\n", swingSpeed);
}
// 获取工作角度
float minAngle = 0.0f, maxAngle = 0.0f;
if (m_currentDevice->GetSwingAngle(minAngle, maxAngle) == 0) {
ui->lineEdit_swing_start->setText(QString::number(minAngle));
ui->lineEdit_swing_stop->setText(QString::number(maxAngle));
LOG_DEBUG("Swing angle range: %.3f to %.3f\n", minAngle, maxAngle);
}
} catch (...) {
StyledMessageBox::critical(this, "错误", "读取相机参数时发生异常");
}
}
bool CommonDialogCamera::ApplyCameraParameters(QString& errorMsg)
{
if (!m_currentDevice) {
errorMsg = "设备未初始化";
return false;
}
try {
QStringList failedParams;
// 设置曝光时间
unsigned int exposeTime = ui->lineEdit_export->text().toUInt();
if (m_currentDevice->SetEyeExpose(exposeTime) != 0) {
LOG_WARNING("Failed to set expose time: %u\n", exposeTime);
failedParams << QString("曝光时间设置失败 (值: %1)").arg(exposeTime);
} else {
LOG_INFO("Set expose time: %u\n", exposeTime);
}
// 设置增益
unsigned int gain = ui->lineEdit_gain->text().toUInt();
if (m_currentDevice->SetEyeGain(gain) != 0) {
LOG_WARNING("Failed to set gain: %u\n", gain);
failedParams << QString("增益设置失败 (值: %1)").arg(gain);
} else {
LOG_INFO("Set gain: %u\n", gain);
}
// 设置帧率
int frame = ui->lineEdit_frame->text().toInt();
if (m_currentDevice->SetFrame(frame) != 0) {
LOG_WARNING("Failed to set frame rate: %d\n", frame);
failedParams << QString("帧率设置失败 (值: %1)").arg(frame);
} else {
LOG_INFO("Set frame rate: %d\n", frame);
}
// 设置摆动速度
float swingSpeed = ui->lineEdit_swing_speed->text().toFloat();
if (m_currentDevice->SetSwingSpeed(swingSpeed) != 0) {
LOG_WARNING("Failed to set swing speed: %.3f\n", swingSpeed);
failedParams << QString("摆动速度设置失败 (值: %1)").arg(swingSpeed);
} else {
LOG_INFO("Set swing speed: %.3f\n", swingSpeed);
}
// 设置工作角度
float minAngle = ui->lineEdit_swing_start->text().toFloat();
float maxAngle = ui->lineEdit_swing_stop->text().toFloat();
if (m_currentDevice->SetSwingAngle(minAngle, maxAngle) != 0) {
LOG_WARNING("Failed to set swing angle: %.3f to %.3f\n", minAngle, maxAngle);
failedParams << QString("工作角度设置失败 (范围: %1 ~ %2)").arg(minAngle).arg(maxAngle);
} else {
LOG_INFO("Set swing angle: %.3f to %.3f\n", minAngle, maxAngle);
}
if (!failedParams.isEmpty()) {
errorMsg = failedParams.join("\n");
return false;
}
return true;
} catch (...) {
StyledMessageBox::critical(this, "错误", "应用相机参数时发生异常");
errorMsg = "应用相机参数时发生异常";
return false;
}
}