projectdialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // for if view the data, will hide the accept button
  118. ui->buttonBox->button(QDialogButtonBox::Ok)->setVisible(true);
  119. }
  120. void ProjectDialog::on_comboBoxAddr_currentIndexChanged(int index) {
  121. if (index >= 0 && index < parentOptions.size()) {
  122. L2AddressOptions = parentOptions[index].toObject()["children"].toArray();
  123. updateL2AddressOptions();
  124. }
  125. }
  126. void ProjectDialog::on_comboBoxAddr_2_currentIndexChanged(int index) {
  127. if (index >= 0 && index < L2AddressOptions.size()) {
  128. QJsonArray subOptions = L2AddressOptions[index].toObject()["children"].toArray();
  129. L3AddressOptions = subOptions;
  130. updateL3AddressOptions();
  131. }
  132. }
  133. void ProjectDialog::on_comboBoxAddr_3_currentIndexChanged(int index) {
  134. if (index >= 0 && index < L3AddressOptions.size()) {
  135. QString grandChildName = L3AddressOptions[index].toObject()["uuid"].toString();
  136. SetComboBoxLora(grandChildName);
  137. }
  138. }
  139. void ProjectDialog::validateAndSaveProject() {
  140. QString detNum = ui->editDetNum->text().trimmed();
  141. QString blastCount = ui->editRegCount->text().trimmed();
  142. QString projectName = ui->editName->text().trimmed();
  143. QString blastName = ui->comboBoxBlast->currentText().trimmed();
  144. QString operatorName = ui->comboBoxOperator->currentText().trimmed();
  145. QString l1AddressUuid = ui->comboBoxAddr->currentData().toString().trimmed();
  146. QString l2AddressUuid = ui->comboBoxAddr_2->currentData().toString().trimmed();
  147. QString l3AddressUuid = ui->comboBoxAddr_3->currentData().toString().trimmed();
  148. QString l1AddressName = ui->comboBoxAddr->currentText().trimmed();
  149. QString l2AddressName = ui->comboBoxAddr_2->currentText().trimmed();
  150. QString l3AddressName = ui->comboBoxAddr_3->currentText().trimmed();
  151. QString loraAddress = ui->comboBoxLora->currentText().trimmed();
  152. QString xmbh = ui->editXMBH->text().trimmed();
  153. QString htid = ui->editHTID->text().trimmed();
  154. if (detNum.isEmpty() || !ui->editDetNum->hasAcceptableInput()) {
  155. QMessageBox::warning(nullptr, "输入错误", "请输入0-10000的数字!");
  156. return;
  157. }
  158. // 创建一个 QMap 集合,存储数据
  159. QMap<QString, QString> data;
  160. data["detNum"] = detNum;
  161. data["name"] = projectName;
  162. data["xmbh"] = xmbh;
  163. data["htid"] = htid;
  164. data["operatorName"] = operatorName;
  165. data["blasterName"] = blastName;
  166. // 选择level最低的uui:w
  167. QString addressUuid = l1AddressUuid;
  168. if (l3AddressUuid != "") {
  169. addressUuid = l3AddressUuid;
  170. } else if (l2AddressUuid != "") {
  171. addressUuid = l2AddressUuid;
  172. } else if (l1AddressUuid != "") {
  173. addressUuid = l1AddressUuid;
  174. } else {
  175. QMessageBox::warning(nullptr, "输入错误", "请选择地址!");
  176. return;
  177. }
  178. data["addressPath"] = l1AddressName + "/" + l2AddressName + "/" + l3AddressName;
  179. data["addressUuid"] = addressUuid;
  180. data["blasterIdentity"] = blasterId;
  181. data["operatorIdentity"] = operatorId;
  182. data["loraAddress"] = loraAddress;
  183. data["loraSn"] = nameLoraSnMap.value(loraAddress);
  184. data["blastCount"] = blastCount;
  185. if (operationStatus == 0) {
  186. emit validateDetNum(data);
  187. } else if (operationStatus == 1) {
  188. emit validateDetNumUpdate(data);
  189. }
  190. clearFormData(); // 清除表单数据
  191. // this->accept();
  192. }
  193. int ProjectDialog::getOperationStatus() const { return operationStatus; }
  194. void ProjectDialog::setOperationStatus(int newOperationStatus) { operationStatus = newOperationStatus; }
  195. void ProjectDialog::setFormData(const HProject &Project) {
  196. try {
  197. clearFormData(); // 清除表单数据
  198. // hide the accept button
  199. ui->buttonBox->button(QDialogButtonBox::Ok)->setVisible(false);
  200. ui->editName->setText(Project.getName());
  201. ui->editDetNum->setText(Project.getDetSum());
  202. ui->editHTID->setText(Project.getHtid());
  203. ui->editXMBH->setText(Project.getXmbh());
  204. ui->editRegCount->setText(Project.getBlastCount());
  205. int indexBlast = ui->comboBoxBlast->findText(Project.getBlasterName());
  206. if (indexBlast != -1) {
  207. ui->comboBoxBlast->setCurrentIndex(indexBlast);
  208. } else {
  209. qDebug() << "indexBlast 未找到选项 " << indexBlast << "。";
  210. }
  211. int indexOper = ui->comboBoxOperator->findText(Project.getOperatorName());
  212. if (indexOper != -1) {
  213. ui->comboBoxOperator->setCurrentIndex(indexOper);
  214. } else {
  215. qDebug() << "operator 未找到选项 " << Project.getOperatorName() << "。";
  216. }
  217. QStringList addressParts = Project.getAddressPath().split("/");
  218. int numAddresses = addressParts.size();
  219. QVector<QString> addressVariables;
  220. for (int i = 0; i < numAddresses; ++i) {
  221. QString part = addressParts[i];
  222. addressVariables.append(part);
  223. }
  224. // 查找目标文本对应的索引
  225. int indexAddr = ui->comboBoxAddr->findText(addressVariables[0]);
  226. if (indexAddr != -1) {
  227. // 如果找到了对应的索引,设置为当前索引
  228. ui->comboBoxAddr->setCurrentIndex(indexAddr);
  229. qDebug() << "已将选项 " << addressVariables[0] << " 设置为当前显示的选项。";
  230. } else {
  231. // 如果没找到,输出提示信息
  232. qDebug() << "未找到选项 " << addressVariables[0];
  233. }
  234. if (addressVariables.size() >= 2) {
  235. int indexAddr_2 = ui->comboBoxAddr_2->findText(addressVariables[1]);
  236. if (indexAddr_2 != -1) {
  237. // 如果找到了对应的索引,设置为当前索引
  238. ui->comboBoxAddr_2->setCurrentIndex(indexAddr_2);
  239. qDebug() << "已将选项 " << addressVariables[1] << " 设置为当前显示的选项。";
  240. } else {
  241. // 如果没找到,输出提示信息
  242. qDebug() << "未找到选项 " << addressVariables[1];
  243. }
  244. }
  245. if (addressVariables.size() >= 3) {
  246. int indexAddr_3 = ui->comboBoxAddr_3->findText(addressVariables[2]);
  247. if (indexAddr_3 != -1) {
  248. // 如果找到了对应的索引,设置为当前索引
  249. ui->comboBoxAddr_3->setCurrentIndex(indexAddr_3);
  250. qDebug() << "已将选项 " << addressVariables[2] << " 设置为当前显示的选项。";
  251. } else {
  252. // 如果没找到,输出提示信息
  253. qDebug() << "未找到选项 " << addressVariables[2];
  254. }
  255. }
  256. } catch (const std::exception &e) {
  257. qDebug() << "发生异常: " << e.what();
  258. } catch (...) {
  259. qDebug() << "发生未知异常";
  260. }
  261. }
  262. ProjectDialog::~ProjectDialog() { delete ui; }