From 5204ffe9abd08615312cd0404a9ef137b3d5cba8 Mon Sep 17 00:00:00 2001 From: xavier Date: Sun, 29 Nov 2009 21:56:48 +0000 Subject: [PATCH] Il est desormais possible de pivoter les textes independants et les textes des conducteurs. git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@798 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/conductor.cpp | 4 ++++ sources/diagram.cpp | 4 +++- sources/diagramcommands.cpp | 13 ++++++++--- sources/diagramcommands.h | 4 +++- sources/diagramtextitem.cpp | 46 +++++++++++++++++++++++++++++++++++-- sources/diagramtextitem.h | 8 +++++++ sources/diagramview.cpp | 15 +++++++++--- 7 files changed, 84 insertions(+), 10 deletions(-) diff --git a/sources/conductor.cpp b/sources/conductor.cpp index 7f484ef8a..c61594cf3 100644 --- a/sources/conductor.cpp +++ b/sources/conductor.cpp @@ -874,6 +874,7 @@ bool Conductor::fromXml(QDomElement &e) { // recupere la "configuration" du conducteur properties_.fromXml(e); readProperties(); + text_item -> setRotationAngle(e.attribute("rotation").toDouble()); // parcourt les elements XML "segment" et en extrait deux listes de longueurs // les segments non valides sont ignores @@ -968,6 +969,9 @@ QDomElement Conductor::toXml(QDomDocument &d, QHash &table_adr_ // exporte la "configuration" du conducteur properties_.toXml(e); + if (text_item -> rotationAngle()) { + e.setAttribute("rotation", QString("%1").arg(text_item -> rotationAngle())); + } return(e); } diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 9804a708c..1c8a758e5 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -1070,7 +1070,9 @@ DiagramContent Diagram::selectedContent() { */ bool Diagram::canRotateSelection() const { foreach(QGraphicsItem * qgi, selectedItems()) { - if (Element *e = qgraphicsitem_cast(qgi)) { + if (/*DiagramTextItem *dti = */qgraphicsitem_cast(qgi)) { + return(true); + } else if (Element *e = qgraphicsitem_cast(qgi)) { // l'element est-il pivotable ? if (e -> orientation().current() != e -> orientation().next()) { return(true); diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index bd40d7d3b..09aa0e82f 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -407,9 +407,10 @@ void ChangeDiagramTextCommand::redo() { @param elements Elements a pivoter associes a leur orientation d'origine @param parent QUndoCommand parent */ -RotateElementsCommand::RotateElementsCommand(const QHash &elements, QUndoCommand *parent) : +RotateElementsCommand::RotateElementsCommand(const QHash &elements, const QList &texts, QUndoCommand *parent) : QUndoCommand(parent), - elements_to_rotate(elements) + elements_to_rotate(elements), + texts_to_rotate(texts) { setText( QString( @@ -417,7 +418,7 @@ RotateElementsCommand::RotateElementsCommand(const QHash setOrientation(elements_to_rotate[e]); } + foreach(DiagramTextItem *dti, texts_to_rotate) { + dti -> rotateBy(90.0); + } } /// refait le pivotement @@ -438,6 +442,9 @@ void RotateElementsCommand::redo() { e -> setOrientation(e -> orientation().next()); e -> update(); } + foreach(DiagramTextItem *dti, texts_to_rotate) { + dti -> rotateBy(-90.0); + } } /** diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 64f971fc3..c90ad388a 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -232,7 +232,7 @@ class ChangeDiagramTextCommand : public QUndoCommand { class RotateElementsCommand : public QUndoCommand { // constructeurs, destructeur public: - RotateElementsCommand(const QHash &elements, QUndoCommand * = 0); + RotateElementsCommand(const QHash &elements, const QList &, QUndoCommand * = 0); virtual ~RotateElementsCommand(); private: RotateElementsCommand(const RotateElementsCommand &); @@ -246,6 +246,8 @@ class RotateElementsCommand : public QUndoCommand { private: /// elements pivotes associes a leur ancienne orientation QHash elements_to_rotate; + /// textes a pivoter + QList texts_to_rotate; }; /** diff --git a/sources/diagramtextitem.cpp b/sources/diagramtextitem.cpp index 411a1cb66..49896063f 100644 --- a/sources/diagramtextitem.cpp +++ b/sources/diagramtextitem.cpp @@ -25,7 +25,8 @@ @param scene La scene a laquelle appartient le champ de texte */ DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : - QGraphicsTextItem(parent, scene) + QGraphicsTextItem(parent, scene), + rotation_angle_(0.0) { setDefaultTextColor(Qt::black); setFont(QETApp::diagramTextsFont()); @@ -41,7 +42,8 @@ DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : */ DiagramTextItem::DiagramTextItem(const QString &text, QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(text, parent, scene), - previous_text(text) + previous_text(text), + rotation_angle_(0.0) { setDefaultTextColor(Qt::black); setFont(QETApp::diagramTextsFont()); @@ -58,6 +60,42 @@ Diagram *DiagramTextItem::diagram() const { return(qobject_cast(scene())); } +/** + @return l'angle de rotation actuel de ce texte +*/ +qreal DiagramTextItem::rotationAngle() const { + return(rotation_angle_); +} + +/** + Permet de tourner le texte a un angle donne. La rotation s'effectue par + rapport au point (0, 0) du texte. + @param rotation Nouvel angle de rotation de ce texte +*/ +void DiagramTextItem::setRotationAngle(const qreal &rotation) { + // ramene l'angle demande entre -360.0 et +360.0 degres + qreal applied_rotation = rotation; + while (applied_rotation < -360.0) applied_rotation += 360.0; + while (applied_rotation > 360.0) applied_rotation -= 360.0; + + rotate(applied_rotation - rotation_angle_); + rotation_angle_ = applied_rotation; +} + +/** + Ajoute + @param added_rotation Angle a ajouter a la rotation actuelle +*/ +void DiagramTextItem::rotateBy(const qreal &added_rotation) { + // ramene l'angle demande entre -360.0 et +360.0 degres + qreal applied_added_rotation = added_rotation; + while (applied_added_rotation < 360.0) applied_added_rotation += 360.0; + while (applied_added_rotation > 360.0) applied_added_rotation -= 360.0; + + rotation_angle_ += applied_added_rotation; + rotate(applied_added_rotation); +} + /** gere la perte de focus du champ de texte */ @@ -91,6 +129,7 @@ void DiagramTextItem::fromXml(const QDomElement &e) { setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble()); setPlainText(e.attribute("text")); previous_text = e.attribute("text"); + setRotationAngle(e.attribute("rotation").toDouble()); } /** @@ -102,6 +141,9 @@ QDomElement DiagramTextItem::toXml(QDomDocument &document) const { result.setAttribute("x", QString("%1").arg(pos().x())); result.setAttribute("y", QString("%1").arg(pos().y())); result.setAttribute("text", toPlainText()); + if (rotation_angle_) { + result.setAttribute("rotation", QString("%1").arg(rotation_angle_)); + } return(result); } diff --git a/sources/diagramtextitem.h b/sources/diagramtextitem.h index ac6c18b16..8e253766f 100644 --- a/sources/diagramtextitem.h +++ b/sources/diagramtextitem.h @@ -48,6 +48,9 @@ class DiagramTextItem : public QGraphicsTextItem { virtual QDomElement toXml(QDomDocument &) const; virtual void setPos(const QPointF &); virtual void setPos(qreal, qreal); + qreal rotationAngle() const; + void setRotationAngle(const qreal &); + void rotateBy(const qreal &); protected: virtual void focusOutEvent(QFocusEvent *); @@ -66,5 +69,10 @@ class DiagramTextItem : public QGraphicsTextItem { // slots public slots: void setNonFocusable(); + + // attributs prives + private: + /// angle de rotation du champ de texte + qreal rotation_angle_; }; #endif diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index f12a02de8..588214810 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -23,6 +23,7 @@ #include "diagramcommands.h" #include "diagramposition.h" #include "conductorpropertieswidget.h" +#include "elementtextitem.h" #include "insetpropertieswidget.h" #include "qetapp.h" #include "qetproject.h" @@ -114,14 +115,22 @@ void DiagramView::deleteSelection() { */ void DiagramView::rotateSelection() { if (scene -> isReadOnly()) return; + + // recupere les elements et les champs de texte a pivoter QHash elements_to_rotate; + QList texts_to_rotate; foreach (QGraphicsItem *item, scene -> selectedItems()) { if (Element *e = qgraphicsitem_cast(item)) { elements_to_rotate.insert(e, e -> orientation().current()); - } + } else if (DiagramTextItem *dti = qgraphicsitem_cast(item)) { + texts_to_rotate << dti; + } /*else if (ElementTextItem *eti = qgraphicsitem_cast(item)) { + }*/ } - if (elements_to_rotate.isEmpty()) return; - scene -> undoStack().push(new RotateElementsCommand(elements_to_rotate)); + + // effectue les rotations s'il y a quelque chose a pivoter + if (elements_to_rotate.isEmpty() && texts_to_rotate.isEmpty()) return; + scene -> undoStack().push(new RotateElementsCommand(elements_to_rotate, texts_to_rotate)); } /**