I am implementing undo/redo for move operation on QGraphicsItemGroup
in my graphics scene. It works decently for point entity.
My command for move looks like:
class CommandMove : public QUndoCommand
{
public:
CommandMove(QGraphicsItemGroup *group, qreal fromX, qreal fromY,
qreal toX, qreal toY)
{
itemGroup = group;
mFrom = QPointF(fromX, fromY);
mTo = QPointF(toX, toY);
setText(QString("Point move (%1,%2) -> (%3,%4)").arg(fromX).arg(fromY)
.arg(toX).arg(toY));
}
virtual void undo()
{
itemGroup->setPos(mFrom);
}
virtual void redo()
{
itemGroup->setPos(mTo);
}
private:
QGraphicsItemGroup *itemGroup;
QPointF mFrom;
QPointF mTo;
};
My command is pushed to the undo stack as:
if (item.first->scenePos() != item.second)
{
mUndoStack->push(new CommandMove(item.first, item.second.x(),
item.second.y(), item.first->x(),
item.first->y()));
}
item
is a QPair
defined as:
typedef QPair<QGraphicsItemGroup *, QPointF> item;
Implemenatation for entities like line, circle etc. requires more information as compared to point. eg., start and end points for line. How do I define my command for moving my entities?
Edit
This is my implementation for line:
if (m1)
{
start_p = event->scenePos();
m1 = false;
m2 = true;
}
else if (!m1 && m2)
{
end_p = event->scenePos();
m3 = true;
m2 = false;
}
if (m3)
{
lineItem = new Line(start_p, end_p);
}
Here event
is mousePressEvent
.
Where do I use setPos
to set the position of line?