MatterElementConfigWidget.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #include "MatterElementConfigWidget.h"
  2. #include "MatterElementItemDelegate.h"
  3. #include "ui_MatterElementConfigWidget.h"
  4. #include "dbService/ScaleInfoService.h"
  5. #include "dbService/GradeInfoService.h"
  6. #include "dbService/GradeIndexInfoService.h"
  7. #include "CMind.h"
  8. #include <QLineEdit>
  9. #include <QMessageBox>
  10. #include <QPushButton>
  11. #include <QDebug>
  12. MatterElementConfigWidget::MatterElementConfigWidget(CMind *mind, int domainLevel, QWidget *parent)
  13. : QWidget(parent), ui(new Ui::MatterElementConfigWidget), m_domainLevel(domainLevel), m_mind(mind)
  14. {
  15. ui->setupUi(this);
  16. initScaleDivisionTable();
  17. initConfigItems();
  18. initDomainTable();
  19. }
  20. MatterElementConfigWidget::~MatterElementConfigWidget()
  21. {
  22. delete ui;
  23. }
  24. void MatterElementConfigWidget::initScaleDivisionTable()
  25. {
  26. QTableView *t = ui->scaleDivisionTable;
  27. t->setAlternatingRowColors(false);
  28. t->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  29. t->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  30. t->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  31. t->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
  32. t->setSelectionMode(QAbstractItemView::SingleSelection);
  33. t->verticalHeader()->hide();
  34. int projectId = m_mind->root().projectId;
  35. m_scaleDivisionmodel = new QStandardItemModel;
  36. t->setModel(m_scaleDivisionmodel);
  37. m_scaleDivisionmodel->setRowCount(1);
  38. m_scaleDivisionmodel->setColumnCount(7);
  39. const QStringList headers = { "定义", "很差", "较差", "一般", "较高", "很高", "相邻定义" };
  40. m_scaleDivisionmodel->setHorizontalHeaderLabels(headers);
  41. QStandardItem *s = nullptr;
  42. MatterElementItemSpinDelegate *sd = new MatterElementItemSpinDelegate(t);
  43. QStringList originValues = { "1.0", "3.0", "5.0", "7.0", "9.0", "[2.0, 4.0, 6.0, 8.0]" };
  44. QList<ScaleInfo *> scaleInfoList;
  45. if (!ScaleInfoService().QueryScaleByProjectId(&scaleInfoList, projectId)) {
  46. QMessageBox::warning(this, "警告", "数据库访问失败");
  47. } else {
  48. if (scaleInfoList.size() == 0) {
  49. for (int num = 0; num < originValues.size(); ++num) {
  50. ScaleInfo sinfo;
  51. sinfo.projectId = projectId;
  52. sinfo.scaleName = headers.at(num + 1);
  53. sinfo.scaleValue = originValues.at(num);
  54. if (!ScaleInfoService().AddScaleInfo(sinfo)) {
  55. QMessageBox::warning(this, "警告", "数据库访问失败");
  56. }
  57. }
  58. } else {
  59. for (const auto &si : scaleInfoList) {
  60. int loc = headers.indexOf(si->scaleName);
  61. originValues[loc - 1] = si->scaleValue;
  62. }
  63. }
  64. }
  65. qDeleteAll(scaleInfoList);
  66. for (int i = 0; i < headers.size(); ++i) {
  67. if (i == 0) {
  68. s = new QStandardItem("标度");
  69. s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  70. s->setTextAlignment(Qt::AlignCenter);
  71. } else {
  72. s = new QStandardItem(originValues.at(i - 1));
  73. s->setTextAlignment(Qt::AlignCenter);
  74. }
  75. m_scaleDivisionmodel->setItem(0, i, s);
  76. if (i > 0 && i < headers.size() - 1) {
  77. t->setItemDelegateForColumn(i, sd);
  78. }
  79. if (i == headers.size() - 1) {
  80. t->setItemDelegateForColumn(i, new MatterElementItemInsideDelegate);
  81. }
  82. }
  83. connect(ui->updateScaleBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onScaleValueUpdate);
  84. }
  85. static void clearTab(QTabWidget *tabWidget)
  86. {
  87. while (tabWidget->count() > 0) {
  88. auto widget = tabWidget->widget(0); // 获取当前页对应的widget
  89. tabWidget->removeTab(0); // 从QTabWidget中删除当前页
  90. widget->setParent(nullptr); // 从父控件中删除widget对象
  91. widget->deleteLater(); // 删除widget对象
  92. }
  93. }
  94. void MatterElementConfigWidget::initDomainLevelLayout()
  95. {
  96. ui->levelLabel->setText(QString::number(m_domainLevel));
  97. QList<GradeInfo *> gradeInfoList;
  98. QMap<QString, QString> levelMaps;
  99. int projectId = m_mind->root().projectId;
  100. if (!GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, projectId, 0)) {
  101. QMessageBox::warning(this, "警告", "数据库访问失败");
  102. } else {
  103. if (gradeInfoList.size() == 0) {
  104. for (int lvl = 0; lvl < m_domainLevel; ++lvl) {
  105. levelMaps.insert(QString("%1级:").arg(lvl + 1), QString("E%1").arg(lvl + 1));
  106. GradeInfo ginfo;
  107. ginfo.projectId = projectId;
  108. ginfo.gradeName = QString("%1级").arg(lvl + 1);
  109. ginfo.gradeValue = QString("E%1").arg(lvl + 1);
  110. ginfo.type = 0;
  111. if (!GradeInfoService().AddGradeInfo(ginfo)) {
  112. QMessageBox::warning(this, "警告", "数据库访问失败");
  113. }
  114. }
  115. } else {
  116. for (const auto &gi : gradeInfoList) {
  117. levelMaps.insert(gi->gradeName, gi->gradeValue);
  118. if (levelMaps.size() >= m_domainLevel) {
  119. break;
  120. }
  121. }
  122. for (int lvl = gradeInfoList.size(); lvl < m_domainLevel; ++lvl) {
  123. levelMaps.insert(QString("%1级:").arg(lvl + 1), QString("E%1").arg(lvl + 1));
  124. GradeInfo ginfo;
  125. ginfo.projectId = projectId;
  126. ginfo.gradeName = QString("%1级").arg(lvl + 1);
  127. ginfo.gradeValue = QString("E%1").arg(lvl + 1);
  128. ginfo.type = 0;
  129. if (!GradeInfoService().AddGradeInfo(ginfo)) {
  130. QMessageBox::warning(this, "警告", "数据库访问失败");
  131. }
  132. }
  133. }
  134. }
  135. qDeleteAll(gradeInfoList);
  136. QFormLayout *formLayout = new QFormLayout;
  137. QMapIterator<QString, QString> iter(levelMaps);
  138. while (iter.hasNext()) {
  139. iter.next();
  140. auto le = new QLineEdit(iter.value());
  141. le->setMinimumHeight(30);
  142. le->setMaximumWidth(80);
  143. m_levelEdits << le;
  144. formLayout->addRow(iter.key() + ":", le);
  145. }
  146. auto levelSaveBtn = new QPushButton("保存");
  147. levelSaveBtn->setIcon(QIcon(":/qfluentwidgets/images/icons/Save_black.svg"));
  148. levelSaveBtn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  149. levelSaveBtn->setMinimumHeight(30);
  150. levelSaveBtn->setMaximumWidth(80);
  151. formLayout->addRow("", levelSaveBtn);
  152. ui->levelWidget->setLayout(formLayout);
  153. connect(levelSaveBtn, &QPushButton::clicked, this, &MatterElementConfigWidget::onLevelSaveBtnClick);
  154. }
  155. void MatterElementConfigWidget::initConfigItems()
  156. {
  157. // 脑图层级 < 2 时无效
  158. if (m_mind->levels() < 2) {
  159. return;
  160. }
  161. m_allConfigItems.clear();
  162. // 使用倒数第二层节点及其子节点
  163. for (CNodeData n : m_mind->nodesInLevel(m_mind->levels() - 1)) {
  164. QVector<MEDomain> children;
  165. for (CNodeData sub : m_mind->subNodes(n)) {
  166. MEDomain d;
  167. d.name = sub.name;
  168. d.leftValues = QVector<double>(m_domainLevel + 1, 0);
  169. d.rightValues = QVector<double>(m_domainLevel + 1, 0);
  170. d.leftCloses = QVector<bool>(m_domainLevel + 1, false);
  171. d.rightCloses = QVector<bool>(m_domainLevel + 1, false);
  172. children.append(d);
  173. }
  174. MEConfigItem i;
  175. i.parentName = n.name;
  176. i.childs = children;
  177. m_allConfigItems.append(i);
  178. }
  179. QList<GradeIndexInfo *> gradeIndexInfoList;
  180. int projectId = m_mind->root().projectId;
  181. if (!GradeIndexInfoService().QueryGradeIndexInfoByProjectId(&gradeIndexInfoList, projectId)) {
  182. QMessageBox::warning(this, "警告", "数据库访问失败");
  183. } else {
  184. if (gradeIndexInfoList.size() == 0) {
  185. for (const auto &configItem : m_allConfigItems) {
  186. for (int c = 0; c < configItem.childs.size(); ++c) {
  187. // 一个指标对应m_domainLevel+1个配置项
  188. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  189. GradeIndexInfo gii;
  190. gii.projectId = projectId;
  191. gii.gradeIndexName = configItem.childs.at(c).name;
  192. gii.gradeParentName = configItem.parentName;
  193. gii.gradeLevel = domain + 1;
  194. gii.gradeIndexValue = QString("%1%2, %3%4")
  195. .arg(configItem.childs.at(c).leftCloses.at(domain) ? "[" : "(")
  196. .arg(configItem.childs.at(c).leftValues.at(domain))
  197. .arg(configItem.childs.at(c).rightValues.at(domain))
  198. .arg(configItem.childs.at(c).rightCloses.at(domain) ? "]" : ")");
  199. if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) {
  200. QMessageBox::warning(this, "警告", "数据库访问失败");
  201. }
  202. }
  203. }
  204. }
  205. } else {
  206. for (auto &configItem : m_allConfigItems) {
  207. for (int c = 0; c < configItem.childs.size(); ++c) {
  208. bool found = false;
  209. for (auto gindex : gradeIndexInfoList) {
  210. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  211. // 默认指标名具有唯一性
  212. if (gindex->gradeIndexName == configItem.childs[c].name
  213. && gindex->gradeLevel == domain + 1) {
  214. QString indexValue = gindex->gradeIndexValue.trimmed();
  215. QStringList list = indexValue.mid(1, indexValue.size() - 2).replace(" ", "").split(",");
  216. configItem.childs[c].leftValues[domain] = list.at(0).toDouble();
  217. configItem.childs[c].rightValues[domain] = list.at(1).toDouble();
  218. configItem.childs[c].leftCloses[domain] = indexValue[0] == "[";
  219. configItem.childs[c].rightCloses[domain] = indexValue.right(1) == "]";
  220. found = true;
  221. }
  222. }
  223. }
  224. if (!found) {
  225. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  226. GradeIndexInfo gii;
  227. gii.projectId = projectId;
  228. gii.gradeIndexName = configItem.childs[c].name;
  229. gii.gradeParentName = configItem.parentName;
  230. gii.gradeLevel = domain + 1;
  231. gii.gradeIndexValue =
  232. QString("%1%2, %3%4")
  233. .arg(configItem.childs.at(c).leftCloses.at(domain) ? "[" : "(")
  234. .arg(configItem.childs.at(c).leftValues.at(domain))
  235. .arg(configItem.childs.at(c).rightValues.at(domain))
  236. .arg(configItem.childs.at(c).rightCloses.at(domain) ? "]" : ")");
  237. if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) {
  238. QMessageBox::warning(this, "警告", "数据库访问失败");
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. qDeleteAll(gradeIndexInfoList);
  247. }
  248. void MatterElementConfigWidget::initDomainTable()
  249. {
  250. initDomainLevelLayout();
  251. m_meRangeDelegate = new MatterElementItemRangeDelegate(this);
  252. const QStringList rangeItems = { "很差", "较差", "一般", "较高", "很高",
  253. "相邻较差", "相邻一般", "相邻较高", "相邻很高" };
  254. QVector<double> rangeValues;
  255. auto scaleValues = getScaleValues();
  256. rangeValues << scaleValues.at(0) << scaleValues.at(1);
  257. m_meRangeDelegate->setOriginItems(rangeItems, rangeValues);
  258. QStringList domainLevels = getDomainLevels();
  259. for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) {
  260. QTableView *table = new QTableView;
  261. table->setAlternatingRowColors(false);
  262. table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  263. table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  264. table->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  265. table->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
  266. table->setSelectionMode(QAbstractItemView::SingleSelection);
  267. table->verticalHeader()->hide();
  268. table->verticalHeader()->setDefaultSectionSize(60);
  269. table->horizontalHeader()->hide();
  270. QStandardItemModel *tModel = new QStandardItemModel(table);
  271. m_domainModels << tModel;
  272. table->setModel(tModel);
  273. ui->tabWidget->addTab(table, m_allConfigItems.at(pindex).parentName);
  274. connect(tModel, &QStandardItemModel::itemChanged, this, &MatterElementConfigWidget::onTableItemChanged);
  275. int indexNumber = m_allConfigItems.at(pindex).childs.size();
  276. tModel->setColumnCount(4 * m_domainLevel);
  277. tModel->setRowCount(indexNumber);
  278. for (int r = 0; r < m_domainLevel + 1; ++r) {
  279. QStandardItem *s = nullptr;
  280. if (r < m_domainLevel) {
  281. s = new QStandardItem(QString("R%1").arg(r + 1));
  282. } else {
  283. s = new QStandardItem("Rp");
  284. }
  285. s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  286. s->setTextAlignment(Qt::AlignCenter);
  287. if (r < m_domainLevel) {
  288. s->setToolTip(QString("经典域R%1").arg(r + 1));
  289. } else {
  290. s->setToolTip("节域p");
  291. }
  292. tModel->setItem(0, r * 4, s);
  293. table->setColumnWidth(r * 4, 20);
  294. table->setSpan(0, r * 4, indexNumber, 1);
  295. if (r < m_domainLevel) {
  296. s = new QStandardItem(domainLevels.at(r));
  297. } else {
  298. s = new QStandardItem("节域p");
  299. }
  300. s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  301. s->setTextAlignment(Qt::AlignCenter);
  302. tModel->setItem(0, r * 4 + 1, s);
  303. table->setSpan(0, r * 4 + 1, indexNumber, 1);
  304. for (int k = 0; k < indexNumber; ++k) {
  305. s = new QStandardItem(m_allConfigItems[pindex].childs[k].name);
  306. s->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  307. s->setTextAlignment(Qt::AlignCenter);
  308. tModel->setItem(k, r * 4 + 2, s);
  309. s = new QStandardItem();
  310. s->setTextAlignment(Qt::AlignCenter);
  311. if (r < m_domainLevel) {
  312. s->setText(QString("%1%2, %3%4")
  313. .arg(m_allConfigItems[pindex].childs[k].leftCloses[r] ? "[" : "(")
  314. .arg(m_allConfigItems[pindex].childs[k].leftValues[r])
  315. .arg(m_allConfigItems[pindex].childs[k].rightValues[r])
  316. .arg(m_allConfigItems[pindex].childs[k].rightCloses[r] ? "]" : ")"));
  317. tModel->setItem(k, r * 4 + 3, s);
  318. }
  319. }
  320. table->setItemDelegateForColumn(r * 4 + 3, m_meRangeDelegate);
  321. }
  322. }
  323. connect(ui->cleanBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onClearBtnClick);
  324. connect(ui->saveBtn, &QToolButton::clicked, this, &MatterElementConfigWidget::onSaveBtnClick);
  325. }
  326. QStringList MatterElementConfigWidget::getDomainLevels() const
  327. {
  328. QStringList lvls;
  329. for (int i = 0; i < m_levelEdits.size(); ++i) {
  330. QString v = m_levelEdits.at(i)->text().trimmed();
  331. if (v.isEmpty()) {
  332. lvls << QString("E%1").arg(i + 1);
  333. } else {
  334. lvls << v;
  335. }
  336. }
  337. return lvls;
  338. }
  339. QVector<QVector<double>> MatterElementConfigWidget::getScaleValues() const
  340. {
  341. QVector<QVector<double>> newValues(2);
  342. for (int c = 1; c < m_scaleDivisionmodel->columnCount() - 1; ++c) {
  343. newValues[0] << m_scaleDivisionmodel->item(0, c)->text().toDouble();
  344. }
  345. QString value = m_scaleDivisionmodel->item(0, m_scaleDivisionmodel->columnCount() - 1)->text();
  346. QStringList tmp = value.mid(1, value.size() - 2).replace(" ", "").split(",");
  347. for (const auto &var : tmp) {
  348. newValues[1] << var.toDouble();
  349. }
  350. return newValues;
  351. }
  352. void MatterElementConfigWidget::onLevelSaveBtnClick()
  353. {
  354. QStringList domainLevels = getDomainLevels();
  355. for (auto model : m_domainModels) {
  356. for (int c = 0; c < domainLevels.size(); ++c) {
  357. model->item(0, c * 4 + 1)->setText(domainLevels.at(c));
  358. }
  359. }
  360. // 更新数据库等级值
  361. QList<GradeInfo *> gradeInfoList;
  362. if (!GradeInfoService().QueryGradeByProjectIdAndType(&gradeInfoList, m_mind->root().projectId, 0)) {
  363. QMessageBox::warning(this, "警告", "数据库访问失败");
  364. return;
  365. }
  366. for (auto gi : gradeInfoList) {
  367. int num = gi->gradeName.mid(0, gi->gradeName.size() - 1).toInt();
  368. if (num < domainLevels.size() + 1) {
  369. if (gi->gradeValue != domainLevels.at(num - 1)) {
  370. gi->gradeValue = domainLevels.at(num - 1);
  371. if (!GradeInfoService().UpdateGradeInfo(*gi)) {
  372. QMessageBox::warning(this, "警告", "数据库访问失败");
  373. return;
  374. }
  375. }
  376. }
  377. }
  378. qDeleteAll(gradeInfoList);
  379. }
  380. void MatterElementConfigWidget::onScaleValueUpdate()
  381. {
  382. ui->scaleDivisionTable->clearSelection(); // 清除表格选中
  383. // 把值聚集起来
  384. QVector<double> rangeValues;
  385. auto scaleValues = getScaleValues();
  386. rangeValues << scaleValues.at(0) << scaleValues.at(1);
  387. m_meRangeDelegate->updateOriginValues(rangeValues);
  388. // 更新数据库
  389. QList<ScaleInfo *> scaleInfoList;
  390. if (!ScaleInfoService().QueryScaleByProjectId(&scaleInfoList, m_mind->root().projectId)) {
  391. QMessageBox::warning(this, "警告", "数据库访问失败");
  392. return;
  393. } else {
  394. for (int c = 1; c < m_scaleDivisionmodel->columnCount(); ++c) {
  395. QString header = m_scaleDivisionmodel->horizontalHeaderItem(c)->text();
  396. QString value = m_scaleDivisionmodel->item(0, c)->text();
  397. for (auto si : scaleInfoList) {
  398. if (si->scaleName == header && value != si->scaleValue) {
  399. si->scaleValue = value;
  400. if (!ScaleInfoService().UpdateScaleInfo(*si)) {
  401. QMessageBox::warning(this, "警告", "数据库访问失败");
  402. }
  403. }
  404. }
  405. }
  406. }
  407. qDeleteAll(scaleInfoList);
  408. }
  409. void MatterElementConfigWidget::onClearBtnClick()
  410. {
  411. for (auto &config : m_allConfigItems) {
  412. for (auto &child : config.childs) {
  413. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  414. child.leftValues[domain] = child.rightValues[domain] = 0;
  415. }
  416. }
  417. }
  418. for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) {
  419. QStandardItemModel *tModel = m_domainModels.at(pindex);
  420. int indexNumber = m_allConfigItems.at(pindex).childs.size();
  421. for (int r = 0; r < m_domainLevel + 1; ++r) {
  422. for (int k = 0; k < indexNumber; ++k) {
  423. tModel->item(k, r * 4 + 3)->setText("(0, 0)");
  424. }
  425. }
  426. }
  427. }
  428. void MatterElementConfigWidget::onSaveBtnClick()
  429. {
  430. for (int pindex = 0; pindex < m_allConfigItems.size(); ++pindex) {
  431. QStandardItemModel *tModel = m_domainModels.at(pindex);
  432. int indexNumber = m_allConfigItems.at(pindex).childs.size();
  433. for (int r = 0; r < m_domainLevel + 1; ++r) {
  434. for (int k = 0; k < indexNumber; ++k) {
  435. QString itemTxt = tModel->item(k, r * 4 + 3)->text();
  436. QStringList list = itemTxt.mid(1, itemTxt.size() - 2).replace(" ", "").split(",");
  437. m_allConfigItems[pindex].childs[k].leftValues[r] = list.at(0).toDouble();
  438. m_allConfigItems[pindex].childs[k].rightValues[r] = list.at(1).toDouble();
  439. m_allConfigItems[pindex].childs[k].leftCloses[r] = itemTxt[0] == "[";
  440. m_allConfigItems[pindex].childs[k].rightCloses[r] = itemTxt.right(1) == "]";
  441. qDebug() << r << k << list;
  442. }
  443. }
  444. }
  445. QList<GradeIndexInfo *> gradeIndexInfoList;
  446. int projectId = m_mind->root().projectId;
  447. if (!GradeIndexInfoService().QueryGradeIndexInfoByProjectId(&gradeIndexInfoList, projectId)) {
  448. QMessageBox::warning(this, "警告", "数据库访问失败");
  449. } else {
  450. if (gradeIndexInfoList.size() == 0) { // 防止数据库被异步删除
  451. for (const auto &configItem : m_allConfigItems) {
  452. for (int c = 0; c < configItem.childs.size(); c++) {
  453. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  454. GradeIndexInfo gii;
  455. gii.projectId = projectId;
  456. gii.gradeIndexName = configItem.childs[c].name;
  457. gii.gradeParentName = configItem.parentName;
  458. gii.gradeLevel = domain + 1;
  459. gii.gradeIndexValue = QString("%1%2, %3%4")
  460. .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(")
  461. .arg(configItem.childs[c].leftValues.at(domain))
  462. .arg(configItem.childs[c].rightValues.at(domain))
  463. .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")");
  464. if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) {
  465. QMessageBox::warning(this, "警告", "数据库访问失败");
  466. }
  467. }
  468. }
  469. }
  470. } else {
  471. for (const auto &configItem : m_allConfigItems) {
  472. for (int c = 0; c < configItem.childs.size(); c++) {
  473. bool found = false;
  474. for (auto &gindex : gradeIndexInfoList) {
  475. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  476. if ((gindex->gradeIndexName == configItem.childs[c].name)
  477. && (gindex->gradeLevel == domain + 1)) {
  478. QString indexValue =
  479. QString("%1%2, %3%4")
  480. .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(")
  481. .arg(configItem.childs[c].leftValues.at(domain))
  482. .arg(configItem.childs[c].rightValues.at(domain))
  483. .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")");
  484. if (gindex->gradeIndexValue != indexValue) {
  485. gindex->gradeIndexValue = indexValue;
  486. if (!GradeIndexInfoService().UpdateGradeIndexInfo(*gindex)) {
  487. QMessageBox::warning(this, "警告", "数据库访问失败");
  488. }
  489. }
  490. found = true;
  491. }
  492. }
  493. }
  494. if (!found) {
  495. for (int domain = 0; domain < m_domainLevel + 1; ++domain) {
  496. QString indexValue = QString("%1%2, %3%4")
  497. .arg(configItem.childs[c].leftCloses.at(domain) ? "[" : "(")
  498. .arg(configItem.childs[c].leftValues.at(domain))
  499. .arg(configItem.childs[c].rightValues.at(domain))
  500. .arg(configItem.childs[c].rightCloses.at(domain) ? "]" : ")");
  501. GradeIndexInfo gii;
  502. gii.projectId = projectId;
  503. gii.gradeIndexName = configItem.childs[c].name;
  504. gii.gradeParentName = configItem.parentName;
  505. gii.gradeLevel = c + 1;
  506. gii.gradeIndexValue = indexValue;
  507. if (!GradeIndexInfoService().AddGradeIndexInfo(gii)) {
  508. QMessageBox::warning(this, "警告", "数据库访问失败");
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }
  515. }
  516. qDeleteAll(gradeIndexInfoList);
  517. }
  518. /// 第一个区间或最后一个区间更新时, 同时更新节域
  519. void MatterElementConfigWidget::onTableItemChanged(QStandardItem *item)
  520. {
  521. /// 判断是否为第一个或最后一个区间
  522. int index1 = 3; // 第一个区间列索引
  523. int index2 = (m_domainLevel - 1) * 4 + 3; // 最后一个区间列索引
  524. if (item->column() != index1 && item->column() != index2) {
  525. return;
  526. }
  527. QStandardItem *item1 = item->model()->item(item->row(), index1);
  528. QStandardItem *item2 = item->model()->item(item->row(), index2);
  529. if (item1 == nullptr || item2 == nullptr) {
  530. return;
  531. }
  532. /// 求节域值
  533. QString left1, right1, left2, right2;
  534. QString sep = ", ";
  535. if (item1->text().length() > 2 && item1->text().contains(sep)) {
  536. left1 = item1->text().mid(1, item1->text().length() - 2).split(sep).first();
  537. right1 = item1->text().mid(1, item1->text().length() - 2).split(sep).last();
  538. }
  539. if (item2->text().length() > 2 && item2->text().contains(sep)) {
  540. left2 = item2->text().mid(1, item2->text().length() - 2).split(sep).first();
  541. right2 = item2->text().mid(1, item2->text().length() - 2).split(sep).last();
  542. }
  543. QString range = QString("(%1, %2)").arg(left1).arg(right2);
  544. if (left1.toDouble() > right2.toDouble()) {
  545. // range = QString("(%1, %2)").arg(right1).arg(left2);
  546. }
  547. /// 显示节域值
  548. int jointIndex = m_domainLevel * 4 + 3; // 节域列索引
  549. QStandardItem *rangeItem = item->model()->item(item->row(), jointIndex);
  550. if (rangeItem) {
  551. rangeItem->setText(range);
  552. } else {
  553. rangeItem = new QStandardItem(range);
  554. rangeItem->setEditable(false);
  555. item->model()->setItem(item->row(), jointIndex, rangeItem);
  556. }
  557. }
  558. QList<MEConfigItem> MatterElementConfigWidget::getAllConfigItems() const
  559. {
  560. return m_allConfigItems;
  561. }