CMindView.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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::sigEditNode, this, &CMindView::slotEditNode);
  55. connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::slotAddSubNode);
  56. connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::slotRemoveNode);
  57. connect(item, &CNodeItem::sigTextChanged, this, &CMindView::slotTextChanged);
  58. connect(item, &CNodeItem::sigWillBeginEditing, this, &CMindView::slotWillBeginEditing);
  59. if (m_root == nullptr) {
  60. m_root = item;
  61. } else {
  62. m_root->endEditing();
  63. m_root->addSubNode(item);
  64. }
  65. item->textItem()->beginEditing();
  66. refreshItems();
  67. }
  68. void CMindView::updateNode(CNodeData n)
  69. {
  70. m_mind->updateNode(n);
  71. CNodeItem *item = m_root->subNode(n.number);
  72. if (item != nullptr) {
  73. item->setData(n);
  74. refreshItems();
  75. }
  76. }
  77. void CMindView::clear()
  78. {
  79. m_mind->clear();
  80. delete m_root;
  81. m_root = nullptr;
  82. refreshItems();
  83. }
  84. void CMindView::refreshItems()
  85. {
  86. for (QGraphicsItem *item : m_items) {
  87. scene()->removeItem(item);
  88. }
  89. m_items.clear();
  90. refreshNodeGeometry(m_root);
  91. collectItems(m_root);
  92. for (QGraphicsItem *item : m_items) {
  93. m_group->addToGroup(item);
  94. }
  95. moveToCenter();
  96. }
  97. void CMindView::collectItems(CNodeItem *node)
  98. {
  99. if (node == nullptr) {
  100. return;
  101. }
  102. m_items.append(node->rectItem());
  103. for (QObject *o : node->children()) {
  104. CNodeItem *n = dynamic_cast<CNodeItem *>(o);
  105. collectItems(n);
  106. }
  107. }
  108. void CMindView::setALignNodes(bool align)
  109. {
  110. m_align = align;
  111. }
  112. CNodeItem *CMindView::root() const
  113. {
  114. return m_root;
  115. }
  116. void CMindView::refreshNodeGeometry(CNodeItem *node, QPointF topLeft)
  117. {
  118. /// 边框
  119. if (node == nullptr) {
  120. return;
  121. }
  122. QRect borderRect = QRect(topLeft.x(), topLeft.y() + (node->treeHeight() - node->borderHeight()) / 2,
  123. node->borderWidth(), node->borderHeight());
  124. node->rectItem()->setRect(borderRect);
  125. /// 文本
  126. QPointF textPos = QPointF(borderRect.left() + node->xMargin(),
  127. borderRect.top() + (node->borderHeight() - node->textHeight()) / 2);
  128. node->textItem()->setPos(textPos);
  129. node->textItem()->setTextWidth(borderRect.width() - node->xMargin() * 2);
  130. node->rectItem()->setPos(QPoint(0, 0));
  131. /// 子节点
  132. int x = borderRect.right() + node->hNodeSpace();
  133. if (m_align) {
  134. x = borderRect.left() + m_root->maxBorderWidthOfLevel(node->depth()) + node->hNodeSpace();
  135. }
  136. int y = topLeft.y();
  137. if (node->borderHeight() > node->childrenHeight()) {
  138. y += (node->borderHeight() - node->childrenHeight()) / 2;
  139. }
  140. for (QObject *obj : node->children()) {
  141. CNodeItem *subNode = dynamic_cast<CNodeItem *>(obj);
  142. refreshNodeGeometry(subNode, QPointF(x, y));
  143. y += subNode->treeHeight() + subNode->vNodeSpace();
  144. subNode->lineItem()->setStartPos(node->rectItem()->centerRight());
  145. subNode->lineItem()->setEndPos(subNode->rectItem()->centerLeft());
  146. }
  147. }
  148. void CMindView::mousePressEvent(QMouseEvent *event)
  149. {
  150. QGraphicsItem *item = itemAt(event->pos());
  151. CTextItem *text = dynamic_cast<CTextItem *>(item);
  152. if (text == nullptr && m_root != nullptr && m_root->editingNode() != nullptr) {
  153. m_root->endEditing();
  154. }
  155. if (isCloseToEdge()) {
  156. moveToCenter();
  157. }
  158. QGraphicsView::mousePressEvent(event);
  159. }
  160. void CMindView::mouseMoveEvent(QMouseEvent *event)
  161. {
  162. QGraphicsView::mouseMoveEvent(event);
  163. }
  164. bool CMindView::isCloseToEdge()
  165. {
  166. qreal left = m_group->pos().x();
  167. qreal right = m_group->pos().x() + m_group->boundingRect().width();
  168. qreal top = m_group->pos().y();
  169. qreal bottom = m_group->pos().y() + m_group->boundingRect().height();
  170. qreal allowedArea = 0.8;
  171. return left > width() * allowedArea || right < width() * (1 - allowedArea) || top > height() * allowedArea
  172. || bottom < height() * (1 - allowedArea);
  173. }
  174. void CMindView::moveToCenter()
  175. {
  176. QRectF r = m_group->childrenBoundingRect();
  177. setSceneRect(QRectF(QPointF(), r.size()));
  178. }
  179. void CMindView::testData()
  180. {
  181. int num[19] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
  182. int pNum[19] = { -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
  183. QStringList name = { "技术方案测试平台", "展开与撤收能力", "覆盖能力", "传输能力", "组网能力",
  184. "展开时间", "撤收时间", "携行重量", "操作人数", "地域覆盖范围",
  185. "节点小区覆盖", "动中通", "最高传输速度", "误码率", "通信时延",
  186. "平均入网时间", "业务成功率", "组网模式", "网络规模" };
  187. for (int i = 0; i < 19; i++) {
  188. CNodeData n = CNodeData(0, 32, num[i], pNum[i]);
  189. n.name = name[i];
  190. addNode(n);
  191. }
  192. }
  193. void CMindView::slotEditNode(CNodeData n)
  194. {
  195. emit sigEditNode(n);
  196. }
  197. void CMindView::slotAddSubNode(int pNumber)
  198. {
  199. emit sigAddSubNode(pNumber);
  200. }
  201. void CMindView::slotRemoveNode(int number)
  202. {
  203. m_mind->removeNode(number);
  204. if (number == m_root->data().number) {
  205. clear();
  206. } else {
  207. m_root->removeNode(number);
  208. }
  209. refreshItems();
  210. }
  211. void CMindView::slotTextChanged()
  212. {
  213. CNodeItem *item = (CNodeItem *)sender();
  214. refreshItems();
  215. emit sigNodeChanged(item->data());
  216. }
  217. void CMindView::slotWillBeginEditing()
  218. {
  219. m_root->endEditing();
  220. }