276 lines
9.2 KiB
C++
276 lines
9.2 KiB
C++
#include "dialogalgoarg.h"
|
||
#include "ui_dialogalgoarg.h"
|
||
#include <QMessageBox>
|
||
#include <QPushButton>
|
||
#include <QtCore/QCoreApplication>
|
||
#include <QtCore/QFileInfo>
|
||
#include <QtCore/QDir>
|
||
#include <QtCore/QStandardPaths>
|
||
#include <QtCore/QFile>
|
||
#include <cstring>
|
||
#include "PathManager.h"
|
||
#include "StyledMessageBox.h"
|
||
|
||
#include "VrLog.h"
|
||
|
||
DialogAlgoarg::DialogAlgoarg(IVrConfig* vrConfig, QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::DialogAlgoarg)
|
||
, m_vrConfig(vrConfig)
|
||
{
|
||
try {
|
||
ui->setupUi(this);
|
||
|
||
// 隐藏标题栏
|
||
// setWindowFlags(Qt::FramelessWindowHint);
|
||
|
||
// 获取配置文件路径
|
||
m_configFilePath = PathManager::GetConfigFilePath();
|
||
|
||
// 检查配置文件路径是否有效
|
||
if (m_configFilePath.isEmpty()) {
|
||
StyledMessageBox::critical(this, "错误", "无法获取配置文件路径!");
|
||
return;
|
||
}
|
||
|
||
// 从配置文件加载数据到界面
|
||
LoadConfigToUI();
|
||
|
||
} catch (const std::exception& e) {
|
||
StyledMessageBox::critical(this, "初始化错误", QString("对话框初始化失败: %1").arg(e.what()));
|
||
} catch (...) {
|
||
StyledMessageBox::critical(this, "初始化错误", "对话框初始化失败!(未知错误)");
|
||
}
|
||
}
|
||
|
||
DialogAlgoarg::~DialogAlgoarg()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void DialogAlgoarg::LoadConfigToUI()
|
||
{
|
||
if (!m_vrConfig) {
|
||
StyledMessageBox::critical(this, "错误", "配置对象未初始化!");
|
||
return;
|
||
}
|
||
|
||
try {
|
||
// 尝试加载配置文件
|
||
int ret = m_vrConfig->LoadConfig(m_configFilePath.toStdString(), m_configData);
|
||
if (ret != LOAD_CONFIG_SUCCESS) {
|
||
// 配置文件加载失败,使用默认参数
|
||
LOG_WARNING("Failed to load config file (error code: %d), using default parameters\n", ret);
|
||
StyledMessageBox::information(this, "提示",
|
||
QString("配置文件加载失败(错误代码: %1),将使用默认参数显示").arg(ret));
|
||
|
||
// 使用 IVrConfig.h 中定义的默认值初始化 ConfigResult
|
||
m_configData = ConfigResult(); // 调用默认构造函数,获取所有默认值
|
||
}
|
||
|
||
// 检查配置文件路径是否有效
|
||
if (m_configFilePath.isEmpty()) {
|
||
LOG_WARNING("Config file path is empty\n");
|
||
}
|
||
|
||
// 加载算法参数到UI(无论是从文件加载还是使用默认值)
|
||
const VrAlgorithmParams& algoParams = m_configData.algorithmParams;
|
||
|
||
// 加载各个参数组
|
||
LoadWorkpieceParamToUI(algoParams.workpieceParam);
|
||
LoadFilterParamToUI(algoParams.filterParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadGrowParamToUI(algoParams.growParam);
|
||
|
||
} catch (const std::exception& e) {
|
||
LOG_ERROR("Exception in LoadConfigToUI: %s\n", e.what());
|
||
StyledMessageBox::warning(this, "警告",
|
||
QString("加载配置时发生异常: %1\n将使用默认参数显示").arg(e.what()));
|
||
|
||
// 发生异常时也使用默认参数
|
||
m_configData = ConfigResult();
|
||
const VrAlgorithmParams& algoParams = m_configData.algorithmParams;
|
||
LoadWorkpieceParamToUI(algoParams.workpieceParam);
|
||
LoadFilterParamToUI(algoParams.filterParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadGrowParamToUI(algoParams.growParam);
|
||
} catch (...) {
|
||
LOG_ERROR("Unknown exception in LoadConfigToUI\n");
|
||
StyledMessageBox::warning(this, "警告", "加载配置文件失败(未知错误),将使用默认参数显示");
|
||
|
||
// 发生未知异常时也使用默认参数
|
||
m_configData = ConfigResult();
|
||
const VrAlgorithmParams& algoParams = m_configData.algorithmParams;
|
||
LoadWorkpieceParamToUI(algoParams.workpieceParam);
|
||
LoadFilterParamToUI(algoParams.filterParam);
|
||
LoadCornerParamToUI(algoParams.cornerParam);
|
||
LoadGrowParamToUI(algoParams.growParam);
|
||
}
|
||
}
|
||
|
||
void DialogAlgoarg::LoadWorkpieceParamToUI(const VrWorkpieceParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_lineLen->setText(QString::number(param.lineLen));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadFilterParamToUI(const VrOutlierFilterParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_continuityTh->setText(QString::number(param.continuityTh));
|
||
ui->lineEdit_outlierTh->setText(QString::number(param.outlierTh));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadCornerParamToUI(const VrCornerParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_cornerTh->setText(QString::number(param.cornerTh));
|
||
ui->lineEdit_scale->setText(QString::number(param.scale));
|
||
ui->lineEdit_minEndingGap->setText(QString::number(param.minEndingGap));
|
||
ui->lineEdit_minEndingGap_z->setText(QString::number(param.minEndingGap_z));
|
||
ui->lineEdit_jumpCornerTh_1->setText(QString::number(param.jumpCornerTh_1));
|
||
ui->lineEdit_jumpCornerTh_2->setText(QString::number(param.jumpCornerTh_2));
|
||
}
|
||
|
||
void DialogAlgoarg::LoadGrowParamToUI(const VrTreeGrowParam& param)
|
||
{
|
||
if (!ui) return;
|
||
|
||
ui->lineEdit_maxLineSkipNum->setText(QString::number(param.maxLineSkipNum));
|
||
ui->lineEdit_yDeviation_max->setText(QString::number(param.yDeviation_max));
|
||
ui->lineEdit_maxSkipDistance->setText(QString::number(param.maxSkipDistance));
|
||
ui->lineEdit_zDeviation_max->setText(QString::number(param.zDeviation_max));
|
||
ui->lineEdit_minLTypeTreeLen->setText(QString::number(param.minLTypeTreeLen));
|
||
ui->lineEdit_minVTypeTreeLen->setText(QString::number(param.minVTypeTreeLen));
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveConfigFromUI()
|
||
{
|
||
if (!m_vrConfig) {
|
||
return false;
|
||
}
|
||
|
||
try {
|
||
VrAlgorithmParams& algoParams = m_configData.algorithmParams;
|
||
|
||
// 保存各个参数组
|
||
if (!SaveWorkpieceParamFromUI(algoParams.workpieceParam)) {
|
||
StyledMessageBox::warning(this, "错误", "工件参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
if (!SaveFilterParamFromUI(algoParams.filterParam)) {
|
||
StyledMessageBox::warning(this, "错误", "滤波参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
if (!SaveCornerParamFromUI(algoParams.cornerParam)) {
|
||
StyledMessageBox::warning(this, "错误", "拐角参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
if (!SaveGrowParamFromUI(algoParams.growParam)) {
|
||
StyledMessageBox::warning(this, "错误", "生长参数输入有误,请检查!");
|
||
return false;
|
||
}
|
||
|
||
// 保存到文件
|
||
return m_vrConfig->SaveConfig(m_configFilePath.toStdString(), m_configData);
|
||
|
||
} catch (const std::exception& e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveWorkpieceParamFromUI(VrWorkpieceParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.lineLen = ui->lineEdit_lineLen->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveFilterParamFromUI(VrOutlierFilterParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.continuityTh = ui->lineEdit_continuityTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.outlierTh = ui->lineEdit_outlierTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveCornerParamFromUI(VrCornerParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.cornerTh = ui->lineEdit_cornerTh->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.scale = ui->lineEdit_scale->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minEndingGap = ui->lineEdit_minEndingGap->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minEndingGap_z = ui->lineEdit_minEndingGap_z->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.jumpCornerTh_1 = ui->lineEdit_jumpCornerTh_1->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.jumpCornerTh_2 = ui->lineEdit_jumpCornerTh_2->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
bool DialogAlgoarg::SaveGrowParamFromUI(VrTreeGrowParam& param)
|
||
{
|
||
bool ok = true;
|
||
|
||
param.maxLineSkipNum = ui->lineEdit_maxLineSkipNum->text().toInt(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.yDeviation_max = ui->lineEdit_yDeviation_max->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.maxSkipDistance = ui->lineEdit_maxSkipDistance->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.zDeviation_max = ui->lineEdit_zDeviation_max->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minLTypeTreeLen = ui->lineEdit_minLTypeTreeLen->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
param.minVTypeTreeLen = ui->lineEdit_minVTypeTreeLen->text().toDouble(&ok);
|
||
if (!ok) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
void DialogAlgoarg::on_btn_camer_ok_clicked()
|
||
{
|
||
if (SaveConfigFromUI()) {
|
||
StyledMessageBox::information(this, "成功", "配置保存成功!");
|
||
accept();
|
||
} else {
|
||
StyledMessageBox::warning(this, "失败", "配置保存失败,请检查文件权限或参数输入!");
|
||
}
|
||
}
|
||
|
||
void DialogAlgoarg::on_btn_camer_cancel_clicked()
|
||
{
|
||
// 直接关闭窗口,不保存任何更改
|
||
reject();
|
||
}
|