diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index e1d610603..a6d0202a9 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -33,6 +33,7 @@ #include "../undocommand/promoteshapecommand.h" #include +#include #include #include #include @@ -613,6 +614,34 @@ QRectF QetShapeItem::sceneOutlineRect() const return mapToScene(outline()).boundingRect(); } +/** + @brief QetShapeItem::setPos + Called with the unsnapped position while the shape is dragged. Dragged + on its own, or with other shapes only, the shape goes on the grid by the + top-left corner of what is drawn, as Snap to grid does: its pos() cannot + be seen, and it is off the corner whenever the shape was drawn or + resized with Ctrl held, or rotated. Dragged together with anything + else, it snaps by pos() as before: the whole selection follows this + shape's movement, and correcting the corner would take the symbols off + the grid instead. + @param p the new position of the item +*/ +void QetShapeItem::setPos(const QPointF &p) +{ + const auto selection = scene() ? scene()->selectedItems() : QList(); + const bool shapes_only = std::all_of(selection.cbegin(), selection.cend(), + [](const QGraphicsItem *item) { return item->type() == QetShapeItem::Type; }); + if (!shapes_only || !isMovable()) { + QetGraphicsItem::setPos(p); + return; + } + + const QPointF corner = sceneOutlineRect().topLeft() + (p - pos()); + const QPointF snapped = p + Diagram::snapToGrid(corner) - corner; + if (snapped != pos()) + QGraphicsItem::setPos(snapped); +} + /** @brief QetShapeItem::shape @return the shape of this item diff --git a/sources/qetgraphicsitem/qetshapeitem.h b/sources/qetgraphicsitem/qetshapeitem.h index cf1c823fd..610f06bf5 100644 --- a/sources/qetgraphicsitem/qetshapeitem.h +++ b/sources/qetgraphicsitem/qetshapeitem.h @@ -221,6 +221,9 @@ class QetShapeItem : public QetGraphicsItem QPainterPath shape() const override; QRectF sceneOutlineRect() const; + using QetGraphicsItem::setPos; + void setPos(const QPointF &p) override; + protected: void paint( QPainter *painter,