Refactor selector classes

This commit is contained in:
Jesse Qu 2026-07-06 18:17:10 +08:00
parent 5d2caba154
commit f9db2eb876
14 changed files with 99 additions and 99 deletions

View File

@ -299,18 +299,18 @@ public:
// IPropertyGroupProvider - 默认返回空字符串(无分组),子类可重写
QString getPropertyGroup(const QString&) const override { return {}; }
virtual void createOperationCopy();
virtual void removeOperationCopy();
virtual void moveOperationCopy(const QPointF&);
virtual void rotateOperationCopy(const double&);
virtual void syncRotationDataFromParent(const double&);
virtual void createOperationCopy() override;
virtual void removeOperationCopy() override;
virtual void moveOperationCopy(const QPointF&) override;
virtual void rotateOperationCopy(const double&) override;
virtual void syncRotationDataFromParent(const double&) override;
//多边形、线段等点选创建的对象需要的函数
virtual void addPoint(const QPointF&) {}
virtual bool endDrawing() { return true; }
protected:
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange, const QVariant&);
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange, const QVariant&) override;
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent*) override;
};
#endif

View File

@ -1,11 +1,11 @@
#ifndef ITEMCONTROLHANDLE_H
#define ITEMCONTROLHANDLE_H
#include <QGraphicsRectItem>
#include <QtWidgets/QGraphicsRectItem>
enum HandleType
{
T_resize, //调整大小
T_resize=50, //调整大小
T_rotate, //旋转
T_editShape //编辑形状
};

View File

@ -50,9 +50,9 @@ private slots:
void onSignal_selectionChanged();
private:
QAction* SavePerspectiveAction = nullptr;
QWidgetAction* PerspectiveListAction = nullptr;
QComboBox* PerspectiveComboBox = nullptr;
QAction* m_pSavePerspectiveAction = nullptr;
QWidgetAction* m_pPerspectiveListAction = nullptr;
QComboBox* m_pPerspectiveComboBox = nullptr;
QUndoStack* m_pUndoStack;

View File

@ -47,8 +47,8 @@ public:
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent*, DesignerScene*);
SelectorType getSelectorType() { return m_type; }
//void setOperationMode(OperationMode m) { m_opMode = m; }
OperationMode getOperationMode() { return ms_opMode; }
// void setOperationMode(OperationMode m) { m_opMode = m; }
// OperationMode getOperationMode() { return s_opMode; }
void setCursor(DesignerScene*, const QCursor&);
@ -56,12 +56,12 @@ signals:
void setWorkingSelector(SelectorType);
protected:
//静态变量用于不同类型selector间的成员共享
static OperationMode ms_opMode;
static QPointF ms_ptMouseDown;
static QPointF ms_ptMouseLast;
static double ms_dAngleMouseDownToItem; //鼠标按下时其位置和item中心点形成的夹角
static int ms_nDragHandle; //当前抓取的控制点
// 静态变量用于不同类型selector间的成员共享
// static OperationMode s_opMode;
static QPointF s_ptMouseDown;
static QPointF s_ptMouseLast;
static double s_dAngleMouseDownToItem; //鼠标按下时其位置和item中心点形成的夹角
static int s_nDragHandle; //当前抓取的控制点
SelectorType m_type;

View File

@ -38,7 +38,6 @@ private:
CreatingMethod m_creatingMethod;
GraphicsItemType m_creatingItemType;
GraphicsBaseItem* m_pCreatingItem;
//QPointF m_scalBasePoint;
};
#endif

View File

@ -24,7 +24,7 @@ public:
void mouseReleaseEvent(QGraphicsSceneMouseEvent*, DesignerScene*);
private:
QPointF m_scalBasePoint;
QPointF m_scaleBasePoint;
};
#endif

View File

@ -42,17 +42,18 @@ private:
CGarbo() {};
~CGarbo()
{
if (m_pInstance != nullptr)
if (s_pInstance != nullptr)
{
//qDebug()<<"goodbye CGarbo";
delete m_pInstance;
m_pInstance = nullptr;
delete s_pInstance;
s_pInstance = nullptr;
}
}
};
private:
static SelectorManager* m_pInstance;
static SelectorManager* s_pInstance;
SelectorType m_curSelector;
QVector<BaseSelector*> m_vecSelectors;
};

View File

@ -4,10 +4,10 @@
#include <graphicsItem/graphicsBaseItem.h>
#include <QDebug>
QPointF BaseSelector::ms_ptMouseDown(0.0,0.0);
QPointF BaseSelector::ms_ptMouseLast(0.0,0.0);
double BaseSelector::ms_dAngleMouseDownToItem = 0.0;
int BaseSelector::ms_nDragHandle = 0;
QPointF BaseSelector::s_ptMouseDown(0.0, 0.0);
QPointF BaseSelector::s_ptMouseLast(0.0, 0.0);
double BaseSelector::s_dAngleMouseDownToItem = 0.0;
int BaseSelector::s_nDragHandle = 0;
BaseSelector::BaseSelector(QObject *parent)
: QObject(parent)
@ -26,8 +26,8 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen
if (event->button() != Qt::LeftButton)
return;
ms_ptMouseDown = event->scenePos();
ms_ptMouseLast = event->scenePos();
s_ptMouseDown = event->scenePos();
s_ptMouseLast = event->scenePos();
if(!m_bHoverOnHandel)
scene->callParentEvent(event); //此处是通过触发QGraphicsScene的事件函数来取消所有被选中item的选中状态
@ -41,27 +41,27 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen
if(item)
{
//需要增加当前是否点击在控制点的判断函数
ms_nDragHandle = item->collidesWithHandle(event->scenePos());
if(ms_nDragHandle != H_none && ms_nDragHandle <= H_left) //在缩放控制点上
s_nDragHandle = item->collidesWithHandle(event->scenePos());
if(s_nDragHandle != H_none && s_nDragHandle <= H_left) //在缩放控制点上
{
m_opMode = OM_scale;
emit setWorkingSelector(ST_scaling);
}
else if(ms_nDragHandle >= H_rotate_leftTop && ms_nDragHandle <= H_rotate_leftBottom) //在旋转控制点上
else if(s_nDragHandle >= H_rotate_leftTop && s_nDragHandle <= H_rotate_leftBottom) //在旋转控制点上
{
m_opMode = OM_rotate;
//计算夹角
QPointF originPoint = item->mapToScene(item->boundingRect().center());
double dLengthY = ms_ptMouseLast.y() - originPoint.y();
double dLengthX = ms_ptMouseLast.x() - originPoint.x();
ms_dAngleMouseDownToItem = atan2(dLengthY, dLengthX) * 180 / M_PI;
double dLengthY = s_ptMouseLast.y() - originPoint.y();
double dLengthX = s_ptMouseLast.x() - originPoint.x();
s_dAngleMouseDownToItem = atan2(dLengthY, dLengthX) * 180 / M_PI;
// if(atan2(dLengthY, dLengthX) < 0)
// ms_dAngleMouseDownToItem += 360.0;
// s_dAngleMouseDownToItem += 360.0;
//创建副本
item->createOperationCopy();
emit setWorkingSelector(ST_rotation);
}
else if(ms_nDragHandle > H_rotate_leftBottom) //编辑控制点上
else if(s_nDragHandle > H_rotate_leftBottom) //编辑控制点上
{
m_opMode = OM_edit;
setCursor(scene, Qt::ClosedHandCursor);
@ -102,7 +102,7 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen
void BaseSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
QList<QGraphicsItem *> items = scene->selectedItems();
@ -111,7 +111,7 @@ void BaseSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene
AbstractShape* item = qgraphicsitem_cast<AbstractShape*>(items.first());
if(item)
{
if(ms_nDragHandle == H_none)
if(s_nDragHandle == H_none)
{
//设置光标样式
int nHandle = item->collidesWithHandle(event->scenePos());
@ -288,7 +288,7 @@ void BaseSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerSc
m_opMode = OM_none;
m_bHoverOnHandel = false;
ms_nDragHandle = H_none;
s_nDragHandle = H_none;
scene->callParentEvent(event);
}

View File

@ -22,8 +22,8 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer
if (event->button() != Qt::LeftButton)
return;
ms_ptMouseDown = event->scenePos();
ms_ptMouseLast = event->scenePos();
s_ptMouseDown = event->scenePos();
s_ptMouseLast = event->scenePos();
if(m_pCreatingItem == nullptr)
{
@ -61,19 +61,19 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer
if(m_creatingMethod == CM_drag)
{
ms_ptMouseDown += QPoint(2, 2);
ms_nDragHandle = H_rightBottom;
s_ptMouseDown += QPoint(2, 2);
s_nDragHandle = H_rightBottom;
}
else if(m_creatingMethod == CM_click)
m_pCreatingItem->addPoint(ms_ptMouseDown);
m_pCreatingItem->addPoint(s_ptMouseDown);
}
}
if(m_pCreatingItem && m_creatingMethod == CM_click)
{
//创建时添加了第一个点,紧接着再次添加第二点,然后从第二个点开始进行移动绘制
m_pCreatingItem->addPoint(ms_ptMouseDown);
ms_nDragHandle = m_pCreatingItem->handleCount();
m_pCreatingItem->addPoint(s_ptMouseDown);
s_nDragHandle = m_pCreatingItem->handleCount();
}
}
@ -81,14 +81,14 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer
void CreatingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
setCursor(scene, Qt::CrossCursor);
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
if (m_pCreatingItem && m_creatingMethod == CM_drag)
{
/*if(m_scalBasePoint.isNull()) //基准点不能采用临时变量因为handle的坐标也在不断变化计算会出现问题
{
m_scalBasePoint = m_pCreatingItem->getSymmetricPointPos(ms_nDragHandle); qDebug() << m_scalBasePoint;
m_scalBasePoint = m_pCreatingItem->getSymmetricPointPos(s_nDragHandle); qDebug() << m_scalBasePoint;
if(m_scalBasePoint.x() == 0)
m_scalBasePoint.setX(1);
if(m_scalBasePoint.y() == 0)
@ -97,18 +97,18 @@ void CreatingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerS
QPointF scaleBasePoint = m_pCreatingItem->boundingRect().topLeft();
//计算缩放倍数
QPointF iniDelta = m_pCreatingItem->mapFromScene(ms_ptMouseDown) - scaleBasePoint;
QPointF lastDelta = m_pCreatingItem->mapFromScene(ms_ptMouseLast) - scaleBasePoint;
QPointF iniDelta = m_pCreatingItem->mapFromScene(s_ptMouseDown) - scaleBasePoint;
QPointF lastDelta = m_pCreatingItem->mapFromScene(s_ptMouseLast) - scaleBasePoint;
double sx = lastDelta.x() / iniDelta.x();
double sy = lastDelta.y() / iniDelta.y();
m_pCreatingItem->resize(ms_nDragHandle, sx, sy, scaleBasePoint);
m_pCreatingItem->resize(s_nDragHandle, sx, sy, scaleBasePoint);
}
else if (m_pCreatingItem && m_creatingMethod == CM_click)
{
if(ms_nDragHandle > H_left)
if(s_nDragHandle > H_left)
{
m_pCreatingItem->editShape(ms_nDragHandle, ms_ptMouseLast);
m_pCreatingItem->editShape(s_nDragHandle, s_ptMouseLast);
}
}
}
@ -117,19 +117,19 @@ void CreatingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, Design
{
if (m_pCreatingItem && m_creatingMethod == CM_drag)
{
if (event->scenePos() == (ms_ptMouseDown - QPoint(2, 2))) //最小拖动范围
if (event->scenePos() == (s_ptMouseDown - QPoint(2, 2))) //最小拖动范围
{
m_pCreatingItem->setSelected(false);
scene->removeItem(m_pCreatingItem);
delete m_pCreatingItem;
}
else if (ms_ptMouseLast != ms_ptMouseDown)
else if (s_ptMouseLast != s_ptMouseDown)
{
m_pCreatingItem->updateCoordinate();
emit scene->signalAddItem(m_pCreatingItem);
}
ms_nDragHandle = H_none;
s_nDragHandle = H_none;
m_pCreatingItem = nullptr;
//m_scalBasePoint = QPointF();
setCursor(scene, Qt::ArrowCursor);
@ -146,7 +146,7 @@ void CreatingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, Design
delete m_pCreatingItem;
}
ms_nDragHandle = H_none;
s_nDragHandle = H_none;
m_pCreatingItem = nullptr;
setCursor(scene, Qt::ArrowCursor);
emit setWorkingSelector(ST_base);

View File

@ -20,16 +20,16 @@ void EditingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerS
void EditingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
if(item)
{
if(ms_nDragHandle > H_left)
if(s_nDragHandle > H_left)
{
item->editShape(ms_nDragHandle, ms_ptMouseLast);
item->editShape(s_nDragHandle, s_ptMouseLast);
}
}
}
@ -41,13 +41,13 @@ void EditingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, Designe
if (items.count() == 1)
{
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
if(item && ms_ptMouseLast != ms_ptMouseDown)
if(item && s_ptMouseLast != s_ptMouseDown)
{
item->updateCoordinate();
}
}
ms_nDragHandle = H_none;
s_nDragHandle = H_none;
setCursor(scene, Qt::ArrowCursor);
emit setWorkingSelector(ST_base);
}

View File

@ -20,14 +20,14 @@ void MovingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerSc
void MovingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
QList<QGraphicsItem *> items = scene->selectedItems();
for(int n = 0; n < items.size(); n++)
{
AbstractShape* item = qgraphicsitem_cast<AbstractShape*>(items.at(n));
if(item)
item->moveOperationCopy(ms_ptMouseLast - ms_ptMouseDown);
item->moveOperationCopy(s_ptMouseLast - s_ptMouseDown);
}
}

View File

@ -20,7 +20,7 @@ void RotationSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer
void RotationSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
@ -29,13 +29,13 @@ void RotationSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerS
{
//计算夹角
QPointF originPoint = item->mapToScene(item->boundingRect().center());
double dLengthY = ms_ptMouseLast.y() - originPoint.y();
double dLengthX = ms_ptMouseLast.x() - originPoint.x();
double dLengthY = s_ptMouseLast.y() - originPoint.y();
double dLengthX = s_ptMouseLast.x() - originPoint.x();
double dAngleMouseToItem = atan2(dLengthY, dLengthX) * 180 / M_PI;
// if(atan2(dLengthY, dLengthX) < 0)
// dAngleMouseToItem += 360.0;
double rotationAngle = item->rotation() + (dAngleMouseToItem - ms_dAngleMouseDownToItem);
double rotationAngle = item->rotation() + (dAngleMouseToItem - s_dAngleMouseDownToItem);
//让角度保持在正负180的区间也就是上下两个半圈这样易于象限判断
if (rotationAngle > 180)
rotationAngle -= 360;
@ -61,7 +61,7 @@ void RotationSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, Design
}
ms_nDragHandle = H_none;
s_nDragHandle = H_none;
setCursor(scene, Qt::ArrowCursor);
emit setWorkingSelector(ST_base);
}

View File

@ -7,7 +7,7 @@ ScalingSelector::ScalingSelector(QObject *parent)
: BaseSelector(parent)
{
m_type = ST_scaling;
m_scalBasePoint = QPointF();
m_scaleBasePoint = QPointF();
}
ScalingSelector::~ScalingSelector()
{
@ -21,31 +21,31 @@ void ScalingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerS
void ScalingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_ptMouseLast = event->scenePos();
s_ptMouseLast = event->scenePos();
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
AbstractShape* item = qgraphicsitem_cast<AbstractShape*>(items.first());
if(item)
{
if(ms_nDragHandle != H_none)
if(s_nDragHandle != H_none)
{
if(m_scalBasePoint.isNull()) //基准点不能采用临时变量因为handle的坐标也在不断变化计算会出现问题
if(m_scaleBasePoint.isNull()) //基准点不能采用临时变量因为handle的坐标也在不断变化计算会出现问题
{
m_scalBasePoint = item->getSymmetricPointPos(ms_nDragHandle);
if(m_scalBasePoint.x() == 0)
m_scalBasePoint.setX(1);
if(m_scalBasePoint.y() == 0)
m_scalBasePoint.setY(1);
m_scaleBasePoint = item->getSymmetricPointPos(s_nDragHandle);
if(m_scaleBasePoint.x() == 0)
m_scaleBasePoint.setX(1);
if(m_scaleBasePoint.y() == 0)
m_scaleBasePoint.setY(1);
}
//计算缩放倍数
QPointF iniDelta = item->mapFromScene(ms_ptMouseDown) - m_scalBasePoint;
QPointF lastDelta = item->mapFromScene(ms_ptMouseLast) - m_scalBasePoint;
QPointF iniDelta = item->mapFromScene(s_ptMouseDown) - m_scaleBasePoint;
QPointF lastDelta = item->mapFromScene(s_ptMouseLast) - m_scaleBasePoint;
double sx = lastDelta.x() / iniDelta.x();
double sy = lastDelta.y() / iniDelta.y();
item->resize(ms_nDragHandle, sx, sy, m_scalBasePoint);
item->resize(s_nDragHandle, sx, sy, m_scaleBasePoint);
}
}
}
@ -57,14 +57,14 @@ void ScalingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, Designe
if (items.count() == 1)
{
AbstractShape* item = qgraphicsitem_cast<AbstractShape*>(items.first());
if(item && ms_ptMouseLast != ms_ptMouseDown)
if(item && s_ptMouseLast != s_ptMouseDown)
{
item->updateCoordinate();
}
}
ms_nDragHandle = H_none;
m_scalBasePoint = QPointF();
s_nDragHandle = H_none;
m_scaleBasePoint = QPointF();
setCursor(scene, Qt::ArrowCursor);
emit setWorkingSelector(ST_base);
}

View File

@ -5,7 +5,7 @@
#include "util/scalingSelector.h"
#include "util/editingSelector.h"
SelectorManager* SelectorManager::m_pInstance = nullptr;
SelectorManager* SelectorManager::s_pInstance = nullptr;
SelectorManager::SelectorManager(QObject *parent)
: QObject(parent)
@ -43,11 +43,11 @@ SelectorManager::~SelectorManager()
SelectorManager* SelectorManager::getInstance()
{
if(m_pInstance == nullptr)
if(s_pInstance == nullptr)
{
m_pInstance = new SelectorManager();
s_pInstance = new SelectorManager();
}
return m_pInstance;
return s_pInstance;
}
BaseSelector* SelectorManager::getWorkingSelector()