projectdialog.cpp 12 KB

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