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.

178 lines
7.3 KiB
C++

3 months ago
#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);
QString filePath = QApplication::applicationDirPath() + "/config/Config.json";
Common::GetInstance()->loadConfig(filePath);
m_productsCfg = Common::GetInstance()->getProductConfigs();
m_workmodesCfg = Common::GetInstance()->getWorkModeConfigs();
m_productNums = m_productsCfg.size();
initUI(); //一定放在前面
loadConfig();
}
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);
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);
QLabel *reworknameLab = new QLabel("返工", groupBox);
groupLayout->addWidget(reworknameLab, 1, 0);
QLineEdit *reworkPathEdit = new QLineEdit(workmodeCfg.reworkPath, groupBox);
groupLayout->addWidget(reworkPathEdit, 1, 1);
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);
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);
comboBox->addItem("GLOBAL", 0);
comboBox->addItem("LOCAL", 1);
m_modeComboBoxes.push_back(comboBox);
layout->addWidget(comboBox, i + 1, 3);
}
// 将 QSignalMapper 的信号连接到槽函数
connect(checkBoxMapper, static_cast<void(QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &SettingWgt::onCheckBoxStateChanged);
}
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);
m_modeComboBoxes[index - 1]->setCurrentIndex(mode);
}
}
void SettingWgt::onCheckBoxStateChanged(int index)
{
QCheckBox *checkBox = m_selectCheckBoxes[index];
if (checkBox && checkBox->isChecked())
{
QString formatStr = m_snFormatLineEdits[index]->text();
if (formatStr.isEmpty())
{
checkBox->setCheckState(Qt::Unchecked);
QMessageBox::warning(this, "启用失败", "产品SN适配为空请先进行设置再进行启用");
}
}
}
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]->currentIndex());
}
Common::GetInstance()->saveConfig(m_productsCfg, m_workmodesCfg);
emit SettingFinishSig();
QMessageBox::information(this,"提示","设置成功!");
}