diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index 300a70f0d..b0a196158 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -360,40 +360,6 @@ void ChangePartCommand::redo() ElementEditionCommand::redo(); } -/** - Constructeur - @param p Polygone edite - @param o_points points avant le changement - @param n_points points apres le changement - @param parent QUndoCommand parent -*/ -ChangePolygonPointsCommand::ChangePolygonPointsCommand( - PartPolygon *p, - const QVector &o_points, - const QVector &n_points, - QUndoCommand *parent -) : - ElementEditionCommand(QObject::tr("modification points polygone", "undo caption"), 0, 0, parent), - polygon(p), - old_points(o_points), - new_points(n_points) -{ -} - -/// Destructeur -ChangePolygonPointsCommand::~ChangePolygonPointsCommand() { -} - -/// Annule le changement -void ChangePolygonPointsCommand::undo() { - polygon -> setPolygon(old_points); -} - -/// Refait le changement -void ChangePolygonPointsCommand::redo() { - polygon -> setPolygon(new_points); -} - /** Constructeur @param element_scene Element edite diff --git a/sources/editor/editorcommands.h b/sources/editor/editorcommands.h index 59299fbd5..b74ba2717 100644 --- a/sources/editor/editorcommands.h +++ b/sources/editor/editorcommands.h @@ -18,12 +18,10 @@ #ifndef EDITOR_COMMANDS_H #define EDITOR_COMMANDS_H #include "customelementpart.h" -#include "partpolygon.h" #include "elementview.h" #include "elementscene.h" #include "elementcontent.h" #include "qgimanager.h" -#include /** * @brief The ElementEditionCommand class @@ -201,32 +199,6 @@ class ChangePartCommand : public ElementEditionCommand QVariant m_new_value; }; -/** - This command changes the points of a polygon when editing an electrical - element. -*/ -class ChangePolygonPointsCommand : public ElementEditionCommand { - // constructors, destructor - public: - ChangePolygonPointsCommand(PartPolygon *, const QVector &, const QVector &, QUndoCommand * = 0); - virtual ~ChangePolygonPointsCommand(); - private: - ChangePolygonPointsCommand(const ChangePolygonPointsCommand &); - - // methods - public: - virtual void undo(); - virtual void redo(); - - // attributes - /// Changed polygon - PartPolygon *polygon; - /// Former points - QVector old_points; - /// New points - QVector new_points; -}; - /** This command changes the translated names of an electrical element. */ diff --git a/sources/editor/graphicspart/partpolygon.cpp b/sources/editor/graphicspart/partpolygon.cpp index 1450c82d6..24f984be3 100644 --- a/sources/editor/graphicspart/partpolygon.cpp +++ b/sources/editor/graphicspart/partpolygon.cpp @@ -199,6 +199,7 @@ void PartPolygon::setPolygon(const QPolygonF &polygon) if (m_polygon == polygon) return; prepareGeometryChange(); m_polygon = polygon; + emit polygonChanged(); } /** @@ -239,6 +240,14 @@ void PartPolygon::removeLastPoint() } } +void PartPolygon::setClosed(bool close) +{ + if (m_closed == close) return; + prepareGeometryChange(); + m_closed = close; + emit closedChange(); +} + /** * @brief PartPolygon::mousePressEvent * Handle mouse press event @@ -274,6 +283,7 @@ void PartPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event) QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos())); prepareGeometryChange(); m_polygon.replace(m_handler_index, pos_); + emit polygonChanged(); } else CustomElementGraphicPart::mouseMoveEvent(event); diff --git a/sources/editor/graphicspart/partpolygon.h b/sources/editor/graphicspart/partpolygon.h index 902ad4e48..57c3bafac 100644 --- a/sources/editor/graphicspart/partpolygon.h +++ b/sources/editor/graphicspart/partpolygon.h @@ -45,6 +45,10 @@ class PartPolygon : public CustomElementGraphicPart private: PartPolygon(const PartPolygon &); + signals: + void closedChange(); + void polygonChanged(); + // methods public: enum { Type = UserType + 1105 }; @@ -78,7 +82,7 @@ class PartPolygon : public CustomElementGraphicPart void removeLastPoint (); bool isClosed () const {return m_closed;} - void setClosed (bool c) {m_closed = c;} + void setClosed (bool close); protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); diff --git a/sources/editor/polygoneditor.cpp b/sources/editor/polygoneditor.cpp index 663548de6..129a8bb8d 100644 --- a/sources/editor/polygoneditor.cpp +++ b/sources/editor/polygoneditor.cpp @@ -18,9 +18,9 @@ #include "polygoneditor.h" #include "partpolygon.h" #include "elementscene.h" -#include "editorcommands.h" #include "qetmessagebox.h" #include "styleeditor.h" +#include "QPropertyUndoCommand/qpropertyundocommand.h" /** Constructeur @@ -58,45 +58,40 @@ PolygonEditor::PolygonEditor(QETElementEditor *editor, PartPolygon *p, QWidget * PolygonEditor::~PolygonEditor() { } -/** - Met a jour le polygone a partir des donnees du formulaire : points et etat ferme ou non -*/ -void PolygonEditor::updatePolygon() { - updatePolygonPoints(); - updatePolygonClosedState(); -} - /** Met a jour les points du polygone et cree un objet d'annulation */ -void PolygonEditor::updatePolygonPoints() { +void PolygonEditor::updatePolygonPoints() +{ if (!part) return; - QVector points = getPointsFromTree(); - if (points.count() < 2) { - QET::QetMessageBox::warning( - this, - tr("Erreur", "message box title"), - tr("Le polygone doit comporter au moins deux points.", "message box content") - ); + QPolygonF points = getPointsFromTree(); + if (points.count() < 2) + { + QET::QetMessageBox::warning(this, tr("Erreur", "message box title"), tr("Le polygone doit comporter au moins deux points.", "message box content")); return; } - undoStack().push(new ChangePolygonPointsCommand(part, part -> polygon(), points)); + + if (points != part->polygon()) + { + QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "polygon", part->property("polygon"), points); + undo->setText(tr("Modifier un polygone")); + undoStack().push(undo); + } } /** Met a jour l'etat ferme ou non du polygone */ -void PolygonEditor::updatePolygonClosedState() { +void PolygonEditor::updatePolygonClosedState() +{ if (!part) return; - undoStack().push( - new ChangePartCommand( - tr("fermeture du polygone"), - part, - "closed", - QVariant(!close_polygon.isChecked()), - QVariant(close_polygon.isChecked()) - ) - ); + bool close = close_polygon.isChecked(); + if (close != part->isClosed()) + { + QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "closed", part->property("closed"), close); + undo->setText(tr("Modifier un polygone")); + undoStack().push(undo); + } } /** @@ -126,20 +121,35 @@ void PolygonEditor::updateForm() { @param new_part Nouvelle primitive a editer @return true si l'editeur a accepter d'editer la primitive, false sinon */ -bool PolygonEditor::setPart(CustomElementPart *new_part) { - if (!new_part) { +bool PolygonEditor::setPart(CustomElementPart *new_part) +{ + if (!new_part) + { + if (part) + { + disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm); + disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm); + } part = 0; style_ -> setPart(0); return(true); } - if (PartPolygon *part_polygon = dynamic_cast(new_part)) { + if (PartPolygon *part_polygon = dynamic_cast(new_part)) + { + if (part == part_polygon) return true; + if (part) + { + disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm); + disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm); + } part = part_polygon; style_ -> setPart(part); updateForm(); + connect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm); + connect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm); return(true); - } else { - return(false); } + return(false); } /** diff --git a/sources/editor/polygoneditor.h b/sources/editor/polygoneditor.h index 3e78f8220..1e97faba5 100644 --- a/sources/editor/polygoneditor.h +++ b/sources/editor/polygoneditor.h @@ -57,7 +57,6 @@ class PolygonEditor : public ElementItemEditor { QVector getPointsFromTree(); public slots: - void updatePolygon(); void updatePolygonPoints(); void updatePolygonClosedState(); void updateForm();