PowerDesignerX/source/graphicsItem/graphicsBaseItem.cpp

151 lines
4.0 KiB
C++
Raw Normal View History

2024-08-09 16:00:52 +08:00
#include "graphicsItem/graphicsBaseItem.h"
GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
:QGraphicsItem(parent)
{
//初始化缩放操作用的handle
m_vecHanle.reserve(H_left);
for(int i = H_leftTop; i <= H_left; i++)
{
ItemControlHandle* pHandle = new ItemControlHandle(this);
pHandle->setType(T_resize);
pHandle->setTag(i);
m_vecHanle.push_back(pHandle);
}
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setAcceptHoverEvents(true);
}
GraphicsBaseItem::~GraphicsBaseItem()
{
for (size_t i = 0; i < m_vecHanle.size(); i++)
{
ItemControlHandle* pHandle = m_vecHanle[i];
if (pHandle)
{
delete pHandle;
pHandle = nullptr;
}
}
}
void GraphicsBaseItem::setWidth(double width)
{
m_dWidth = width;
updateCoordinate();
}
void GraphicsBaseItem::setHeight(double height)
{
m_dHeight = height;
updateCoordinate();
}
QVariant GraphicsBaseItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
{
if (change == QGraphicsItem::ItemSelectedHasChanged)
{
QGraphicsItemGroup *group = dynamic_cast<QGraphicsItemGroup *>(parentItem());
if(!group)
setHandleVisible(value.toBool());
else //在某一组群中,由组群展示是否选中,自身不做展示
{
setSelected(false);
return QVariant::fromValue<bool>(false);
}
}
return QGraphicsItem::itemChange(change, value);
}
void GraphicsBaseItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
Q_UNUSED(event);
}
void GraphicsBaseItem::setHandleVisible(bool visible)
{
for(auto it = m_vecHanle.begin(); it != m_vecHanle.end(); it++)
{
if(visible)
(*it)->show();
else
(*it)->hide();
}
}
QPointF GraphicsBaseItem::getSymmetricPointPos(int nHandle)
{
QPointF pt;
//编号从1开始因此下标需要-1
switch (nHandle)
{
case H_leftTop:
pt = m_vecHanle.at(H_rightBottom - 1)->pos();
break;
case H_top:
pt = m_vecHanle.at(H_bottom - 1)->pos();
break;
case H_rightTop:
pt = m_vecHanle.at(H_leftBottom - 1)->pos();
break;
case H_right:
pt = m_vecHanle.at(H_left - 1)->pos();
break;
case H_rightBottom:
pt = m_vecHanle.at(H_leftTop - 1)->pos();
break;
case H_bottom:
pt = m_vecHanle.at(H_top - 1)->pos();
break;
case H_leftBottom:
pt = m_vecHanle.at(H_rightTop - 1)->pos();
break;
case H_left:
pt = m_vecHanle.at(H_right - 1)->pos();
break;
default:
break;
}
return pt;
}
void GraphicsBaseItem::updateHandles()
{
const QRectF& boundRect = this->boundingRect();
for(auto it = m_vecHanle.begin(); it != m_vecHanle.end(); it++)
{
switch ((*it)->getTag()) {
case H_leftTop:
(*it)->move(boundRect.x(), boundRect.y());
break;
case H_top:
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y());
break;
case H_rightTop:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y());
break;
case H_right:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y() + boundRect.height() * 0.5);
break;
case H_rightBottom:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y() + boundRect.height());
break;
case H_bottom:
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y() + boundRect.height());
break;
case H_leftBottom:
(*it)->move(boundRect.x(), boundRect.y() + boundRect.height());
break;
case H_left:
(*it)->move(boundRect.x(), boundRect.y() + boundRect.height() * 0.5);
break;
default:
break;
}
}
}