diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp index 9891e676a..853500a58 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp @@ -29,12 +29,12 @@ QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) : {} /** - * @brief QetGraphicsHandlerUtility::DrawHandler + * @brief QetGraphicsHandlerUtility::drawHandler * Draw the handler at pos @point, using the QPainter @painter. * @param painter : painter to use for drawing the handler * @param point : point to draw the handler */ -void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &point, bool color2) +void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2) { //Color of handler QColor inner(0xFF, 0xFF, 0xFF); @@ -53,6 +53,18 @@ void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &po painter->restore(); } +/** + * @brief QetGraphicsHandlerUtility::drawHandler + * Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2) + * @param painter + * @param points + * @param color2 + */ +void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector &points, bool color2) { + foreach(QPointF point, points) + drawHandler(painter, point, color2); +} + /** * @brief QetGraphicsHandlerUtility::pointIsInHandler * @param point : point to compare @@ -109,15 +121,62 @@ QRectF QetGraphicsHandlerUtility::getRect(const QPointF &point) const /** * @brief QetGraphicsHandlerUtility::pointsForRect - * Return the point of the rect in vector. - * The point are stored like this : - * top left, top right, bottom left, bottom right; + * Return the keys points of the rectangle, stored in a vector. + * The points in the vector are stored like this : + * ********** + * 0---1---2 + * | | + * 3 4 + * | | + * 5---6---7 + * ************ * @param rect * @return */ QVector QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect) { QVector vector; - vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight(); + QPointF point; + vector << rect.topLeft();//*****Top left + point = rect.center(); + point.setY(rect.top()); + vector << point;//**************Middle top + vector << rect.topRight();//****Top right + point = rect.center(); + point.setX(rect.left()); + vector << point;//**************Middle left + point.setX(rect.right()); + vector << point;//**************Middle right + vector << rect.bottomLeft();//**Bottom left + point = rect.center(); + point.setY(rect.bottom()); + vector << point;//*************Middle bottom + vector << rect.bottomRight();//*Bottom right return vector; } + +/** + * @brief QetGraphicsHandlerUtility::rectForPosAtIndex + * Return a rectangle after modification of the point '@pos' at index '@index' of original rectangle '@old_rect'. + * @param old_rect - the rectangle befor modification + * @param pos - the new position of a key point + * @param index - the index of the key point to modifie see QetGraphicsHandlerUtility::pointsForRect to know + * the index of each keys points of a rectangle) + * @return : the rectangle with modification. If index is lower than 0 or higher than 7, this method return old_rect. + */ +QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index) +{ + if (index < 0 || index > 7) return old_rect; + + QRectF rect = old_rect; + if (index == 0) rect.setTopLeft(pos); + else if (index == 1) rect.setTop(pos.y()); + else if (index == 2) rect.setTopRight(pos); + else if (index == 3) rect.setLeft(pos.x()); + else if (index == 4) rect.setRight(pos.x()); + else if (index == 5) rect.setBottomLeft(pos); + else if (index == 6) rect.setBottom(pos.y()); + else if (index == 7) rect.setBottomRight(pos); + + return rect; +} diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h index 2a7a8be63..cfbb31632 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h @@ -18,7 +18,6 @@ #ifndef QETGRAPHICSHANDLERUTILITY_H #define QETGRAPHICSHANDLERUTILITY_H -#include #include class QPainter; @@ -27,13 +26,15 @@ class QPainter; * @brief The QetGraphicsHandlerUtility class * This class provide some methods to create and use handler for * modify graphics shape like line rectangle etc... + * They also provide some conveniance static method. */ class QetGraphicsHandlerUtility { public: QetGraphicsHandlerUtility (qreal size = 1); void setSize(qreal size) {m_size = size;} - void DrawHandler (QPainter *painter, const QPointF & point, bool color2 = false); + void drawHandler (QPainter *painter, const QPointF & point, bool color2 = false); + void drawHandler(QPainter *painter, const QVector &points, bool color2 = false); QPointF posForHandler(const QPointF &point) const; bool pointIsInHandler (const QPointF &point, const QPointF &key_point) const; int pointIsHoverHandler (const QPointF &point, const QVector &vector) const; @@ -45,7 +46,8 @@ class QetGraphicsHandlerUtility qreal m_zoom_factor; public: - static QVector pointsForRect (const QRectF & rect); + static QVector pointsForRect (const QRectF &rect); + static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index); }; #endif // QETGRAPHICSHANDLERUTILITY_H diff --git a/sources/editor/elementprimitivedecorator.cpp b/sources/editor/elementprimitivedecorator.cpp index 7b54d2be5..5e44ea0d0 100644 --- a/sources/editor/elementprimitivedecorator.cpp +++ b/sources/editor/elementprimitivedecorator.cpp @@ -97,9 +97,8 @@ void ElementPrimitiveDecorator::paint(QPainter *painter, const QStyleOptionGraph pen.setCosmetic(true); painter -> setPen(pen); painter -> drawRect(modified_bounding_rect_); - //Draw the handler - foreach (QPointF point, getResizingsPoints()) - m_handler.DrawHandler(painter, point, decorated_items_.size()-1); + //Draw the handlers + m_handler.drawHandler(painter, getResizingsPoints(), decorated_items_.size()-1); // uncomment to draw the real bouding rect (=adjusted internal bounding rect) // painter -> setBrush(QBrush(QColor(240, 0, 0, 127))); diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index b6519f482..9cc81a901 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -260,48 +260,33 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti painter -> setPen(pen); - //vector use to draw handler if needed - QVector point_vector; - - //Draw the shape + //Draw the shape and handlers if is selected switch (m_shapeType) { case Line: painter->drawLine(QLineF(m_P1, m_P2)); if (isSelected()) - point_vector << m_P1 << m_P2; + m_handler.drawHandler(painter, QVector{m_P1, m_P2}); break; case Rectangle: painter->drawRect(QRectF(m_P1, m_P2)); if (isSelected()) - { - QRectF rect (m_P1, m_P2); - point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft(); - } + m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2))); break; case Ellipse: painter->drawEllipse(QRectF(m_P1, m_P2)); if (isSelected()) - { - QRectF rect (m_P1, m_P2); - point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft(); - } + m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2))); break; case Polyline: - { painter->drawPolyline(m_polygon); - point_vector = m_polygon; - } + if (isSelected()) + m_handler.drawHandler(painter, m_polygon); break; } - - //Draw handler if shape is selected - if (isSelected()) - foreach(QPointF point, point_vector) - m_handler.DrawHandler(painter, point); } /** @@ -388,26 +373,11 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } break; - case Rectangle: { - QRectF rect(m_P1, m_P2); - if (m_vector_index == 0) rect.setTopLeft(new_pos); - else if (m_vector_index == 1) rect.setTopRight(new_pos); - else if (m_vector_index == 2) rect.setBottomLeft(new_pos); - else if (m_vector_index == 3) rect.setBottomRight(new_pos); + case Rectangle: + setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); - setRect(rect); - } - break; - - case Ellipse: { - QRectF rect(m_P1, m_P2); - if (m_vector_index == 0) rect.setTopLeft(new_pos); - else if (m_vector_index == 1) rect.setTopRight(new_pos); - else if (m_vector_index == 2) rect.setBottomLeft(new_pos); - else if (m_vector_index == 3) rect.setBottomRight(new_pos); - - setRect(rect); - } + case Ellipse: + setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break; case Polyline: {