IndexSystemWidget.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if (menu->actions().size() > 0) {
  40. menu->exec(event->globalPos() + QPoint(-40, -20));
  41. }
  42. QWidget::contextMenuEvent(event);
  43. }
  44. void IndexSystemWidget::setupTabWidget()
  45. {
  46. m_tab->clear();
  47. for (int i : indexList()) {
  48. CMindView *m = new CMindView(this);
  49. ProjectManager::IndexType t = (ProjectManager::IndexType)i;
  50. QString s = ProjectManager::nameOfIndexType(t);
  51. m_tab->addTab(m, s);
  52. QList<CNodeData> list;
  53. bool ret = CNodeDataService().QueryAll(list, proj()->id, t);
  54. if (ret) {
  55. m->setNodeList(list);
  56. }
  57. connect(m, &CMindView::sigEditNode, this, &IndexSystemWidget::slotEditNode);
  58. connect(m, &CMindView::sigAddSubNode, this, &IndexSystemWidget::slotAddSubNode);
  59. connect(m->mind(), &CMind::sigRemoveNode, this, &IndexSystemWidget::slotRemoveNode);
  60. connect(m, &CMindView::sigNodeChanged, this, &IndexSystemWidget::slotUpdateNode);
  61. }
  62. }
  63. void IndexSystemWidget::initWidgets()
  64. {
  65. m_editNode = new EditNodeWidget(this);
  66. }
  67. void IndexSystemWidget::initLayout() { }
  68. void IndexSystemWidget::connectSignalsAndSlots()
  69. {
  70. connect(m_tab, &QTabWidget::currentChanged, this, &IndexSystemWidget::slotTabCurrentChanged);
  71. connect(m_editNode, &EditNodeWidget::sigSaveNode, this, &IndexSystemWidget::slotNodeEdited);
  72. }
  73. void IndexSystemWidget::addNode(CNodeData node)
  74. {
  75. CMindView *m = (CMindView *)m_tab->currentWidget();
  76. if (m->mind()->canAddNode(node)) {
  77. int id = CNodeDataService().AddCNodeData(node);
  78. if (id >= 0) {
  79. node.id = id;
  80. m->addNode(node);
  81. }
  82. }
  83. }
  84. void IndexSystemWidget::slotTabCurrentChanged(int c)
  85. {
  86. Q_UNUSED(c)
  87. }
  88. void IndexSystemWidget::slotSelectAllNodes() { }
  89. void IndexSystemWidget::slotClearAllNodes()
  90. {
  91. CMindView *m = (CMindView *)m_tab->currentWidget();
  92. m->clear();
  93. }
  94. void IndexSystemWidget::slotCreateRootNode()
  95. {
  96. int t = indexList()[m_tab->currentIndex()];
  97. CNodeData n = CNodeData(m_proj->id, t, 0);
  98. n.name = m_proj->projectName;
  99. addNode(n);
  100. }
  101. void IndexSystemWidget::slotEditNode(CNodeData n)
  102. {
  103. m_editNode->setNode(n);
  104. m_editNode->show();
  105. }
  106. void IndexSystemWidget::slotAddSubNode(int pNumber)
  107. {
  108. if (pNumber < 0) {
  109. return;
  110. }
  111. CMindView *m = (CMindView *)m_tab->currentWidget();
  112. CNodeData data = m->root()->data();
  113. CNodeData n = CNodeData(data.projectId, data.indexType, m->mind()->maxNumber() + 1, pNumber);
  114. addNode(n);
  115. }
  116. void IndexSystemWidget::slotUpdateNode(CNodeData node)
  117. {
  118. CNodeDataService().UpdateCNodeData(node);
  119. }
  120. void IndexSystemWidget::slotRemoveNode(int id)
  121. {
  122. CNodeDataService().DeleteCNodeDataById(id);
  123. }
  124. void IndexSystemWidget::slotNodeEdited(CNodeData node)
  125. {
  126. // 在弹窗中编辑节点后, 先保存数据库, 再更新界面
  127. bool ret = CNodeDataService().UpdateCNodeData(node);
  128. if (ret) {
  129. CMindView *m = (CMindView *)m_tab->currentWidget();
  130. m->updateNode(node);
  131. }
  132. }