123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #include "CMindView.h"
- #include "CRectItem.h"
- #include "CLineItem.h"
- #include "CTextItem.h"
- #include "CMind.h"
- #include "CNodeItem.h"
- #include <QMenu>
- #include <QContextMenuEvent>
- #include <QDebug>
- 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<CNodeData> 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<CNodeItem *>(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<CNodeItem *>(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<CTextItem *>(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();
- }
|