CMindView.cpp 8.0 KB

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