IndexSystemWidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "IndexSystemWidget.h"
  2. #include "EditNodeWidget.h"
  3. #include <dbService/ClassSet.h>
  4. #include <dbService/CNodeDataService.h>
  5. #include <CMindView.h>
  6. #include <CMind.h>
  7. #include <CNodeItem.h>
  8. #include <Widgets/Menu.h>
  9. #include <QLayout>
  10. #include <QMenu>
  11. #include <QContextMenuEvent>
  12. #include <QTabWidget>
  13. #include <QDebug>
  14. IndexSystemWidget::IndexSystemWidget(ProjectInfo *proj, QWidget *parent) : EvalWidget(proj, parent)
  15. {
  16. setTitle("指标体系设计");
  17. initWidgets();
  18. initLayout();
  19. connectSignalsAndSlots();
  20. }
  21. void IndexSystemWidget::setType(int type)
  22. {
  23. EvalWidget::setType(type);
  24. setupTabWidget();
  25. }
  26. void IndexSystemWidget::contextMenuEvent(QContextMenuEvent *event)
  27. {
  28. RoundMenu *menu = new RoundMenu();
  29. CMindView *m = (CMindView *)m_tab->currentWidget();
  30. if (m->root() == nullptr) {
  31. QAction *act3 = new QAction("创建根节点");
  32. menu->addAction(act3);
  33. connect(act3, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode);
  34. } else {
  35. QAction *act2 = new QAction("清空");
  36. menu->addAction(act2);
  37. connect(act2, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes);
  38. }
  39. menu->exec(event->globalPos() + QPoint(-40, -20));
  40. QWidget::contextMenuEvent(event);
  41. }
  42. void IndexSystemWidget::setupTabWidget()
  43. {
  44. m_tab->clear();
  45. for (int i : indexList()) {
  46. CMindView *m = new CMindView(this);
  47. ProjectManager::IndexType t = (ProjectManager::IndexType)i;
  48. QString s = ProjectManager::nameOfIndexType(t);
  49. m_tab->addTab(m, s);
  50. QList<CNodeData> list;
  51. bool ret = CNodeDataService().QueryAll(list, proj()->id, t);
  52. if (ret) {
  53. m->setNodeList(list);
  54. }
  55. connect(m, &CMindView::sigEditNode, this, &IndexSystemWidget::slotEditNode);
  56. connect(m, &CMindView::sigAddSubNode, this, &IndexSystemWidget::slotAddSubNode);
  57. connect(m->mind(), &CMind::sigRemoveNode, this, &IndexSystemWidget::slotRemoveNode);
  58. connect(m, &CMindView::sigNodeChanged, this, &IndexSystemWidget::slotUpdateNode);
  59. }
  60. }
  61. void IndexSystemWidget::initWidgets()
  62. {
  63. m_editNode = new EditNodeWidget(this);
  64. }
  65. void IndexSystemWidget::initLayout() { }
  66. void IndexSystemWidget::connectSignalsAndSlots()
  67. {
  68. connect(m_tab, &QTabWidget::currentChanged, this, &IndexSystemWidget::slotTabCurrentChanged);
  69. connect(m_editNode, &EditNodeWidget::sigSaveNode, this, &IndexSystemWidget::slotNodeEdited);
  70. }
  71. void IndexSystemWidget::addNode(CNodeData node)
  72. {
  73. CMindView *m = (CMindView *)m_tab->currentWidget();
  74. if (m->mind()->canAddNode(node)) {
  75. int id = CNodeDataService().AddCNodeData(node);
  76. if (id >= 0) {
  77. node.id = id;
  78. m->addNode(node);
  79. }
  80. }
  81. }
  82. void IndexSystemWidget::slotTabCurrentChanged(int c)
  83. {
  84. Q_UNUSED(c)
  85. }
  86. void IndexSystemWidget::slotSelectAllNodes() { }
  87. void IndexSystemWidget::slotClearAllNodes()
  88. {
  89. CMindView *m = (CMindView *)m_tab->currentWidget();
  90. m->clear();
  91. }
  92. void IndexSystemWidget::slotCreateRootNode()
  93. {
  94. int t = indexList()[m_tab->currentIndex()];
  95. CNodeData n = CNodeData(m_proj->id, t, 0);
  96. n.name = m_proj->projectName;
  97. addNode(n);
  98. }
  99. void IndexSystemWidget::slotEditNode(CNodeData n)
  100. {
  101. m_editNode->setNode(n);
  102. m_editNode->show();
  103. }
  104. void IndexSystemWidget::slotAddSubNode(int pNumber)
  105. {
  106. if (pNumber < 0) {
  107. return;
  108. }
  109. CMindView *m = (CMindView *)m_tab->currentWidget();
  110. CNodeData data = m->root()->data();
  111. CNodeData n = CNodeData(data.projectId, data.indexType, m->mind()->maxNumber() + 1, pNumber);
  112. addNode(n);
  113. }
  114. void IndexSystemWidget::slotUpdateNode(CNodeData node)
  115. {
  116. CNodeDataService().UpdateCNodeData(node);
  117. }
  118. void IndexSystemWidget::slotRemoveNode(int id)
  119. {
  120. CNodeDataService().DeleteCNodeDataById(id);
  121. }
  122. void IndexSystemWidget::slotNodeEdited(CNodeData node)
  123. {
  124. // 在弹窗中编辑节点后, 先保存数据库, 再更新界面
  125. bool ret = CNodeDataService().UpdateCNodeData(node);
  126. if (ret) {
  127. CMindView *m = (CMindView *)m_tab->currentWidget();
  128. m->updateNode(node);
  129. }
  130. }