添加图形编辑模块,并实现了圆角矩形的编辑操作

This commit is contained in:
duanshengchao 2024-08-16 11:09:51 +08:00
parent 76c24a73be
commit 66cdb48554
6 changed files with 152 additions and 23 deletions

View File

@ -77,7 +77,8 @@ public:
virtual void moveMovingCopy(const QPointF&) {};
virtual void resize(int,double, double, const QPointF&) {}
virtual void move(const QPointF& point) { Q_UNUSED(point); }
virtual void move(const QPointF&) {}
virtual void editShape(int, const QPointF&) {}
virtual void updateCoordinate() {}
@ -130,33 +131,34 @@ public:
}
virtual void updateHandles()
{
int nMargin = 5;
const QRectF& boundRect = this->boundingRect();
for(auto it = m_vecHanle.begin(); it != m_vecHanle.end(); it++)
{
switch ((*it)->getTag()) {
case H_leftTop:
(*it)->move(boundRect.x(), boundRect.y());
(*it)->move(boundRect.x() - nMargin, boundRect.y() - nMargin);
break;
case H_top:
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y());
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y() - nMargin);
break;
case H_rightTop:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y());
(*it)->move(boundRect.x() + boundRect.width() + nMargin, boundRect.y() - nMargin);
break;
case H_right:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y() + boundRect.height() * 0.5);
(*it)->move(boundRect.x() + boundRect.width() + nMargin, boundRect.y() + boundRect.height() * 0.5 + nMargin);
break;
case H_rightBottom:
(*it)->move(boundRect.x() + boundRect.width(), boundRect.y() + boundRect.height());
(*it)->move(boundRect.x() + boundRect.width() + nMargin, boundRect.y() + boundRect.height() + nMargin);
break;
case H_bottom:
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y() + boundRect.height());
(*it)->move(boundRect.x() + boundRect.width() * 0.5, boundRect.y() + boundRect.height()+ nMargin);
break;
case H_leftBottom:
(*it)->move(boundRect.x(), boundRect.y() + boundRect.height());
(*it)->move(boundRect.x() - nMargin, boundRect.y() + boundRect.height() + nMargin);
break;
case H_left:
(*it)->move(boundRect.x(), boundRect.y() + boundRect.height() * 0.5);
(*it)->move(boundRect.x() - nMargin, boundRect.y() + boundRect.height() * 0.5);
break;
default:
break;

View File

@ -12,6 +12,7 @@ public:
void resize(int,double, double, const QPointF&);
void updateCoordinate();
void move(const QPointF&);
void editShape(int, const QPointF&);
protected:
virtual QPainterPath shape();

View File

@ -0,0 +1,33 @@
/**
*\file editingSelector.h
*
*\brief selector
*
*\author dsc
*/
#ifndef EDITINGSELECTOR_H
#define EDITINGSELECTOR_H
#include "baseSelector.h"
#include "global.h"
class GraphicsBaseItem;
class EditingSelector : public BaseSelector
{
Q_OBJECT
public:
explicit EditingSelector(QObject *parent = 0);
virtual ~EditingSelector();
public:
void mousePressEvent(QGraphicsSceneMouseEvent*, DesignerScene*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*, DesignerScene*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*, DesignerScene*);
private:
GraphicsBaseItem* m_pEditingItem;
};
#endif

View File

@ -15,10 +15,12 @@ GraphicsRectItem::GraphicsRectItem(const QRect &rect, bool isRound, QGraphicsIte
if (m_bIsRound) //圆角矩形添加两个圆角大小控制点
{
//横轴X控制点
ItemControlHandle* pHandle1 = new ItemControlHandle(this);
pHandle1->setType(T_editShape);
pHandle1->setTag(H_edit);
m_vecHanle.push_back(pHandle1);
//纵轴Y控制点
ItemControlHandle* pHandle2 = new ItemControlHandle(this);
pHandle2->setType(T_editShape);
pHandle2->setTag(H_edit+1);
@ -31,7 +33,6 @@ GraphicsRectItem::~GraphicsRectItem()
}
QPainterPath GraphicsRectItem::shape()
{
QPainterPath path;
@ -50,7 +51,6 @@ QPainterPath GraphicsRectItem::shape()
return path;
}
void GraphicsRectItem::updateHandles()
{
GraphicsBaseItem::updateHandles();
@ -84,25 +84,24 @@ void GraphicsRectItem::updateCoordinate()
m_lastBoudingRect = m_boundingRect;
}
void GraphicsRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->setPen(m_pen);
painter->setBrush(m_brush);
double dHandleX = 0.0;
double dHandleY = 0.0;
if(m_dRatioX>0)
dHandleX = m_dWidth * m_dRatioX + 0.5;
if(m_dRatioY>0)
dHandleY = m_dHeight * m_dRatioY + 0.5;
if(m_bIsRound)
painter->drawRoundedRect(m_boundingRect, dHandleX, dHandleY);
{
double dRadiusX = 0.0;
double dRadiusY = 0.0;
if(m_dRatioX>0)
dRadiusX = m_dWidth * m_dRatioX + 0.5;
if(m_dRatioY>0)
dRadiusY = m_dHeight * m_dRatioY + 0.5;
painter->drawRoundedRect(m_boundingRect, dRadiusX, dRadiusY);
}
else
painter->drawRect(m_boundingRect);
}
void GraphicsRectItem::resize(int nHandle,double dSX, double dSY, const QPointF& basePoint)
@ -138,3 +137,43 @@ void GraphicsRectItem::move(const QPointF& point)
{
moveBy(point.x(), point.y());
}
void GraphicsRectItem::editShape(int nHandle,const QPointF& ptMouse)
{
switch (nHandle)
{
//横轴X控制点
case H_edit:
{
double dMouseX = ptMouse.x();
if(dMouseX < m_boundingRect.center().x())
dMouseX = m_boundingRect.center().x();
else if(dMouseX > m_boundingRect.right())
dMouseX = m_boundingRect.right();
double dWidth = m_boundingRect.width();
if(dWidth == 0.0)
dWidth = 1.0;
m_dRatioX = (m_boundingRect.right() - dMouseX) / dWidth;
}
break;
//纵轴Y控制点
case H_edit + 1:
{
double dMouseY = ptMouse.y();
if(dMouseY > m_boundingRect.center().y())
dMouseY = m_boundingRect.center().y();
else if(dMouseY < m_boundingRect.top())
dMouseY = m_boundingRect.top();
double dHeight = m_boundingRect.height();
if(dHeight == 0.0)
dHeight = 1.0;
m_dRatioY = (dMouseY - m_boundingRect.top()) / dHeight;
}
break;
default:
break;
}
prepareGeometryChange();
updateHandles();
}

View File

@ -55,8 +55,8 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen
else
{
m_opMode = OM_move;
emit setWorkingSelector(ST_moving);
setCursor(scene, Qt::ClosedHandCursor);
emit setWorkingSelector(ST_moving);
}
}
}

View File

@ -0,0 +1,54 @@
#include "util/EditingSelector.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <graphicsItem/graphicsBaseItem.h>
EditingSelector::EditingSelector(QObject *parent)
: BaseSelector(parent)
{
m_type = ST_editing;
}
EditingSelector::~EditingSelector()
{
}
void EditingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
}
void EditingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
ms_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)
{
QPointF ptOnItem = item->mapFromParent(ms_ptMouseLast);
item->editShape(ms_nDragHandle, ptOnItem);
}
}
}
}
void EditingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene)
{
QList<QGraphicsItem *> items = scene->selectedItems();
if (items.count() == 1)
{
GraphicsBaseItem* item = qgraphicsitem_cast<GraphicsBaseItem*>(items.first());
if(item && ms_ptMouseLast != ms_ptMouseDown)
{
item->updateCoordinate();
}
}
ms_nDragHandle = H_none;
setCursor(scene, Qt::ArrowCursor);
emit setWorkingSelector(ST_base);
}