projectdialog.cpp 9.6 KB

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