chengxr преди 1 година
родител
ревизия
f849f3f845
променени са 7 файла, в които са добавени 156 реда и са изтрити 30 реда
  1. 45 24
      QFD/CCanvas/CMindView.cpp
  2. 9 4
      QFD/CCanvas/CMindView.h
  3. 32 0
      QFD/CCanvas/CNodeItem.cpp
  4. 6 0
      QFD/CCanvas/CNodeItem.h
  5. 2 0
      QFD/CCanvas/CRectItem.cpp
  6. 49 2
      QFD/CCanvas/CTextItem.cpp
  7. 13 0
      QFD/CCanvas/CTextItem.h

+ 45 - 24
QFD/CCanvas/CMindView.cpp

@@ -40,36 +40,18 @@ 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);
+    connect(item, &CNodeItem::sigAddSubItem, this, &CMindView::slotAddSubNode);
+    connect(item, &CNodeItem::sigRemoveItem, this, &CMindView::slotRemoveNode);
+    connect(item, &CNodeItem::sigTextChanged, this, &CMindView::slotTextChanged);
 
     if (m_root == nullptr) {
         m_root = item;
     } else {
+        m_root->endEditing();
         m_root->addSubNode(item);
     }
-    refreshItems();
-}
-
-void CMindView::createSubNode(int pNumber)
-{
-    if (pNumber < 0) {
-        return;
-    }
-    CNodeData data = root()->data();
-    CNodeData n    = CNodeData(data.projectId, data.evalType, m_mind->maxNumber() + 1, pNumber);
-
-    addNode(n);
-}
+    item->textItem()->beginEditing();
 
-void CMindView::removeNode(int number)
-{
-    m_mind->removeNode(number);
-    if (number == m_root->data().number) {
-        clear();
-    } else {
-        m_root->removeNode(number);
-    }
     refreshItems();
 }
 
@@ -154,9 +136,10 @@ void CMindView::refreshNodeGeometry(CNodeItem *node, QPointF topLeft)
     node->rectItem()->setRect(borderRect);
 
     /// 文本
-    QPointF textPos = QPointF(borderRect.left() + m_hNodeSpace,
+    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));
 
@@ -180,3 +163,41 @@ void CMindView::refreshNodeGeometry(CNodeItem *node, QPointF topLeft)
         subNode->lineItem()->setEndPos(subNode->rectItem()->centerLeft());
     }
 }
+
+void CMindView::mousePressEvent(QMouseEvent *event)
+{
+    QGraphicsItem *item = itemAt(event->pos());
+    CTextItem *text     = dynamic_cast<CTextItem *>(item);
+    if (text == nullptr && m_root != nullptr && m_root->isEditing()) {
+        m_root->endEditing();
+        refreshItems();
+    }
+    QGraphicsView::mousePressEvent(event);
+}
+
+void CMindView::slotAddSubNode(int pNumber)
+{
+    if (pNumber < 0) {
+        return;
+    }
+    CNodeData data = root()->data();
+    CNodeData n    = CNodeData(data.projectId, data.evalType, m_mind->maxNumber() + 1, pNumber);
+
+    addNode(n);
+}
+
+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()
+{
+    refreshItems();
+}

+ 9 - 4
QFD/CCanvas/CMindView.h

@@ -26,10 +26,6 @@ public:
 
     void addNode(CNodeData n);  // 添加一个节点
 
-    void createSubNode(int pNumber);  // 指定父节点, 创建子节点
-
-    void removeNode(int number);  // 移除节点
-
     void clear();  // 清空
 
     void refreshItems();
@@ -48,6 +44,15 @@ public:
 
     void refreshNodeGeometry(CNodeItem *node, QPointF topLeft = QPointF());
 
+    void mousePressEvent(QMouseEvent *event) override;
+
+public slots:
+    void slotAddSubNode(int pNumber);  // 指定父节点, 创建子节点
+
+    void slotRemoveNode(int number);  // 移除节点
+
+    void slotTextChanged();  // 编辑节点
+
 private:
     CMind *m_mind = nullptr;
 

+ 32 - 0
QFD/CCanvas/CNodeItem.cpp

@@ -126,6 +126,7 @@ 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);
+    connect(m_textItem, &CTextItem::sigTextChanged, this, &CNodeItem::slotTextChanged);
 }
 
 void CNodeItem::updateItemsGeometry()
@@ -253,6 +254,31 @@ qreal CNodeItem::maxBorderWidthOfLevel(int lev) const
     return w;
 }
 
+void CNodeItem::endEditing()
+{
+    m_textItem->endEditing();
+    for (QObject *obj : children()) {
+        CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
+        item->endEditing();
+    }
+}
+
+bool CNodeItem::isEditing() const
+{
+    bool e = m_textItem->isEditing();
+
+    if (e == false) {
+        for (QObject *obj : children()) {
+            CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
+            if (item->isEditing()) {
+                e = true;
+            }
+        }
+    }
+
+    return e;
+}
+
 void CNodeItem::slotSelect()
 {
     m_rectItem->setHighlighted(!m_rectItem->highlighted());
@@ -267,3 +293,9 @@ void CNodeItem::slotRemove()
 {
     emit sigRemoveItem(data().number);
 }
+
+void CNodeItem::slotTextChanged()
+{
+    qDebug() << __FUNCTION__ << __LINE__ << m_textItem->toPlainText() << endl;
+    emit sigTextChanged();
+}

+ 6 - 0
QFD/CCanvas/CNodeItem.h

@@ -77,14 +77,20 @@ public:
     ///
     qreal maxBorderWidthOfLevel(int lev = 1) const;
 
+    void endEditing();
+
+    bool isEditing() const;
+
 signals:
     void sigAddSubItem(int pNumber);
     void sigRemoveItem(int number);
+    void sigTextChanged();
 
 public slots:
     void slotSelect();
     void slotSubNode();
     void slotRemove();
+    void slotTextChanged();
 
 private:
     CNodeData m_data;

+ 2 - 0
QFD/CCanvas/CRectItem.cpp

@@ -9,6 +9,8 @@ CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
 
 CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
 {
+    setHandlesChildEvents(false);
+
     updatePath();
 
     m_menu    = new QMenu();

+ 49 - 2
QFD/CCanvas/CTextItem.cpp

@@ -1,5 +1,52 @@
 #include "CTextItem.h"
 
-CTextItem::CTextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent) { }
+#include <QKeyEvent>
+#include <QGraphicsSceneMouseEvent>
 
-CTextItem::CTextItem(const QString &text, QGraphicsItem *parent) : QGraphicsTextItem(text, parent) { }
+#include <QDebug>
+
+CTextItem::CTextItem(QGraphicsItem *parent) : CTextItem(QString(), parent) { }
+
+CTextItem::CTextItem(const QString &text, QGraphicsItem *parent) : QGraphicsTextItem(text, parent)
+{
+    setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable);
+    setTextInteractionFlags(Qt::TextEditorInteraction);
+}
+
+void CTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
+{
+    if (event->button() == Qt::LeftButton) {  //左键双击进入可编辑状态并打开焦点
+        beginEditing();
+        QGraphicsTextItem::mouseDoubleClickEvent(event);
+    }
+}
+
+void CTextItem::keyPressEvent(QKeyEvent *event)
+{
+    switch (event->key()) {
+    case Qt::Key_Return: {
+        endEditing();
+        emit sigTextChanged();
+    }
+    }
+
+    QGraphicsTextItem::keyPressEvent(event);
+}
+
+void CTextItem::beginEditing()
+{
+    setTextInteractionFlags(Qt::TextEditorInteraction);
+    setFocus();
+}
+
+void CTextItem::endEditing()
+{
+    setTextInteractionFlags(Qt::NoTextInteraction);
+    setSelected(false);
+    clearFocus();
+}
+
+bool CTextItem::isEditing() const
+{
+    return (textInteractionFlags() & Qt::TextEditorInteraction) == Qt::TextEditorInteraction;
+}

+ 13 - 0
QFD/CCanvas/CTextItem.h

@@ -10,6 +10,19 @@ public:
     explicit CTextItem(QGraphicsItem *parent = nullptr);
 
     explicit CTextItem(const QString &text = QString(), QGraphicsItem *parent = nullptr);
+
+    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
+
+    void keyPressEvent(QKeyEvent *event) override;
+
+    void beginEditing();
+
+    void endEditing();
+
+    bool isEditing() const;
+
+signals:
+    void sigTextChanged();
 };
 
 #endif  // CTEXTITEM_H