projectdialog.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "projectdialog.h"
  2. #include "regex.h"
  3. #include "ui_projectdialog.h"
  4. ProjectDialog::ProjectDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ProjectDialog) {
  5. ui->setupUi(this);
  6. connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ProjectDialog::validateInput);
  7. connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &ProjectDialog::clearFormData);
  8. }
  9. // 初始化爆破员列表
  10. void ProjectDialog::SetComboBoxBlast(const QJsonArray &bapoYuanArray) {
  11. blasterArray = bapoYuanArray;
  12. ui->comboBoxBlast->clear();
  13. fillComboBox(ui->comboBoxBlast, bapoYuanArray);
  14. ui->comboBoxBlast->setCurrentIndex(-1); // 设置当前索引为 -1
  15. }
  16. // 初始化操作员列表
  17. void ProjectDialog::SetComboBoxOperator(const QJsonArray &anQuanYuanArray) {
  18. operatorArray = anQuanYuanArray;
  19. ui->comboBoxOperator->clear();
  20. fillComboBox(ui->comboBoxOperator, anQuanYuanArray);
  21. ui->comboBoxOperator->setCurrentIndex(-1); // 设置当前索引为 -1
  22. }
  23. void ProjectDialog::on_comboBoxOperator_currentIndexChanged(int index) {
  24. if (index >= 0 && index < operatorArray.size()) {
  25. operatorId = operatorArray[index].toObject()["identity"].toString();
  26. }
  27. }
  28. void ProjectDialog::on_comboBoxBlast_currentIndexChanged(int index) {
  29. if (index >= 0 && index < blasterArray.size()) {
  30. blasterId = blasterArray[index].toObject()["identity"].toString();
  31. }
  32. }
  33. void ProjectDialog::fillComboBox(QComboBox *comboBox, const QJsonArray &jsonArray) {
  34. for (const QJsonValue &value : jsonArray) {
  35. if (value.isObject()) {
  36. QJsonObject obj = value.toObject();
  37. if (obj.contains("name")) {
  38. QString name = obj["name"].toString();
  39. comboBox->addItem(name);
  40. }
  41. }
  42. }
  43. }
  44. // 初始化一级地址列表
  45. void ProjectDialog::SetComboBoxAddress(const QJsonArray &Options) {
  46. parentOptions = Options;
  47. ui->comboBoxAddr->clear();
  48. for (const QJsonValue &option : Options) {
  49. ui->comboBoxAddr->addItem(option.toString());
  50. }
  51. ui->comboBoxAddr->setCurrentIndex(-1); // 设置当前索引为 -1
  52. }
  53. // 初始化二级地址列表
  54. void ProjectDialog::SetComboBoxAddress2(const QString &parentName) {
  55. ui->comboBoxAddr_2->clear();
  56. for (const QJsonValue &item : dataOptions) {
  57. const QJsonObject &obj = item.toObject();
  58. if (obj["name"].toString() == parentName) {
  59. const QJsonArray &children = obj["children"].toArray();
  60. childOptions = children;
  61. for (const QJsonValue &child : children) {
  62. ui->comboBoxAddr_2->addItem(child.toObject()["name"].toString());
  63. }
  64. break; // 找到对应的 parentName 后,跳出循环
  65. }
  66. };
  67. ui->comboBoxAddr_2->setCurrentIndex(-1); // 设置当前索引为 -1
  68. }
  69. void ProjectDialog::SetComboBoxAddress3(const QString &childName) {
  70. ui->comboBoxAddr_3->clear();
  71. for (const QJsonValue &item : dataOptions) {
  72. const QJsonArray &childrenArray = item["children"].toArray();
  73. for (const QJsonValue &child : childrenArray) {
  74. const QJsonObject &childObj = child.toObject();
  75. if (childObj["name"].toString() == childName) {
  76. const QJsonArray &grandChildrenArray = childObj["children"].toArray();
  77. for (const QJsonValue &grandChild : grandChildrenArray) {
  78. const QJsonObject &grandChildObj = grandChild.toObject();
  79. ui->comboBoxAddr_3->addItem(grandChildObj["name"].toString());
  80. }
  81. break; // 找到对应的 childName 后,跳出循环
  82. }
  83. };
  84. }
  85. ui->comboBoxAddr_3->setCurrentIndex(-1); // 设置当前索引为 -1
  86. }
  87. void ProjectDialog::setChildOptions(const QJsonArray &newDataOptions) {
  88. dataOptions = newDataOptions;
  89. }
  90. void ProjectDialog::clearFormData() {
  91. // 清除所有 QLineEdit 的文本
  92. QList<QLineEdit *> lineEdits = findChildren<QLineEdit *>();
  93. for (QLineEdit *lineEdit : lineEdits) {
  94. lineEdit->clear();
  95. }
  96. // 清除所有 QComboBox 的选择
  97. QList<QComboBox *> comboBoxes = findChildren<QComboBox *>();
  98. for (QComboBox *comboBox : comboBoxes) {
  99. comboBox->setCurrentIndex(-1);
  100. }
  101. // 可按需添加更多控件类型的清除逻辑
  102. }
  103. void ProjectDialog::on_comboBoxAddr_currentIndexChanged(int index) {
  104. if (index >= 0 && index < parentOptions.size()) {
  105. QString parentName = parentOptions[index].toString();
  106. SetComboBoxAddress2(parentName);
  107. }
  108. }
  109. void ProjectDialog::on_comboBoxAddr_2_currentIndexChanged(int index) {
  110. if (index >= 0 && index < childOptions.size()) {
  111. QString childName = childOptions[index].toObject()["name"].toString();
  112. SetComboBoxAddress3(childName);
  113. }
  114. }
  115. void ProjectDialog::validateInput() {
  116. QString detNum = ui->editDetNum->text().trimmed();
  117. QString projectName = ui->editName->text().trimmed();
  118. QString blastName = ui->comboBoxBlast->currentText().trimmed();
  119. QString operatorName = ui->comboBoxOperator->currentText().trimmed();
  120. QString parentAddress = ui->comboBoxAddr->currentText().trimmed();
  121. QString childAddress = ui->comboBoxAddr_2->currentText().trimmed();
  122. QString grandChildAddress = ui->comboBoxAddr_3->currentText().trimmed();
  123. // 检查电子邮件是否为空或格式是否正确
  124. if (detNum.isEmpty() || !ui->editDetNum->hasAcceptableInput()) {
  125. QMessageBox::warning(this, "输入错误", "请输入0-10000的数字!");
  126. return;
  127. }
  128. // 创建一个 QMap 集合,存储数据
  129. QMap<QString, QString> data;
  130. data["detNum"] = detNum;
  131. data["name"] = projectName;
  132. data["operatorName"] = operatorName;
  133. data["blasterName"] = blastName;
  134. // 拼接 addressUuid
  135. QString addressUuid = parentAddress;
  136. if (!childAddress.isEmpty()) {
  137. qDebug() << "childAddress" << childAddress;
  138. addressUuid += "/" + childAddress;
  139. if (!grandChildAddress.isEmpty()) {
  140. addressUuid += "/" + grandChildAddress;
  141. }
  142. }
  143. data["addressUuid"] = addressUuid;
  144. data["blasterIdentity"] = blasterId;
  145. data["operatorIdentity"] = operatorId;
  146. if (operationStatus == 0) {
  147. // 发射信号
  148. emit validateDetNum(data);
  149. } else if (operationStatus == 1) {
  150. emit validateDetNumUpdate(data);
  151. }
  152. clearFormData(); // 清除表单数据
  153. qDebug() << "Emitting validateDetNum signal with data*******:" << data; // 确认信号发射
  154. // this->accept();
  155. }
  156. int ProjectDialog::getOperationStatus() const { return operationStatus; }
  157. void ProjectDialog::setOperationStatus(int newOperationStatus) {
  158. operationStatus = newOperationStatus;
  159. }
  160. void ProjectDialog::setFormData(const HProject &Project) {
  161. try {
  162. ui->editName->setText(Project.getName());
  163. ui->editDetNum->setText(Project.getDetSum());
  164. ui->editHTID->setText(Project.getHtid());
  165. ui->editXMBH->setText(Project.getXmbh());
  166. int indexBlast = ui->comboBoxBlast->findText(Project.getBlasterName());
  167. if (indexBlast != -1) {
  168. ui->comboBoxBlast->setCurrentIndex(indexBlast);
  169. } else {
  170. qDebug() << "未找到选项 " << "。";
  171. }
  172. int indexOper = ui->comboBoxOperator->findText(Project.getOperatorName());
  173. if (indexOper != -1) {
  174. ui->comboBoxOperator->setCurrentIndex(indexOper);
  175. } else {
  176. qDebug() << "未找到选项 " << "。";
  177. }
  178. QStringList addressParts = Project.getAddressUuid().split("/");
  179. int numAddresses = addressParts.size();
  180. qDebug() << "numAddresses " << numAddresses;
  181. QVector<QString> addressVariables;
  182. for (int i = 0; i < numAddresses; ++i) {
  183. QString part = addressParts[i];
  184. addressVariables.append(part);
  185. }
  186. // 查找目标文本对应的索引
  187. int indexAddr = ui->comboBoxAddr->findText(addressVariables[0]);
  188. if (indexAddr != -1) {
  189. // 如果找到了对应的索引,设置为当前索引
  190. ui->comboBoxAddr->setCurrentIndex(indexAddr);
  191. qDebug() << "已将选项 " << addressVariables[0] << " 设置为当前显示的选项。";
  192. } else {
  193. // 如果没找到,输出提示信息
  194. qDebug() << "未找到选项 " << addressVariables[0];
  195. }
  196. if (addressVariables.size() >= 2) {
  197. int indexAddr_2 = ui->comboBoxAddr_2->findText(addressVariables[1]);
  198. if (indexAddr_2 != -1) {
  199. // 如果找到了对应的索引,设置为当前索引
  200. ui->comboBoxAddr_2->setCurrentIndex(indexAddr_2);
  201. qDebug() << "已将选项 " << addressVariables[1] << " 设置为当前显示的选项。";
  202. } else {
  203. // 如果没找到,输出提示信息
  204. qDebug() << "未找到选项 " << addressVariables[1];
  205. }
  206. }
  207. if (addressVariables.size() >= 3) {
  208. int indexAddr_3 = ui->comboBoxAddr_3->findText(addressVariables[2]);
  209. if (indexAddr_3 != -1) {
  210. // 如果找到了对应的索引,设置为当前索引
  211. ui->comboBoxAddr_3->setCurrentIndex(indexAddr_3);
  212. qDebug() << "已将选项 " << addressVariables[2] << " 设置为当前显示的选项。";
  213. } else {
  214. // 如果没找到,输出提示信息
  215. qDebug() << "未找到选项 " << addressVariables[2];
  216. }
  217. }
  218. } catch (const std::exception &e) {
  219. qDebug() << "发生异常: " << e.what();
  220. } catch (...) {
  221. qDebug() << "发生未知异常";
  222. }
  223. }
  224. ProjectDialog::~ProjectDialog() { delete ui; }