Skip to content

Commit

Permalink
Instead of creating three commands classes use one class for shapes c…
Browse files Browse the repository at this point in the history
…hange undo/redo.
  • Loading branch information
adeas31 committed Oct 19, 2015
1 parent a13a6c9 commit c5ef284
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 154 deletions.
71 changes: 46 additions & 25 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp
Expand Up @@ -1081,10 +1081,9 @@ void ShapeAnnotation::applyRotation(qreal angle)
if (angle == 360) {
angle = 0;
}
Transformation oldTransformation = mTransformation;
mTransformation.setRotateAngle(angle);
setTransform(mTransformation.getTransformationMatrix());
mRotation = angle;
mpGraphicsView->setAddClassAnnotationNeeded(true);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand Down Expand Up @@ -1430,7 +1429,10 @@ void ShapeAnnotation::sendBackward()
*/
void ShapeAnnotation::rotateClockwise()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new RotateShapeCommand(this, true));
qreal oldRotation = StringHandler::getNormalizedAngle(mTransformation.getRotateAngle());
qreal rotateIncrement = -90;
qreal angle = oldRotation + rotateIncrement;
applyRotation(angle);
}

/*!
Expand All @@ -1440,7 +1442,10 @@ void ShapeAnnotation::rotateClockwise()
*/
void ShapeAnnotation::rotateAntiClockwise()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new RotateShapeCommand(this, false));
qreal oldRotation = StringHandler::getNormalizedAngle(mTransformation.getRotateAngle());
qreal rotateIncrement = 90;
qreal angle = oldRotation + rotateIncrement;
applyRotation(angle);
}

/*!
Expand All @@ -1451,8 +1456,9 @@ void ShapeAnnotation::rotateAntiClockwise()
*/
void ShapeAnnotation::moveUp()
{
qreal y = mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, y, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep());
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1463,8 +1469,9 @@ void ShapeAnnotation::moveUp()
*/
void ShapeAnnotation::moveShiftUp()
{
qreal y = mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep() * 5;
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, y, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep() * 5);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1475,7 +1482,9 @@ void ShapeAnnotation::moveShiftUp()
*/
void ShapeAnnotation::moveCtrlUp()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, 1, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, 1);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1486,8 +1495,9 @@ void ShapeAnnotation::moveCtrlUp()
*/
void ShapeAnnotation::moveDown()
{
qreal y = -mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, y, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, -mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep());
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1498,8 +1508,9 @@ void ShapeAnnotation::moveDown()
*/
void ShapeAnnotation::moveShiftDown()
{
qreal y = -(mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep() * 5);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, y, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, -(mpGraphicsView->getCoOrdinateSystem()->getVerticalGridStep() * 5));
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1510,7 +1521,9 @@ void ShapeAnnotation::moveShiftDown()
*/
void ShapeAnnotation::moveCtrlDown()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 0, -1, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(0, -1);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1521,8 +1534,9 @@ void ShapeAnnotation::moveCtrlDown()
*/
void ShapeAnnotation::moveLeft()
{
qreal x = -mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, x, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(-mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep(), 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1533,8 +1547,9 @@ void ShapeAnnotation::moveLeft()
*/
void ShapeAnnotation::moveShiftLeft()
{
qreal x = -(mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep() * 5);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, x, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(-(mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep() * 5), 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1545,7 +1560,9 @@ void ShapeAnnotation::moveShiftLeft()
*/
void ShapeAnnotation::moveCtrlLeft()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, -1, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(-1, 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1556,8 +1573,9 @@ void ShapeAnnotation::moveCtrlLeft()
*/
void ShapeAnnotation::moveRight()
{
qreal x = mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, x, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep(), 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1568,8 +1586,9 @@ void ShapeAnnotation::moveRight()
*/
void ShapeAnnotation::moveShiftRight()
{
qreal x = mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep() * 5;
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, x, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(mpGraphicsView->getCoOrdinateSystem()->getHorizontalGridStep() * 5, 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand All @@ -1580,7 +1599,9 @@ void ShapeAnnotation::moveShiftRight()
*/
void ShapeAnnotation::moveCtrlRight()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new MoveShapeKeyCommand(this, 1, 0, mpGraphicsView));
Transformation oldTransformation = mTransformation;
mTransformation.adjustPosition(1, 0);
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateShapeCommand(this, oldTransformation, mTransformation, mpGraphicsView));
}

/*!
Expand Down
115 changes: 18 additions & 97 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -77,131 +77,52 @@ void AddShapeCommand::undo()
mpGraphicsView->addClassAnnotation();
}

MoveShapeMouseCommand::MoveShapeMouseCommand(ShapeAnnotation *pShapeAnnotation, QPointF oldScenePos, QPointF newScenePos,
GraphicsView *pGraphicsView, QUndoCommand *pParent)
UpdateShapeCommand::UpdateShapeCommand(ShapeAnnotation *pShapeAnnotation, const Transformation &oldTransformation,
const Transformation &newTransformation, GraphicsView *pGraphicsView, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpShapeAnnotation = pShapeAnnotation;
mOldScenePosition = oldScenePos;
mNewScenePosition = newScenePos;
mOldTransformation = oldTransformation;
mNewTransformation = newTransformation;
mpGraphicsView = pGraphicsView;
}

/*!
* \brief MoveShapeMouseCommand::redo
* Redo the MoveShapeMouseCommand.
* \brief UpdateShapeCommand::redo
* Redo the UpdateShapeCommand.
*/
void MoveShapeMouseCommand::redo()
void UpdateShapeCommand::redo()
{
mpShapeAnnotation->mTransformation.setOrigin(mNewScenePosition);
bool state = mpShapeAnnotation->flags().testFlag(QGraphicsItem::ItemSendsGeometryChanges);
mpShapeAnnotation->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
mpShapeAnnotation->setPos(0, 0);
mpShapeAnnotation->setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
mpShapeAnnotation->setTransform(mpShapeAnnotation->mTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mpShapeAnnotation->mTransformation.getPosition());
mpShapeAnnotation->setTransform(mNewTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mNewTransformation.getPosition());
mpShapeAnnotation->setRotationAngle(mNewTransformation.getRotateAngle());
mpShapeAnnotation->mTransformation = mNewTransformation;
mpShapeAnnotation->emitChanged();
mpGraphicsView->setAddClassAnnotationNeeded(true);
}

/*!
* \brief MoveShapeMouseCommand::undo
* Undo the MoveShapeMouseCommand.
* \brief UpdateShapeCommand::undo
* Undo the UpdateShapeCommand.
*/
void MoveShapeMouseCommand::undo()
void UpdateShapeCommand::undo()
{
mpShapeAnnotation->mTransformation.setOrigin(mOldScenePosition);
bool state = mpShapeAnnotation->flags().testFlag(QGraphicsItem::ItemSendsGeometryChanges);
mpShapeAnnotation->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
mpShapeAnnotation->setPos(0, 0);
mpShapeAnnotation->setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
mpShapeAnnotation->setTransform(mpShapeAnnotation->mTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mpShapeAnnotation->mTransformation.getPosition());
mpShapeAnnotation->emitChanged();
mpGraphicsView->setAddClassAnnotationNeeded(true);
}

MoveShapeKeyCommand::MoveShapeKeyCommand(ShapeAnnotation *pShapeAnnotation, qreal x, qreal y, GraphicsView *pGraphicsView,
QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpShapeAnnotation = pShapeAnnotation;
mX = x;
mY = y;
mpGraphicsView = pGraphicsView;
}

/*!
* \brief MoveShapeKeyCommand::redo
* Redo the MoveShapeKeyCommand.
*/
void MoveShapeKeyCommand::redo()
{
mpShapeAnnotation->mTransformation.adjustPosition(mX, mY);
mpShapeAnnotation->setTransform(mpShapeAnnotation->mTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mpShapeAnnotation->mTransformation.getPosition());
mpShapeAnnotation->emitChanged();
mpGraphicsView->setAddClassAnnotationNeeded(true);
}

/*!
* \brief MoveShapeKeyCommand::undo
* Undo the MoveShapeKeyCommand.
*/
void MoveShapeKeyCommand::undo()
{
mpShapeAnnotation->mTransformation.adjustPosition(-mX, -mY);
mpShapeAnnotation->setTransform(mpShapeAnnotation->mTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mpShapeAnnotation->mTransformation.getPosition());
mpShapeAnnotation->setTransform(mOldTransformation.getTransformationMatrix());
mpShapeAnnotation->setOrigin(mOldTransformation.getPosition());
mpShapeAnnotation->setRotationAngle(mOldTransformation.getRotateAngle());
mpShapeAnnotation->mTransformation = mOldTransformation;
mpShapeAnnotation->emitChanged();
mpGraphicsView->setAddClassAnnotationNeeded(true);
}

RotateShapeCommand::RotateShapeCommand(ShapeAnnotation *pShapeAnnotation, bool clockwise, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpShapeAnnotation = pShapeAnnotation;
mClockwise = clockwise;
}

/*!
* \brief RotateShapeCommand::redo
* Redo the RotateShapeCommand.
*/
void RotateShapeCommand::redo()
{
if (mClockwise) {
qreal oldRotation = StringHandler::getNormalizedAngle(mpShapeAnnotation->mTransformation.getRotateAngle());
qreal rotateIncrement = -90;
qreal angle = oldRotation + rotateIncrement;
mpShapeAnnotation->applyRotation(angle);
} else {
qreal oldRotation = StringHandler::getNormalizedAngle(mpShapeAnnotation->mTransformation.getRotateAngle());
qreal rotateIncrement = 90;
qreal angle = oldRotation + rotateIncrement;
mpShapeAnnotation->applyRotation(angle);
}
}

/*!
* \brief RotateShapeCommand::undo
* Undo the RotateShapeCommand.
*/
void RotateShapeCommand::undo()
{
if (mClockwise) {
qreal oldRotation = StringHandler::getNormalizedAngle(mpShapeAnnotation->mTransformation.getRotateAngle());
qreal rotateIncrement = 90;
qreal angle = oldRotation + rotateIncrement;
mpShapeAnnotation->applyRotation(angle);
} else {
qreal oldRotation = StringHandler::getNormalizedAngle(mpShapeAnnotation->mTransformation.getRotateAngle());
qreal rotateIncrement = -90;
qreal angle = oldRotation + rotateIncrement;
mpShapeAnnotation->applyRotation(angle);
}
}

DeleteShapeCommand::DeleteShapeCommand(ShapeAnnotation *pShapeAnnotation, GraphicsView *pGraphicsView, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
Expand Down
34 changes: 5 additions & 29 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -47,44 +47,20 @@ class AddShapeCommand : public QUndoCommand
GraphicsView *mpGraphicsView;
};

class MoveShapeMouseCommand : public QUndoCommand
class UpdateShapeCommand : public QUndoCommand
{
public:
MoveShapeMouseCommand(ShapeAnnotation *pShapeAnnotation, QPointF oldScenePos, QPointF newScenePos, GraphicsView *pGraphicsView,
QUndoCommand *pParent = 0);
UpdateShapeCommand(ShapeAnnotation *pShapeAnnotation, const Transformation &oldTransformation, const Transformation &newTransformation,
GraphicsView *pGraphicsView, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
ShapeAnnotation *mpShapeAnnotation;
QPointF mOldScenePosition;
QPointF mNewScenePosition;
GraphicsView *mpGraphicsView;
};

class MoveShapeKeyCommand : public QUndoCommand
{
public:
MoveShapeKeyCommand(ShapeAnnotation *pShapeAnnotation, qreal x, qreal y, GraphicsView *pGraphicsView, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
ShapeAnnotation *mpShapeAnnotation;
qreal mX;
qreal mY;
Transformation mOldTransformation;
Transformation mNewTransformation;
GraphicsView *mpGraphicsView;
};

class RotateShapeCommand : public QUndoCommand
{
public:
RotateShapeCommand(ShapeAnnotation *pShapeAnnotation, bool clockwise, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
ShapeAnnotation *mpShapeAnnotation;
bool mClockwise;
};

class DeleteShapeCommand : public QUndoCommand
{
public:
Expand Down
8 changes: 5 additions & 3 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -1616,9 +1616,10 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
mpModelWidget->getUndoStack()->beginMacro("Move items by mouse");
beginMacro = true;
}
MoveShapeMouseCommand *pMoveShapeCommand = new MoveShapeMouseCommand(pShapeAnnotation, pShapeAnnotation->getOldScenePosition(),
pShapeAnnotation->scenePos(), this);
mpModelWidget->getUndoStack()->push(pMoveShapeCommand);
Transformation oldTransformation = pShapeAnnotation->mTransformation;
pShapeAnnotation->mTransformation.setOrigin(pShapeAnnotation->scenePos());
mpModelWidget->getUndoStack()->push(new UpdateShapeCommand(pShapeAnnotation, oldTransformation, pShapeAnnotation->mTransformation,
this));
hasShapeMoved = true;
}
}
Expand Down Expand Up @@ -1755,6 +1756,7 @@ void GraphicsView::keyPressEvent(QKeyEvent *event)
} else if (shiftModifier && !controlModifier && event->key() == Qt::Key_Right && isAnyItemSelectedAndEditable(event->key())) {
mpModelWidget->getUndoStack()->beginMacro("Move shift right by key press");
emit keyPressShiftRight();
mpModelWidget->getUndoStack()->endMacro();
} else if (!shiftModifier && controlModifier && event->key() == Qt::Key_Right && isAnyItemSelectedAndEditable(event->key())) {
mpModelWidget->getUndoStack()->beginMacro("Move control right by key press");
emit keyPressCtrlRight();
Expand Down

0 comments on commit c5ef284

Please sign in to comment.