2024-08-16 11:39:30 +08:00
|
|
|
|
#include "util/creatingSelector.h"
|
|
|
|
|
|
#include "graphicsItem/graphicsRectItem.h"
|
2024-08-20 19:42:42 +08:00
|
|
|
|
#include "graphicsItem/graphicsPolygonItem.h"
|
2024-08-16 11:39:30 +08:00
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
|
#include <QGraphicsView>
|
|
|
|
|
|
|
|
|
|
|
|
CreatingSelector::CreatingSelector(QObject *parent)
|
|
|
|
|
|
: BaseSelector(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_type = ST_cerating;
|
2024-08-20 19:42:42 +08:00
|
|
|
|
m_creatingMethod = CM_drag;
|
2024-08-16 11:39:30 +08:00
|
|
|
|
m_pCreatingItem = nullptr;
|
2024-11-20 14:40:13 +08:00
|
|
|
|
//m_scalBasePoint = QPointF();
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
CreatingSelector::~CreatingSelector()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (event->button() != Qt::LeftButton)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
ms_ptMouseDown = event->scenePos();
|
|
|
|
|
|
ms_ptMouseLast = event->scenePos();
|
|
|
|
|
|
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if(m_pCreatingItem == nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
scene->clearSelection();
|
|
|
|
|
|
|
|
|
|
|
|
switch (m_creatingItemType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case GIT_rect:
|
|
|
|
|
|
{
|
|
|
|
|
|
m_creatingMethod = CM_drag;
|
2024-11-20 14:40:13 +08:00
|
|
|
|
m_pCreatingItem = new GraphicsRectItem(QRect(-2, -2, 4, 4));
|
2024-08-20 19:42:42 +08:00
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
break;
|
2024-08-20 19:42:42 +08:00
|
|
|
|
case GIT_roundRect:
|
|
|
|
|
|
{
|
|
|
|
|
|
m_creatingMethod = CM_drag;
|
|
|
|
|
|
m_pCreatingItem = new GraphicsRectItem(QRect(1, 1, 1, 1), true);
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
break;
|
2024-08-20 19:42:42 +08:00
|
|
|
|
case GIT_polygon:
|
|
|
|
|
|
{
|
|
|
|
|
|
m_creatingMethod = CM_click;
|
|
|
|
|
|
m_pCreatingItem = new GraphicPolygonItem();
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
break;
|
2024-08-20 19:42:42 +08:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(m_pCreatingItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pCreatingItem->setPos(event->scenePos());
|
|
|
|
|
|
m_pCreatingItem->setSelected(true);
|
|
|
|
|
|
scene->addItem(m_pCreatingItem);
|
|
|
|
|
|
|
|
|
|
|
|
if(m_creatingMethod == CM_drag)
|
|
|
|
|
|
{
|
|
|
|
|
|
ms_ptMouseDown += QPoint(2, 2);
|
|
|
|
|
|
ms_nDragHandle = H_rightBottom;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(m_creatingMethod == CM_click)
|
|
|
|
|
|
m_pCreatingItem->addPoint(ms_ptMouseDown);
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if(m_pCreatingItem && m_creatingMethod == CM_click)
|
|
|
|
|
|
{
|
|
|
|
|
|
//创建时添加了第一个点,紧接着再次添加第二点,然后从第二个点开始进行移动绘制
|
|
|
|
|
|
m_pCreatingItem->addPoint(ms_ptMouseDown);
|
|
|
|
|
|
ms_nDragHandle = m_pCreatingItem->handleCount();
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CreatingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
|
|
|
|
{
|
2024-08-20 19:42:42 +08:00
|
|
|
|
setCursor(scene, Qt::CrossCursor);
|
2024-08-16 11:39:30 +08:00
|
|
|
|
ms_ptMouseLast = event->scenePos();
|
|
|
|
|
|
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if (m_pCreatingItem && m_creatingMethod == CM_drag)
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
2024-11-20 14:40:13 +08:00
|
|
|
|
/*if(m_scalBasePoint.isNull()) //基准点不能采用临时变量,因为handle的坐标也在不断变化,计算会出现问题
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
2024-11-20 14:40:13 +08:00
|
|
|
|
m_scalBasePoint = m_pCreatingItem->getSymmetricPointPos(ms_nDragHandle); qDebug() << m_scalBasePoint;
|
2024-08-16 11:39:30 +08:00
|
|
|
|
if(m_scalBasePoint.x() == 0)
|
|
|
|
|
|
m_scalBasePoint.setX(1);
|
|
|
|
|
|
if(m_scalBasePoint.y() == 0)
|
|
|
|
|
|
m_scalBasePoint.setY(1);
|
2024-11-20 14:40:13 +08:00
|
|
|
|
}*/
|
2024-08-16 11:39:30 +08:00
|
|
|
|
|
2024-11-20 14:40:13 +08:00
|
|
|
|
QPointF scaleBasePoint = m_pCreatingItem->boundingRect().topLeft();
|
2024-08-16 11:39:30 +08:00
|
|
|
|
//计算缩放倍数
|
2024-11-20 14:40:13 +08:00
|
|
|
|
QPointF iniDelta = m_pCreatingItem->mapFromScene(ms_ptMouseDown) - scaleBasePoint;
|
|
|
|
|
|
QPointF lastDelta = m_pCreatingItem->mapFromScene(ms_ptMouseLast) - scaleBasePoint;
|
2024-08-16 11:39:30 +08:00
|
|
|
|
double sx = lastDelta.x() / iniDelta.x();
|
|
|
|
|
|
double sy = lastDelta.y() / iniDelta.y();
|
|
|
|
|
|
|
2024-11-20 14:40:13 +08:00
|
|
|
|
m_pCreatingItem->resize(ms_nDragHandle, sx, sy, scaleBasePoint);
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|
2024-08-20 19:42:42 +08:00
|
|
|
|
else if (m_pCreatingItem && m_creatingMethod == CM_click)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(ms_nDragHandle > H_left)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pCreatingItem->editShape(ms_nDragHandle, ms_ptMouseLast);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CreatingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
|
|
|
|
|
|
{
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if (m_pCreatingItem && m_creatingMethod == CM_drag)
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if (event->scenePos() == (ms_ptMouseDown - QPoint(2, 2))) //最小拖动范围
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_pCreatingItem->setSelected(false);
|
|
|
|
|
|
scene->removeItem(m_pCreatingItem);
|
|
|
|
|
|
delete m_pCreatingItem;
|
|
|
|
|
|
}
|
2024-08-20 19:42:42 +08:00
|
|
|
|
else if (ms_ptMouseLast != ms_ptMouseDown)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pCreatingItem->updateCoordinate();
|
|
|
|
|
|
emit scene->signalAddItem(m_pCreatingItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ms_nDragHandle = H_none;
|
|
|
|
|
|
m_pCreatingItem = nullptr;
|
2024-11-20 14:40:13 +08:00
|
|
|
|
//m_scalBasePoint = QPointF();
|
2024-08-20 19:42:42 +08:00
|
|
|
|
setCursor(scene, Qt::ArrowCursor);
|
|
|
|
|
|
emit setWorkingSelector(ST_base);
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|
2024-08-20 19:42:42 +08:00
|
|
|
|
else if (m_pCreatingItem && m_creatingMethod == CM_click && event->button() == Qt::RightButton) //右键结束绘制
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
2024-08-20 19:42:42 +08:00
|
|
|
|
if(m_pCreatingItem->endDrawing())
|
|
|
|
|
|
m_pCreatingItem->updateCoordinate();
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pCreatingItem->setSelected(false);
|
|
|
|
|
|
scene->removeItem(m_pCreatingItem);
|
|
|
|
|
|
delete m_pCreatingItem;
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
|
2024-08-20 19:42:42 +08:00
|
|
|
|
ms_nDragHandle = H_none;
|
|
|
|
|
|
m_pCreatingItem = nullptr;
|
|
|
|
|
|
setCursor(scene, Qt::ArrowCursor);
|
|
|
|
|
|
emit setWorkingSelector(ST_base);
|
|
|
|
|
|
}
|
2024-08-16 11:39:30 +08:00
|
|
|
|
}
|