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