projectdialog.cpp 12 KB

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