166 lines
4.7 KiB
C++
166 lines
4.7 KiB
C++
#include "graphicsItem/graphicsBaseItem.h"
|
||
#include "graphicsItem/itemPort.h"
|
||
#include <QGraphicsScene>
|
||
|
||
|
||
GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
|
||
: AbstractShapeType<QGraphicsItem>(parent)
|
||
{
|
||
m_type = T_item;
|
||
_portId = 1;
|
||
_lastPort = -1;
|
||
//初始化缩放操作用的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.insert(i-1,pHandle);
|
||
}
|
||
for(int i = H_rotate_leftTop; i <= H_rotate_leftBottom; i++)
|
||
{
|
||
ItemControlHandle* pHandle = new ItemControlHandle(this);
|
||
pHandle->setType(T_rotate);
|
||
pHandle->setTag(i);
|
||
m_vecHanle.insert(i-1,pHandle);
|
||
}
|
||
|
||
setFlag(QGraphicsItem::ItemIsMovable, true);
|
||
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
||
setAcceptHoverEvents(true);
|
||
}
|
||
|
||
GraphicsBaseItem::~GraphicsBaseItem()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
void GraphicsBaseItem::createOperationCopy()
|
||
{
|
||
m_pOperationCopy = new QGraphicsPathItem(this->shape());
|
||
m_pOperationCopy->setPen(Qt::DashLine);
|
||
m_pOperationCopy->setPos(this->pos());
|
||
m_pOperationCopy->setTransformOriginPoint(this->transformOriginPoint());
|
||
m_pOperationCopy->setTransform(this->transform());
|
||
m_pOperationCopy->setRotation(this->rotation());
|
||
m_pOperationCopy->setScale(this->scale());
|
||
m_pOperationCopy->setZValue(this->zValue());
|
||
|
||
QGraphicsScene* scene = this->scene();
|
||
if(scene && m_pOperationCopy)
|
||
{
|
||
scene->addItem(m_pOperationCopy);
|
||
m_movingIniPos = this->pos();
|
||
}
|
||
}
|
||
|
||
void GraphicsBaseItem::removeOperationCopy()
|
||
{
|
||
QGraphicsScene* scene = this->scene();
|
||
if(scene && m_pOperationCopy)
|
||
{
|
||
if(this->pos() != m_pOperationCopy->pos())
|
||
this->setPos(m_pOperationCopy->pos()); //本体移动到副本的位置
|
||
if(this->rotation() != m_pOperationCopy->rotation())
|
||
this->setRotation(m_pOperationCopy->rotation()); //本体旋转至副本的角度
|
||
|
||
scene->removeItem(m_pOperationCopy);
|
||
delete m_pOperationCopy;
|
||
m_pOperationCopy = nullptr;
|
||
}
|
||
}
|
||
|
||
void GraphicsBaseItem::moveOperationCopy(const QPointF& distance)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setPos(m_movingIniPos + distance);
|
||
}
|
||
|
||
void GraphicsBaseItem::rotateOperationCopy(const double& dAngle)
|
||
{
|
||
if(m_pOperationCopy)
|
||
m_pOperationCopy->setRotation(dAngle);
|
||
}
|
||
|
||
void GraphicsBaseItem::syncRotationDataFromParent(const double& data)
|
||
{
|
||
//m_dSyncRotationByParent = rotation() + data;
|
||
m_dSyncRotationByParent += data;
|
||
//让角度保持在正负180的区间,也就是上下两个半圈,这样易于象限判断
|
||
if (m_dSyncRotationByParent > 180)
|
||
m_dSyncRotationByParent -= 360;
|
||
if (m_dSyncRotationByParent < -180)
|
||
m_dSyncRotationByParent += 360;
|
||
}
|
||
|
||
QVariant GraphicsBaseItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
|
||
{
|
||
if (change == QGraphicsItem::ItemSelectedHasChanged)
|
||
{
|
||
QGraphicsItemGroup *group = dynamic_cast<QGraphicsItemGroup *>(parentItem());
|
||
if(!group)
|
||
setHandleVisible(value.toBool());
|
||
//setFunctionHandleVisible(false);
|
||
else //在某一组群中,由组群展示是否选中,自身不做展示
|
||
{
|
||
setSelected(false);
|
||
return QVariant::fromValue<bool>(false);
|
||
}
|
||
}
|
||
return QGraphicsItem::itemChange(change, value);
|
||
}
|
||
|
||
void GraphicsBaseItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
|
||
{
|
||
Q_UNUSED(event);
|
||
}
|
||
|
||
int GraphicsBaseItem::addPort(PortState typ,QPointF vec)
|
||
{
|
||
int ntagId = -1;
|
||
for(ntagId = H_connect;ntagId < 999;++ntagId) //添加到未占用位置
|
||
{
|
||
if(!m_vecHanle.contains(ntagId))
|
||
break;
|
||
}
|
||
if(typ == p_movable)
|
||
{
|
||
ItemPort* pPort = new ItemPort(this);
|
||
pPort->setType(T_lineInOut);
|
||
pPort->setTag(ntagId);
|
||
m_vecHanle.insert(ntagId,pPort);
|
||
pPort->setPos(vec);
|
||
|
||
m_mapPort.insert(QString::number(_portId++),pPort);
|
||
}
|
||
return ntagId;
|
||
}
|
||
|
||
void GraphicsBaseItem::addPort(PortState typ,int ntagId,QPointF vec)
|
||
{
|
||
if(typ == p_movable)
|
||
{
|
||
ItemPort* pPort = new ItemPort(this);
|
||
pPort->setType(T_lineInOut);
|
||
pPort->setTag(ntagId);
|
||
m_vecHanle.insert(ntagId,pPort);
|
||
pPort->setPos(vec);
|
||
|
||
m_mapPort.insert(QString::number(_portId++),pPort);
|
||
}
|
||
}
|
||
|
||
ItemPort* GraphicsBaseItem::getPortPtr(int n) const
|
||
{
|
||
for(auto iter:m_mapPort)
|
||
{
|
||
int tag = iter->getTag();
|
||
if(tag == n)
|
||
return iter;
|
||
}
|
||
return nullptr;
|
||
}
|