#include "CMindView.h" #include "CRectItem.h" #include "CLineItem.h" #include "CTextItem.h" #include "CMind.h" #include "CNodeItem.h" #include #include #include CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), parent) { setRenderHints(QPainter::Antialiasing); // 抗锯齿 // setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_mind = new CMind(this); m_group = new QGraphicsItemGroup(); m_group->setFlags(QGraphicsItem::ItemIsMovable); m_group->setHandlesChildEvents(false); scene()->addItem(m_group); setStyleSheet("QGraphicsView {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(244, 244, " "255);}"); } CMind *CMindView::mind() const { return m_mind; } void CMindView::setNodeList(QList list) { clear(); for (int i = 0; i < list.count() - 1; i++) { for (int j = 0; j < list.count() - 1 - i; j++) { if ((list[j].pNumber > list[j + 1].pNumber) || ((list[j].pNumber == list[j + 1].pNumber) && (list[j].number > list[j + 1].number))) { CNodeData temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } for (int i = 0; i < list.count(); i++) { CNodeData n = list[i]; if (m_mind->canAddNode(n)) { addNode(n); } } if (m_root != nullptr) { m_root->endEditing(); } } void CMindView::addNode(CNodeData n) { m_mind->addNode(n); CNodeItem *item = new CNodeItem(n); item->textItem()->setAllowEdit(m_allowEdit); item->rectItem()->setAllowEdit(m_allowEdit); connect(item, &CNodeItem::sigEditNode, this, &CMindView::slotEditNode); connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::slotAddSubNode); connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::slotRemoveNode); connect(item, &CNodeItem::sigTextChanged, this, &CMindView::slotTextChanged); connect(item, &CNodeItem::sigWillBeginEditing, this, &CMindView::slotWillBeginEditing); if (m_root == nullptr) { m_root = item; } else { m_root->endEditing(); m_root->addSubNode(item); } item->textItem()->beginEditing(); refreshItems(); } void CMindView::updateNode(CNodeData n) { m_mind->updateNode(n); CNodeItem *item = m_root->subNode(n.number); if (item != nullptr) { item->setData(n); refreshItems(); } } void CMindView::clear() { m_mind->clear(); delete m_root; m_root = nullptr; refreshItems(); } void CMindView::refreshItems() { for (QGraphicsItem *item : m_items) { scene()->removeItem(item); } m_items.clear(); refreshNodeGeometry(m_root); collectItems(m_root); for (QGraphicsItem *item : m_items) { m_group->addToGroup(item); } moveToCenter(); } void CMindView::collectItems(CNodeItem *node) { if (node == nullptr) { return; } m_items.append(node->rectItem()); for (QObject *o : node->children()) { CNodeItem *n = dynamic_cast(o); collectItems(n); } } void CMindView::setALignNodes(bool align) { m_align = align; } CNodeItem *CMindView::root() const { return m_root; } void CMindView::refreshNodeGeometry(CNodeItem *node, QPointF topLeft) { /// 边框 if (node == nullptr) { return; } QRect borderRect = QRect(topLeft.x(), topLeft.y() + (node->treeHeight() - node->borderHeight()) / 2, node->borderWidth(), node->borderHeight()); node->rectItem()->setRect(borderRect); /// 文本 QPointF textPos = QPointF(borderRect.left() + node->xMargin(), borderRect.top() + (node->borderHeight() - node->textHeight()) / 2); node->textItem()->setPos(textPos); node->textItem()->setTextWidth(borderRect.width() - node->xMargin() * 2); node->rectItem()->setPos(QPoint(0, 0)); /// 子节点 int x = borderRect.right() + node->hNodeSpace(); if (m_align) { x = borderRect.left() + m_root->maxBorderWidthOfLevel(node->depth()) + node->hNodeSpace(); } int y = topLeft.y(); if (node->borderHeight() > node->childrenHeight()) { y += (node->borderHeight() - node->childrenHeight()) / 2; } for (QObject *obj : node->children()) { CNodeItem *subNode = dynamic_cast(obj); refreshNodeGeometry(subNode, QPointF(x, y)); y += subNode->treeHeight() + subNode->vNodeSpace(); subNode->lineItem()->setStartPos(node->rectItem()->centerRight()); subNode->lineItem()->setEndPos(subNode->rectItem()->centerLeft()); } node->setHighlighted(m_mind->isInvalidated(node->data().number)); } void CMindView::mousePressEvent(QMouseEvent *event) { QGraphicsItem *item = itemAt(event->pos()); CTextItem *text = dynamic_cast(item); if (text == nullptr && m_root != nullptr && m_root->editingNode() != nullptr) { m_root->endEditing(); } if (isCloseToEdge()) { moveToCenter(); } m_isMovingItem = item != nullptr && (item->flags() & QGraphicsObject::ItemIsMovable) == QGraphicsObject::ItemIsMovable; updateCursor(event->pos()); QGraphicsView::mousePressEvent(event); } void CMindView::mouseMoveEvent(QMouseEvent *event) { QGraphicsView::mouseMoveEvent(event); updateCursor(event->pos()); } void CMindView::mouseReleaseEvent(QMouseEvent *event) { m_isMovingItem = false; updateCursor(event->pos()); QGraphicsView::mouseReleaseEvent(event); } bool CMindView::isCloseToEdge() { qreal left = m_group->pos().x(); qreal right = m_group->pos().x() + m_group->boundingRect().width(); qreal top = m_group->pos().y(); qreal bottom = m_group->pos().y() + m_group->boundingRect().height(); qreal allowedArea = 0.8; return left > width() * allowedArea || right < width() * (1 - allowedArea) || top > height() * allowedArea || bottom < height() * (1 - allowedArea); } void CMindView::moveToCenter() { QRectF r = m_group->childrenBoundingRect(); setSceneRect(QRectF(QPointF(), r.size())); } void CMindView::testData() { int num[19] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }; int pNum[19] = { -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; QStringList name = { "技术方案测试平台", "展开与撤收能力", "覆盖能力", "传输能力", "组网能力", "展开时间", "撤收时间", "携行重量", "操作人数", "地域覆盖范围", "节点小区覆盖", "动中通", "最高传输速度", "误码率", "通信时延", "平均入网时间", "业务成功率", "组网模式", "网络规模" }; for (int i = 0; i < 19; i++) { CNodeData n = CNodeData(0, 32, num[i], pNum[i]); n.name = name[i]; addNode(n); } } void CMindView::updateCursor(QPoint pos) { QGraphicsItem *item = itemAt(pos); if (m_isMovingItem) { setCursor(Qt::ClosedHandCursor); } else { bool movable = item != nullptr && (item->flags() & QGraphicsObject::ItemIsMovable) == QGraphicsObject::ItemIsMovable; if (movable) { setCursor(Qt::OpenHandCursor); } else { setCursor(Qt::ArrowCursor); } } } void CMindView::setAllowEdit(bool allow) { m_allowEdit = allow; } bool CMindView::allowEdit() const { return m_allowEdit; } void CMindView::slotEditNode(CNodeData n) { emit sigEditNode(n); } void CMindView::slotAddSubNode(int pNumber) { emit sigAddSubNode(pNumber); } void CMindView::slotRemoveNode(int number) { m_mind->removeNode(number); if (number == m_root->data().number) { clear(); } else { m_root->removeNode(number); } refreshItems(); } void CMindView::slotTextChanged() { CNodeItem *item = (CNodeItem *)sender(); refreshItems(); emit sigNodeChanged(item->data()); } void CMindView::slotWillBeginEditing() { m_root->endEditing(); }