projectdialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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::validateAndSaveProject);
  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::SetL1AddressOptions(const QJsonArray &Options) {
  42. parentOptions = Options;
  43. ui->comboBoxAddr->clear();
  44. for (const QJsonValue &option : Options) {
  45. ui->comboBoxAddr->addItem(option.toObject()["name"].toString(), option.toObject()["uuid"].toString());
  46. }
  47. ui->comboBoxAddr->setCurrentIndex(-1);
  48. }
  49. void ProjectDialog::updateL2AddressOptions() {
  50. ui->comboBoxAddr_2->clear();
  51. for (const QJsonValue &child : L2AddressOptions) {
  52. ui->comboBoxAddr_2->addItem(child.toObject()["name"].toString(), child.toObject()["uuid"].toString());
  53. }
  54. ui->comboBoxAddr_2->setCurrentIndex(-1);
  55. }
  56. void ProjectDialog::updateL3AddressOptions() {
  57. ui->comboBoxAddr_3->clear();
  58. for (const QJsonValue &child : L3AddressOptions) {
  59. ui->comboBoxAddr_3->addItem(child.toObject()["name"].toString(), child.toObject()["uuid"].toString());
  60. }
  61. ui->comboBoxAddr_3->setCurrentIndex(-1);
  62. }
  63. void ProjectDialog::setChildOptions(const QJsonArray &newDataOptions) { dataOptions = newDataOptions; }
  64. void ProjectDialog::SetComboBoxLora(const QString &grandChildName) {
  65. ui->comboBoxLora->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. const QJsonArray &grandChildrenArray = childObj["children"].toArray();
  71. for (const QJsonValue &grandChild : grandChildrenArray) {
  72. const QJsonObject &grandChildObj = grandChild.toObject();
  73. if (grandChildObj["name"].toString() == grandChildName) {
  74. const QJsonArray &greatGrandChildrenArray = grandChildObj["children"].toArray();
  75. for (const QJsonValue &greatGrandChild : greatGrandChildrenArray) {
  76. const QJsonObject &greatGrandChildObj = greatGrandChild.toObject();
  77. qDebug() << "greatGrandChildObj " << greatGrandChildObj;
  78. QString name = greatGrandChildObj["name"].toString();
  79. QString loraSn = greatGrandChildObj["loraSn"].toString();
  80. ui->comboBoxLora->addItem(name);
  81. nameLoraSnMap.insert(name, loraSn);
  82. QHash<QString, QString>::const_iterator i;
  83. for (i = nameLoraSnMap.constBegin(); i != nameLoraSnMap.constEnd(); ++i) {
  84. qDebug() << "Key:" << i.key() << ", Value:" << i.value();
  85. }
  86. }
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. ui->comboBoxLora->setCurrentIndex(-1);
  93. }
  94. void ProjectDialog::extractNames(const QJsonArray &array, QStringList &names) {
  95. for (const auto &item : array) {
  96. if (item.isObject()) {
  97. QJsonObject obj = item.toObject();
  98. if (obj.contains("name")) {
  99. names.append(obj["name"].toString());
  100. }
  101. if (obj.contains("children") && obj["children"].isArray()) {
  102. extractNames(obj["children"].toArray(), names);
  103. }
  104. }
  105. }
  106. }
  107. void ProjectDialog::clearFormData() {
  108. QList<QLineEdit *> lineEdits = findChildren<QLineEdit *>();
  109. for (QLineEdit *lineEdit : lineEdits) {
  110. lineEdit->clear();
  111. }
  112. QList<QComboBox *> comboBoxes = findChildren<QComboBox *>();
  113. for (QComboBox *comboBox : comboBoxes) {
  114. comboBox->setCurrentIndex(-1);
  115. }
  116. }
  117. void ProjectDialog::on_comboBoxAddr_currentIndexChanged(int index) {
  118. if (index >= 0 && index < parentOptions.size()) {
  119. L2AddressOptions = parentOptions[index].toObject()["children"].toArray();
  120. updateL2AddressOptions();
  121. }
  122. }
  123. void ProjectDialog::on_comboBoxAddr_2_currentIndexChanged(int index) {
  124. if (index >= 0 && index < L2AddressOptions.size()) {
  125. QJsonArray subOptions = L2AddressOptions[index].toObject()["children"].toArray();
  126. L3AddressOptions = subOptions;
  127. updateL3AddressOptions();
  128. }
  129. }
  130. void ProjectDialog::on_comboBoxAddr_3_currentIndexChanged(int index) {
  131. if (index >= 0 && index < L3AddressOptions.size()) {
  132. QString grandChildName = L3AddressOptions[index].toObject()["uuid"].toString();
  133. SetComboBoxLora(grandChildName);
  134. }
  135. }
  136. void ProjectDialog::validateAndSaveProject() {
  137. QString detNum = ui->editDetNum->text().trimmed();
  138. QString blastCount = ui->editRegCount->text().trimmed();
  139. QString projectName = ui->editName->text().trimmed();
  140. QString blastName = ui->comboBoxBlast->currentText().trimmed();
  141. QString operatorName = ui->comboBoxOperator->currentText().trimmed();
  142. QString l1AddressUuid = ui->comboBoxAddr->currentData().toString().trimmed();
  143. QString l2AddressUuid = ui->comboBoxAddr_2->currentData().toString().trimmed();
  144. QString l3AddressUuid = ui->comboBoxAddr_3->currentData().toString().trimmed();
  145. QString l1AddressName = ui->comboBoxAddr->currentText().trimmed();
  146. QString l2AddressName = ui->comboBoxAddr_2->currentText().trimmed();
  147. QString l3AddressName = ui->comboBoxAddr_3->currentText().trimmed();
  148. QString loraAddress = ui->comboBoxLora->currentText().trimmed();
  149. if (detNum.isEmpty() || !ui->editDetNum->hasAcceptableInput()) {
  150. QMessageBox::warning(nullptr, "输入错误", "请输入0-10000的数字!");
  151. return;
  152. }
  153. // 创建一个 QMap 集合,存储数据
  154. QMap<QString, QString> data;
  155. data["detNum"] = detNum;
  156. data["name"] = projectName;
  157. data["operatorName"] = operatorName;
  158. data["blasterName"] = blastName;
  159. // 选择level最低的uui:w
  160. QString addressUuid = l1AddressUuid;
  161. if (l3AddressUuid != "") {
  162. addressUuid = l3AddressUuid;
  163. } else if (l2AddressUuid != "") {
  164. addressUuid = l2AddressUuid;
  165. } else if (l1AddressUuid != "") {
  166. addressUuid = l1AddressUuid;
  167. } else {
  168. QMessageBox::warning(nullptr, "输入错误", "请选择地址!");
  169. return;
  170. }
  171. data["addressPath"] = l1AddressName + "/" + l2AddressName + "/" + l3AddressName;
  172. data["addressUuid"] = addressUuid;
  173. data["blasterIdentity"] = blasterId;
  174. data["operatorIdentity"] = operatorId;
  175. data["loraAddress"] = loraAddress;
  176. data["loraSn"] = nameLoraSnMap.value(loraAddress);
  177. data["blastCount"] = blastCount;
  178. if (operationStatus == 0) {
  179. emit validateDetNum(data);
  180. } else if (operationStatus == 1) {
  181. emit validateDetNumUpdate(data);
  182. }
  183. clearFormData(); // 清除表单数据
  184. // this->accept();
  185. }
  186. int ProjectDialog::getOperationStatus() const { return operationStatus; }
  187. void ProjectDialog::setOperationStatus(int newOperationStatus) { operationStatus = newOperationStatus; }
  188. void ProjectDialog::setFormData(const HProject &Project) {
  189. try {
  190. ui->editName->setText(Project.getName());
  191. ui->editDetNum->setText(Project.getDetSum());
  192. ui->editHTID->setText(Project.getHtid());
  193. ui->editXMBH->setText(Project.getXmbh());
  194. int indexBlast = ui->comboBoxBlast->findText(Project.getBlasterName());
  195. if (indexBlast != -1) {
  196. ui->comboBoxBlast->setCurrentIndex(indexBlast);
  197. } else {
  198. qDebug() << "indexBlast 未找到选项 " << indexBlast << "。";
  199. }
  200. int indexOper = ui->comboBoxOperator->findText(Project.getOperatorName());
  201. if (indexOper != -1) {
  202. ui->comboBoxOperator->setCurrentIndex(indexOper);
  203. } else {
  204. qDebug() << "operator 未找到选项 " << Project.getOperatorName() << "。";
  205. }
  206. QStringList addressParts = Project.getAddressPath().split("/");
  207. int numAddresses = addressParts.size();
  208. qDebug() << "numAddresses " << numAddresses;
  209. QVector<QString> addressVariables;
  210. for (int i = 0; i < numAddresses; ++i) {
  211. QString part = addressParts[i];
  212. addressVariables.append(part);
  213. }
  214. // 查找目标文本对应的索引
  215. int indexAddr = ui->comboBoxAddr->findText(addressVariables[0]);
  216. if (indexAddr != -1) {
  217. // 如果找到了对应的索引,设置为当前索引
  218. ui->comboBoxAddr->setCurrentIndex(indexAddr);
  219. qDebug() << "已将选项 " << addressVariables[0] << " 设置为当前显示的选项。";
  220. } else {
  221. // 如果没找到,输出提示信息
  222. qDebug() << "未找到选项 " << addressVariables[0];
  223. }
  224. if (addressVariables.size() >= 2) {
  225. int indexAddr_2 = ui->comboBoxAddr_2->findText(addressVariables[1]);
  226. if (indexAddr_2 != -1) {
  227. // 如果找到了对应的索引,设置为当前索引
  228. ui->comboBoxAddr_2->setCurrentIndex(indexAddr_2);
  229. qDebug() << "已将选项 " << addressVariables[1] << " 设置为当前显示的选项。";
  230. } else {
  231. // 如果没找到,输出提示信息
  232. qDebug() << "未找到选项 " << addressVariables[1];
  233. }
  234. }
  235. if (addressVariables.size() >= 3) {
  236. int indexAddr_3 = ui->comboBoxAddr_3->findText(addressVariables[2]);
  237. if (indexAddr_3 != -1) {
  238. // 如果找到了对应的索引,设置为当前索引
  239. ui->comboBoxAddr_3->setCurrentIndex(indexAddr_3);
  240. qDebug() << "已将选项 " << addressVariables[2] << " 设置为当前显示的选项。";
  241. } else {
  242. // 如果没找到,输出提示信息
  243. qDebug() << "未找到选项 " << addressVariables[2];
  244. }
  245. }
  246. } catch (const std::exception &e) {
  247. qDebug() << "发生异常: " << e.what();
  248. } catch (...) {
  249. qDebug() << "发生未知异常";
  250. }
  251. }
  252. ProjectDialog::~ProjectDialog() { delete ui; }