diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 5517d8101..f5a8288d0 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -598,7 +598,7 @@ RotateElementsCommand::~RotateElementsCommand() { /// defait le pivotement void RotateElementsCommand::undo() { foreach(Element *e, elements_to_rotate.keys()) { - e -> setOrientation(elements_to_rotate[e]); + rotateElement(e, elements_to_rotate[e]); } foreach(DiagramTextItem *dti, texts_to_rotate) { dti -> rotateBy(-applied_rotation_angle_); @@ -608,8 +608,7 @@ void RotateElementsCommand::undo() { /// refait le pivotement void RotateElementsCommand::redo() { foreach(Element *e, elements_to_rotate.keys()) { - e -> setOrientation(e -> orientation().next()); - e -> update(); + rotateElement(e, e -> orientation().next()); } foreach(DiagramTextItem *dti, texts_to_rotate) { dti -> rotateBy(applied_rotation_angle_); @@ -630,6 +629,36 @@ void RotateElementsCommand::setAppliedRotationAngle(const qreal &angle) { applied_rotation_angle_ = QET::correctAngle(angle); } +/** + Passe un element a une orientation donnee, en prenant soin de gerer ses textes enfants + @param element Element a orienter soigneusement + @param orientation Nouvelle orientation de l'element +*/ +void RotateElementsCommand::rotateElement(Element *element, QET::Orientation orientation) { + qreal rotation_value = 90.0 * (orientation - element -> orientation().current()); + element -> setOrientation(orientation); + element -> update(); + if (rotation_value) { + // repositionne les textes de l'element qui ne comportent pas l'option "FollowParentRotations" + foreach(ElementTextItem *eti, element -> texts()) { + if (!eti -> followParentRotations()) { + // on souhaite pivoter le champ de texte par rapport a son centre + QPointF eti_center = eti -> boundingRect().center(); + // pour ce faire, on repere la position de son centre par rapport a son parent + QPointF parent_eti_center_before = eti -> mapToParent(eti_center); + // on applique ensuite une simple rotation contraire, qui sera donc appliquee sur le milieu du cote gauche du champ de texte + eti -> rotateBy(-rotation_value); + // on regarde ensuite la nouvelle position du centre du champ de texte par rapport a son parent + QPointF parent_eti_center_after = eti -> mapToParent(eti_center); + // on determine la translation a appliquer + QPointF eti_translation = parent_eti_center_before - parent_eti_center_after; + // on applique cette translation + eti -> setPos(eti -> pos() + eti_translation); + } + } + } +} + /** Constructeur @param previous_state Hash associant les textes impactes par l'action et leur angle de rotation avant l'action diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 44c87b883..ab76117bf 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -319,6 +319,8 @@ class RotateElementsCommand : public QUndoCommand { virtual void redo(); qreal appliedRotationAngle() const; void setAppliedRotationAngle(const qreal &); + private: + void rotateElement(Element *, QET::Orientation); // attributs private: diff --git a/sources/diagramtextitem.cpp b/sources/diagramtextitem.cpp index 23f4d1456..2355c435b 100644 --- a/sources/diagramtextitem.cpp +++ b/sources/diagramtextitem.cpp @@ -249,14 +249,14 @@ void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { } /** - Effetue la rotation du texte en elle-meme + Effectue la rotation du texte en elle-meme Pour les DiagramTextItem, la rotation s'effectue autour du point (0, 0). - Cette methode peut toutefois etre redefinie dans des classes + Cette methode peut toutefois etre redefinie dans des classes filles @param angle Angle de la rotation a effectuer */ void DiagramTextItem::applyRotation(const qreal &angle) { - // un simple appel a QGraphicsTextItem::rotate suffit - QGraphicsTextItem::rotate(angle); + // un simple appel a QGraphicsTextItem::setRotation suffit + QGraphicsTextItem::setRotation(QGraphicsTextItem::rotation() + angle); } /** diff --git a/sources/element.cpp b/sources/element.cpp index e909f297a..242ef6ae6 100644 --- a/sources/element.cpp +++ b/sources/element.cpp @@ -185,19 +185,7 @@ bool Element::setOrientation(QET::Orientation o) { foreach(QGraphicsItem *qgi, childItems()) { if (Terminal *p = qgraphicsitem_cast(qgi)) { p -> updateConductor(); - } /* else if (ElementTextItem *eti = qgraphicsitem_cast(qgi)) { - // applique une rotation contraire si besoin - if (!eti -> followParentRotations()) { - QMatrix new_matrix = eti -> matrix(); - qreal dx = eti -> boundingRect().width() / 2.0; - qreal dy = eti -> boundingRect().height() / 2.0; - new_matrix.translate(dx, dy); - new_matrix.rotate(-rotation_value); - new_matrix.translate(-dx, -dy); - eti -> setMatrix(new_matrix); - } } - */ } return(true); } diff --git a/sources/elementtextitem.cpp b/sources/elementtextitem.cpp index b56167db4..ef884eccb 100644 --- a/sources/elementtextitem.cpp +++ b/sources/elementtextitem.cpp @@ -35,6 +35,7 @@ ElementTextItem::ElementTextItem(Element *parent_element, Diagram *parent_diagra // par defaut, les DiagramTextItem sont Selectable et Movable // cela nous convient, on ne touche pas a ces flags + adjustItemPosition(1); // ajuste la position du QGraphicsItem lorsque le QTextDocument change connect(document(), SIGNAL(blockCountChanged(int)), this, SLOT(adjustItemPosition(int))); } @@ -55,6 +56,7 @@ ElementTextItem::ElementTextItem(const QString &text, Element *parent_element, D // par defaut, les DiagramTextItem sont Selectable et Movable // cela nous convient, on ne touche pas a ces flags + adjustItemPosition(1); // ajuste la position du QGraphicsItem lorsque le QTextDocument change connect(document(), SIGNAL(blockCountChanged(int)), this, SLOT(adjustItemPosition(int))); } @@ -75,16 +77,7 @@ Element *ElementTextItem::parentElement() const { @param pos La nouvelle position du champ de texte */ void ElementTextItem::setPos(const QPointF &pos) { - // annule toute transformation (rotation notamment) - resetTransform(); - - // effectue le positionnement en lui-meme - QPointF actual_pos = pos; - actual_pos -= QPointF(0.0, boundingRect().bottom() / 2.0); - QGraphicsTextItem::setPos(actual_pos); - - // applique a nouveau la rotation du champ de texte - applyRotation(rotationAngle()); + QGraphicsTextItem::setPos(pos); } /** @@ -100,9 +93,7 @@ void ElementTextItem::setPos(qreal x, qreal y) { @return La position (bidouillee) du champ de texte */ QPointF ElementTextItem::pos() const { - QPointF actual_pos = DiagramTextItem::pos(); - actual_pos += QPointF(0.0, boundingRect().bottom() / 2.0); - return(actual_pos); + return(QGraphicsTextItem::pos()); } /** @@ -198,7 +189,12 @@ qreal ElementTextItem::originalRotationAngle() const { */ void ElementTextItem::adjustItemPosition(int new_block_count) { Q_UNUSED(new_block_count); - setPos(known_position_); + qreal origin_offset = boundingRect().bottom() / 2.0; + + QTransform base_translation; + base_translation.translate(0.0, -origin_offset); + setTransform(base_translation, false); + setTransformOriginPoint(0.0, origin_offset); } /** @@ -208,14 +204,7 @@ void ElementTextItem::adjustItemPosition(int new_block_count) { @param angle Angle de la rotation a effectuer */ void ElementTextItem::applyRotation(const qreal &angle) { - qreal origin_offset = boundingRect().bottom() / 2.0; - - QTransform rotation; - rotation.translate(0.0, origin_offset); - rotation.rotate(angle); - rotation.translate(0.0, -origin_offset); - - QGraphicsTextItem::setTransform(rotation, true); + QGraphicsTextItem::setRotation(QGraphicsTextItem::rotation() + angle); } /** @@ -309,17 +298,3 @@ void ElementTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { QGraphicsTextItem::mouseReleaseEvent(e); } } - -/** - Gere les changements intervenant sur ce champ de texte - @param change Type de changement - @param value Valeur numerique relative au changement -*/ -QVariant ElementTextItem::itemChange(GraphicsItemChange change, const QVariant &value) { - if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged) { - // memorise la nouvelle position "officielle" du champ de texte - // cette information servira a le recentrer en cas d'ajout / retrait de lignes - known_position_ = pos(); - } - return(DiagramTextItem::itemChange(change, value)); -} diff --git a/sources/elementtextitem.h b/sources/elementtextitem.h index 6ee7f8bd7..67bda0afd 100644 --- a/sources/elementtextitem.h +++ b/sources/elementtextitem.h @@ -45,7 +45,6 @@ class ElementTextItem : public DiagramTextItem { Element *parent_element_; bool follow_parent_rotations; QPointF original_position; - QPointF known_position_; qreal original_rotation_angle_; bool first_move_; @@ -75,7 +74,6 @@ class ElementTextItem : public DiagramTextItem { virtual void mousePressEvent(QGraphicsSceneMouseEvent *); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *); - virtual QVariant itemChange(GraphicsItemChange, const QVariant &); }; /**