diff --git a/include/graphicsItem/graphicsBaseItem.h b/include/graphicsItem/graphicsBaseItem.h index 5537978..0f9a7d6 100644 --- a/include/graphicsItem/graphicsBaseItem.h +++ b/include/graphicsItem/graphicsBaseItem.h @@ -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; diff --git a/include/graphicsItem/graphicsRectItem.h b/include/graphicsItem/graphicsRectItem.h index d03315a..0b0ee6f 100644 --- a/include/graphicsItem/graphicsRectItem.h +++ b/include/graphicsItem/graphicsRectItem.h @@ -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(); diff --git a/include/util/editingSelector.h b/include/util/editingSelector.h new file mode 100644 index 0000000..03d053b --- /dev/null +++ b/include/util/editingSelector.h @@ -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 diff --git a/source/graphicsItem/graphicsRectItem.cpp b/source/graphicsItem/graphicsRectItem.cpp index b70e497..1eb3377 100644 --- a/source/graphicsItem/graphicsRectItem.cpp +++ b/source/graphicsItem/graphicsRectItem.cpp @@ -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(); +} diff --git a/source/util/baseSelector.cpp b/source/util/baseSelector.cpp index 2e01e8e..a051ee2 100644 --- a/source/util/baseSelector.cpp +++ b/source/util/baseSelector.cpp @@ -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); } } } diff --git a/source/util/editingSelector.cpp b/source/util/editingSelector.cpp new file mode 100644 index 0000000..d70b0ba --- /dev/null +++ b/source/util/editingSelector.cpp @@ -0,0 +1,54 @@ +#include "util/EditingSelector.h" +#include +#include +#include + +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 items = scene->selectedItems(); + if (items.count() == 1) + { + GraphicsBaseItem* item = qgraphicsitem_cast(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 items = scene->selectedItems(); + if (items.count() == 1) + { + GraphicsBaseItem* item = qgraphicsitem_cast(items.first()); + if(item && ms_ptMouseLast != ms_ptMouseDown) + { + item->updateCoordinate(); + } + } + + ms_nDragHandle = H_none; + setCursor(scene, Qt::ArrowCursor); + emit setWorkingSelector(ST_base); +}