98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
#include "graphicsItem/functionModelItem/graphicsFunctionModelItem.h"
|
||
|
||
GraphicsFunctionModelItem::GraphicsFunctionModelItem(QGraphicsItem *parent)
|
||
: GraphicsProjectModelItem(parent)
|
||
{
|
||
|
||
}
|
||
|
||
GraphicsProjectModelItem::~GraphicsProjectModelItem()
|
||
{
|
||
}
|
||
|
||
void GraphicsProjectModelItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
||
{
|
||
Q_UNUSED(option)
|
||
Q_UNUSED(widget)
|
||
}
|
||
|
||
|
||
/********************************功能模item组*************************************/
|
||
|
||
GraphicsFunctionModelGroup::GraphicsFunctionModelGroup(QGraphicsItem *parent)
|
||
:GraphicsFunctionModelItem(parent)
|
||
{
|
||
|
||
}
|
||
|
||
GraphicsFunctionModelGroup::~GraphicsFunctionModelGroup()
|
||
{
|
||
|
||
}
|
||
|
||
void GraphicsFunctionModelGroup::addItem(GraphicsFunctionModelItem* item)
|
||
{
|
||
item->setParentItem(this); // 关键:设置父项
|
||
m_childItems.append(item);
|
||
updateLayout();
|
||
}
|
||
|
||
QRectF GraphicsFunctionModelGroup::updateBoundRect()
|
||
{
|
||
QRectF rect;
|
||
if(m_childItems.size()){
|
||
for (auto* child : m_childItems) {
|
||
rect |= child->boundingRect().translated(child->pos());
|
||
}
|
||
|
||
m_boundingRect = rect;
|
||
}
|
||
else
|
||
{
|
||
return m_boundingRect;
|
||
}
|
||
|
||
updateHandles();
|
||
return rect;
|
||
}
|
||
|
||
void GraphicsFunctionModelGroup::updateLayout()
|
||
{
|
||
if (m_childItems.isEmpty()) return;
|
||
|
||
// 计算所有子项的总尺寸
|
||
qreal totalSize = 0;
|
||
QList<qreal> childSizes;
|
||
|
||
for (GraphicsBaseItem *child : m_childItems) {
|
||
QRectF childRect = child->boundingRect();
|
||
qreal size = (m_direction == 0) ? childRect.width() : childRect.height();
|
||
childSizes.append(size);
|
||
totalSize += size;
|
||
}
|
||
|
||
// 计算总间距
|
||
qreal totalSpacing = m_spacing * (m_childItems.size() - 1);
|
||
|
||
// 计算起始位置(相对于父项中心)
|
||
qreal startPos = -(totalSize + totalSpacing) / 2;
|
||
|
||
// 定位每个子项
|
||
qreal currentPos = startPos;
|
||
for (int i = 0; i < m_childItems.size(); ++i) {
|
||
GraphicsBaseItem *child = m_childItems[i];
|
||
QRectF childRect = child->boundingRect();
|
||
|
||
if (m_direction == 0) {
|
||
// 水平布局:x坐标变化,y坐标保持中心对齐
|
||
child->setPos(currentPos, -childRect.height() / 2);
|
||
currentPos += childSizes[i] + m_spacing;
|
||
} else {
|
||
// 垂直布局:y坐标变化,x坐标保持中心对齐
|
||
child->setPos(-childRect.width() / 2, currentPos);
|
||
currentPos += childSizes[i] + m_spacing;
|
||
}
|
||
}
|
||
updateBoundRect();
|
||
}
|