CMindView.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "CMindView.h"
  2. #include "CRectItem.h"
  3. #include "CLineItem.h"
  4. #include "CTextItem.h"
  5. #include "CMind.h"
  6. #include "CNodeItem.h"
  7. #include <QMenu>
  8. #include <QContextMenuEvent>
  9. #include <QDebug>
  10. CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), parent)
  11. {
  12. setRenderHints(QPainter::Antialiasing); // 抗锯齿
  13. // setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  14. m_mind = new CMind(this);
  15. m_group = new QGraphicsItemGroup();
  16. m_group->setFlags(QGraphicsItem::ItemIsMovable);
  17. m_group->setHandlesChildEvents(false);
  18. scene()->addItem(m_group);
  19. setStyleSheet("QGraphicsView {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(244, 244, "
  20. "255);}");
  21. // testData();
  22. }
  23. CMind *CMindView::mind() const
  24. {
  25. return m_mind;
  26. }
  27. void CMindView::setNodeList(QList<CNodeData> list)
  28. {
  29. clear();
  30. for (int i = 0; i < list.count() - 1; i++) {
  31. for (int j = 0; j < list.count() - 1 - i; j++) {
  32. if ((list[j].pNumber > list[j + 1].pNumber)
  33. || ((list[j].pNumber == list[j + 1].pNumber) && (list[j].number > list[j + 1].number))) {
  34. CNodeData temp = list[j];
  35. list[j] = list[j + 1];
  36. list[j + 1] = temp;
  37. }
  38. }
  39. }
  40. for (int i = 0; i < list.count(); i++) {
  41. CNodeData n = list[i];
  42. if (m_mind->canAddNode(n)) {
  43. addNode(n);
  44. }
  45. }
  46. if (m_root != nullptr) {
  47. m_root->endEditing();
  48. }
  49. }
  50. void CMindView::addNode(CNodeData n)
  51. {
  52. m_mind->addNode(n);
  53. CNodeItem *item = new CNodeItem(n);
  54. connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::slotAddSubNode);
  55. connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::slotRemoveNode);
  56. connect(item, &CNodeItem::sigTextChanged, this, &CMindView::slotTextChanged);
  57. connect(item, &CNodeItem::sigWillBeginEditing, this, &CMindView::slotWillBeginEditing);
  58. if (m_root == nullptr) {
  59. m_root = item;
  60. } else {
  61. m_root->endEditing();
  62. m_root->addSubNode(item);
  63. }
  64. item->textItem()->beginEditing();
  65. refreshItems();
  66. }
  67. void CMindView::clear()
  68. {
  69. m_mind->clear();
  70. delete m_root;
  71. m_root = nullptr;
  72. refreshItems();
  73. }
  74. void CMindView::refreshItems()
  75. {
  76. for (QGraphicsItem *item : m_items) {
  77. scene()->removeItem(item);
  78. }
  79. m_items.clear();
  80. refreshNodeGeometry(m_root);
  81. collectItems(m_root);
  82. for (QGraphicsItem *item : m_items) {
  83. m_group->addToGroup(item);
  84. }
  85. moveToCenter();
  86. }
  87. void CMindView::collectItems(CNodeItem *node)
  88. {
  89. if (node == nullptr) {
  90. return;
  91. }
  92. m_items.append(node->rectItem());
  93. for (QObject *o : node->children()) {
  94. CNodeItem *n = dynamic_cast<CNodeItem *>(o);
  95. collectItems(n);
  96. }
  97. }
  98. void CMindView::setALignNodes(bool align)
  99. {
  100. m_align = align;
  101. }
  102. CNodeItem *CMindView::root() const
  103. {
  104. return m_root;
  105. }
  106. void CMindView::refreshNodeGeometry(CNodeItem *node, QPointF topLeft)
  107. {
  108. /// 边框
  109. if (node == nullptr) {
  110. return;
  111. }
  112. QRect borderRect = QRect(topLeft.x(), topLeft.y() + (node->treeHeight() - node->borderHeight()) / 2,
  113. node->borderWidth(), node->borderHeight());
  114. node->rectItem()->setRect(borderRect);
  115. /// 文本
  116. QPointF textPos = QPointF(borderRect.left() + node->xMargin(),
  117. borderRect.top() + (node->borderHeight() - node->textHeight()) / 2);
  118. node->textItem()->setPos(textPos);
  119. node->textItem()->setTextWidth(borderRect.width() - node->xMargin() * 2);
  120. node->rectItem()->setPos(QPoint(0, 0));
  121. /// 子节点
  122. int x = borderRect.right() + node->hNodeSpace();
  123. if (m_align) {
  124. x = borderRect.left() + m_root->maxBorderWidthOfLevel(node->depth()) + node->hNodeSpace();
  125. }
  126. int y = topLeft.y();
  127. if (node->borderHeight() > node->childrenHeight()) {
  128. y += (node->borderHeight() - node->childrenHeight()) / 2;
  129. }
  130. for (QObject *obj : node->children()) {
  131. CNodeItem *subNode = dynamic_cast<CNodeItem *>(obj);
  132. refreshNodeGeometry(subNode, QPointF(x, y));
  133. y += subNode->treeHeight() + subNode->vNodeSpace();
  134. subNode->lineItem()->setStartPos(node->rectItem()->centerRight());
  135. subNode->lineItem()->setEndPos(subNode->rectItem()->centerLeft());
  136. }
  137. }
  138. void CMindView::mousePressEvent(QMouseEvent *event)
  139. {
  140. QGraphicsItem *item = itemAt(event->pos());
  141. CTextItem *text = dynamic_cast<CTextItem *>(item);
  142. if (text == nullptr && m_root != nullptr && m_root->editingNode() != nullptr) {
  143. m_root->endEditing();
  144. }
  145. if (isCloseToEdge()) {
  146. moveToCenter();
  147. }
  148. QGraphicsView::mousePressEvent(event);
  149. }
  150. void CMindView::mouseMoveEvent(QMouseEvent *event)
  151. {
  152. QGraphicsView::mouseMoveEvent(event);
  153. }
  154. bool CMindView::isCloseToEdge()
  155. {
  156. qreal left = m_group->pos().x();
  157. qreal right = m_group->pos().x() + m_group->boundingRect().width();
  158. qreal top = m_group->pos().y();
  159. qreal bottom = m_group->pos().y() + m_group->boundingRect().height();
  160. qreal allowedArea = 0.8;
  161. return left > width() * allowedArea || right < width() * (1 - allowedArea) || top > height() * allowedArea
  162. || bottom < height() * (1 - allowedArea);
  163. }
  164. void CMindView::moveToCenter()
  165. {
  166. QRectF r = m_group->childrenBoundingRect();
  167. setSceneRect(QRectF(QPointF(), r.size()));
  168. }
  169. void CMindView::testData()
  170. {
  171. int num[19] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
  172. int pNum[19] = { -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
  173. QStringList name = { "技术方案测试平台", "展开与撤收能力", "覆盖能力", "传输能力", "组网能力",
  174. "展开时间", "撤收时间", "携行重量", "操作人数", "地域覆盖范围",
  175. "节点小区覆盖", "动中通", "最高传输速度", "误码率", "通信时延",
  176. "平均入网时间", "业务成功率", "组网模式", "网络规模" };
  177. for (int i = 0; i < 19; i++) {
  178. CNodeData n = CNodeData(0, 32, num[i], pNum[i]);
  179. n.name = name[i];
  180. addNode(n);
  181. }
  182. }
  183. void CMindView::slotAddSubNode(int pNumber)
  184. {
  185. emit sigAddSubNode(pNumber);
  186. }
  187. void CMindView::slotRemoveNode(int number)
  188. {
  189. m_mind->removeNode(number);
  190. if (number == m_root->data().number) {
  191. clear();
  192. } else {
  193. m_root->removeNode(number);
  194. }
  195. refreshItems();
  196. }
  197. void CMindView::slotTextChanged()
  198. {
  199. CNodeItem *item = (CNodeItem *)sender();
  200. refreshItems();
  201. emit sigNodeChanged(item->data());
  202. }
  203. void CMindView::slotWillBeginEditing()
  204. {
  205. m_root->endEditing();
  206. }