Quellcode durchsuchen

MindView 和 node 节点菜单

chengxr vor 1 Jahr
Ursprung
Commit
1491d0aaf7

+ 30 - 3
QFD/CCanvas/CMindView.cpp

@@ -16,6 +16,8 @@ CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), pare
     setRenderHints(QPainter::Antialiasing);  // 抗锯齿
 }
 
+void CMindView::connectSignalsAndSlots() { }
+
 CMind *CMindView::mind() const
 {
     return m_mind;
@@ -29,12 +31,21 @@ void CMindView::setMind(CMind *mind)
 
 void CMindView::refresh()
 {
+    for (CNodeItem *item : m_itemList) {
+        scene()->removeItem(item->rectItem());
+        delete item;
+    }
+    m_itemList.clear();
+
     for (CNodeData n : m_mind->nodeList()) {
-        CNodeItem *item = new CNodeItem(n.number);
+        CNodeItem *item = new CNodeItem(this, n.number);
         item->setText(n.name);
-        item->setPos(QPointF());
+        m_itemList.append(item);
+
         scene()->addItem(item->rectItem());
-        scene()->addItem(item->textItem());
+
+        connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::addSubNode);
+        connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::removeNode);
     }
 }
 
@@ -43,6 +54,22 @@ void CMindView::setALignment(CMindView::Alignment align)
     m_align = align;
 }
 
+void CMindView::addNode(CNodeData n)
+{
+    m_mind->addNode(n);
+    refresh();
+}
+
+void CMindView::removeNode(int number)
+{
+    qDebug() << __FUNCTION__ << __LINE__ << number << endl;
+}
+
+void CMindView::addSubNode(int pNumber)
+{
+    qDebug() << __FUNCTION__ << __LINE__ << pNumber << endl;
+}
+
 void CMindView::testItems()
 {
     CRectItem *r = new CRectItem(QRectF(0, 0, 100, 100));

+ 13 - 0
QFD/CCanvas/CMindView.h

@@ -3,7 +3,10 @@
 
 #include <QGraphicsView>
 
+#include "CNode.h"
+
 class CMind;
+class CNodeItem;
 
 class CMindView : public QGraphicsView
 {
@@ -17,6 +20,8 @@ class CMindView : public QGraphicsView
 public:
     explicit CMindView(QWidget *parent = nullptr);
 
+    void connectSignalsAndSlots();
+
     CMind *mind() const;
     void setMind(CMind *mind);
 
@@ -24,9 +29,17 @@ public:
 
     void setALignment(Alignment align);
 
+    void addNode(CNodeData n);
+
+    void removeNode(int number);
+
+    void addSubNode(int pNumber);
+
 private:
     CMind *m_mind = nullptr;
 
+    QList<CNodeItem *> m_itemList;
+
     Alignment m_align = NoAlignment;
 
     void testItems();

+ 38 - 3
QFD/CCanvas/CNodeItem.cpp

@@ -3,12 +3,17 @@
 #include "CRectItem.h"
 #include "CTextItem.h"
 
+#include <QAction>
 #include <QTextDocument>
 
-CNodeItem::CNodeItem(int number, QObject *parent) : QObject(parent), m_number(number)
+#include <QDebug>
+
+CNodeItem::CNodeItem(QObject *parent, int number, QPointF pos) : QObject(parent), m_number(number), m_pos(pos)
 {
     m_rectItem = new CRectItem();
     m_textItem = new CTextItem(m_rectItem);
+    updateItemsGeometry();
+    connectSignalsAndSlots();
 }
 
 int CNodeItem::number() const
@@ -49,6 +54,36 @@ QPointF CNodeItem::pos() const
 void CNodeItem::setPos(const QPointF pos)
 {
     m_pos = pos;
-    m_rectItem->setRect(QRectF(pos.x(), pos.y(), 100, 30));
-    m_textItem->setPos(pos + QPointF(10, 5));
+    updateItemsGeometry();
+}
+
+void CNodeItem::connectSignalsAndSlots()
+{
+    connect(m_rectItem->selectAction(), &QAction::triggered, this, &CNodeItem::slotSelect);
+    connect(m_rectItem->subNodeAction(), &QAction::triggered, this, &CNodeItem::slotSubNode);
+    connect(m_rectItem->removeAction(), &QAction::triggered, this, &CNodeItem::slotRemove);
+}
+
+void CNodeItem::updateItemsGeometry()
+{
+    m_rectItem->setRect(QRectF(m_pos.x(), m_pos.y(), 100, 30));
+    m_textItem->setPos(m_pos + QPointF(10, 5));
+}
+
+void CNodeItem::slotSelect()
+{
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+    m_rectItem->setHighlighted(!m_rectItem->highlighted());
+}
+
+void CNodeItem::slotSubNode()
+{
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+    emit sigAddSubItem(m_number);
+}
+
+void CNodeItem::slotRemove()
+{
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+    emit sigRemoveItem(m_number);
 }

+ 13 - 1
QFD/CCanvas/CNodeItem.h

@@ -11,7 +11,7 @@ class CNodeItem : public QObject
 {
     Q_OBJECT
 public:
-    explicit CNodeItem(int number = -1, QObject *parent = nullptr);
+    explicit CNodeItem(QObject *parent = nullptr, int number = -1, QPointF pos = QPointF());
 
     int number() const;
     void setNumber(int n);
@@ -25,7 +25,19 @@ public:
     QPointF pos() const;
     void setPos(const QPointF pos);
 
+    void connectSignalsAndSlots();
+
+private:
+    void updateItemsGeometry();
+
 signals:
+    void sigAddSubItem(int pNumber);
+    void sigRemoveItem(int number);
+
+public slots:
+    void slotSelect();
+    void slotSubNode();
+    void slotRemove();
 
 private:
     int m_number = -1;  // 对应 CNodeData.number

+ 38 - 1
QFD/CCanvas/CRectItem.cpp

@@ -1,5 +1,8 @@
 #include "CRectItem.h"
 
+#include <QGraphicsSceneContextMenuEvent>
+#include <QMenu>
+
 #include <QDebug>
 
 CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
@@ -7,6 +10,11 @@ CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
 CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
 {
     updatePath();
+
+    m_menu    = new QMenu();
+    m_select  = m_menu->addAction("选中");
+    m_subNode = m_menu->addAction("添加子节点");
+    m_remove  = m_menu->addAction("删除");
 }
 
 QRectF CRectItem::rect() const
@@ -38,7 +46,36 @@ void CRectItem::updatePath()
     setPath(path);
 }
 
+void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
+{
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+
+    const QString txt = highlighted() ? "取消选中" : "选中";
+    m_select->setText(txt);
+    m_menu->exec(event->screenPos());
+}
+
 void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
-    qDebug() << __FUNCTION__ << __LINE__ << event << endl;
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+}
+
+QAction *CRectItem::selectAction() const
+{
+    return m_select;
+}
+
+QAction *CRectItem::subNodeAction() const
+{
+    return m_subNode;
+}
+
+QAction *CRectItem::removeAction() const
+{
+    return m_remove;
+}
+
+void CRectItem::slotSelect()
+{
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
 }

+ 17 - 0
QFD/CCanvas/CRectItem.h

@@ -3,6 +3,9 @@
 
 #include "CPathItem.h"
 
+class QMenu;
+class QAction;
+
 class CRectItem : public CPathItem
 {
 public:
@@ -20,12 +23,26 @@ public:
 
     void updatePath() override;
 
+    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
+
     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
 
+    QAction *selectAction() const;
+    QAction *subNodeAction() const;
+    QAction *removeAction() const;
+
+private slots:
+    void slotSelect();
+
 private:
     QRectF m_rect;
 
     qreal m_cornerRadius = 5;
+
+    QMenu *m_menu      = nullptr;
+    QAction *m_select  = nullptr;
+    QAction *m_subNode = nullptr;
+    QAction *m_remove  = nullptr;
 };
 
 #endif  // CRECTITEM_H

+ 9 - 8
QFD/widgets/IndexSystemWidget.cpp

@@ -32,17 +32,19 @@ void IndexSystemWidget::initLayout()
 
 void IndexSystemWidget::contextMenuEvent(QContextMenuEvent *event)
 {
+    qDebug() << __FUNCTION__ << __LINE__ << endl;
+
     QMenu *menu = new QMenu();
 
-    QAction *selectAction = menu->addAction("全选");
-    connect(selectAction, &QAction::triggered, this, &IndexSystemWidget::slotSelectAllNodes);
+    QAction *act1 = menu->addAction("全选");
+    connect(act1, &QAction::triggered, this, &IndexSystemWidget::slotSelectAllNodes);
 
-    QAction *clearAction = menu->addAction("清空");
-    connect(clearAction, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes);
+    QAction *act2 = menu->addAction("清空");
+    connect(act2, &QAction::triggered, this, &IndexSystemWidget::slotClearAllNodes);
 
     if (m_mindView->mind() != nullptr && m_mindView->mind()->nodeList().count() <= 0) {
-        QAction *root = menu->addAction("创建根节点");
-        connect(root, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode);
+        QAction *act3 = menu->addAction("创建根节点");
+        connect(act3, &QAction::triggered, this, &IndexSystemWidget::slotCreateRootNode);
     }
 
     menu->exec(event->globalPos());
@@ -66,6 +68,5 @@ void IndexSystemWidget::slotCreateRootNode()
 
     CNodeData n = CNodeData(m_proj->id, m_type, 0);
     n.name      = m_proj->projectName;
-    m_mindView->mind()->addNode(n);
-    m_mindView->refresh();
+    m_mindView->addNode(n);
 }