GrabBag/AppUtils/UICommon/Src/NetworkConfigWidget.cpp

238 lines
9.6 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 "NetworkConfigWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QFont>
NetworkConfigWidget::NetworkConfigWidget(bool showPlcConfig, bool showTcpConfig, QWidget *parent)
: QWidget(parent)
, m_comboEulerOrder(nullptr)
, m_comboDirVectorInvert(nullptr)
, m_comboByteOrder(nullptr)
, m_comboLongAxisDir(nullptr)
, m_groupPlc(nullptr)
, m_editPlcIp(nullptr)
, m_editPlcPort(nullptr)
, m_editAddrPhotoRequest(nullptr)
, m_editAddrDataComplete(nullptr)
, m_editAddrCoordDataStart(nullptr)
, m_groupTcp(nullptr)
, m_editTcpPort(nullptr)
{
setupUI(showPlcConfig, showTcpConfig);
}
NetworkConfigWidget::~NetworkConfigWidget()
{
}
void NetworkConfigWidget::setupUI(bool showPlcConfig, bool showTcpConfig)
{
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(10);
// 通用样式
QString labelStyle = "color: rgb(221, 225, 233); background-color: transparent;";
QString editStyle = "color: rgb(221, 225, 233); background-color: rgb(47, 48, 52); border: 1px solid rgb(70, 72, 78); padding: 4px;";
QString comboStyle = editStyle;
QString groupStyle = "QGroupBox { color: rgb(221, 225, 233); border: 1px solid rgb(100, 100, 100); border-radius: 4px; margin-top: 12px; padding-top: 8px; } QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 3px 0 3px; }";
QFont font;
font.setPointSize(14);
// 通用参数组
QGroupBox* groupCommon = new QGroupBox("通信参数", this);
groupCommon->setFont(font);
groupCommon->setStyleSheet(groupStyle);
QFormLayout* commonForm = new QFormLayout(groupCommon);
commonForm->setSpacing(8);
// 欧拉角旋转顺序
m_comboEulerOrder = new QComboBox(this);
m_comboEulerOrder->setFont(font);
m_comboEulerOrder->setStyleSheet(comboStyle);
initEulerOrderComboBox();
QLabel* labelEuler = new QLabel("欧拉角旋转顺序:");
labelEuler->setFont(font);
labelEuler->setStyleSheet(labelStyle);
commonForm->addRow(labelEuler, m_comboEulerOrder);
// 方向向量反向
m_comboDirVectorInvert = new QComboBox(this);
m_comboDirVectorInvert->setFont(font);
m_comboDirVectorInvert->setStyleSheet(comboStyle);
initDirVectorInvertComboBox();
QLabel* labelDir = new QLabel("方向向量调整:");
labelDir->setFont(font);
labelDir->setStyleSheet(labelStyle);
commonForm->addRow(labelDir, m_comboDirVectorInvert);
// 目标物长边对应轴
m_comboLongAxisDir = new QComboBox(this);
m_comboLongAxisDir->setFont(font);
m_comboLongAxisDir->setStyleSheet(comboStyle);
initLongAxisDirComboBox();
QLabel* labelLongAxis = new QLabel("长边对应轴:");
labelLongAxis->setFont(font);
labelLongAxis->setStyleSheet(labelStyle);
commonForm->addRow(labelLongAxis, m_comboLongAxisDir);
// 数据字节序
m_comboByteOrder = new QComboBox(this);
m_comboByteOrder->setFont(font);
m_comboByteOrder->setStyleSheet(comboStyle);
initByteOrderComboBox();
QLabel* labelByte = new QLabel("数据字节序:");
labelByte->setFont(font);
labelByte->setStyleSheet(labelStyle);
commonForm->addRow(labelByte, m_comboByteOrder);
mainLayout->addWidget(groupCommon);
// PLC服务端配置组可选
if (showPlcConfig) {
m_groupPlc = new QGroupBox("PLC服务端配置", this);
m_groupPlc->setFont(font);
m_groupPlc->setStyleSheet(groupStyle);
QFormLayout* plcForm = new QFormLayout(m_groupPlc);
plcForm->setSpacing(8);
m_editPlcIp = new QLineEdit(this);
m_editPlcIp->setFont(font);
m_editPlcIp->setStyleSheet(editStyle);
QLabel* l1 = new QLabel("PLC IP地址:"); l1->setFont(font); l1->setStyleSheet(labelStyle);
plcForm->addRow(l1, m_editPlcIp);
m_editPlcPort = new QLineEdit(this);
m_editPlcPort->setFont(font);
m_editPlcPort->setStyleSheet(editStyle);
QLabel* l2 = new QLabel("PLC端口:"); l2->setFont(font); l2->setStyleSheet(labelStyle);
plcForm->addRow(l2, m_editPlcPort);
m_editAddrPhotoRequest = new QLineEdit(this);
m_editAddrPhotoRequest->setFont(font);
m_editAddrPhotoRequest->setStyleSheet(editStyle);
QLabel* l3 = new QLabel("拍照请求地址:"); l3->setFont(font); l3->setStyleSheet(labelStyle);
plcForm->addRow(l3, m_editAddrPhotoRequest);
m_editAddrDataComplete = new QLineEdit(this);
m_editAddrDataComplete->setFont(font);
m_editAddrDataComplete->setStyleSheet(editStyle);
QLabel* l4 = new QLabel("数据完成地址:"); l4->setFont(font); l4->setStyleSheet(labelStyle);
plcForm->addRow(l4, m_editAddrDataComplete);
m_editAddrCoordDataStart = new QLineEdit(this);
m_editAddrCoordDataStart->setFont(font);
m_editAddrCoordDataStart->setStyleSheet(editStyle);
QLabel* l5 = new QLabel("坐标数据起始地址:"); l5->setFont(font); l5->setStyleSheet(labelStyle);
plcForm->addRow(l5, m_editAddrCoordDataStart);
mainLayout->addWidget(m_groupPlc);
}
// TCP服务端配置组可选
if (showTcpConfig) {
m_groupTcp = new QGroupBox("TCP服务端配置", this);
m_groupTcp->setFont(font);
m_groupTcp->setStyleSheet(groupStyle);
QFormLayout* tcpForm = new QFormLayout(m_groupTcp);
tcpForm->setSpacing(8);
m_editTcpPort = new QLineEdit(this);
m_editTcpPort->setFont(font);
m_editTcpPort->setStyleSheet(editStyle);
QLabel* l6 = new QLabel("TCP端口:"); l6->setFont(font); l6->setStyleSheet(labelStyle);
tcpForm->addRow(l6, m_editTcpPort);
mainLayout->addWidget(m_groupTcp);
}
mainLayout->addStretch();
}
void NetworkConfigWidget::initEulerOrderComboBox()
{
m_comboEulerOrder->addItem("RZ-RY-RX (外旋ZYX)", EULER_ORDER_ZYX);
m_comboEulerOrder->addItem("RX-RY-RZ (外旋XYZ)", EULER_ORDER_XYZ);
m_comboEulerOrder->addItem("RZ-RX-RY (外旋ZXY)", EULER_ORDER_ZXY);
m_comboEulerOrder->addItem("RY-RX-RZ (外旋YXZ)", EULER_ORDER_YXZ);
m_comboEulerOrder->addItem("RY-RZ-RX (外旋YZX)", EULER_ORDER_YZX);
m_comboEulerOrder->addItem("RX-RZ-RY (外旋XZY)", EULER_ORDER_XZY);
m_comboEulerOrder->setCurrentIndex(0); // 默认ZYX
}
void NetworkConfigWidget::initDirVectorInvertComboBox()
{
m_comboDirVectorInvert->addItem("不调整", NET_DIR_INVERT_NONE);
m_comboDirVectorInvert->addItem("XY反向", NET_DIR_INVERT_XY);
m_comboDirVectorInvert->addItem("XZ反向", NET_DIR_INVERT_XZ);
m_comboDirVectorInvert->addItem("YZ反向", NET_DIR_INVERT_YZ);
m_comboDirVectorInvert->setCurrentIndex(0); // 默认不调整
}
void NetworkConfigWidget::initByteOrderComboBox()
{
m_comboByteOrder->addItem("大端序 (ABCD)", NET_BYTE_ORDER_BIG);
m_comboByteOrder->addItem("小端序 (DCBA)", NET_BYTE_ORDER_LITTLE);
m_comboByteOrder->setCurrentIndex(0); // 默认大端序
}
void NetworkConfigWidget::initLongAxisDirComboBox()
{
m_comboLongAxisDir->addItem("X轴", 0);
m_comboLongAxisDir->addItem("Y轴", 1);
m_comboLongAxisDir->setCurrentIndex(0); // 默认X轴
}
void NetworkConfigWidget::setConfig(const NetworkConfigData& config)
{
// 欧拉角旋转顺序
int idx = m_comboEulerOrder->findData(config.eulerOrder);
if (idx >= 0) m_comboEulerOrder->setCurrentIndex(idx);
// 方向向量反向
idx = m_comboDirVectorInvert->findData(config.dirVectorInvert);
if (idx >= 0) m_comboDirVectorInvert->setCurrentIndex(idx);
// 长边对应轴
idx = m_comboLongAxisDir->findData(config.longAxisDir);
if (idx >= 0) m_comboLongAxisDir->setCurrentIndex(idx);
// 字节序
idx = m_comboByteOrder->findData(config.byteOrder);
if (idx >= 0) m_comboByteOrder->setCurrentIndex(idx);
// PLC配置
if (m_editPlcIp) m_editPlcIp->setText(config.plcServerIp);
if (m_editPlcPort) m_editPlcPort->setText(QString::number(config.plcServerPort));
if (m_editAddrPhotoRequest) m_editAddrPhotoRequest->setText(QString::number(config.addrPhotoRequest));
if (m_editAddrDataComplete) m_editAddrDataComplete->setText(QString::number(config.addrDataComplete));
if (m_editAddrCoordDataStart) m_editAddrCoordDataStart->setText(QString::number(config.addrCoordDataStart));
// TCP配置
if (m_editTcpPort) m_editTcpPort->setText(QString::number(config.tcpServerPort));
}
NetworkConfigData NetworkConfigWidget::getConfig() const
{
NetworkConfigData config;
config.eulerOrder = m_comboEulerOrder->currentData().toInt();
config.dirVectorInvert = m_comboDirVectorInvert->currentData().toInt();
config.byteOrder = m_comboByteOrder->currentData().toInt();
config.longAxisDir = m_comboLongAxisDir->currentData().toInt();
if (m_editPlcIp) config.plcServerIp = m_editPlcIp->text().trimmed();
if (m_editPlcPort) config.plcServerPort = m_editPlcPort->text().toInt();
if (m_editAddrPhotoRequest) config.addrPhotoRequest = m_editAddrPhotoRequest->text().toInt();
if (m_editAddrDataComplete) config.addrDataComplete = m_editAddrDataComplete->text().toInt();
if (m_editAddrCoordDataStart) config.addrCoordDataStart = m_editAddrCoordDataStart->text().toInt();
if (m_editTcpPort) config.tcpServerPort = m_editTcpPort->text().toInt();
return config;
}
int NetworkConfigWidget::eulerOrder() const { return m_comboEulerOrder->currentData().toInt(); }
int NetworkConfigWidget::dirVectorInvert() const { return m_comboDirVectorInvert->currentData().toInt(); }
int NetworkConfigWidget::byteOrder() const { return m_comboByteOrder->currentData().toInt(); }
int NetworkConfigWidget::longAxisDir() const { return m_comboLongAxisDir->currentData().toInt(); }