chengxr 1 year ago
parent
commit
561b4bd054

+ 6 - 2
QFD/CCanvas/CCanvas.pri

@@ -1,16 +1,20 @@
 INCLUDEPATH += $$PWD
 
 HEADERS += \
+    $$PWD/CLineItem.h \
     $$PWD/CMind.h \
     $$PWD/CMindView.h \
     $$PWD/CNode.h \
     $$PWD/CPathItem.h \
-    $$PWD/CRectItem.h
+    $$PWD/CRectItem.h \
+    $$PWD/CTextItem.h
 
 SOURCES += \
+    $$PWD/CLineItem.cpp \
     $$PWD/CMind.cpp \
     $$PWD/CMindView.cpp \
     $$PWD/CNode.cpp \
     $$PWD/CPathItem.cpp \
-    $$PWD/CRectItem.cpp
+    $$PWD/CRectItem.cpp \
+    $$PWD/CTextItem.cpp
 

+ 60 - 0
QFD/CCanvas/CLineItem.cpp

@@ -0,0 +1,60 @@
+#include "CLineItem.h"
+
+CLineItem::CLineItem(QGraphicsItem *parent) : CLineItem(QPointF(), QPointF(), parent) { }
+
+CLineItem::CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsItem *parent)
+    : CPathItem(parent), m_startPos(startPos), m_endPos(endPos)
+{
+    setNormalLineColor(Qt::gray);
+    setLineWidth(5);
+    updatePath();
+}
+
+CLineItem::CLineType CLineItem::lineType() const
+{
+    return m_lineType;
+}
+
+void CLineItem::setLineType(CLineItem::CLineType type)
+{
+    m_lineType = type;
+    updatePath();
+}
+
+void CLineItem::updatePath()
+{
+    QPainterPath path;
+    switch (m_lineType) {
+    case Curve: {
+        path.moveTo(m_startPos);
+        double d     = (m_endPos.x() - m_startPos.x()) / 4;
+        QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
+        QPointF ctr2 = QPointF(m_endPos.x() - d, m_endPos.y());
+        path.cubicTo(ctr1, ctr2, m_endPos);
+        break;
+    }
+    case Line: {
+        path.moveTo(m_startPos);
+        path.lineTo(m_endPos);
+        break;
+    }
+    case Polyline: {
+        path.moveTo(m_startPos);
+        path.lineTo(QPointF(m_startPos.x() + 20, m_startPos.y()));
+        path.lineTo(QPointF(m_startPos.x() + 20, m_endPos.y()));
+        path.lineTo(m_endPos);
+        break;
+    }
+    case CurveToLine: {
+        path.moveTo(m_startPos);
+        double d     = 5;
+        QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
+        QPointF ctr2 = QPointF(m_startPos.x() + d * 2, m_endPos.y());
+        QPointF p    = QPointF(m_startPos.x() + d * 3, m_endPos.y());
+        path.cubicTo(ctr1, ctr2, p);
+        path.lineTo(m_endPos);
+        break;
+    }
+    }
+    setPath(path);
+}

+ 34 - 0
QFD/CCanvas/CLineItem.h

@@ -0,0 +1,34 @@
+#ifndef CLINEITEM_H
+#define CLINEITEM_H
+
+#include "CPathItem.h"
+
+class CLineItem : public CPathItem
+{
+public:
+    typedef enum
+    {
+        Curve,
+        Line,
+        Polyline,
+        CurveToLine,
+    } CLineType;
+
+    explicit CLineItem(QGraphicsItem *parent = nullptr);
+
+    explicit CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsItem *parent = nullptr);
+
+    CLineType lineType() const;
+
+    void setLineType(CLineType type);
+
+    void updatePath() override;
+
+private:
+    QPointF m_startPos;
+    QPointF m_endPos;
+
+    CLineType m_lineType = Curve;
+};
+
+#endif  // CLINEITEM_H

+ 9 - 2
QFD/CCanvas/CMindView.cpp

@@ -1,9 +1,11 @@
 #include "CMindView.h"
 
-#include <QGraphicsRectItem>
+#include "CRectItem.h"
+#include "CLineItem.h"
 
 CMindView::CMindView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), parent)
 {
+    setRenderHints(QPainter::Antialiasing);  // 抗锯齿
     testItems();
 }
 
@@ -27,6 +29,11 @@ void CMindView::setALignment(CMindView::Alignment align)
 
 void CMindView::testItems()
 {
-    QGraphicsRectItem *r = new QGraphicsRectItem(QRectF(0, 0, 100, 100));
+    CRectItem *r = new CRectItem(QRectF(0, 0, 100, 100));
     scene()->addItem(r);
+
+    CLineItem *l = new CLineItem(QPointF(-100, -100), QPointF(50, 50));
+    scene()->addItem(l);
+    l->setFlags(QGraphicsItem::ItemIsMovable);
+    l->setLineType(CLineItem::CurveToLine);
 }

+ 1 - 1
QFD/CCanvas/CMindView.h

@@ -15,7 +15,7 @@ class CMindView : public QGraphicsView
     };
 
 public:
-    CMindView(QWidget *parent = nullptr);
+    explicit CMindView(QWidget *parent = nullptr);
 
     CMind *mind() const;
     void setMind(CMind *mind);

+ 60 - 22
QFD/CCanvas/CPathItem.cpp

@@ -2,47 +2,85 @@
 
 #include <QPen>
 
-CPathItem::CPathItem(QGraphicsItem *parent) : QGraphicsPathItem(parent) { }
+CPathItem::CPathItem(QGraphicsItem *parent) : QGraphicsPathItem(parent)
+{
+    useCustomSettings();
+}
 
 int CPathItem::lineWidth() const
 {
-    QPen pen = this->pen();
-    return pen.width();
+    return m_lineWidth;
 }
 
-void CPathItem::setLineWidth(int width)
+void CPathItem::setLineWidth(int w)
 {
-    QPen pen = this->pen();
-    pen.setWidth(width);
-    setPen(pen);
+    m_lineWidth = w;
+    useCustomSettings();
 }
 
-QColor CPathItem::lineColor() const
+QColor CPathItem::normaLineColor() const
 {
-    QPen pen = this->pen();
-    return pen.color();
+    return m_normalLineColor;
 }
 
-void CPathItem::setLineColor(QColor color)
+void CPathItem::setNormalLineColor(QColor c)
 {
-    QPen pen = this->pen();
-    pen.setColor(color);
-    setPen(pen);
+    m_normalLineColor = c;
+    useCustomSettings();
 }
 
-QColor CPathItem::fillColor() const
+QColor CPathItem::highlightLineColor() const
 {
-    QBrush brush = this->brush();
-    return brush.color();
+    return m_highlightLineColor;
+}
+
+void CPathItem::setHighlightLineColor(QColor c)
+{
+    m_highlightLineColor = c;
+    useCustomSettings();
 }
 
-void CPathItem::setFillColor(QColor color)
+QColor CPathItem::normalFillColor() const
 {
-    setBrush(color);
+    return m_normalFillColor;
 }
 
-void CPathItem::setHighlighted(bool highlighted)
+void CPathItem::setNormalFillColor(QColor c)
 {
-    setLineColor(highlighted ? Qt::blue : Qt::transparent);
-    setFillColor(highlighted ? Qt::gray : Qt::white);
+    m_normalFillColor = c;
+    useCustomSettings();
+}
+
+QColor CPathItem::highlightFillColor() const
+{
+    return m_highlightFillColor;
+}
+
+void CPathItem::setHighlightFillColor(QColor c)
+{
+    m_highlightFillColor = c;
+    useCustomSettings();
+}
+
+bool CPathItem::highlighted() const
+{
+    return m_highlighted;
+}
+
+void CPathItem::setHighlighted(bool h)
+{
+    m_highlighted = h;
+    useCustomSettings();
+}
+
+void CPathItem::useCustomSettings()
+{
+    QPen pen = this->pen();
+    pen.setWidth(m_lineWidth);
+    pen.setColor(m_highlighted ? m_highlightLineColor : m_normalLineColor);
+    setPen(pen);
+
+    QBrush brush = this->brush();
+    brush.setColor(m_highlighted ? m_highlightFillColor : m_normalFillColor);
+    setBrush(brush);
 }

+ 29 - 6
QFD/CCanvas/CPathItem.h

@@ -9,15 +9,38 @@ public:
     CPathItem(QGraphicsItem *parent = nullptr);
 
     int lineWidth() const;
-    void setLineWidth(int width);
+    void setLineWidth(int w);
 
-    QColor lineColor() const;
-    void setLineColor(QColor color);
+    QColor normaLineColor() const;
+    void setNormalLineColor(QColor c);
 
-    QColor fillColor() const;
-    void setFillColor(QColor color);
+    QColor highlightLineColor() const;
+    void setHighlightLineColor(QColor c);
 
-    virtual void setHighlighted(bool highlighted);
+    QColor normalFillColor() const;
+    void setNormalFillColor(QColor c);
+
+    QColor highlightFillColor() const;
+    void setHighlightFillColor(QColor c);
+
+    bool highlighted() const;
+    void setHighlighted(bool h);
+
+    virtual void updatePath() = 0;
+
+private:
+    void useCustomSettings();
+
+private:
+    int m_lineWidth = 2;
+
+    QColor m_normalLineColor    = Qt::gray;
+    QColor m_highlightLineColor = Qt::blue;
+
+    QColor m_normalFillColor    = Qt::white;
+    QColor m_highlightFillColor = Qt::lightGray;
+
+    bool m_highlighted = false;
 };
 
 #endif  // CPATHITEM_H

+ 40 - 2
QFD/CCanvas/CRectItem.cpp

@@ -1,5 +1,43 @@
 #include "CRectItem.h"
 
-CRectItem::CRectItem(QGraphicsItem *parent) : CPathItem(parent) { }
+#include <QDebug>
 
-void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { }
+CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
+
+CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
+{
+    updatePath();
+}
+
+QRectF CRectItem::rect() const
+{
+    return m_rect;
+}
+
+void CRectItem::setRect(const QRectF &rect)
+{
+    m_rect = rect;
+}
+
+int CRectItem::cornerRadius() const
+{
+    return m_cornerRadius;
+}
+
+void CRectItem::setCornerRadius(qreal radius)
+{
+    m_cornerRadius = radius;
+    updatePath();
+}
+
+void CRectItem::updatePath()
+{
+    QPainterPath path;
+    path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
+    setPath(path);
+}
+
+void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
+{
+    qDebug() << __FUNCTION__ << __LINE__ << event << endl;
+}

+ 17 - 0
QFD/CCanvas/CRectItem.h

@@ -8,7 +8,24 @@ class CRectItem : public CPathItem
 public:
     explicit CRectItem(QGraphicsItem *parent = nullptr);
 
+    explicit CRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr);
+
+    QRectF rect() const;
+
+    void setRect(const QRectF &rect);
+
+    int cornerRadius() const;
+
+    void setCornerRadius(qreal radius);
+
+    void updatePath() override;
+
     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
+
+private:
+    QRectF m_rect;
+
+    qreal m_cornerRadius = 5;
 };
 
 #endif  // CRECTITEM_H

+ 6 - 0
QFD/CCanvas/CTextItem.cpp

@@ -0,0 +1,6 @@
+#include "CTextItem.h"
+
+CTextItem::CTextItem()
+{
+
+}

+ 13 - 0
QFD/CCanvas/CTextItem.h

@@ -0,0 +1,13 @@
+#ifndef CTEXTITEM_H
+#define CTEXTITEM_H
+
+#include <QGraphicsTextItem>
+
+class CTextItem : public QGraphicsTextItem
+{
+    Q_OBJECT
+public:
+    CTextItem();
+};
+
+#endif // CTEXTITEM_H

+ 1 - 1
QFD/view/HomeView.cpp

@@ -44,7 +44,7 @@ void HomeView::hideEvent(QHideEvent *event)
 void HomeView::initWidgets()
 {
     m_title = new QLabel(this);
-    m_title->setText("项目列表");
+    m_title->setText("评估项目管理");
     QFont ft("Microsoft YaHei", 12);
     m_title->setFont(ft);
 

+ 3 - 0
QFD/view/MainWindow.cpp

@@ -244,6 +244,7 @@ void MainWindow::slotOpenProject(ProjectInfo *proj)
         m_stackWidget->setCurrentIndex(index);
     } else {
         CMindView *m = new CMindView(m_mainWidget);
+
         m->setObjectName(n);
         m_stackWidget->addWidget(m);
 
@@ -251,5 +252,7 @@ void MainWindow::slotOpenProject(ProjectInfo *proj)
         m_naviInterface->addItem(m->objectName(), NEWFLICON(QFDIcon, Project), t, this, SLOT(slotNaviItemClicked()),
                                  true, NavigationItemPosition::SCROLL);
         m_stackWidget->setCurrentWidget(m);
+
+        m->setWindowTitle(t);
     }
 }