218 lines
6.9 KiB
C++
218 lines
6.9 KiB
C++
|
|
#include "graphicsItem/graphicsItemGroup.h"
|
|||
|
|
#include <QGraphicsScene>
|
|||
|
|
#include <QPainter>
|
|||
|
|
#include <QStyleOption>
|
|||
|
|
|
|||
|
|
GraphicsItemGroup::GraphicsItemGroup(QGraphicsItem *parent)
|
|||
|
|
: AbstractShapeType<QGraphicsItemGroup>(parent)
|
|||
|
|
{
|
|||
|
|
m_boundingRect = QRectF();
|
|||
|
|
|
|||
|
|
//初始化缩放操作用的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);
|
|||
|
|
}
|
|||
|
|
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.push_back(pHandle);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
setFlag(QGraphicsItem::ItemIsMovable, true);
|
|||
|
|
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|||
|
|
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|||
|
|
setAcceptHoverEvents(true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
GraphicsItemGroup::~GraphicsItemGroup()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QPainterPath GraphicsItemGroup::shape()
|
|||
|
|
{
|
|||
|
|
QPainterPath path;
|
|||
|
|
path.addRect(m_boundingRect);
|
|||
|
|
return path;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|||
|
|
{
|
|||
|
|
if (option->state & QStyle::State_Selected) //是选中状态,绘制选中框
|
|||
|
|
{
|
|||
|
|
int nPenWidth = 1;
|
|||
|
|
painter->setPen(QPen(QColor(70,70,70), nPenWidth, Qt::DashLine)); //蓝色的外框
|
|||
|
|
painter->setBrush(Qt::NoBrush);
|
|||
|
|
painter->drawRect(m_boundingRect_selected);
|
|||
|
|
|
|||
|
|
//绘制变换原点
|
|||
|
|
// QPointF originPoint = transformOriginPoint();
|
|||
|
|
// painter->setBrush(Qt::red);
|
|||
|
|
// painter->drawEllipse(originPoint, 4, 4);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
|
|||
|
|
{
|
|||
|
|
Q_UNUSED(event);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QVariant GraphicsItemGroup::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 QGraphicsItemGroup::itemChange(change, value);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::updateCoordinate() //当执行了resie和editShape函数后,boundingRect发生了变换,需要将item的原点(以中心点为原点)校准至boundingRect.center()
|
|||
|
|
{
|
|||
|
|
if (!parentItem())
|
|||
|
|
{
|
|||
|
|
if (m_boundingRect.isNull())
|
|||
|
|
{
|
|||
|
|
m_boundingRect = QGraphicsItemGroup::boundingRect();
|
|||
|
|
m_dWidth = m_boundingRect.width();
|
|||
|
|
m_dHeight = m_boundingRect.height();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QPointF pt1, pt2, delta;
|
|||
|
|
pt1 = mapToScene(transformOriginPoint());
|
|||
|
|
pt2 = mapToScene(m_boundingRect.center());
|
|||
|
|
delta = pt1 - pt2;
|
|||
|
|
|
|||
|
|
prepareGeometryChange();
|
|||
|
|
//boundingRect发生变化后原点偏离中心,将原点设为中心点,因为group内含有其它item,所以采用transform变换的方式
|
|||
|
|
//m_boundingRect = QRectF(-m_dWidth / 2, -m_dHeight / 2, m_dWidth, m_dHeight);
|
|||
|
|
setTransform(transform().translate(delta.x(), delta.y()));
|
|||
|
|
setTransformOriginPoint(m_boundingRect.center());
|
|||
|
|
//更改原点后自身绘制会在新坐标系下进行,会有跳转,所以需要将item整体做对应的移动
|
|||
|
|
moveBy(-delta.x(), -delta.y());
|
|||
|
|
updateHandles();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
m_lastBoudingRect = m_boundingRect;
|
|||
|
|
|
|||
|
|
//调用该组内所有item(过滤handle)相关函数
|
|||
|
|
foreach (QGraphicsItem *item, childItems())
|
|||
|
|
{
|
|||
|
|
GraphicsBaseItem *baseItem = qgraphicsitem_cast<GraphicsBaseItem *>(item);
|
|||
|
|
if (baseItem && !qgraphicsitem_cast<ItemControlHandle *>(baseItem))
|
|||
|
|
{
|
|||
|
|
baseItem->updateCoordinate();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::resize(int nHandle,double dSX, double dSY, const QPointF& basePoint)
|
|||
|
|
{
|
|||
|
|
switch (nHandle)
|
|||
|
|
{
|
|||
|
|
case H_left:
|
|||
|
|
case H_right:
|
|||
|
|
dSY = 1; //拖拽的是左点右点,为水平缩放,忽略垂直变化
|
|||
|
|
break;
|
|||
|
|
case H_top:
|
|||
|
|
case H_bottom:
|
|||
|
|
dSX = 1; //拖拽的是顶点底点,为垂直缩放,忽略水平变化
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QTransform trans;
|
|||
|
|
//缩放是以图元原点(中心)位置为基准,所以每帧都先移动移动到想要的基准点,缩放之后再移回
|
|||
|
|
trans.translate(basePoint.x(), basePoint.y());
|
|||
|
|
trans.scale(dSX, dSY);
|
|||
|
|
trans.translate(-basePoint.x(), -basePoint.y());
|
|||
|
|
|
|||
|
|
prepareGeometryChange();
|
|||
|
|
m_boundingRect = trans.mapRect(m_lastBoudingRect);
|
|||
|
|
m_dWidth = m_boundingRect.width();
|
|||
|
|
m_dHeight = m_boundingRect.height();
|
|||
|
|
updateHandles();
|
|||
|
|
|
|||
|
|
//调用该组内所有item(过滤handle)相关函数
|
|||
|
|
foreach (QGraphicsItem *item, childItems())
|
|||
|
|
{
|
|||
|
|
GraphicsBaseItem *baseItem = qgraphicsitem_cast<GraphicsBaseItem *>(item);
|
|||
|
|
if (baseItem && !qgraphicsitem_cast<ItemControlHandle *>(baseItem))
|
|||
|
|
{
|
|||
|
|
baseItem->resize(nHandle, dSX, dSY, baseItem->mapFromParent(basePoint));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::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 GraphicsItemGroup::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 GraphicsItemGroup::moveOperationCopy(const QPointF& distance)
|
|||
|
|
{
|
|||
|
|
if(m_pOperationCopy)
|
|||
|
|
m_pOperationCopy->setPos(m_movingIniPos + distance);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::rotateOperationCopy(const double& dAngle)
|
|||
|
|
{
|
|||
|
|
if(m_pOperationCopy)
|
|||
|
|
m_pOperationCopy->setRotation(dAngle);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void GraphicsItemGroup::addItems(const QList<QGraphicsItem*>& items)
|
|||
|
|
{
|
|||
|
|
foreach (QGraphicsItem *item, items)
|
|||
|
|
{
|
|||
|
|
item->setSelected(false);
|
|||
|
|
addToGroup(item);
|
|||
|
|
m_listItem.push_back(item);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
updateCoordinate();
|
|||
|
|
}
|