From 2339a95585bcb0a75a4546c05798a799dcd9d2db Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 28 Mar 2016 15:49:35 +0300 Subject: [PATCH] Fixed broken redo/undo command moving of a label. --HG-- branch : develop --- .../tooldoublepoint/vtooldoublepoint.cpp | 34 +++++++++++++++++-- .../tooldoublepoint/vtooldoublepoint.h | 1 + .../toolsinglepoint/vtoolsinglepoint.cpp | 14 ++++++++ .../toolsinglepoint/vtoolsinglepoint.h | 1 + .../drawTools/toolpoint/vabstractpoint.h | 12 +++---- .../vtools/undocommands/movedoublelabel.cpp | 25 +++++++++++--- .../vtools/undocommands/movedoublelabel.h | 7 +++- src/libs/vtools/undocommands/movelabel.cpp | 8 ++++- src/libs/vtools/undocommands/movelabel.h | 3 ++ 9 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp index c0f7eee87..70c0c93f3 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.cpp @@ -160,6 +160,33 @@ void VToolDoublePoint::FullUpdateFromFile() SetVisualization(); } +//--------------------------------------------------------------------------------------------------------------------- +void VToolDoublePoint::DoChangePosition(quint32 id, qreal mx, qreal my) +{ + if (id == p1id) + { + VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(p1id)); + point->setMx(mx); + point->setMy(my); + VAbstractTool::data.UpdateGObject(p1id, point); + firstPoint->blockSignals(true); + firstPoint->setPos(QPointF(mx, my)); + firstPoint->blockSignals(false); + RefreshLine(p1id); + } + else if (id == p2id) + { + VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(p2id)); + point->setMx(mx); + point->setMy(my); + VAbstractTool::data.UpdateGObject(p2id, point); + secondPoint->blockSignals(true); + secondPoint->setPos(QPointF(mx, my)); + secondPoint->blockSignals(false); + RefreshLine(p2id); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VToolDoublePoint::UpdateNamePosition(quint32 id) { @@ -167,14 +194,17 @@ void VToolDoublePoint::UpdateNamePosition(quint32 id) { const VPointF *p1 = VAbstractTool::data.GeometricObject(p1id).data(); - auto moveLabel = new MoveDoubleLabel(doc, p1->mx(), p1->my(), DoublePoint::FirstPoint, this->id, scene()); + auto moveLabel = new MoveDoubleLabel(doc, p1->mx(), p1->my(), DoublePoint::FirstPoint, this->id, p1id, scene()); + connect(moveLabel, &MoveDoubleLabel::ChangePosition, this, &VToolDoublePoint::DoChangePosition); qApp->getUndoStack()->push(moveLabel); } else if (id == p2id) { const VPointF *p2 = VAbstractTool::data.GeometricObject(p2id).data(); - auto moveLabel = new MoveDoubleLabel(doc, p2->mx(), p2->my(), DoublePoint::SecondPoint, this->id, scene()); + auto moveLabel = new MoveDoubleLabel(doc, p2->mx(), p2->my(), DoublePoint::SecondPoint, this->id, p2id, + scene()); + connect(moveLabel, &MoveDoubleLabel::ChangePosition, this, &VToolDoublePoint::DoChangePosition); qApp->getUndoStack()->push(moveLabel); } } diff --git a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h index 1f4eb3f7d..88bced16f 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h +++ b/src/libs/vtools/tools/drawTools/toolpoint/tooldoublepoint/vtooldoublepoint.h @@ -68,6 +68,7 @@ public slots: void Point1Choosed(); void Point2Choosed(); virtual void FullUpdateFromFile() Q_DECL_OVERRIDE; + virtual void DoChangePosition(quint32 id, qreal mx, qreal my) Q_DECL_OVERRIDE; protected: VSimplePoint *firstPoint; diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp index 5432ed334..fe9c75350 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.cpp @@ -129,6 +129,7 @@ void VToolSinglePoint::UpdateNamePosition(quint32 id) { const QSharedPointer point = VAbstractTool::data.GeometricObject(id); auto moveLabel = new MoveLabel(doc, point->mx(), point->my(), id, scene()); + connect(moveLabel, &MoveLabel::ChangePosition, this, &VToolSinglePoint::DoChangePosition); qApp->getUndoStack()->push(moveLabel); } @@ -321,3 +322,16 @@ void VToolSinglePoint::SaveOptions(QDomElement &tag, QSharedPointer &o doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx())); doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my())); } + +//--------------------------------------------------------------------------------------------------------------------- +void VToolSinglePoint::DoChangePosition(quint32 id, qreal mx, qreal my) +{ + VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(id)); + point->setMx(mx); + point->setMy(my); + VAbstractTool::data.UpdateGObject(id, point); + namePoint->blockSignals(true); + namePoint->setPos(QPointF(mx, my)); + namePoint->blockSignals(false); + RefreshLine(id); +} diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h index 0a1c7f809..927126917 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolsinglepoint.h @@ -62,6 +62,7 @@ public slots: virtual void EnableToolMove(bool move) Q_DECL_OVERRIDE; void PointChoosed(); virtual void FullUpdateFromFile() Q_DECL_OVERRIDE; + virtual void DoChangePosition(quint32 id, qreal mx, qreal my) Q_DECL_OVERRIDE; protected: /** @brief radius radius circle. */ qreal radius; diff --git a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h index c38bb4903..3348743e3 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h +++ b/src/libs/vtools/tools/drawTools/toolpoint/vabstractpoint.h @@ -48,12 +48,14 @@ public: public slots: virtual void ShowTool(quint32 id, bool enable) Q_DECL_OVERRIDE; void DeleteFromLabel(); + virtual void DoChangePosition(quint32 id, qreal mx, qreal my) =0; protected: - void SetPointName(quint32 id, const QString &name); + void SetPointName(quint32 id, const QString &name); template - void ChangePosition(T *item, quint32 id, const QPointF &pos); + void ChangePosition(T *item, quint32 id, const QPointF &pos); + virtual void UpdateNamePosition(quint32 id)=0; virtual void RefreshLine(quint32 id)=0; @@ -111,12 +113,8 @@ void VAbstractPoint::SetToolEnabled(T *item, bool enabled) template void VAbstractPoint::ChangePosition(T *item, quint32 id, const QPointF &pos) { - VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject(id)); const QPointF p = pos - item->pos(); - point->setMx(p.x()); - point->setMy(p.y()); - VAbstractTool::data.UpdateGObject(id, point); - RefreshLine(id); + DoChangePosition(id, p.x(), p.y()); UpdateNamePosition(id); } diff --git a/src/libs/vtools/undocommands/movedoublelabel.cpp b/src/libs/vtools/undocommands/movedoublelabel.cpp index 88ead7b9c..d2c8fe6bb 100644 --- a/src/libs/vtools/undocommands/movedoublelabel.cpp +++ b/src/libs/vtools/undocommands/movedoublelabel.cpp @@ -35,11 +35,13 @@ //--------------------------------------------------------------------------------------------------------------------- MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const double &y, DoublePoint type, - const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent) + quint32 tooId, quint32 pointId, QGraphicsScene *scene, QUndoCommand *parent) : VUndoCommand(QDomElement(), doc, parent), oldMx(0.0), oldMy(0.0), newMx(x), newMy(y), - scene(scene), type(type) + scene(scene), type(type), + pointId(pointId), + isRedo(false) { if (type == DoublePoint::FirstPoint) { @@ -49,7 +51,7 @@ MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const d { setText(tr("move the second dart label")); } - nodeId = id; + nodeId = tooId; qCDebug(vUndo, "Point id %u", nodeId); if (type == DoublePoint::FirstPoint) @@ -64,7 +66,7 @@ MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const d } SCASSERT(scene != nullptr); - QDomElement domElement = doc->elementById(id); + QDomElement domElement = doc->elementById(tooId); if (domElement.isElement()) { if (type == DoublePoint::FirstPoint) @@ -106,6 +108,8 @@ void MoveDoubleLabel::undo() qCDebug(vUndo, "Undo."); Do(oldMx, oldMy); + isRedo = true; + emit ChangePosition(pointId, oldMx, oldMy); } //--------------------------------------------------------------------------------------------------------------------- @@ -114,6 +118,10 @@ void MoveDoubleLabel::redo() qCDebug(vUndo, "Redo."); Do(newMx, newMy); + if (isRedo) + { + emit ChangePosition(pointId, newMx, newMy); + } } //--------------------------------------------------------------------------------------------------------------------- @@ -122,7 +130,8 @@ bool MoveDoubleLabel::mergeWith(const QUndoCommand *command) const MoveDoubleLabel *moveCommand = static_cast(command); SCASSERT(moveCommand != nullptr); - if (moveCommand->getPointId() != nodeId || moveCommand->getPointType() != type) + if (moveCommand->getPointId() != nodeId || moveCommand->getPointType() != type || + moveCommand->getLabelId() != pointId) { return false; } @@ -149,6 +158,12 @@ int MoveDoubleLabel::id() const return static_cast(UndoCommand::MoveDoubleLabel); } +//--------------------------------------------------------------------------------------------------------------------- +quint32 MoveDoubleLabel::getLabelId() const +{ + return pointId; +} + //--------------------------------------------------------------------------------------------------------------------- void MoveDoubleLabel::Do(double mx, double my) { diff --git a/src/libs/vtools/undocommands/movedoublelabel.h b/src/libs/vtools/undocommands/movedoublelabel.h index aae347691..b26728add 100644 --- a/src/libs/vtools/undocommands/movedoublelabel.h +++ b/src/libs/vtools/undocommands/movedoublelabel.h @@ -40,17 +40,20 @@ class MoveDoubleLabel : public VUndoCommand Q_OBJECT public: MoveDoubleLabel(VAbstractPattern *doc, const double &x, const double &y, DoublePoint type, - const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent = 0); + quint32 tooId, quint32 pointId, QGraphicsScene *scene, QUndoCommand *parent = 0); virtual ~MoveDoubleLabel() Q_DECL_OVERRIDE; virtual void undo() Q_DECL_OVERRIDE; virtual void redo() Q_DECL_OVERRIDE; virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE; virtual int id() const Q_DECL_OVERRIDE; quint32 getPointId() const; + quint32 getLabelId() const; double getNewMx() const; double getNewMy() const; DoublePoint getPointType() const; void Do(double mx, double my); +signals: + void ChangePosition(quint32 id, qreal mx, qreal my); private: Q_DISABLE_COPY(MoveDoubleLabel) double oldMx; @@ -59,6 +62,8 @@ private: double newMy; QGraphicsScene *scene; DoublePoint type; + quint32 pointId; + bool isRedo; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/undocommands/movelabel.cpp b/src/libs/vtools/undocommands/movelabel.cpp index bb1769752..c5c8dfea4 100644 --- a/src/libs/vtools/undocommands/movelabel.cpp +++ b/src/libs/vtools/undocommands/movelabel.cpp @@ -36,7 +36,7 @@ //--------------------------------------------------------------------------------------------------------------------- MoveLabel::MoveLabel(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent) - : VUndoCommand(QDomElement(), doc, parent), oldMx(0.0), oldMy(0.0), newMx(x), newMy(y), scene(scene) + : VUndoCommand(QDomElement(), doc, parent), oldMx(0.0), oldMy(0.0), newMx(x), newMy(y), isRedo(false), scene(scene) { setText(tr("move point label")); nodeId = id; @@ -72,6 +72,8 @@ void MoveLabel::undo() qCDebug(vUndo, "Undo."); Do(oldMx, oldMy); + isRedo = true; + emit ChangePosition(nodeId, oldMx, oldMy); } //--------------------------------------------------------------------------------------------------------------------- @@ -80,6 +82,10 @@ void MoveLabel::redo() qCDebug(vUndo, "Redo."); Do(newMx, newMy); + if (isRedo) + { + emit ChangePosition(nodeId, newMx, newMy); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/undocommands/movelabel.h b/src/libs/vtools/undocommands/movelabel.h index 3d74e0342..a3a04167d 100644 --- a/src/libs/vtools/undocommands/movelabel.h +++ b/src/libs/vtools/undocommands/movelabel.h @@ -48,12 +48,15 @@ public: double getNewMx() const; double getNewMy() const; void Do(double mx, double my); +signals: + void ChangePosition(quint32 id, qreal mx, qreal my); private: Q_DISABLE_COPY(MoveLabel) double oldMx; double oldMy; double newMx; double newMy; + bool isRedo; QGraphicsScene *scene; };