Fixed broken redo/undo command moving of a label.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-03-28 15:49:35 +03:00
parent 0c9b8453bf
commit 2339a95585
9 changed files with 89 additions and 16 deletions

View File

@ -160,6 +160,33 @@ void VToolDoublePoint::FullUpdateFromFile()
SetVisualization(); SetVisualization();
} }
//---------------------------------------------------------------------------------------------------------------------
void VToolDoublePoint::DoChangePosition(quint32 id, qreal mx, qreal my)
{
if (id == p1id)
{
VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject<VPointF>(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<VPointF>(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) void VToolDoublePoint::UpdateNamePosition(quint32 id)
{ {
@ -167,14 +194,17 @@ void VToolDoublePoint::UpdateNamePosition(quint32 id)
{ {
const VPointF *p1 = VAbstractTool::data.GeometricObject<VPointF>(p1id).data(); const VPointF *p1 = VAbstractTool::data.GeometricObject<VPointF>(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); qApp->getUndoStack()->push(moveLabel);
} }
else if (id == p2id) else if (id == p2id)
{ {
const VPointF *p2 = VAbstractTool::data.GeometricObject<VPointF>(p2id).data(); const VPointF *p2 = VAbstractTool::data.GeometricObject<VPointF>(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); qApp->getUndoStack()->push(moveLabel);
} }
} }

View File

@ -68,6 +68,7 @@ public slots:
void Point1Choosed(); void Point1Choosed();
void Point2Choosed(); void Point2Choosed();
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE; virtual void FullUpdateFromFile() Q_DECL_OVERRIDE;
virtual void DoChangePosition(quint32 id, qreal mx, qreal my) Q_DECL_OVERRIDE;
protected: protected:
VSimplePoint *firstPoint; VSimplePoint *firstPoint;

View File

@ -129,6 +129,7 @@ void VToolSinglePoint::UpdateNamePosition(quint32 id)
{ {
const QSharedPointer<VPointF> point = VAbstractTool::data.GeometricObject<VPointF>(id); const QSharedPointer<VPointF> point = VAbstractTool::data.GeometricObject<VPointF>(id);
auto moveLabel = new MoveLabel(doc, point->mx(), point->my(), id, scene()); auto moveLabel = new MoveLabel(doc, point->mx(), point->my(), id, scene());
connect(moveLabel, &MoveLabel::ChangePosition, this, &VToolSinglePoint::DoChangePosition);
qApp->getUndoStack()->push(moveLabel); qApp->getUndoStack()->push(moveLabel);
} }
@ -321,3 +322,16 @@ void VToolSinglePoint::SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &o
doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx())); doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx()));
doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my())); doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my()));
} }
//---------------------------------------------------------------------------------------------------------------------
void VToolSinglePoint::DoChangePosition(quint32 id, qreal mx, qreal my)
{
VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject<VPointF>(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);
}

View File

@ -62,6 +62,7 @@ public slots:
virtual void EnableToolMove(bool move) Q_DECL_OVERRIDE; virtual void EnableToolMove(bool move) Q_DECL_OVERRIDE;
void PointChoosed(); void PointChoosed();
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE; virtual void FullUpdateFromFile() Q_DECL_OVERRIDE;
virtual void DoChangePosition(quint32 id, qreal mx, qreal my) Q_DECL_OVERRIDE;
protected: protected:
/** @brief radius radius circle. */ /** @brief radius radius circle. */
qreal radius; qreal radius;

View File

@ -48,6 +48,7 @@ public:
public slots: public slots:
virtual void ShowTool(quint32 id, bool enable) Q_DECL_OVERRIDE; virtual void ShowTool(quint32 id, bool enable) Q_DECL_OVERRIDE;
void DeleteFromLabel(); void DeleteFromLabel();
virtual void DoChangePosition(quint32 id, qreal mx, qreal my) =0;
protected: protected:
void SetPointName(quint32 id, const QString &name); void SetPointName(quint32 id, const QString &name);
@ -55,6 +56,7 @@ protected:
template <typename T> template <typename T>
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 UpdateNamePosition(quint32 id)=0;
virtual void RefreshLine(quint32 id)=0; virtual void RefreshLine(quint32 id)=0;
@ -111,12 +113,8 @@ void VAbstractPoint::SetToolEnabled(T *item, bool enabled)
template <typename T> template <typename T>
void VAbstractPoint::ChangePosition(T *item, quint32 id, const QPointF &pos) void VAbstractPoint::ChangePosition(T *item, quint32 id, const QPointF &pos)
{ {
VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject<VPointF>(id));
const QPointF p = pos - item->pos(); const QPointF p = pos - item->pos();
point->setMx(p.x()); DoChangePosition(id, p.x(), p.y());
point->setMy(p.y());
VAbstractTool::data.UpdateGObject(id, point);
RefreshLine(id);
UpdateNamePosition(id); UpdateNamePosition(id);
} }

View File

@ -35,11 +35,13 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const double &y, DoublePoint type, 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), : VUndoCommand(QDomElement(), doc, parent),
oldMx(0.0), oldMy(0.0), oldMx(0.0), oldMy(0.0),
newMx(x), newMy(y), newMx(x), newMy(y),
scene(scene), type(type) scene(scene), type(type),
pointId(pointId),
isRedo(false)
{ {
if (type == DoublePoint::FirstPoint) if (type == DoublePoint::FirstPoint)
{ {
@ -49,7 +51,7 @@ MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const d
{ {
setText(tr("move the second dart label")); setText(tr("move the second dart label"));
} }
nodeId = id; nodeId = tooId;
qCDebug(vUndo, "Point id %u", nodeId); qCDebug(vUndo, "Point id %u", nodeId);
if (type == DoublePoint::FirstPoint) if (type == DoublePoint::FirstPoint)
@ -64,7 +66,7 @@ MoveDoubleLabel::MoveDoubleLabel(VAbstractPattern *doc, const double &x, const d
} }
SCASSERT(scene != nullptr); SCASSERT(scene != nullptr);
QDomElement domElement = doc->elementById(id); QDomElement domElement = doc->elementById(tooId);
if (domElement.isElement()) if (domElement.isElement())
{ {
if (type == DoublePoint::FirstPoint) if (type == DoublePoint::FirstPoint)
@ -106,6 +108,8 @@ void MoveDoubleLabel::undo()
qCDebug(vUndo, "Undo."); qCDebug(vUndo, "Undo.");
Do(oldMx, oldMy); Do(oldMx, oldMy);
isRedo = true;
emit ChangePosition(pointId, oldMx, oldMy);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -114,6 +118,10 @@ void MoveDoubleLabel::redo()
qCDebug(vUndo, "Redo."); qCDebug(vUndo, "Redo.");
Do(newMx, newMy); 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<const MoveDoubleLabel *>(command); const MoveDoubleLabel *moveCommand = static_cast<const MoveDoubleLabel *>(command);
SCASSERT(moveCommand != nullptr); SCASSERT(moveCommand != nullptr);
if (moveCommand->getPointId() != nodeId || moveCommand->getPointType() != type) if (moveCommand->getPointId() != nodeId || moveCommand->getPointType() != type ||
moveCommand->getLabelId() != pointId)
{ {
return false; return false;
} }
@ -149,6 +158,12 @@ int MoveDoubleLabel::id() const
return static_cast<int>(UndoCommand::MoveDoubleLabel); return static_cast<int>(UndoCommand::MoveDoubleLabel);
} }
//---------------------------------------------------------------------------------------------------------------------
quint32 MoveDoubleLabel::getLabelId() const
{
return pointId;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void MoveDoubleLabel::Do(double mx, double my) void MoveDoubleLabel::Do(double mx, double my)
{ {

View File

@ -40,17 +40,20 @@ class MoveDoubleLabel : public VUndoCommand
Q_OBJECT Q_OBJECT
public: public:
MoveDoubleLabel(VAbstractPattern *doc, const double &x, const double &y, DoublePoint type, 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 ~MoveDoubleLabel() Q_DECL_OVERRIDE;
virtual void undo() Q_DECL_OVERRIDE; virtual void undo() Q_DECL_OVERRIDE;
virtual void redo() Q_DECL_OVERRIDE; virtual void redo() Q_DECL_OVERRIDE;
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE; virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE;
virtual int id() const Q_DECL_OVERRIDE; virtual int id() const Q_DECL_OVERRIDE;
quint32 getPointId() const; quint32 getPointId() const;
quint32 getLabelId() const;
double getNewMx() const; double getNewMx() const;
double getNewMy() const; double getNewMy() const;
DoublePoint getPointType() const; DoublePoint getPointType() const;
void Do(double mx, double my); void Do(double mx, double my);
signals:
void ChangePosition(quint32 id, qreal mx, qreal my);
private: private:
Q_DISABLE_COPY(MoveDoubleLabel) Q_DISABLE_COPY(MoveDoubleLabel)
double oldMx; double oldMx;
@ -59,6 +62,8 @@ private:
double newMy; double newMy;
QGraphicsScene *scene; QGraphicsScene *scene;
DoublePoint type; DoublePoint type;
quint32 pointId;
bool isRedo;
}; };
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -36,7 +36,7 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
MoveLabel::MoveLabel(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, MoveLabel::MoveLabel(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene,
QUndoCommand *parent) 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")); setText(tr("move point label"));
nodeId = id; nodeId = id;
@ -72,6 +72,8 @@ void MoveLabel::undo()
qCDebug(vUndo, "Undo."); qCDebug(vUndo, "Undo.");
Do(oldMx, oldMy); Do(oldMx, oldMy);
isRedo = true;
emit ChangePosition(nodeId, oldMx, oldMy);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -80,6 +82,10 @@ void MoveLabel::redo()
qCDebug(vUndo, "Redo."); qCDebug(vUndo, "Redo.");
Do(newMx, newMy); Do(newMx, newMy);
if (isRedo)
{
emit ChangePosition(nodeId, newMx, newMy);
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -48,12 +48,15 @@ public:
double getNewMx() const; double getNewMx() const;
double getNewMy() const; double getNewMy() const;
void Do(double mx, double my); void Do(double mx, double my);
signals:
void ChangePosition(quint32 id, qreal mx, qreal my);
private: private:
Q_DISABLE_COPY(MoveLabel) Q_DISABLE_COPY(MoveLabel)
double oldMx; double oldMx;
double oldMy; double oldMy;
double newMx; double newMx;
double newMy; double newMy;
bool isRedo;
QGraphicsScene *scene; QGraphicsScene *scene;
}; };