chengxr 1 year ago
parent
commit
390e92bdca

+ 22 - 0
QFD/CCanvas/CLineItem.cpp

@@ -10,6 +10,28 @@ CLineItem::CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsIt
     updatePath();
 }
 
+QPointF CLineItem::startPos() const
+{
+    return m_startPos;
+}
+
+void CLineItem::setStartPos(const QPointF start)
+{
+    m_startPos = start;
+    updatePath();
+}
+
+QPointF CLineItem::endPos() const
+{
+    return m_endPos;
+}
+
+void CLineItem::setEndPos(const QPointF end)
+{
+    m_endPos = end;
+    updatePath();
+}
+
 CLineItem::CLineType CLineItem::lineType() const
 {
     return m_lineType;

+ 6 - 0
QFD/CCanvas/CLineItem.h

@@ -18,6 +18,12 @@ public:
 
     explicit CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsItem *parent = nullptr);
 
+    QPointF startPos() const;
+    void setStartPos(const QPointF start);
+
+    QPointF endPos() const;
+    void setEndPos(const QPointF end);
+
     CLineType lineType() const;
 
     void setLineType(CLineType type);

+ 83 - 41
QFD/CCanvas/CMindView.cpp

@@ -13,79 +13,121 @@
 
 CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), parent)
 {
-    setRenderHints(QPainter::Antialiasing);  // 抗锯齿
     m_mind = new CMind(this);
+    setRenderHints(QPainter::Antialiasing);  // 抗锯齿
 }
 
-void CMindView::connectSignalsAndSlots() { }
-
 CMind *CMindView::mind() const
 {
     return m_mind;
 }
 
-void CMindView::setMind(CMind *mind)
+void CMindView::setNodeList(QList<CNodeData> list)
+{
+    clear();
+    m_mind->setNodeList(list);
+    m_root = new CNodeItem(m_mind->root());
+    refreshItems();
+}
+
+void CMindView::addNode(CNodeData n)
+{
+    m_mind->addNode(n);
+    CNodeItem *item = new CNodeItem(n);
+    connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::createSubNode);
+    connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::removeNode);
+
+    if (m_root == nullptr) {
+        m_root = item;
+    } else {
+        m_root->addSubNode(item);
+    }
+    refreshItems();
+}
+
+void CMindView::createSubNode(int pNumber)
 {
-    m_mind = mind;
-    refresh();
+    if (pNumber < 0) {
+        return;
+    }
+    CNodeData data = root()->data();
+    CNodeData n    = CNodeData(data.projectId, data.evalType, 0, data.number);
+    addNode(n);
 }
 
-void CMindView::refresh()
+void CMindView::removeNode(int number)
 {
-    for (CNodeItem *item : m_itemList) {
-        scene()->removeItem(item->rectItem());
-        delete item;
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+    m_mind->removeNode(number);
+    if (number == m_root->data().number) {
+        clear();
+        qDebug() << __FUNCTION__ << __LINE__ << endl;
+    } else {
+        m_root->removeNode(number);
     }
-    m_itemList.clear();
+    refreshItems();
+}
 
-    for (CNodeData n : m_mind->nodeList()) {
-        CNodeItem *item = new CNodeItem(this, n.number);
-        item->setText(n.name);
-        m_itemList.append(item);
+void CMindView::clear()
+{
+    m_mind->nodeList().clear();
+    delete m_root;
+    m_root = nullptr;
+    refreshItems();
+}
 
-        scene()->addItem(item->rectItem());
+void CMindView::refreshItems()
+{
+    for (QGraphicsItem *item : m_items) {
+        scene()->removeItem(item);
+    }
+    m_items.clear();
 
-        connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::addSubNode);
-        connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::removeNode);
+    collectItems(m_root);
+    for (QGraphicsItem *item : m_items) {
+        scene()->addItem(item);
     }
 }
 
-void CMindView::setALignment(CMindView::Alignment align)
+void CMindView::collectItems(CNodeItem *node)
 {
-    m_align = align;
+    m_items.clear();
+    if (node == nullptr) {
+        return;
+    }
+    m_items.append(node->rectItem());
+    for (QObject *o : node->children()) {
+        CNodeItem *n = dynamic_cast<CNodeItem *>(o);
+        collectItems(n);
+    }
 }
 
-void CMindView::addNode(CNodeData n)
+qreal CMindView::hNodeSpace() const
 {
-    m_mind->addNode(n);
-    refresh();
+    return m_hNodeSpace;
 }
 
-void CMindView::removeNode(int number)
+void CMindView::setHNodeSpace(qreal h)
 {
-    qDebug() << __FUNCTION__ << __LINE__ << number << endl;
-    m_mind->removeNode(number);
-    refresh();
+    m_hNodeSpace = h;
 }
 
-void CMindView::addSubNode(int pNumber)
+qreal CMindView::vNodeSpace() const
 {
-    qDebug() << __FUNCTION__ << __LINE__ << pNumber << endl;
-    CNodeData root = m_mind->root();
-    CNodeData n    = CNodeData(root.projectId, root.evalType, m_mind->maxNumber() + 1, pNumber);
-    addNode(n);
+    return m_vNodeSpace;
 }
 
-void CMindView::testItems()
+void CMindView::setVNodeSpace(qreal v)
 {
-    CRectItem *r = new CRectItem(QRectF(0, 0, 100, 100));
-    scene()->addItem(r);
+    m_vNodeSpace = v;
+}
 
-    CLineItem *l = new CLineItem(QPointF(-100, -100), QPointF(50, 50));
-    scene()->addItem(l);
-    l->setFlags(QGraphicsItem::ItemIsMovable);
-    l->setLineType(CLineItem::CurveToLine);
+void CMindView::setALignment(CMindView::Alignment align)
+{
+    m_align = align;
+}
 
-    CTextItem *t = new CTextItem("test", QPoint(0, 0));
-    scene()->addItem(t);
+CNodeItem *CMindView::root() const
+{
+    return m_root;
 }

+ 27 - 12
QFD/CCanvas/CMindView.h

@@ -2,10 +2,11 @@
 #define CMINDVIEW_H
 
 #include <QGraphicsView>
+#include <list>
 
 #include "CNode.h"
+#include "CMind.h"
 
-class CMind;
 class CNodeItem;
 
 class CMindView : public QGraphicsView
@@ -20,29 +21,43 @@ class CMindView : public QGraphicsView
 public:
     explicit CMindView(QWidget *parent = nullptr);
 
-    void connectSignalsAndSlots();
-
     CMind *mind() const;
-    void setMind(CMind *mind);
+    void setNodeList(QList<CNodeData> list);
 
-    void refresh();
+    void addNode(CNodeData n);  // 添加一个节点
 
-    void setALignment(Alignment align);
+    void createSubNode(int pNumber);  // 指定父节点, 创建子节点
+
+    void removeNode(int number);  // 移除节点
 
-    void addNode(CNodeData n);
+    void clear();  // 清空
 
-    void removeNode(int number);
+    void refreshItems();
 
-    void addSubNode(int pNumber);
+    void collectItems(CNodeItem *node);
+
+    qreal hNodeSpace() const;
+    void setHNodeSpace(qreal h);
+
+    qreal vNodeSpace() const;
+    void setVNodeSpace(qreal v);
+
+    void setALignment(Alignment align);
+
+    CNodeItem *root() const;
 
 private:
     CMind *m_mind = nullptr;
 
-    QList<CNodeItem *> m_itemList;
+    CNodeItem *m_root = nullptr;
 
-    Alignment m_align = NoAlignment;
+    // 场景中显示的项目
+    QList<QGraphicsItem *> m_items;
 
-    void testItems();
+    qreal m_hNodeSpace = 50;
+    qreal m_vNodeSpace = 20;
+
+    Alignment m_align = NoAlignment;
 };
 
 #endif  // CMINDVIEW_H

+ 30 - 16
QFD/CCanvas/CNodeItem.cpp

@@ -2,39 +2,53 @@
 
 #include "CRectItem.h"
 #include "CTextItem.h"
+#include "CLineItem.h"
 
 #include <QAction>
 #include <QTextDocument>
 
 #include <QDebug>
 
-CNodeItem::CNodeItem(QObject *parent, int number, QPointF pos) : QObject(parent), m_number(number), m_pos(pos)
+CNodeItem::CNodeItem(const CNodeData data, CNodeItem *parent) : QObject(parent), m_data(data)
 {
     m_rectItem = new CRectItem();
-    m_textItem = new CTextItem(m_rectItem);
+    m_textItem = new CTextItem(data.name, m_rectItem);
+    m_lineItem = new CLineItem(m_rectItem);
     updateItemsGeometry();
     connectSignalsAndSlots();
 }
 
-int CNodeItem::number() const
+CNodeData CNodeItem::data() const
 {
-    return m_number;
+    return m_data;
 }
 
-void CNodeItem::setNumber(int n)
+void CNodeItem::addSubNode(CNodeItem *n)
 {
-    m_number = n;
+    if (n == nullptr) {
+        return;
+    }
+    if (n->data().pNumber == data().number) {
+        n->setParent(this);
+    } else {
+        for (QObject *obj : children()) {
+            CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
+            item->addSubNode(n);
+        }
+    }
 }
 
-QString CNodeItem::text() const
+void CNodeItem::removeNode(int number)
 {
-    return m_textItem->toPlainText();
-}
-
-void CNodeItem::setText(const QString text)
-{
-    m_textItem->document()->setPlainText(text);
-    updateItemsGeometry();
+    for (QObject *obj : children()) {
+        CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
+        if (item->data().number == number) {
+            item->setParent(nullptr);
+            delete item;
+        } else {
+            item->removeNode(number);
+        }
+    }
 }
 
 CRectItem *CNodeItem::rectItem() const
@@ -128,11 +142,11 @@ void CNodeItem::slotSelect()
 void CNodeItem::slotSubNode()
 {
     qDebug() << __FUNCTION__ << __LINE__ << endl;
-    emit sigAddSubItem(m_number);
+    emit sigAddSubItem(data().number);
 }
 
 void CNodeItem::slotRemove()
 {
     qDebug() << __FUNCTION__ << __LINE__ << endl;
-    emit sigRemoveItem(m_number);
+    emit sigRemoveItem(data().number);
 }

+ 11 - 6
QFD/CCanvas/CNodeItem.h

@@ -2,22 +2,25 @@
 #define CNODEITEM_H
 
 #include <QObject>
+
+#include "CNode.h"
+
 #include <QPoint>
 
 class CRectItem;
 class CTextItem;
+class CLineItem;
 
 class CNodeItem : public QObject
 {
     Q_OBJECT
 public:
-    explicit CNodeItem(QObject *parent = nullptr, int number = -1, QPointF pos = QPointF());
+    explicit CNodeItem(const CNodeData data, CNodeItem *parent = nullptr);
 
-    int number() const;
-    void setNumber(int n);
+    CNodeData data() const;
 
-    QString text() const;
-    void setText(const QString text);
+    void addSubNode(CNodeItem *n);
+    void removeNode(int number);
 
     CRectItem *rectItem() const;
     CTextItem *textItem() const;
@@ -52,9 +55,11 @@ public slots:
     void slotRemove();
 
 private:
-    int m_number = -1;  // 对应 CNodeData.number
+    CNodeData m_data;
+
     CRectItem *m_rectItem;
     CTextItem *m_textItem;
+    CLineItem *m_lineItem;
 
     QPointF m_pos;
 

+ 8 - 8
QFD/CCanvas/CPathItem.cpp

@@ -4,7 +4,7 @@
 
 CPathItem::CPathItem(QGraphicsItem *parent) : QGraphicsPathItem(parent)
 {
-    useCustomSettings();
+    applySettings();
 }
 
 int CPathItem::lineWidth() const
@@ -15,7 +15,7 @@ int CPathItem::lineWidth() const
 void CPathItem::setLineWidth(int w)
 {
     m_lineWidth = w;
-    useCustomSettings();
+    applySettings();
 }
 
 QColor CPathItem::normaLineColor() const
@@ -26,7 +26,7 @@ QColor CPathItem::normaLineColor() const
 void CPathItem::setNormalLineColor(QColor c)
 {
     m_normalLineColor = c;
-    useCustomSettings();
+    applySettings();
 }
 
 QColor CPathItem::highlightLineColor() const
@@ -37,7 +37,7 @@ QColor CPathItem::highlightLineColor() const
 void CPathItem::setHighlightLineColor(QColor c)
 {
     m_highlightLineColor = c;
-    useCustomSettings();
+    applySettings();
 }
 
 QColor CPathItem::normalFillColor() const
@@ -48,7 +48,7 @@ QColor CPathItem::normalFillColor() const
 void CPathItem::setNormalFillColor(QColor c)
 {
     m_normalFillColor = c;
-    useCustomSettings();
+    applySettings();
 }
 
 QColor CPathItem::highlightFillColor() const
@@ -59,7 +59,7 @@ QColor CPathItem::highlightFillColor() const
 void CPathItem::setHighlightFillColor(QColor c)
 {
     m_highlightFillColor = c;
-    useCustomSettings();
+    applySettings();
 }
 
 bool CPathItem::highlighted() const
@@ -70,10 +70,10 @@ bool CPathItem::highlighted() const
 void CPathItem::setHighlighted(bool h)
 {
     m_highlighted = h;
-    useCustomSettings();
+    applySettings();
 }
 
-void CPathItem::useCustomSettings()
+void CPathItem::applySettings()
 {
     QPen pen = this->pen();
     pen.setWidth(m_lineWidth);

+ 1 - 1
QFD/CCanvas/CPathItem.h

@@ -29,7 +29,7 @@ public:
     virtual void updatePath() = 0;
 
 private:
-    void useCustomSettings();
+    void applySettings();
 
 private:
     int m_lineWidth = 2;

+ 2 - 5
QFD/CCanvas/CTextItem.cpp

@@ -1,8 +1,5 @@
 #include "CTextItem.h"
 
-CTextItem::CTextItem(QGraphicsItem *parent) : CTextItem(QString(), QPointF(), parent) { }
+CTextItem::CTextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent) { }
 
-CTextItem::CTextItem(const QString &text, const QPointF &pos, QGraphicsItem *parent) : QGraphicsTextItem(text, parent)
-{
-    setPos(pos);
-}
+CTextItem::CTextItem(const QString &text, QGraphicsItem *parent) : QGraphicsTextItem(text, parent) { }

+ 1 - 2
QFD/CCanvas/CTextItem.h

@@ -9,8 +9,7 @@ class CTextItem : public QGraphicsTextItem
 public:
     explicit CTextItem(QGraphicsItem *parent = nullptr);
 
-    explicit CTextItem(const QString &text = QString(), const QPointF &pos = QPointF(),
-                       QGraphicsItem *parent = nullptr);
+    explicit CTextItem(const QString &text = QString(), QGraphicsItem *parent = nullptr);
 };
 
 #endif  // CTEXTITEM_H

+ 1 - 1
QFD/widgets/IndexSystemWidget.cpp

@@ -40,7 +40,7 @@ void IndexSystemWidget::contextMenuEvent(QContextMenuEvent *event)
     QAction *act2 = menu->addAction("清空");
     connect(act2, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes);
 
-    if (m_mindView->mind() != nullptr && m_mindView->mind()->nodeList().count() <= 0) {
+    if (m_mindView->root() == nullptr) {
         QAction *act3 = menu->addAction("创建根节点");
         connect(act3, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode);
     }