From b71d3a4c7d1c356c2f8aae40e79c11caf4f068e6 Mon Sep 17 00:00:00 2001 From: blacksun Date: Tue, 19 Jul 2016 17:12:30 +0000 Subject: [PATCH] Element editor : graphic part Diagram editor : shape item Gain a new way to be resized: mirror resizing. Click on the item for switch the resize mode git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4583 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- .../qetgraphicshandlerutility.cpp | 85 ++++++++++++++++--- .../qetgraphicshandlerutility.h | 12 ++- sources/editor/elementprimitivedecorator.cpp | 6 +- sources/editor/graphicspart/partarc.cpp | 24 +++++- sources/editor/graphicspart/partarc.h | 4 + sources/editor/graphicspart/partellipse.cpp | 24 +++++- sources/editor/graphicspart/partellipse.h | 4 + sources/editor/graphicspart/partrectangle.cpp | 24 +++++- sources/editor/graphicspart/partrectangle.h | 4 + sources/qetgraphicsitem/qetshapeitem.cpp | 36 +++++++- sources/qetgraphicsitem/qetshapeitem.h | 4 + 11 files changed, 201 insertions(+), 26 deletions(-) diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp index 0050123b2..80b2588c7 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.cpp @@ -24,8 +24,7 @@ * @param size : the size of the handler */ QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) : - m_size (size), - m_zoom_factor(1) + m_size (size) {} /** @@ -34,19 +33,15 @@ QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) : * @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) { - //Color of handler - QColor inner(0xFF, 0xFF, 0xFF); - QColor outer(0x00, 0x61, 0xFF); - if(color2) outer = QColor(0x1A, 0x5C, 0x14); //Setup the zoom factor to draw the handler in the same size at screen, //no matter the zoom of the QPainter m_zoom_factor = 1.0/painter->transform().m11(); painter->save(); - painter->setBrush(QBrush(inner)); - QPen square_pen(QBrush(outer), 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); + painter->setBrush(QBrush(m_inner_color)); + QPen square_pen(QBrush(m_outer_color), 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin); square_pen.setCosmetic(true); painter->setPen(square_pen); painter->drawRect(getRect(point)); @@ -55,14 +50,14 @@ void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &po /** * @brief QetGraphicsHandlerUtility::drawHandler - * Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2) + * Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point) * @param painter * @param points * @param color2 */ -void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector &points, bool color2) { +void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector &points) { foreach(QPointF point, points) - drawHandler(painter, point, color2); + drawHandler(painter, point); } /** @@ -107,6 +102,14 @@ QVector QetGraphicsHandlerUtility::handlerRect(const QVector &v return rect_vector; } +void QetGraphicsHandlerUtility::setInnerColor(QColor color) { + m_inner_color = color; +} + +void QetGraphicsHandlerUtility::setOuterColor(QColor color) { + m_outer_color = color; +} + /** * @brief QetGraphicsHandlerUtility::getRect * @param point @@ -192,6 +195,64 @@ QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, cons return rect; } +/** + * @brief QetGraphicsHandlerUtility::mirrorRectForPosAtIndex + * Return a rectangle after modification of the point '@pos' at index '@index' of original rectangle '@old_rect'. + * the opposite edge is modified inversely (like a mirror) + * @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::mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index) +{ + if (index < 0 || index > 7) return old_rect; + + QRectF rect = old_rect; + QPointF center = rect.center(); + + if (index == 0) { + qreal x = pos.x() + (pos.x() - rect.topLeft().x()); + qreal y = pos.y() + (pos.y() - rect.topLeft().y()); + rect.setTopLeft(QPointF(x,y)); + } + else if (index == 1) { + qreal y = pos.y() + (pos.y() - rect.topLeft().y()); + rect.setTop(y); + } + else if (index == 2) { + qreal x = pos.x() + (pos.x() - rect.topRight().x()); + qreal y = pos.y() + (pos.y() - rect.topLeft().y()); + rect.setTopRight(QPointF(x,y)); + } + else if (index == 3) { + qreal x = pos.x() + (pos.x() - rect.left()); + rect.setLeft(x); + } + else if (index == 4) { + qreal x = pos.x() + (pos.x() - rect.right()); + rect.setRight(x); + } + else if (index == 5) { + qreal x = pos.x() + (pos.x() - rect.bottomLeft().x()); + qreal y = pos.y() + (pos.y() - rect.bottomLeft().y()); + rect.setBottomLeft(QPointF(x,y)); + } + else if (index == 6) { + qreal y = pos.y() + (pos.y() - rect.bottom()); + rect.setBottom(y); + } + else if (index == 7) { + qreal x = pos.x() + (pos.x() - rect.bottomRight().x()); + qreal y = pos.y() + (pos.y() - rect.bottomRight().y()); + rect.setBottomRight(QPointF(x,y)); + } + + rect.moveCenter(center); + return rect; +} + /** * @brief QetGraphicsHandlerUtility::lineForPosAtIndex * Return a line after modification of @pos at index @index of @old_line. diff --git a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h index 27a254910..a74db520b 100644 --- a/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h +++ b/sources/QetGraphicsItemModeler/qetgraphicshandlerutility.h @@ -20,6 +20,7 @@ #include #include +#include class QPainter; @@ -34,22 +35,27 @@ 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 QVector &points, bool color2 = false); + void drawHandler (QPainter *painter, const QPointF & point); + void drawHandler(QPainter *painter, const QVector &points); 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; QVector handlerRect (const QVector &vector) const; + void setInnerColor (QColor color); + void setOuterColor (QColor color); private: QRectF getRect (const QPointF &point) const; qreal m_size; - qreal m_zoom_factor; + qreal m_zoom_factor = 1; + QColor m_inner_color = Qt::white, + m_outer_color = Qt::blue; public: static QVector pointsForRect (const QRectF &rect); static QVector pointsForLine (const QLineF &line); static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index); + static QRectF mirrorRectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index); static QLineF lineForPosAtIndex (const QLineF &old_line, const QPointF &pos, int index); }; diff --git a/sources/editor/elementprimitivedecorator.cpp b/sources/editor/elementprimitivedecorator.cpp index d32c8d568..0b9df4076 100644 --- a/sources/editor/elementprimitivedecorator.cpp +++ b/sources/editor/elementprimitivedecorator.cpp @@ -21,11 +21,9 @@ #include "editorcommands.h" #include "qet.h" #include -#include #include #include #include -#include /** Constructor @@ -36,6 +34,7 @@ ElementPrimitiveDecorator::ElementPrimitiveDecorator(QGraphicsItem *parent): m_handler(10) { init(); + m_handler.setOuterColor(Qt::darkGreen); } /** @@ -97,8 +96,9 @@ void ElementPrimitiveDecorator::paint(QPainter *painter, const QStyleOptionGraph pen.setCosmetic(true); painter -> setPen(pen); painter -> drawRect(modified_bounding_rect_); + //Draw the handlers - m_handler.drawHandler(painter, getResizingsPoints(), decorated_items_.size()-1); + m_handler.drawHandler(painter, getResizingsPoints()); // uncomment to draw the real bouding rect (=adjusted internal bounding rect) // painter -> setBrush(QBrush(QColor(240, 0, 0, 127))); diff --git a/sources/editor/graphicspart/partarc.cpp b/sources/editor/graphicspart/partarc.cpp index acc8a6a5b..213bf5dde 100644 --- a/sources/editor/graphicspart/partarc.cpp +++ b/sources/editor/graphicspart/partarc.cpp @@ -232,7 +232,11 @@ void PartArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos())); prepareGeometryChange(); - setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + + if (m_resize_mode == 1) + setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + else + setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index)); } else CustomElementGraphicPart::mouseMoveEvent(event); @@ -245,8 +249,11 @@ void PartArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) */ void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (event->button() == Qt::LeftButton) + if (event->button() == Qt::LeftButton) { setCursor(Qt::OpenHandCursor); + if (event->buttonDownPos(Qt::LeftButton) == event->pos()) + switchResizeMode(); + } if (m_handler_index >= 0 && m_handler_index <= 7) { @@ -261,3 +268,16 @@ void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) else CustomElementGraphicPart::mouseReleaseEvent(event); } + +void PartArc::switchResizeMode() +{ + if (m_resize_mode == 1) { + m_resize_mode = 2; + m_handler.setOuterColor(Qt::darkGreen); + } + else { + m_resize_mode = 1; + m_handler.setOuterColor(Qt::blue); + } + update(); +} diff --git a/sources/editor/graphicspart/partarc.h b/sources/editor/graphicspart/partarc.h index e139626ba..dce6d4acc 100644 --- a/sources/editor/graphicspart/partarc.h +++ b/sources/editor/graphicspart/partarc.h @@ -64,9 +64,13 @@ class PartArc : public AbstractPartEllipse virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + private: + void switchResizeMode(); + private: QetGraphicsHandlerUtility m_handler; int m_handler_index; QPropertyUndoCommand *m_undo_command; + int m_resize_mode = 1; }; #endif diff --git a/sources/editor/graphicspart/partellipse.cpp b/sources/editor/graphicspart/partellipse.cpp index 73b80b309..11cc78ab3 100644 --- a/sources/editor/graphicspart/partellipse.cpp +++ b/sources/editor/graphicspart/partellipse.cpp @@ -227,7 +227,11 @@ void PartEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos())); prepareGeometryChange(); - setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + + if (m_resize_mode == 1) + setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + else + setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index)); } else CustomElementGraphicPart::mouseMoveEvent(event); @@ -240,8 +244,11 @@ void PartEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event) */ void PartEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (event->button() == Qt::LeftButton) + if (event->button() == Qt::LeftButton) { setCursor(Qt::OpenHandCursor); + if (event->buttonDownPos(Qt::LeftButton) == event->pos()) + switchResizeMode(); + } if (m_handler_index >= 0 && m_handler_index <= 7) { @@ -256,3 +263,16 @@ void PartEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) else CustomElementGraphicPart::mouseReleaseEvent(event); } + +void PartEllipse::switchResizeMode() +{ + if (m_resize_mode == 1) { + m_resize_mode = 2; + m_handler.setOuterColor(Qt::darkGreen); + } + else { + m_resize_mode = 1; + m_handler.setOuterColor(Qt::blue); + } + update(); +} diff --git a/sources/editor/graphicspart/partellipse.h b/sources/editor/graphicspart/partellipse.h index ff98584fc..422f5cc91 100644 --- a/sources/editor/graphicspart/partellipse.h +++ b/sources/editor/graphicspart/partellipse.h @@ -66,9 +66,13 @@ class PartEllipse : public AbstractPartEllipse virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + private: + void switchResizeMode(); + private: QetGraphicsHandlerUtility m_handler; int m_handler_index; QPropertyUndoCommand *m_undo_command; + int m_resize_mode = 1; }; #endif diff --git a/sources/editor/graphicspart/partrectangle.cpp b/sources/editor/graphicspart/partrectangle.cpp index 03b16916d..f3a5de034 100644 --- a/sources/editor/graphicspart/partrectangle.cpp +++ b/sources/editor/graphicspart/partrectangle.cpp @@ -298,7 +298,11 @@ void PartRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos())); prepareGeometryChange(); - setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + + if (m_resize_mode == 1) + setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index)); + else + setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index)); } else CustomElementGraphicPart::mouseMoveEvent(event); @@ -311,8 +315,11 @@ void PartRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event) */ void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { - if (event->button() == Qt::LeftButton) + if (event->button() == Qt::LeftButton) { setCursor(Qt::OpenHandCursor); + if (event->buttonDownPos(Qt::LeftButton) == event->pos()) + switchResizeMode(); + } if (m_handler_index >= 0 && m_handler_index <= 7) { @@ -327,3 +334,16 @@ void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) else CustomElementGraphicPart::mouseReleaseEvent(event); } + +void PartRectangle::switchResizeMode() +{ + if (m_resize_mode == 1) { + m_resize_mode = 2; + m_handler.setOuterColor(Qt::darkGreen); + } + else { + m_resize_mode = 1; + m_handler.setOuterColor(Qt::blue); + } + update(); +} diff --git a/sources/editor/graphicspart/partrectangle.h b/sources/editor/graphicspart/partrectangle.h index 6cd072e67..69831898f 100644 --- a/sources/editor/graphicspart/partrectangle.h +++ b/sources/editor/graphicspart/partrectangle.h @@ -79,6 +79,9 @@ class PartRectangle : public CustomElementGraphicPart virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + + private: + void switchResizeMode(); private: QRectF m_rect; @@ -86,5 +89,6 @@ class PartRectangle : public CustomElementGraphicPart QetGraphicsHandlerUtility m_handler; int m_handler_index; QPropertyUndoCommand *m_undo_command; + int m_resize_mode = 1; }; #endif diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 9de6436d9..6d578d38a 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -435,8 +435,24 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos; break; - case Rectangle: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break; - case Ellipse: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break; + case Rectangle: + if (m_resize_mode == 1) { + setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); + break; + } + else { + setRect(m_handler.mirrorRectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); + break; + } + case Ellipse: + if (m_resize_mode == 1) { + setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); + break; + } + else { + setRect(m_handler.mirrorRectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); + break; + } case Polygon: prepareGeometryChange(); @@ -456,6 +472,9 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) */ void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { + if ((m_shapeType & (Rectangle | Ellipse)) && event->buttonDownPos(Qt::LeftButton) == event->pos()) + switchResizeMode(); + if (m_mouse_grab_handler) { m_mouse_grab_handler = false; @@ -488,6 +507,19 @@ void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) QetGraphicsItem::mouseReleaseEvent(event); } +void QetShapeItem::switchResizeMode() +{ + if (m_resize_mode == 1) { + m_resize_mode = 2; + m_handler.setOuterColor(Qt::darkGreen); + } + else { + m_resize_mode = 1; + m_handler.setOuterColor(Qt::blue); + } + update(); +} + /** * @brief QetShapeItem::fromXml * Build this item from the xml description diff --git a/sources/qetgraphicsitem/qetshapeitem.h b/sources/qetgraphicsitem/qetshapeitem.h index c323d3a8c..672d2c9a2 100644 --- a/sources/qetgraphicsitem/qetshapeitem.h +++ b/sources/qetgraphicsitem/qetshapeitem.h @@ -102,6 +102,9 @@ class QetShapeItem : public QetGraphicsItem virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *event); virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *event); + private: + void switchResizeMode(); + ///ATTRIBUTES private: ShapeType m_shapeType; @@ -114,5 +117,6 @@ class QetShapeItem : public QetGraphicsItem int m_vector_index; QetGraphicsHandlerUtility m_handler; bool m_close = false; + int m_resize_mode = 1; }; #endif // QETSHAPEITEM_H