diff --git a/sources/editor/elementscene.cpp b/sources/editor/elementscene.cpp index e6f36061e..858ab9b56 100644 --- a/sources/editor/elementscene.cpp +++ b/sources/editor/elementscene.cpp @@ -385,9 +385,17 @@ void ElementScene::setGrid(int x_g, int y_g) { representer tout l'element ou seulement les elements selectionnes @return un document XML decrivant l'element */ -const QDomDocument ElementScene::toXml(bool all_parts) const { - //define the size of the element by the upper multiple of 10 +const QDomDocument ElementScene::toXml(bool all_parts) { QRectF size= elementSceneGeometricRect(); + //if the element doesn't contains the origin point of the scene + //we move the element to the origin for solve this default before saving + if (!size.contains(0,0) && all_parts) { + centerElementToOrigine(); + //recalcul the size after movement + size= elementSceneGeometricRect(); + } + + //define the size of the element by the upper multiple of 10 int upwidth = ((qRound(size.width())/10)*10)+10; if ((qRound(size.width())%10) > 6) upwidth+=10; @@ -1148,6 +1156,33 @@ bool ElementScene::zValueLessThan(QGraphicsItem *item1, QGraphicsItem *item2) { return(item1-> zValue() < item2 -> zValue()); } +/** + * @brief ElementScene::centerElementToOrigine + * try to center better is possible the element to the scene + * (the calcul isn't optimal but work good) + */ +void ElementScene::centerElementToOrigine() { + QRectF size= elementSceneGeometricRect(); + int center_x = qRound(size.center().x()); + int center_y = qRound(size.center().y()); + + //define the movement of translation + int move_x = center_x - (qRound(center_x) %10); + if (center_x < 0) move_x -= 10; + int move_y = center_y - (qRound(center_y) %10); + if (center_y < 0) move_y -= 10; + + //move each primitive by @move + foreach (QGraphicsItem *qgi, items()) { + if (qgi -> type() == ElementPrimitiveDecorator::Type) continue; + if (qgi -> type() == QGraphicsRectItem::Type) continue; + //deselect item for disable decorator + qgi -> setSelected(false); + qgi -> moveBy(-(move_x), -(move_y)); + } + emit (needZoomFit()); +} + /** Ensure the decorator is adequately shown, hidden or updated so it always represents the current selection. diff --git a/sources/editor/elementscene.h b/sources/editor/elementscene.h index 912c33ec1..23e7255d4 100644 --- a/sources/editor/elementscene.h +++ b/sources/editor/elementscene.h @@ -121,7 +121,7 @@ class ElementScene : public QGraphicsScene { virtual int xGrid() const; virtual int yGrid() const; virtual void setGrid(int, int); - virtual const QDomDocument toXml(bool = true) const; + virtual const QDomDocument toXml(bool = true); virtual QRectF boundingRectFromXml(const QDomDocument &); virtual void fromXml(const QDomDocument &, const QPointF & = QPointF(), bool = true, ElementContent * = 0); virtual void reset(); @@ -159,6 +159,7 @@ class ElementScene : public QGraphicsScene { bool mustSnapToGrid(QGraphicsSceneMouseEvent *); static bool zValueLessThan(QGraphicsItem *, QGraphicsItem *); QMutex *decorator_lock_; + void centerElementToOrigine(); public slots: void slot_move(); @@ -200,6 +201,8 @@ class ElementScene : public QGraphicsScene { void partsZValueChanged(); /// Signal emitted when users have defined the copy/paste area void pasteAreaDefined(const QRectF &); + /// Signal emitted when need zoomFit + void needZoomFit(); }; Q_DECLARE_OPERATORS_FOR_FLAGS(ElementScene::ItemOptions) diff --git a/sources/editor/elementview.cpp b/sources/editor/elementview.cpp index 3b8dad980..e68cd3c89 100644 --- a/sources/editor/elementview.cpp +++ b/sources/editor/elementview.cpp @@ -36,6 +36,7 @@ ElementView::ElementView(ElementScene *scene, QWidget *parent) : zoomReset(); connect(scene_, SIGNAL(pasteAreaDefined(const QRectF &)), this, SLOT(pasteAreaDefined(const QRectF &))); connect(scene_, SIGNAL(partsAdded()), this, SLOT(adjustSceneRect())); + connect(scene_, SIGNAL(needZoomFit()), this, SLOT(zoomFit())); } /// Destructeur