You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
8.0 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma execution_character_set("utf-8")
#include "settingwgt.h"
#include "ui_settingwgt.h"
#include <QApplication>
#include <QSettings>
#include <QFileDialog>
#include <QSignalMapper>
#include <QVBoxLayout>
#include <QMessageBox>
#include <fstream>
#include <QDebug>
SettingWgt::SettingWgt(QWidget *parent) :
QWidget(parent),
ui(new Ui::SettingDlg)
{
ui->setupUi(this);
m_productsCfg = Common::GetInstance()->getProductConfigs();
m_workmodesCfg = Common::GetInstance()->getWorkModeConfigs();
m_productNums = m_productsCfg.size();
m_workModeNums = m_workmodesCfg.size();
initUI(); //一定放在前面
}
SettingWgt::~SettingWgt()
{
delete ui;
}
void SettingWgt::initUI()
{
//加载工作模式配置并显示
ui->frame_set_mode->setFrameStyle(QFrame::Box | QFrame::Raised);
QVBoxLayout *vlayout = new QVBoxLayout(ui->frame_set_mode);
QSignalMapper *selectWorkPathMapper = new QSignalMapper(this);
QSignalMapper *selectReworkPathMapper = new QSignalMapper(this);
for (const auto& workmodeCfg : m_workmodesCfg)
{
QGroupBox *groupBox = new QGroupBox(workmodeCfg.name, ui->frame_set_mode);
QGridLayout *groupLayout = new QGridLayout(groupBox);
QLabel *nameLab = new QLabel("正常", groupBox);
groupLayout->addWidget(nameLab, 0, 0);
QLineEdit *workPathEdit = new QLineEdit(workmodeCfg.workPath, groupBox);
workPathEdit->setEnabled(false);
groupLayout->addWidget(workPathEdit, 0, 1);
QPushButton *selectworkPathBtn = new QPushButton("<", groupBox);
selectworkPathBtn->setMaximumWidth(30);
groupLayout->addWidget(selectworkPathBtn, 0, 2);
connect(selectworkPathBtn, &QPushButton::clicked, selectWorkPathMapper, qOverload<>(&QSignalMapper::map));
selectWorkPathMapper->setMapping(selectworkPathBtn, workmodeCfg.index);
selectworkPathBtn->hide();
QLabel *reworknameLab = new QLabel("返工", groupBox);
groupLayout->addWidget(reworknameLab, 1, 0);
QLineEdit *reworkPathEdit = new QLineEdit(workmodeCfg.reworkPath, groupBox);
groupLayout->addWidget(reworkPathEdit, 1, 1);
reworkPathEdit->setEnabled(false);
QPushButton *selectReworkPathBtn = new QPushButton("<", groupBox);
selectReworkPathBtn->setMaximumWidth(30);
groupLayout->addWidget(selectReworkPathBtn, 1, 2);
connect(selectReworkPathBtn, &QPushButton::clicked, selectReworkPathMapper, qOverload<>(&QSignalMapper::map));
selectReworkPathMapper->setMapping(selectReworkPathBtn, workmodeCfg.index);
selectReworkPathBtn->hide();
m_workPathLineEdits.insert(workmodeCfg.index, qMakePair(workPathEdit, reworkPathEdit));
vlayout->addWidget(groupBox);
}
// 连接 QSignalMapper 的信号到目标槽函数
connect(selectWorkPathMapper, qOverload<int>(&QSignalMapper::mapped), [&](int index){
SelectPathButton_clicked(index, false);
});
connect(selectReworkPathMapper, qOverload<int>(&QSignalMapper::mapped), [&](int index){
SelectPathButton_clicked(index, true);
});
// 产品列表
ui->frame_setting->setFrameStyle(QFrame::Box | QFrame::Raised);
QGridLayout *layout = new QGridLayout(ui->frame_setting);
layout->setVerticalSpacing(10);
QLabel *head1 = new QLabel("启用", ui->frame_setting);
head1->setAlignment(Qt::AlignCenter);
QLabel *head2 = new QLabel("产品SN适配设置", ui->frame_setting);
head2->setAlignment(Qt::AlignCenter);
QLabel *head3 = new QLabel("模式", ui->frame_setting);
head3->setAlignment(Qt::AlignCenter);
layout->addWidget(head1, 0, 1);
layout->addWidget(head2, 0, 2);
layout->addWidget(head3, 0, 3);
QSignalMapper *checkBoxMapper = new QSignalMapper(this);
for (int i = 0; i < m_productNums; ++i)
{
// 行头创建
QString str = QString("产品%1").arg(i + 1);
QLabel *label = new QLabel(str, ui->frame_setting);
layout->addWidget(label, i + 1, 0);
// 启用复选框创建
QCheckBox *checkbox = new QCheckBox(ui->frame_setting);
connect(checkbox, &QCheckBox::stateChanged, checkBoxMapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
checkBoxMapper->setMapping(checkbox, i);
m_selectCheckBoxes.push_back(checkbox);
layout->addWidget(checkbox, i + 1, 1);
// 产品SN适配设置输入框创建
QLineEdit *lineEdit = new QLineEdit(ui->frame_setting);
lineEdit->setEnabled(false);
m_snFormatLineEdits.push_back(lineEdit);
layout->addWidget(lineEdit, i + 1, 2);
// 工作模式下拉框创建
QComboBox *comboBox = new QComboBox(ui->frame_setting);
for (const auto& productCfg : m_workmodesCfg)
{
comboBox->addItem(productCfg.name, productCfg.index);
}
m_modeComboBoxes.push_back(comboBox);
layout->addWidget(comboBox, i + 1, 3);
comboBox->setEnabled(false);
}
// 将 QSignalMapper 的信号连接到槽函数
connect(checkBoxMapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &SettingWgt::onCheckBoxStateChanged);
loadConfig();
}
void SettingWgt::loadConfig()
{
// 显示产品配置
for (const auto product : m_productsCfg)
{
int index = product.productId;
bool selected = product.selected;
int mode = product.workMode;
m_snFormatLineEdits[index - 1]->setText(product.productFormat);
m_selectCheckBoxes[index - 1]->setCheckState(selected ? Qt::Checked : Qt::Unchecked);
int pos = m_modeComboBoxes[index - 1]->findData(QVariant(mode));
if (pos != -1)
{
m_modeComboBoxes[index - 1]->setCurrentIndex(pos);
}
}
}
void SettingWgt::onCheckBoxStateChanged(int index)
{
QCheckBox *checkBox = m_selectCheckBoxes[index];
if (checkBox)
{
if (checkBox->isChecked())
{
QString formatStr = m_snFormatLineEdits[index]->text();
if (formatStr.isEmpty())
{
checkBox->setCheckState(Qt::Unchecked);
QMessageBox::warning(this, "启用失败", "产品SN适配为空请先进行设置再进行启用");
return;
}
}
m_productsCfg[index + 1].selected = checkBox->isChecked();
Common::GetInstance()->updateProductsConfig(m_productsCfg);
}
}
void SettingWgt::SelectPathButton_clicked(int index, bool isRework)
{
// 定义选择的路径和是否是 reworkPath 的标志
QString exe_path = QFileDialog::getOpenFileName(this, tr("选择可执行程序"),
QCoreApplication::applicationDirPath(),
tr("可执行程序(*.exe);;其他(*.*)"));
if (exe_path.isEmpty())
return;
if (!isRework)
{
m_workPathLineEdits[index].first->setText(exe_path);
m_workmodesCfg[index].workPath = exe_path;
}
else
{
m_workPathLineEdits[index].second->setText(exe_path);
m_workmodesCfg[index].reworkPath = exe_path;
}
}
void SettingWgt::on_pushButton_ok_clicked()
{
for (int i = 0 ; i < m_productNums; ++i)
{
m_productsCfg[i + 1].selected = m_selectCheckBoxes[i]->isChecked();
m_productsCfg[i + 1].productFormat = m_snFormatLineEdits[i]->text().trimmed();
m_productsCfg[i + 1].workMode = static_cast<Mode>(m_modeComboBoxes[i]->currentData().toInt());
}
for (auto& workmodeCfg : m_workmodesCfg)
{
workmodeCfg.workPath = m_workPathLineEdits[workmodeCfg.index].first->text().trimmed();
workmodeCfg.reworkPath = m_workPathLineEdits[workmodeCfg.index].second->text().trimmed();
}
Common::GetInstance()->saveConfig(m_productsCfg, m_workmodesCfg);
emit SettingFinishSig();
QMessageBox::information(this,"提示","设置成功!");
}