#include "NuclideRayDialog.h" NuclideRayDialog::NuclideRayDialog(bool isEdit, const QStringList& rayData, QWidget *parent) : QDialog(parent), m_isEdit(isEdit) { // 编辑模式下提取ID和已有数据 if (isEdit && !rayData.isEmpty()) { m_id = rayData[0]; // 第一个元素是ID } initUI(); // 编辑模式:填充已有数据 if (isEdit && rayData.size() >= 8) { m_cmbRayType->setCurrentText(rayData[1]); m_leRayMev->setText(rayData[2]); m_leRayMevUnc->setText(rayData[3]); m_leBranchRatio->setText(rayData[4]); m_leBranchRatioUnc->setText(rayData[5]); m_cmbMainRayIdent->setCurrentText(rayData[6]); m_leJiahePeak->setText(rayData[7]); } } void NuclideRayDialog::initUI() { setWindowTitle(m_isEdit ? "编辑射线信息" : "添加射线信息"); setFixedSize(450, 300); QFormLayout *formLayout = new QFormLayout(this); // 射线类型下拉框(常用核素射线类型) m_cmbRayType = new QComboBox(this); m_cmbRayType->addItems({"γ", "β⁻", "β⁺", "α", "X射线", "中子", "电子俘获"}); formLayout->addRow("射线类型", m_cmbRayType); m_leRayMev = new QLineEdit(this); m_leRayMev->setPlaceholderText("例如:1.332 MeV"); formLayout->addRow("射线能量", m_leRayMev); m_leRayMevUnc = new QLineEdit(this); m_leRayMevUnc->setPlaceholderText("例如:0.001 MeV"); formLayout->addRow("能量不确定度", m_leRayMevUnc); m_leBranchRatio = new QLineEdit(this); m_leBranchRatio->setPlaceholderText("例如:0.9998"); formLayout->addRow("分支比", m_leBranchRatio); m_leBranchRatioUnc = new QLineEdit(this); m_leBranchRatioUnc->setPlaceholderText("例如:0.0001"); formLayout->addRow("分支比不确定度", m_leBranchRatioUnc); // 主射线标识下拉框 m_cmbMainRayIdent = new QComboBox(this); m_cmbMainRayIdent->addItems({"是", "否"}); formLayout->addRow("是否为主射线", m_cmbMainRayIdent); m_leJiahePeak = new QLineEdit(this); m_leJiahePeak->setPlaceholderText("无则留空"); formLayout->addRow("加和峰", m_leJiahePeak); // 按钮盒 QDialogButtonBox *btnBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); formLayout->addRow(btnBox); // 信号连接 connect(btnBox, &QDialogButtonBox::accepted, this, [this]() { if (validateInput()) { accept(); } }); connect(btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } bool NuclideRayDialog::validateInput() { // 必填项校验 if (m_leRayMev->text().trimmed().isEmpty()) { QMessageBox::warning(this, "输入错误", "射线能量不能为空!"); return false; } if (m_leRayMevUnc->text().trimmed().isEmpty()) { QMessageBox::warning(this, "输入错误", "能量不确定度不能为空!"); return false; } if (m_leBranchRatioUnc->text().trimmed().isEmpty()) { QMessageBox::warning(this, "输入错误", "分支比不确定度不能为空!"); return false; } // 数字格式校验(允许带单位) bool ok; // 提取数字部分进行校验 QString mevText = m_leRayMev->text().trimmed().replace(QRegExp("[^0-9.]"), ""); mevText.toDouble(&ok); if (!ok) { QMessageBox::warning(this, "输入错误", "射线能量必须为有效数字!"); return false; } QString mevUncText = m_leRayMevUnc->text().trimmed().replace(QRegExp("[^0-9.]"), ""); mevUncText.toDouble(&ok); if (!ok) { QMessageBox::warning(this, "输入错误", "能量不确定度必须为有效数字!"); return false; } QString ratioUncText = m_leBranchRatioUnc->text().trimmed().replace(QRegExp("[^0-9.]"), ""); ratioUncText.toDouble(&ok); if (!ok) { QMessageBox::warning(this, "输入错误", "分支比不确定度必须为有效数字!"); return false; } return true; } QStringList NuclideRayDialog::getRayData() const { QStringList data; if (m_isEdit) { data << m_id; // 编辑模式返回ID } data << m_cmbRayType->currentText().trimmed() << m_leRayMev->text().trimmed() << m_leRayMevUnc->text().trimmed() << m_leBranchRatio->text().trimmed() << m_leBranchRatioUnc->text().trimmed() << m_cmbMainRayIdent->currentText().trimmed() << m_leJiahePeak->text().trimmed(); return data; }