projectdialog.cpp 11 KB

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