diff --git a/conducer.cpp b/conducer.cpp index db2a7f97f..b065ea7a4 100644 --- a/conducer.cpp +++ b/conducer.cpp @@ -4,6 +4,7 @@ #include "conducersegmentprofile.h" #include "element.h" #include "diagram.h" +#include "diagramcommands.h" #define PR(x) qDebug() << #x " = " << x; bool Conducer::pen_and_brush_initialized = false; @@ -938,7 +939,11 @@ void Conducer::calculateTextItemPosition() { dans priv_modifieConducer. */ void Conducer::saveProfile() { + ConducerProfile old_profile = conducer_profile; conducer_profile.fromConducer(this); + if (Diagram *dia = diagram()) { + dia -> undoStack().push(new ChangeConducerCommand(this, old_profile, conducer_profile)); + } } /** @@ -957,3 +962,19 @@ int Conducer::getCoeff(const qreal &value1, const qreal &value2) { int Conducer::getSign(const qreal &value) { return(value < 0 ? -1 : 1); } + +/** + Applique un nouveau profil a ce conducteur + @param cp Profil a appliquer a ce conducteur +*/ +void Conducer::setProfile(const ConducerProfile &cp) { + conducer_profile = cp; + if (conducer_profile.isNull()) { + priv_calculeConducer(terminal1 -> amarrageConducer(), terminal1 -> orientation(), terminal2 -> amarrageConducer(), terminal2 -> orientation()); + modified_path = false; + } else { + priv_modifieConducer(terminal1 -> amarrageConducer(), terminal1 -> orientation(), terminal2 -> amarrageConducer(), terminal2 -> orientation()); + modified_path = true; + } + calculateTextItemPosition(); +} diff --git a/conducer.h b/conducer.h index bf302dbf9..2051ac248 100644 --- a/conducer.h +++ b/conducer.h @@ -63,6 +63,7 @@ class Conducer : public QGraphicsPathItem { bool fromXml(QDomElement &); QDomElement toXml(QDomDocument &, QHash &) const; const QList segmentsList() const; + void setProfile(const ConducerProfile &); protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent *); diff --git a/conducerprofile.cpp b/conducerprofile.cpp index c24162fb2..9cd4fffab 100644 --- a/conducerprofile.cpp +++ b/conducerprofile.cpp @@ -14,11 +14,40 @@ ConducerProfile::ConducerProfile(Conducer *conducer) { fromConducer(conducer); } +/** + Constructeur de copie + @param c autre conducteur +*/ +ConducerProfile::ConducerProfile(const ConducerProfile &c) { + beginOrientation = c.beginOrientation; + endOrientation = c.endOrientation; + foreach(ConducerSegmentProfile *csp, c.segments) { + segments << new ConducerSegmentProfile(*csp); + } +} + +/** + Operateur = + @param c autre conducteur +*/ +ConducerProfile &ConducerProfile::operator=(const ConducerProfile &c) { + if (&c == this) return(*this); + + // supprime ses informations + setNull(); + + // copie les informations de l'autre profil de conducteur + beginOrientation = c.beginOrientation; + endOrientation = c.endOrientation; + foreach(ConducerSegmentProfile *csp, c.segments) { + segments << new ConducerSegmentProfile(*csp); + } + return(*this); +} + /// destructeur ConducerProfile::~ConducerProfile() { - foreach(ConducerSegmentProfile * s, segments) { - delete s; - } + setNull(); } /// @return true si le profil est nul @@ -26,6 +55,12 @@ bool ConducerProfile::isNull() const { return(segments.isEmpty()); } +/// supprime les segments du profil de conducteur +void ConducerProfile::setNull() { + foreach(ConducerSegmentProfile *csp, segments) delete csp; + segments.clear(); +} + /// @return la largeur occupee par le conducteur qreal ConducerProfile::width() const { qreal width = 0.0; @@ -78,8 +113,7 @@ QList ConducerProfile::verticalSegments() { void ConducerProfile::fromConducer(Conducer *conducer) { // supprime les segments precedents - foreach(ConducerSegmentProfile *csp, segments) delete csp; - segments.clear(); + setNull(); foreach(ConducerSegment *conducer_segment, conducer -> segmentsList()) { segments << new ConducerSegmentProfile(conducer_segment); diff --git a/conducerprofile.h b/conducerprofile.h index 0cd9a3deb..e305a2f39 100644 --- a/conducerprofile.h +++ b/conducerprofile.h @@ -13,6 +13,8 @@ class ConducerProfile { // constructeurs, destructeur ConducerProfile(); ConducerProfile(Conducer *conducer); + ConducerProfile(const ConducerProfile &); + ConducerProfile &operator=(const ConducerProfile &); virtual ~ConducerProfile(); // attributs @@ -24,6 +26,7 @@ class ConducerProfile { // methodes public: bool isNull() const; + void setNull(); qreal width() const; qreal height() const; uint nbSegments(QET::ConducerSegmentType) const; diff --git a/diagramcommands.cpp b/diagramcommands.cpp index 575fea33d..1efb3d09d 100644 --- a/diagramcommands.cpp +++ b/diagramcommands.cpp @@ -244,16 +244,10 @@ MoveElementsCommand::MoveElementsCommand( movement(m) { setText(QObject::tr("d\351placer ") + QET::ElementsAndConducersSentence(elements_to_move.count(), conducers_to_move.count())); - foreach(QGraphicsItem *qgi, elements_to_move) diagram -> qgiManager().manage(qgi); - foreach(QGraphicsItem *qgi, conducers_to_move) diagram -> qgiManager().manage(qgi); - foreach(QGraphicsItem *qgi, conducers_to_update) diagram -> qgiManager().manage(qgi); } /// Destructeur MoveElementsCommand::~MoveElementsCommand() { - foreach(QGraphicsItem *qgi, elements_to_move) diagram -> qgiManager().release(qgi); - foreach(QGraphicsItem *qgi, conducers_to_move) diagram -> qgiManager().release(qgi); - foreach(QGraphicsItem *qgi, conducers_to_update) diagram -> qgiManager().release(qgi); } /// annule le deplacement @@ -357,3 +351,39 @@ void RotateElementsCommand::redo() { e -> update(); } } + +/** + Constructeur + @param c Conducteur modifie + @param old_p ancien profil du conducteur + @param new_p nouveau profil du conducteur + @param parent QUndoCommand parent +*/ +ChangeConducerCommand::ChangeConducerCommand( + Conducer *c, + const ConducerProfile &old_p, + const ConducerProfile &new_p, + QUndoCommand *parent +) : + QUndoCommand(QObject::tr("modifier un conducteur"), parent), + conducer(c), + old_profile(old_p), + new_profile(new_p), + first_redo(true) +{ +} + +/// Destructeur +ChangeConducerCommand::~ChangeConducerCommand() { +} + +/// Annule la modification du conducteur +void ChangeConducerCommand::undo() { + conducer -> setProfile(old_profile); +} + +/// Refait la modification du conducteur +void ChangeConducerCommand::redo() { + if (first_redo) first_redo = false; + else conducer -> setProfile(new_profile); +} diff --git a/diagramcommands.h b/diagramcommands.h index c97cf1a3f..593ba6ba8 100644 --- a/diagramcommands.h +++ b/diagramcommands.h @@ -3,6 +3,7 @@ #include "qet.h" #include "diagram.h" #include "diagramtextitem.h" +#include "conducer.h" #include /** Cette classe represente l'action d'ajouter un element au schema @@ -182,7 +183,6 @@ class ChangeDiagramTextCommand : public QUndoCommand { QString text_after; /// booleen pour ne pas executer le premier redo() bool first_redo; - }; /** @@ -206,4 +206,32 @@ class RotateElementsCommand : public QUndoCommand { /// texte avant changement QHash elements_to_rotate; }; + +/** + Cette classe represente l'action de modifier un conducteur +*/ +class ChangeConducerCommand : public QUndoCommand { + // constructeurs, destructeur + public: + ChangeConducerCommand(Conducer *, const ConducerProfile &, const ConducerProfile &, QUndoCommand * = 0); + virtual ~ChangeConducerCommand(); + private: + ChangeConducerCommand(const ChangeConducerCommand &); + + // methodes + public: + virtual void undo(); + virtual void redo(); + + // attributs + private: + /// DiagramTextItem modifie + Conducer *conducer; + /// texte avant changement + ConducerProfile old_profile; + /// texte apres changement + ConducerProfile new_profile; + /// booleen pour ne pas executer le premier redo() + bool first_redo; +}; #endif