projectdialog.cpp 12 KB

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