瀏覽代碼

节点尺寸约束

chengxr 1 年之前
父節點
當前提交
c2983d46b7
共有 2 個文件被更改,包括 37 次插入3 次删除
  1. 28 3
      QFD/CCanvas/CNodeItem.cpp
  2. 9 0
      QFD/CCanvas/CNodeItem.h

+ 28 - 3
QFD/CCanvas/CNodeItem.cpp

@@ -80,6 +80,28 @@ void CNodeItem::setYMargin(qreal y)
     updateItemsGeometry();
 }
 
+qreal CNodeItem::minWidth() const
+{
+    return m_minWidth;
+}
+
+void CNodeItem::setMinWidth(qreal w)
+{
+    m_minWidth = w;
+    updateItemsGeometry();
+}
+
+qreal CNodeItem::minHeight() const
+{
+    return m_minHeight;
+}
+
+void CNodeItem::setMinHeight(qreal h)
+{
+    m_minHeight = h;
+    updateItemsGeometry();
+}
+
 void CNodeItem::connectSignalsAndSlots()
 {
     connect(m_rectItem->selectAction(), &QAction::triggered, this, &CNodeItem::slotSelect);
@@ -89,9 +111,12 @@ void CNodeItem::connectSignalsAndSlots()
 
 void CNodeItem::updateItemsGeometry()
 {
-    QSizeF s = m_textItem->boundingRect().size() + QSizeF(m_xMargin * 2, m_yMargin * 2);
-    m_rectItem->setRect(QRectF(m_pos, s));
-    m_textItem->setPos(m_pos + QPointF(m_xMargin, m_yMargin));
+    QRectF tr = m_textItem->boundingRect();
+    qreal w   = std::max(tr.width() + m_xMargin * 2, m_minWidth);
+    qreal h   = std::max(tr.height() + m_yMargin * 2, m_minHeight);
+
+    m_rectItem->setRect(QRectF(m_pos, QSizeF(w, h)));
+    m_textItem->setPos(m_pos + QPointF(m_xMargin, (h - tr.height()) / 2));
 }
 
 void CNodeItem::slotSelect()

+ 9 - 0
QFD/CCanvas/CNodeItem.h

@@ -31,6 +31,12 @@ public:
     qreal yMargin() const;
     void setYMargin(qreal y);
 
+    qreal minWidth() const;
+    void setMinWidth(qreal w);
+
+    qreal minHeight() const;
+    void setMinHeight(qreal h);
+
     void connectSignalsAndSlots();
 
 private:
@@ -54,6 +60,9 @@ private:
 
     qreal m_xMargin = 10;
     qreal m_yMargin = 5;
+
+    qreal m_minWidth  = 100;
+    qreal m_minHeight = 30;
 };
 
 #endif  // CNODEITEM_H