DiagramDesigner/source/graphicsItem/graphicsBaseItem.cpp

117 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "graphicsItem/graphicsBaseItem.h"
#include <QGraphicsScene>
GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent)
: AbstractShapeType<QGraphicsItem>(parent)
{
m_type = T_item;
//初始化缩放操作用的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());
else //在某一组群中,由组群展示是否选中,自身不做展示
{
setSelected(false);
return QVariant::fromValue<bool>(false);
}
}
return QGraphicsItem::itemChange(change, value);
}
void GraphicsBaseItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
Q_UNUSED(event);
}