diff --git a/conducer.cpp b/conducer.cpp index 91f883075..85ce495c3 100644 --- a/conducer.cpp +++ b/conducer.cpp @@ -52,9 +52,9 @@ Conducer::Conducer(Terminal *p1, Terminal* p2, Element *parent, QGraphicsScene * setAcceptsHoverEvents(true); // ajout du champ de texte editable - text_item = new QGraphicsTextItem(); + text_item = new DiagramTextItem(); text_item -> setPlainText("_"); - text_item -> setTextInteractionFlags(Qt::TextEditorInteraction); + text_item -> previous_text = "_"; calculateTextItemPosition(); text_item -> setParentItem(this); } @@ -781,6 +781,7 @@ bool Conducer::hasClickedOn(QPointF press_point, QPointF point) const { */ bool Conducer::fromXml(QDomElement &e) { text_item -> setPlainText(e.attribute("num")); + text_item -> previous_text = e.attribute("num"); // parcourt les elements XML "segment" et en extrait deux listes de longueurs // les segments non valides sont ignores diff --git a/conducer.h b/conducer.h index 0b7b1f122..bf302dbf9 100644 --- a/conducer.h +++ b/conducer.h @@ -3,6 +3,7 @@ #include #include "terminal.h" #include "conducerprofile.h" +#include "diagramtextitem.h" class ConducerSegment; class Element; /** @@ -30,7 +31,7 @@ class Conducer : public QGraphicsPathItem { private: /// booleen indiquant si le fil est encore valide bool destroyed; - QGraphicsTextItem *text_item; + DiagramTextItem *text_item; ConducerSegment *segments; QPointF press_point; bool moving_point; @@ -64,10 +65,10 @@ class Conducer : public QGraphicsPathItem { const QList segmentsList() const; protected: - void mousePressEvent(QGraphicsSceneMouseEvent *); - void mouseMoveEvent(QGraphicsSceneMouseEvent *); - void mouseReleaseEvent(QGraphicsSceneMouseEvent *); - void hoverMoveEvent(QGraphicsSceneHoverEvent *); + virtual void mousePressEvent(QGraphicsSceneMouseEvent *); + virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *); + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *); + virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *); private: void segmentsToPath(); diff --git a/diagramcommands.cpp b/diagramcommands.cpp index c66ce87d5..f17543c1d 100644 --- a/diagramcommands.cpp +++ b/diagramcommands.cpp @@ -221,7 +221,12 @@ CutDiagramCommand::~CutDiagramCommand() { /** Constructeur - @param + @param dia Schema sur lequel on deplace des elements + @param move_elements Elements a deplacer + @param move_conducers Conducteurs a deplacer + @param modify_conducers Conducteurs a mettre a jour + @param m translation subie par les elements + @param parent QUndoCommand parent */ MoveElementsCommand::MoveElementsCommand( Diagram *dia, @@ -262,7 +267,10 @@ void MoveElementsCommand::redo() { else move(movement); } -/// +/** + deplace les elements et conducteurs + @param actual_movement translation a effectuer sur les elements et conducteurs +*/ void MoveElementsCommand::move(const QPointF &actual_movement) { // deplace les elements foreach(Element *element, elements_to_move) { @@ -279,3 +287,43 @@ void MoveElementsCommand::move(const QPointF &actual_movement) { conducer -> updateWithNewPos(QRectF(), conducers_to_update[conducer], conducers_to_update[conducer] -> amarrageConducer()); } } + +/** + Constructeur + @param dti Champ de texte modifie + @param before texte avant + @param after texte apres + @param parent QUndoCommand parent +*/ +ChangeDiagramTextCommand::ChangeDiagramTextCommand( + DiagramTextItem *dti, + const QString &before, + const QString &after, + QUndoCommand *parent +) : + QUndoCommand(QObject::tr("modifier le texte"), parent), + text_item(dti), + text_before(before), + text_after(after), + first_redo(true) +{ +} + +/// destructeur +ChangeDiagramTextCommand::~ChangeDiagramTextCommand() { +} + +/// annule la modification de texte +void ChangeDiagramTextCommand::undo() { + text_item -> setPlainText(text_before); + text_item -> previous_text = text_before; +} + +/// refait la modification de texte +void ChangeDiagramTextCommand::redo() { + if (first_redo) first_redo = false; + else { + text_item -> setPlainText(text_after); + text_item -> previous_text = text_after; + } +} diff --git a/diagramcommands.h b/diagramcommands.h index 279894101..beb840f98 100644 --- a/diagramcommands.h +++ b/diagramcommands.h @@ -1,6 +1,7 @@ #ifndef DIAGRAM_COMMANDS_H #define DIAGRAM_COMMANDS_H #include "diagram.h" +#include "diagramtextitem.h" #include /** Cette classe represente l'action d'ajouter un element au schema @@ -153,4 +154,33 @@ class MoveElementsCommand : public QUndoCommand { /// booleen pour ne pas executer le premier redo() bool first_redo; }; + +/** + Cette classe represente la modification d'un champ de texte +*/ +class ChangeDiagramTextCommand : public QUndoCommand { + // constructeurs, destructeur + public: + ChangeDiagramTextCommand(DiagramTextItem *, const QString &before, const QString &after, QUndoCommand * = 0); + virtual ~ChangeDiagramTextCommand(); + private: + ChangeDiagramTextCommand(const ChangeDiagramTextCommand &); + + // methodes + public: + virtual void undo(); + virtual void redo(); + + // attributs + private: + /// DiagramTextItem modifie + DiagramTextItem *text_item; + /// texte avant changement + QString text_before; + /// texte apres changement + QString text_after; + /// booleen pour ne pas executer le premier redo() + bool first_redo; + +}; #endif diff --git a/diagramtextitem.cpp b/diagramtextitem.cpp new file mode 100644 index 000000000..5b2b80203 --- /dev/null +++ b/diagramtextitem.cpp @@ -0,0 +1,49 @@ +#include "diagramtextitem.h" +#include "diagramcommands.h" + +/** + Constructeur + @param parent Le QGraphicsItem parent du champ de texte + @param scene La scene a laquelle appartient le champ de texte +*/ +DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : + QGraphicsTextItem(parent, scene) +{ + setTextInteractionFlags(Qt::TextEditorInteraction); +} + +/** + Constructeur + @param parent Le QGraphicsItem parent du champ de texte + @param scene La scene a laquelle appartient le champ de texte + @param text Le texte affiche par le champ de texte +*/ +DiagramTextItem::DiagramTextItem(const QString &text, QGraphicsItem *parent, QGraphicsScene *scene) : + QGraphicsTextItem(text, parent, scene), + previous_text(text) +{ + setTextInteractionFlags(Qt::TextEditorInteraction); +} + +/// Destructeur +DiagramTextItem::~DiagramTextItem() { +} + +/// @return le Diagram auquel ce texte appartient, ou 0 si ce texte est independant +Diagram *DiagramTextItem::diagram() const { + return(qobject_cast(scene())); +} + +/** + gere la perte de focus du champ de texte +*/ +void DiagramTextItem::focusOutEvent(QFocusEvent *e) { + QGraphicsTextItem::focusOutEvent(e); + // si le texte a ete modifie + if (toPlainText() != previous_text) { + if (Diagram *dia = diagram()) { + dia -> undoStack().push(new ChangeDiagramTextCommand(this, previous_text, toPlainText())); + previous_text = toPlainText(); + } + } +} diff --git a/diagramtextitem.h b/diagramtextitem.h new file mode 100644 index 000000000..60e0388cd --- /dev/null +++ b/diagramtextitem.h @@ -0,0 +1,25 @@ +#ifndef DIAGRAM_TEXT_ITEM_H +#define DIAGRAM_TEXT_ITEM_H +#include +#include "diagram.h" +class DiagramTextItem : public QGraphicsTextItem { + // constructeurs, destructeur + public: + DiagramTextItem(QGraphicsItem * = 0, QGraphicsScene * = 0); + DiagramTextItem(const QString &, QGraphicsItem * = 0, QGraphicsScene * = 0); + virtual ~DiagramTextItem(); + + // attributs + public: + enum { Type = UserType + 1004 }; + QString previous_text; + + // methodes + public: + virtual int type() const { return Type; } + Diagram *diagram() const; + + protected: + virtual void focusOutEvent(QFocusEvent *); +}; +#endif diff --git a/elementtextitem.cpp b/elementtextitem.cpp index 38e55fea5..5d0b7ce7f 100644 --- a/elementtextitem.cpp +++ b/elementtextitem.cpp @@ -1,14 +1,16 @@ #include "elementtextitem.h" #include "diagram.h" +#include "diagramcommands.h" /** Constructeur @param parent Le QGraphicsItem parent du champ de texte @param scene La scene a laquelle appartient le champ de texte */ -ElementTextItem::ElementTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(parent, scene) { - follow_parent_rotations = false; - setTextInteractionFlags(Qt::TextEditorInteraction); +ElementTextItem::ElementTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : + DiagramTextItem(parent, scene), + follow_parent_rotations(false) +{ } /** @@ -17,11 +19,13 @@ ElementTextItem::ElementTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : @param scene La scene a laquelle appartient le champ de texte @param text Le texte affiche par le champ de texte */ -ElementTextItem::ElementTextItem(const QString &text, QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(text, parent, scene) { - follow_parent_rotations = false; - setTextInteractionFlags(Qt::TextEditorInteraction); +ElementTextItem::ElementTextItem(const QString &text, QGraphicsItem *parent, QGraphicsScene *scene) : + DiagramTextItem(text, parent, scene), + follow_parent_rotations(false) +{ } +/// Destructeur ElementTextItem::~ElementTextItem() { } @@ -32,7 +36,7 @@ ElementTextItem::~ElementTextItem() { void ElementTextItem::setPos(const QPointF &pos) { QPointF actual_pos = pos; actual_pos -= QPointF(0.0, boundingRect().height() / 2.0); - QGraphicsItem::setPos(actual_pos); + DiagramTextItem::setPos(actual_pos); } /** @@ -48,7 +52,7 @@ void ElementTextItem::setPos(qreal x, qreal y) { @return La position (bidouillee) du champ de texte */ QPointF ElementTextItem::pos() const { - QPointF actual_pos = QGraphicsTextItem::pos(); + QPointF actual_pos = DiagramTextItem::pos(); actual_pos += QPointF(0.0, boundingRect().height() / 2.0); return(actual_pos); } @@ -63,6 +67,7 @@ void ElementTextItem::fromXml(const QDomElement &e) { QPointF _pos = pos(); if (e.attribute("x").toDouble() == _pos.x() && e.attribute("y").toDouble() == _pos.y()) { setPlainText(e.attribute("text")); + previous_text = e.attribute("text"); } } @@ -77,8 +82,3 @@ QDomElement ElementTextItem::toXml(QDomDocument &document) const { result.setAttribute("text", toPlainText()); return(result); } - -/// @return le Diagram auquel ce texte appartient, ou 0 si ce texte est independant -Diagram *ElementTextItem::diagram() const { - return(qobject_cast(scene())); -} diff --git a/elementtextitem.h b/elementtextitem.h index 314879649..7dae53e9d 100644 --- a/elementtextitem.h +++ b/elementtextitem.h @@ -1,6 +1,6 @@ #ifndef ELEMENT_TEXT_ITEM_H #define ELEMENT_TEXT_ITEM_H -#include +#include "diagramtextitem.h" #include class Diagram; /** @@ -8,7 +8,7 @@ class Diagram; Il est possible pour ce champ de texte de rester dans le sens de la lecture malgre les rotations de son element parent. */ -class ElementTextItem : public QGraphicsTextItem { +class ElementTextItem : public DiagramTextItem { // constructeurs, destructeur public: ElementTextItem(QGraphicsItem * = 0, QGraphicsScene * = 0); @@ -32,7 +32,6 @@ class ElementTextItem : public QGraphicsTextItem { void setPos(const QPointF &); void setPos(qreal, qreal); QPointF pos() const; - Diagram *diagram() const; }; /** diff --git a/qelectrotech.pro b/qelectrotech.pro index 605074a2f..4b52675be 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -64,7 +64,9 @@ HEADERS += aboutqet.h \ editor/terminaleditor.h \ editor/texteditor.h \ editor/textfieldeditor.h \ - diagramcommands.h + diagramcommands.h \ + diagramitem.h \ + diagramtextitem.h SOURCES += aboutqet.cpp \ borderinset.cpp \ conducer.cpp \ @@ -121,7 +123,8 @@ SOURCES += aboutqet.cpp \ editor/terminaleditor.cpp \ editor/texteditor.cpp \ editor/textfieldeditor.cpp \ - diagramcommands.cpp + diagramcommands.cpp \ + diagramtextitem.cpp RESOURCES += qelectrotech.qrc TRANSLATIONS += lang/qet_en.ts lang/qt_fr.ts RC_FILE = ico/windows_icon/application_icon/qelectrotech.rc