397 lines
14 KiB
C++
397 lines
14 KiB
C++
#include "DialogConfig.h"
|
||
#include "ui_dialogconfig.h"
|
||
#include "DemoControlPresenter.h"
|
||
#include "StyledMessageBox.h"
|
||
#include <QDebug>
|
||
#include <QSettings>
|
||
#include <QCoreApplication>
|
||
#include <QStandardPaths>
|
||
#include <QDir>
|
||
#include <QFile>
|
||
|
||
DialogConfig::DialogConfig(DemoControlPresenter* presenter, QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::DialogConfig)
|
||
, m_presenter(presenter)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
// 设置窗口标志
|
||
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
|
||
|
||
// 加载当前配置
|
||
loadCurrentConfig();
|
||
}
|
||
|
||
DialogConfig::~DialogConfig()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
QString DialogConfig::GetConfigFilePath()
|
||
{
|
||
// 获取用户应用数据目录
|
||
QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||
|
||
// 确保目录存在
|
||
QDir dir(appDataPath);
|
||
if (!dir.exists()) {
|
||
dir.mkpath(appDataPath);
|
||
}
|
||
|
||
// 配置文件路径
|
||
QString configFilePath = appDataPath + "/config.ini";
|
||
|
||
// 如果配置文件不存在,尝试从应用程序目录复制默认配置
|
||
if (!QFile::exists(configFilePath)) {
|
||
QString defaultConfigPath = QCoreApplication::applicationDirPath() + "/config.ini";
|
||
if (QFile::exists(defaultConfigPath)) {
|
||
QFile::copy(defaultConfigPath, configFilePath);
|
||
qDebug() << "已从应用程序目录复制默认配置到用户目录:" << configFilePath;
|
||
} else {
|
||
qDebug() << "默认配置文件不存在,将创建新配置:" << configFilePath;
|
||
}
|
||
}
|
||
|
||
return configFilePath;
|
||
}
|
||
|
||
void DialogConfig::loadConfigFromFile()
|
||
{
|
||
QSettings settings(GetConfigFilePath(), QSettings::IniFormat);
|
||
|
||
// 控制器配置
|
||
ui->lineEdit_controller_ip->setText(settings.value("Controller/IP", "192.168.0.88").toString());
|
||
ui->spinBox_controller_port->setValue(settings.value("Controller/Port", 502).toInt());
|
||
|
||
// 机械臂配置
|
||
ui->lineEdit_robot_ip->setText(settings.value("Robot/IP", "192.168.58.2").toString());
|
||
|
||
// 准备位置
|
||
ui->doubleSpinBox_ready_x->setValue(settings.value("ReadyPosition/X", 0.0).toDouble());
|
||
ui->doubleSpinBox_ready_y->setValue(settings.value("ReadyPosition/Y", 0.0).toDouble());
|
||
ui->doubleSpinBox_ready_z->setValue(settings.value("ReadyPosition/Z", 500.0).toDouble());
|
||
ui->doubleSpinBox_ready_rx->setValue(settings.value("ReadyPosition/RX", 0.0).toDouble());
|
||
ui->doubleSpinBox_ready_ry->setValue(settings.value("ReadyPosition/RY", 0.0).toDouble());
|
||
ui->doubleSpinBox_ready_rz->setValue(settings.value("ReadyPosition/RZ", 0.0).toDouble());
|
||
|
||
// 放置位置
|
||
ui->doubleSpinBox_place_x->setValue(settings.value("PlacePosition/X", 500.0).toDouble());
|
||
ui->doubleSpinBox_place_y->setValue(settings.value("PlacePosition/Y", 0.0).toDouble());
|
||
ui->doubleSpinBox_place_z->setValue(settings.value("PlacePosition/Z", 200.0).toDouble());
|
||
ui->doubleSpinBox_place_rx->setValue(settings.value("PlacePosition/RX", 0.0).toDouble());
|
||
ui->doubleSpinBox_place_ry->setValue(settings.value("PlacePosition/RY", 0.0).toDouble());
|
||
ui->doubleSpinBox_place_rz->setValue(settings.value("PlacePosition/RZ", 0.0).toDouble());
|
||
|
||
// 抓取偏移
|
||
ui->doubleSpinBox_grasp_offset->setValue(settings.value("Grasp/OffsetZ", -50.0).toDouble());
|
||
|
||
// 运动参数
|
||
ui->spinBox_tool->setValue(settings.value("Motion/ToolNumber", 0).toInt());
|
||
ui->doubleSpinBox_velocity->setValue(settings.value("Motion/Velocity", 50.0).toDouble());
|
||
ui->doubleSpinBox_acceleration->setValue(settings.value("Motion/Acceleration", 50.0).toDouble());
|
||
}
|
||
|
||
void DialogConfig::saveConfigToFile()
|
||
{
|
||
QSettings settings(GetConfigFilePath(), QSettings::IniFormat);
|
||
|
||
// 控制器配置
|
||
settings.setValue("Controller/IP", ui->lineEdit_controller_ip->text());
|
||
settings.setValue("Controller/Port", ui->spinBox_controller_port->value());
|
||
|
||
// 机械臂配置
|
||
settings.setValue("Robot/IP", ui->lineEdit_robot_ip->text());
|
||
|
||
// 准备位置
|
||
settings.setValue("ReadyPosition/X", ui->doubleSpinBox_ready_x->value());
|
||
settings.setValue("ReadyPosition/Y", ui->doubleSpinBox_ready_y->value());
|
||
settings.setValue("ReadyPosition/Z", ui->doubleSpinBox_ready_z->value());
|
||
settings.setValue("ReadyPosition/RX", ui->doubleSpinBox_ready_rx->value());
|
||
settings.setValue("ReadyPosition/RY", ui->doubleSpinBox_ready_ry->value());
|
||
settings.setValue("ReadyPosition/RZ", ui->doubleSpinBox_ready_rz->value());
|
||
|
||
// 放置位置
|
||
settings.setValue("PlacePosition/X", ui->doubleSpinBox_place_x->value());
|
||
settings.setValue("PlacePosition/Y", ui->doubleSpinBox_place_y->value());
|
||
settings.setValue("PlacePosition/Z", ui->doubleSpinBox_place_z->value());
|
||
settings.setValue("PlacePosition/RX", ui->doubleSpinBox_place_rx->value());
|
||
settings.setValue("PlacePosition/RY", ui->doubleSpinBox_place_ry->value());
|
||
settings.setValue("PlacePosition/RZ", ui->doubleSpinBox_place_rz->value());
|
||
|
||
// 抓取偏移
|
||
settings.setValue("Grasp/OffsetZ", ui->doubleSpinBox_grasp_offset->value());
|
||
|
||
// 运动参数
|
||
settings.setValue("Motion/ToolNumber", ui->spinBox_tool->value());
|
||
settings.setValue("Motion/Velocity", ui->doubleSpinBox_velocity->value());
|
||
settings.setValue("Motion/Acceleration", ui->doubleSpinBox_acceleration->value());
|
||
|
||
settings.sync();
|
||
}
|
||
|
||
void DialogConfig::loadCurrentConfig()
|
||
{
|
||
// 从配置文件加载
|
||
loadConfigFromFile();
|
||
}
|
||
|
||
void DialogConfig::saveConfig()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
// 保存到配置文件
|
||
saveConfigToFile();
|
||
|
||
// 保存准备位置
|
||
m_presenter->SetReadyPosition(
|
||
ui->doubleSpinBox_ready_x->value(),
|
||
ui->doubleSpinBox_ready_y->value(),
|
||
ui->doubleSpinBox_ready_z->value(),
|
||
ui->doubleSpinBox_ready_rx->value(),
|
||
ui->doubleSpinBox_ready_ry->value(),
|
||
ui->doubleSpinBox_ready_rz->value()
|
||
);
|
||
|
||
// 保存放置位置
|
||
m_presenter->SetPlacePosition(
|
||
ui->doubleSpinBox_place_x->value(),
|
||
ui->doubleSpinBox_place_y->value(),
|
||
ui->doubleSpinBox_place_z->value(),
|
||
ui->doubleSpinBox_place_rx->value(),
|
||
ui->doubleSpinBox_place_ry->value(),
|
||
ui->doubleSpinBox_place_rz->value()
|
||
);
|
||
|
||
// 保存抓取偏移
|
||
m_presenter->SetGraspOffset(ui->doubleSpinBox_grasp_offset->value());
|
||
|
||
// 保存运动参数
|
||
m_presenter->SetToolNumber(ui->spinBox_tool->value());
|
||
m_presenter->SetVelocity(ui->doubleSpinBox_velocity->value());
|
||
m_presenter->SetAcceleration(ui->doubleSpinBox_acceleration->value());
|
||
}
|
||
|
||
// 静态方法:从配置文件加载配置到Presenter
|
||
void DialogConfig::LoadConfigToPresenter(DemoControlPresenter* presenter)
|
||
{
|
||
if (!presenter) {
|
||
return;
|
||
}
|
||
|
||
QSettings settings(GetConfigFilePath(), QSettings::IniFormat);
|
||
|
||
// 加载准备位置
|
||
presenter->SetReadyPosition(
|
||
settings.value("ReadyPosition/X", 0.0).toDouble(),
|
||
settings.value("ReadyPosition/Y", 0.0).toDouble(),
|
||
settings.value("ReadyPosition/Z", 500.0).toDouble(),
|
||
settings.value("ReadyPosition/RX", 0.0).toDouble(),
|
||
settings.value("ReadyPosition/RY", 0.0).toDouble(),
|
||
settings.value("ReadyPosition/RZ", 0.0).toDouble()
|
||
);
|
||
|
||
// 加载放置位置
|
||
presenter->SetPlacePosition(
|
||
settings.value("PlacePosition/X", 500.0).toDouble(),
|
||
settings.value("PlacePosition/Y", 0.0).toDouble(),
|
||
settings.value("PlacePosition/Z", 200.0).toDouble(),
|
||
settings.value("PlacePosition/RX", 0.0).toDouble(),
|
||
settings.value("PlacePosition/RY", 0.0).toDouble(),
|
||
settings.value("PlacePosition/RZ", 0.0).toDouble()
|
||
);
|
||
|
||
// 加载抓取偏移
|
||
presenter->SetGraspOffset(settings.value("Grasp/OffsetZ", -50.0).toDouble());
|
||
|
||
// 加载运动参数
|
||
presenter->SetToolNumber(settings.value("Motion/ToolNumber", 0).toInt());
|
||
presenter->SetVelocity(settings.value("Motion/Velocity", 50.0).toDouble());
|
||
presenter->SetAcceleration(settings.value("Motion/Acceleration", 50.0).toDouble());
|
||
}
|
||
|
||
void DialogConfig::on_btn_save_clicked()
|
||
{
|
||
saveConfig();
|
||
StyledMessageBox::information(this, "保存成功", "配置已保存,重启后生效");
|
||
accept();
|
||
}
|
||
|
||
void DialogConfig::on_btn_cancel_clicked()
|
||
{
|
||
reject();
|
||
}
|
||
|
||
void DialogConfig::on_btn_test_controller_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
QString ip = ui->lineEdit_controller_ip->text();
|
||
int port = ui->spinBox_controller_port->value();
|
||
|
||
// 断开当前连接
|
||
m_presenter->DisconnectController();
|
||
|
||
// 尝试连接
|
||
bool success = m_presenter->ConnectToController(ip, port);
|
||
|
||
if (success) {
|
||
StyledMessageBox::information(this, "测试成功", "控制器连接成功");
|
||
} else {
|
||
StyledMessageBox::warning(this, "测试失败", "控制器连接失败,请检查IP和端口");
|
||
}
|
||
}
|
||
|
||
void DialogConfig::on_btn_test_robot_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
QString ip = ui->lineEdit_robot_ip->text();
|
||
|
||
// 断开当前连接
|
||
m_presenter->DisconnectRobot();
|
||
|
||
// 尝试连接
|
||
bool success = m_presenter->ConnectToRobot(ip);
|
||
|
||
if (success) {
|
||
StyledMessageBox::information(this, "测试成功", "机械臂连接成功");
|
||
} else {
|
||
StyledMessageBox::warning(this, "测试失败", "机械臂连接失败,请检查IP地址");
|
||
}
|
||
}
|
||
|
||
void DialogConfig::on_btn_test_ready_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
if (!m_presenter->IsRobotConnected()) {
|
||
StyledMessageBox::warning(this, "测试失败", "机械臂未连接,请先连接机械臂");
|
||
return;
|
||
}
|
||
|
||
// 先保存当前配置到Presenter(不保存到文件)
|
||
m_presenter->SetReadyPosition(
|
||
ui->doubleSpinBox_ready_x->value(),
|
||
ui->doubleSpinBox_ready_y->value(),
|
||
ui->doubleSpinBox_ready_z->value(),
|
||
ui->doubleSpinBox_ready_rx->value(),
|
||
ui->doubleSpinBox_ready_ry->value(),
|
||
ui->doubleSpinBox_ready_rz->value()
|
||
);
|
||
|
||
// 测试移动
|
||
bool success = m_presenter->TestMoveToReady();
|
||
|
||
if (success) {
|
||
StyledMessageBox::information(this, "测试成功", "机械臂已移动到准备位置");
|
||
} else {
|
||
StyledMessageBox::warning(this, "测试失败", "机械臂移动失败,请检查位置参数");
|
||
}
|
||
}
|
||
|
||
void DialogConfig::on_btn_test_place_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
if (!m_presenter->IsRobotConnected()) {
|
||
StyledMessageBox::warning(this, "测试失败", "机械臂未连接,请先连接机械臂");
|
||
return;
|
||
}
|
||
|
||
// 先保存当前配置到Presenter(不保存到文件)
|
||
m_presenter->SetPlacePosition(
|
||
ui->doubleSpinBox_place_x->value(),
|
||
ui->doubleSpinBox_place_y->value(),
|
||
ui->doubleSpinBox_place_z->value(),
|
||
ui->doubleSpinBox_place_rx->value(),
|
||
ui->doubleSpinBox_place_ry->value(),
|
||
ui->doubleSpinBox_place_rz->value()
|
||
);
|
||
|
||
// 测试移动
|
||
bool success = m_presenter->TestMoveToPlace();
|
||
|
||
if (success) {
|
||
StyledMessageBox::information(this, "测试成功", "机械臂已移动到放置位置");
|
||
} else {
|
||
StyledMessageBox::warning(this, "测试失败", "机械臂移动失败,请检查位置参数");
|
||
}
|
||
}
|
||
|
||
void DialogConfig::on_btn_read_ready_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
if (!m_presenter->IsRobotConnected()) {
|
||
StyledMessageBox::warning(this, "读取失败", "机械臂未连接,请先连接机械臂");
|
||
return;
|
||
}
|
||
|
||
// 读取当前位置
|
||
double x, y, z, rx, ry, rz;
|
||
bool success = m_presenter->GetCurrentPosition(x, y, z, rx, ry, rz);
|
||
|
||
if (success) {
|
||
// 填充到准备位置输入框
|
||
ui->doubleSpinBox_ready_x->setValue(x);
|
||
ui->doubleSpinBox_ready_y->setValue(y);
|
||
ui->doubleSpinBox_ready_z->setValue(z);
|
||
ui->doubleSpinBox_ready_rx->setValue(rx);
|
||
ui->doubleSpinBox_ready_ry->setValue(ry);
|
||
ui->doubleSpinBox_ready_rz->setValue(rz);
|
||
|
||
QString message = QString("已读取当前位置到准备位置") +
|
||
QString("\nX=%1, Y=%2, Z=%3").arg(x, 0, 'f', 2).arg(y, 0, 'f', 2).arg(z, 0, 'f', 2) +
|
||
QString("\nRX=%1, RY=%2, RZ=%3").arg(rx, 0, 'f', 2).arg(ry, 0, 'f', 2).arg(rz, 0, 'f', 2);
|
||
StyledMessageBox::information(this, "读取成功", message);
|
||
} else {
|
||
StyledMessageBox::warning(this, "读取失败", "读取机械臂当前位置失败");
|
||
}
|
||
}
|
||
|
||
void DialogConfig::on_btn_read_place_clicked()
|
||
{
|
||
if (!m_presenter) {
|
||
return;
|
||
}
|
||
|
||
if (!m_presenter->IsRobotConnected()) {
|
||
StyledMessageBox::warning(this, "读取失败", "机械臂未连接,请先连接机械臂");
|
||
return;
|
||
}
|
||
|
||
// 读取当前位置
|
||
double x, y, z, rx, ry, rz;
|
||
bool success = m_presenter->GetCurrentPosition(x, y, z, rx, ry, rz);
|
||
|
||
if (success) {
|
||
// 填充到放置位置输入框
|
||
ui->doubleSpinBox_place_x->setValue(x);
|
||
ui->doubleSpinBox_place_y->setValue(y);
|
||
ui->doubleSpinBox_place_z->setValue(z);
|
||
ui->doubleSpinBox_place_rx->setValue(rx);
|
||
ui->doubleSpinBox_place_ry->setValue(ry);
|
||
ui->doubleSpinBox_place_rz->setValue(rz);
|
||
|
||
QString message = QString("已读取当前位置到放置位置") +
|
||
QString("\nX=%1, Y=%2, Z=%3").arg(x, 0, 'f', 2).arg(y, 0, 'f', 2).arg(z, 0, 'f', 2) +
|
||
QString("\nRX=%1, RY=%2, RZ=%3").arg(rx, 0, 'f', 2).arg(ry, 0, 'f', 2).arg(rz, 0, 'f', 2);
|
||
StyledMessageBox::information(this, "读取成功", message);
|
||
} else {
|
||
StyledMessageBox::warning(this, "读取失败", "读取机械臂当前位置失败");
|
||
}
|
||
}
|