123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "projectdialog.h"
- #include "ui_projectdialog.h"
- #include "regex.h"
- ProjectDialog::ProjectDialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::ProjectDialog)
- {
- ui->setupUi(this);
- connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ProjectDialog::validateInput);
- connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ProjectDialog::clearFormData);
- }
- //初始化爆破员列表
- void ProjectDialog::SetComboBoxBlast(const QJsonArray& bapoYuanArray)
- {
- blasterArray =bapoYuanArray;
- ui->comboBoxBlast->clear();
- fillComboBox(ui->comboBoxBlast, bapoYuanArray);
- ui->comboBoxBlast->setCurrentIndex(-1); // 设置当前索引为 -1
- }
- //初始化操作员列表
- void ProjectDialog::SetComboBoxOperator(const QJsonArray& anQuanYuanArray)
- {
- operatorArray =anQuanYuanArray;
- ui->comboBoxOperator->clear();
- fillComboBox(ui->comboBoxOperator, anQuanYuanArray);
- ui->comboBoxOperator->setCurrentIndex(-1); // 设置当前索引为 -1
- }
- void ProjectDialog::on_comboBoxOperator_currentIndexChanged(int index)
- {
- if (index >= 0 && index < operatorArray.size()) {
- operatorId = operatorArray[index].toObject()["identity"].toString();
- }
- }
- void ProjectDialog::on_comboBoxBlast_currentIndexChanged(int index)
- {
- if (index >= 0 && index < blasterArray.size()) {
- blasterId = blasterArray[index].toObject()["identity"].toString();
- }
- }
- void ProjectDialog::fillComboBox(QComboBox* comboBox, const QJsonArray& jsonArray) {
- for (const QJsonValue &value : jsonArray) {
- if (value.isObject()) {
- QJsonObject obj = value.toObject();
- if (obj.contains("name")) {
- QString name = obj["name"].toString();
- comboBox->addItem(name);
- }
- }
- }
- }
- //初始化一级地址列表
- void ProjectDialog::SetComboBoxAddress(const QJsonArray& Options)
- { parentOptions = Options;
- ui->comboBoxAddr->clear();
- for (const QJsonValue &option : Options) {
- ui->comboBoxAddr->addItem(option.toString());
- }
- ui->comboBoxAddr->setCurrentIndex(-1); // 设置当前索引为 -1
- }
- //初始化二级地址列表
- void ProjectDialog::SetComboBoxAddress2(const QString &parentName)
- {
- ui->comboBoxAddr_2->clear();
- for (const QJsonValue &item : dataOptions) {
- const QJsonObject &obj = item.toObject();
- if (obj["name"].toString() == parentName) {
- const QJsonArray &children = obj["children"].toArray();
- childOptions = children;
- for (const QJsonValue &child : children) {
- ui->comboBoxAddr_2->addItem(child.toObject()["name"].toString());
- }
- break; // 找到对应的 parentName 后,跳出循环
- }
- };
- ui->comboBoxAddr_2->setCurrentIndex(-1); // 设置当前索引为 -1
- }
- void ProjectDialog::SetComboBoxAddress3(const QString &childName)
- {
- ui->comboBoxAddr_3->clear();
- for (const QJsonValue &item : dataOptions) {
- const QJsonArray &childrenArray = item["children"].toArray();
- for (const QJsonValue &child : childrenArray) {
- const QJsonObject &childObj = child.toObject();
- if (childObj["name"].toString() == childName) {
- const QJsonArray &grandChildrenArray = childObj["children"].toArray();
- for (const QJsonValue &grandChild : grandChildrenArray) {
- const QJsonObject &grandChildObj = grandChild.toObject();
- ui->comboBoxAddr_3->addItem(grandChildObj["name"].toString());
- }
- break; // 找到对应的 childName 后,跳出循环
- }
- };
- }
- ui->comboBoxAddr_3->setCurrentIndex(-1); // 设置当前索引为 -1
- }
- void ProjectDialog::setChildOptions( const QJsonArray& newDataOptions)
- {
- dataOptions = newDataOptions;
- }
- void ProjectDialog::clearFormData()
- {
- // 清除所有 QLineEdit 的文本
- QList<QLineEdit*> lineEdits = findChildren<QLineEdit*>();
- for (QLineEdit* lineEdit : lineEdits) {
- lineEdit->clear();
- }
- // 清除所有 QComboBox 的选择
- QList<QComboBox*> comboBoxes = findChildren<QComboBox*>();
- for (QComboBox* comboBox : comboBoxes) {
- comboBox->setCurrentIndex(-1);
- }
- // 可按需添加更多控件类型的清除逻辑
- }
- void ProjectDialog::on_comboBoxAddr_currentIndexChanged(int index)
- {
- if (index >= 0 && index < parentOptions.size()) {
- QString parentName = parentOptions[index].toString();
- SetComboBoxAddress2(parentName);
- }
- }
- void ProjectDialog::on_comboBoxAddr_2_currentIndexChanged(int index)
- {
- if (index >= 0 && index < childOptions.size()) {
- QString childName = childOptions[index].toObject()["name"].toString();
- SetComboBoxAddress3(childName);
- }
- }
- void ProjectDialog::validateInput()
- {
- QString detNum = ui->editDetNum->text().trimmed();
- QString projectName = ui->editName->text().trimmed();
- QString blastName= ui->comboBoxBlast->currentText().trimmed();
- QString operatorName= ui->comboBoxOperator->currentText().trimmed();
- QString parentAddress= ui->comboBoxAddr->currentText().trimmed();
- QString childAddress= ui->comboBoxAddr_2->currentText().trimmed();
- QString grandChildAddress= ui->comboBoxAddr_3->currentText().trimmed();
- // 检查电子邮件是否为空或格式是否正确
- if (detNum.isEmpty() || !ui->editDetNum->hasAcceptableInput()) {
- QMessageBox::warning(this, "输入错误", "请输入0-10000的数字!");
- return;
- }
- // 创建一个 QMap 集合,存储数据
- QMap<QString, QString> data;
- data["detNum"] = detNum;
- data["name"] = projectName;
- data["operatorName"] = operatorName;
- data["blasterName"] = blastName;
- // 拼接 addressUuid
- QString addressUuid = parentAddress;
- if (!childAddress.isEmpty()) {
- qDebug()<<"childAddress"<<childAddress;
- addressUuid += "/" + childAddress;
- if (!grandChildAddress.isEmpty()) {
- addressUuid += "/" + grandChildAddress;
- }
- }
- data["addressUuid"] = addressUuid;
- data["blasterIdentity"] = blasterId;
- data["operatorIdentity"] = operatorId;
- if(operationStatus==0){
- // 发射信号
- emit validateDetNum(data);
- } else if (operationStatus==1) {
- emit validateDetNumUpdate(data);
- }
- clearFormData(); // 清除表单数据
- qDebug() << "Emitting validateDetNum signal with data*******:" << data; // 确认信号发射
- // this->accept();
- }
- int ProjectDialog::getOperationStatus() const
- {
- return operationStatus;
- }
- void ProjectDialog::setOperationStatus(int newOperationStatus)
- {
- operationStatus = newOperationStatus;
- }
- void ProjectDialog::setFormData(const HProject &Project)
- {
- try {
- ui->editName->setText(Project.getName());
- ui->editDetNum->setText(Project.getDetSum());
- ui->editHTID->setText(Project.getHtid());
- ui->editXMBH->setText(Project.getXmbh());
- int indexBlast = ui->comboBoxBlast->findText(Project.getBlasterName());
- if (indexBlast!= -1) {
- ui->comboBoxBlast->setCurrentIndex(indexBlast);
- } else {
- qDebug() << "未找到选项 " << "。";
- }
- int indexOper = ui->comboBoxOperator->findText(Project.getOperatorName());
- if (indexOper!= -1) {
- ui->comboBoxOperator->setCurrentIndex(indexOper);
- } else {
- qDebug() << "未找到选项 " << "。";
- }
- QStringList addressParts = Project.getAddressUuid().split("/");
- int numAddresses = addressParts.size();
- qDebug() << "numAddresses " << numAddresses;
- QVector<QString> addressVariables;
- for (int i = 0; i < numAddresses; ++i) {
- QString part = addressParts[i];
- addressVariables.append(part);
- }
- // 查找目标文本对应的索引
- int indexAddr = ui->comboBoxAddr->findText(addressVariables[0]);
- if (indexAddr != -1) {
- // 如果找到了对应的索引,设置为当前索引
- ui->comboBoxAddr->setCurrentIndex(indexAddr);
- qDebug() << "已将选项 " << addressVariables[0] << " 设置为当前显示的选项。";
- } else {
- // 如果没找到,输出提示信息
- qDebug() << "未找到选项 " << addressVariables[0];
- }
- if(addressVariables.size()>=2){
- int indexAddr_2 = ui->comboBoxAddr_2->findText(addressVariables[1]);
- if (indexAddr_2 != -1) {
- // 如果找到了对应的索引,设置为当前索引
- ui->comboBoxAddr_2->setCurrentIndex(indexAddr_2);
- qDebug() << "已将选项 " << addressVariables[1] << " 设置为当前显示的选项。";
- } else {
- // 如果没找到,输出提示信息
- qDebug() << "未找到选项 " << addressVariables[1];
- }
- }
- if(addressVariables.size()>=3){
- int indexAddr_3 = ui->comboBoxAddr_3->findText(addressVariables[2]);
- if (indexAddr_3 != -1) {
- // 如果找到了对应的索引,设置为当前索引
- ui->comboBoxAddr_3->setCurrentIndex(indexAddr_3);
- qDebug() << "已将选项 " << addressVariables[2] << " 设置为当前显示的选项。";
- } else {
- // 如果没找到,输出提示信息
- qDebug() << "未找到选项 " << addressVariables[2];
- }
- }
- } catch (const std::exception& e) {
- qDebug() << "发生异常: " << e.what();
- } catch (...) {
- qDebug() << "发生未知异常";
- }
- }
- ProjectDialog::~ProjectDialog()
- {
- delete ui;
- }
|