CMindView.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. node->setHighlighted(m_mind->isInvalidated(node->data().number));
  149. }
  150. void CMindView::mousePressEvent(QMouseEvent *event)
  151. {
  152. QGraphicsItem *item = itemAt(event->pos());
  153. CTextItem *text = dynamic_cast<CTextItem *>(item);
  154. if (text == nullptr && m_root != nullptr && m_root->editingNode() != nullptr) {
  155. m_root->endEditing();
  156. }
  157. if (isCloseToEdge()) {
  158. moveToCenter();
  159. }
  160. m_isMovingItem =
  161. item != nullptr && (item->flags() & QGraphicsObject::ItemIsMovable) == QGraphicsObject::ItemIsMovable;
  162. updateCursor(event->pos());
  163. QGraphicsView::mousePressEvent(event);
  164. }
  165. void CMindView::mouseMoveEvent(QMouseEvent *event)
  166. {
  167. QGraphicsView::mouseMoveEvent(event);
  168. updateCursor(event->pos());
  169. }
  170. void CMindView::mouseReleaseEvent(QMouseEvent *event)
  171. {
  172. m_isMovingItem = false;
  173. updateCursor(event->pos());
  174. QGraphicsView::mouseReleaseEvent(event);
  175. }
  176. bool CMindView::isCloseToEdge()
  177. {
  178. qreal left = m_group->pos().x();
  179. qreal right = m_group->pos().x() + m_group->boundingRect().width();
  180. qreal top = m_group->pos().y();
  181. qreal bottom = m_group->pos().y() + m_group->boundingRect().height();
  182. qreal allowedArea = 0.8;
  183. return left > width() * allowedArea || right < width() * (1 - allowedArea) || top > height() * allowedArea
  184. || bottom < height() * (1 - allowedArea);
  185. }
  186. void CMindView::moveToCenter()
  187. {
  188. QRectF r = m_group->childrenBoundingRect();
  189. setSceneRect(QRectF(QPointF(), r.size()));
  190. }
  191. void CMindView::testData()
  192. {
  193. int num[19] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
  194. int pNum[19] = { -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
  195. QStringList name = { "技术方案测试平台", "展开与撤收能力", "覆盖能力", "传输能力", "组网能力",
  196. "展开时间", "撤收时间", "携行重量", "操作人数", "地域覆盖范围",
  197. "节点小区覆盖", "动中通", "最高传输速度", "误码率", "通信时延",
  198. "平均入网时间", "业务成功率", "组网模式", "网络规模" };
  199. for (int i = 0; i < 19; i++) {
  200. CNodeData n = CNodeData(0, 32, num[i], pNum[i]);
  201. n.name = name[i];
  202. addNode(n);
  203. }
  204. }
  205. void CMindView::updateCursor(QPoint pos)
  206. {
  207. QGraphicsItem *item = itemAt(pos);
  208. if (m_isMovingItem) {
  209. setCursor(Qt::ClosedHandCursor);
  210. } else {
  211. bool movable =
  212. item != nullptr && (item->flags() & QGraphicsObject::ItemIsMovable) == QGraphicsObject::ItemIsMovable;
  213. if (movable) {
  214. setCursor(Qt::OpenHandCursor);
  215. } else {
  216. setCursor(Qt::ArrowCursor);
  217. }
  218. }
  219. }
  220. void CMindView::setAllowEdit(bool allow)
  221. {
  222. m_allowEdit = allow;
  223. }
  224. bool CMindView::allowEdit() const
  225. {
  226. return m_allowEdit;
  227. }
  228. void CMindView::slotEditNode(CNodeData n)
  229. {
  230. emit sigEditNode(n);
  231. }
  232. void CMindView::slotAddSubNode(int pNumber)
  233. {
  234. emit sigAddSubNode(pNumber);
  235. }
  236. void CMindView::slotRemoveNode(int number)
  237. {
  238. m_mind->removeNode(number);
  239. if (number == m_root->data().number) {
  240. clear();
  241. } else {
  242. m_root->removeNode(number);
  243. }
  244. refreshItems();
  245. }
  246. void CMindView::slotTextChanged()
  247. {
  248. CNodeItem *item = (CNodeItem *)sender();
  249. refreshItems();
  250. emit sigNodeChanged(item->data());
  251. }
  252. void CMindView::slotWillBeginEditing()
  253. {
  254. m_root->endEditing();
  255. }