Skip to content

Commit

Permalink
Allow to undo/redo component transformation changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Oct 22, 2015
1 parent 52fdcec commit d56f65a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
58 changes: 46 additions & 12 deletions OMEdit/OMEditGUI/Component/Component.cpp
Expand Up @@ -487,7 +487,7 @@ void Component::createResizerItems()
connect(mpBottomLeftResizerItem, SIGNAL(resizerItemPressed(ResizerItem*)), SLOT(prepareResizeComponent(ResizerItem*)));
connect(mpBottomLeftResizerItem, SIGNAL(resizerItemMoved(QPointF)), SLOT(resizeComponent(QPointF)));
connect(mpBottomLeftResizerItem, SIGNAL(resizerItemReleased()), SLOT(finishResizeComponent()));
connect(mpBottomLeftResizerItem, SIGNAL(resizerItemPositionChanged()), SIGNAL(transformHasChanged()));
connect(mpBottomLeftResizerItem, SIGNAL(resizerItemPositionChanged()), SLOT(resizedComponent()));
mpBottomLeftResizerItem->blockSignals(isSystemLibrary || isInheritedComponent());
//Top left resizer
mpTopLeftResizerItem = new ResizerItem(this);
Expand All @@ -496,7 +496,7 @@ void Component::createResizerItems()
connect(mpTopLeftResizerItem, SIGNAL(resizerItemPressed(ResizerItem*)), SLOT(prepareResizeComponent(ResizerItem*)));
connect(mpTopLeftResizerItem, SIGNAL(resizerItemMoved(QPointF)), SLOT(resizeComponent(QPointF)));
connect(mpTopLeftResizerItem, SIGNAL(resizerItemReleased()), SLOT(finishResizeComponent()));
connect(mpTopLeftResizerItem, SIGNAL(resizerItemPositionChanged()), SIGNAL(transformHasChanged()));
connect(mpTopLeftResizerItem, SIGNAL(resizerItemPositionChanged()), SLOT(resizedComponent()));
mpTopLeftResizerItem->blockSignals(isSystemLibrary || isInheritedComponent());
//Top Right resizer
mpTopRightResizerItem = new ResizerItem(this);
Expand All @@ -505,7 +505,7 @@ void Component::createResizerItems()
connect(mpTopRightResizerItem, SIGNAL(resizerItemPressed(ResizerItem*)), SLOT(prepareResizeComponent(ResizerItem*)));
connect(mpTopRightResizerItem, SIGNAL(resizerItemMoved(QPointF)), SLOT(resizeComponent(QPointF)));
connect(mpTopRightResizerItem, SIGNAL(resizerItemReleased()), SLOT(finishResizeComponent()));
connect(mpTopRightResizerItem, SIGNAL(resizerItemPositionChanged()), SIGNAL(transformHasChanged()));
connect(mpTopRightResizerItem, SIGNAL(resizerItemPositionChanged()), SLOT(resizedComponent()));
mpTopRightResizerItem->blockSignals(isSystemLibrary || isInheritedComponent());
//Bottom Right resizer
mpBottomRightResizerItem = new ResizerItem(this);
Expand All @@ -514,7 +514,7 @@ void Component::createResizerItems()
connect(mpBottomRightResizerItem, SIGNAL(resizerItemPressed(ResizerItem*)), SLOT(prepareResizeComponent(ResizerItem*)));
connect(mpBottomRightResizerItem, SIGNAL(resizerItemMoved(QPointF)), SLOT(resizeComponent(QPointF)));
connect(mpBottomRightResizerItem, SIGNAL(resizerItemReleased()), SLOT(finishResizeComponent()));
connect(mpBottomRightResizerItem, SIGNAL(resizerItemPositionChanged()), SIGNAL(transformHasChanged()));
connect(mpBottomRightResizerItem, SIGNAL(resizerItemPositionChanged()), SLOT(resizedComponent()));
mpBottomRightResizerItem->blockSignals(isSystemLibrary || isInheritedComponent());
}

Expand Down Expand Up @@ -1105,8 +1105,14 @@ void Component::referenceComponentDeleted()
}
}

/*!
* \brief Component::prepareResizeComponent
* Slot is activated when ResizerItem::resizerItemPressed() SIGNAL is raised.
* \param pResizerItem
*/
void Component::prepareResizeComponent(ResizerItem *pResizerItem)
{
mOldTransformation = mTransformation;
mpSelectedResizerItem = pResizerItem;
mTransform = transform();
mSceneBoundingRect = sceneBoundingRect();
Expand Down Expand Up @@ -1135,6 +1141,11 @@ void Component::prepareResizeComponent(ResizerItem *pResizerItem)
mpResizerRectangle->setPos(pos());
}

/*!
* \brief Component::resizeComponent
* Slot is activated when ResizerItem::resizerItemMoved() SIGNAL is raised.
* \param newPosition
*/
void Component::resizeComponent(QPointF newPosition)
{
float xDistance; //X distance between the current position of the mouse and the starting position mouse
Expand Down Expand Up @@ -1188,6 +1199,10 @@ void Component::resizeComponent(QPointF newPosition)
emit transformChange();
}

/*!
* \brief Component::finishResizeComponent
* Slot is activated when ResizerItem resizerItemReleased SIGNAL is raised.
*/
void Component::finishResizeComponent()
{
if (isSelected()) {
Expand All @@ -1197,6 +1212,17 @@ void Component::finishResizeComponent()
}
}

/*!
* \brief Component::resizedComponent
* Slot is activated when ResizerItem resizerItemPositionChanged SIGNAL is raised.
*/
void Component::resizedComponent()
{
UpdateComponentCommand *pUpdateComponentCommand = new UpdateComponentCommand(this, mOldTransformation, mTransformation, mpGraphicsView);
mpGraphicsView->getModelWidget()->getUndoStack()->push(pUpdateComponentCommand);
mpGraphicsView->getModelWidget()->updateModelicaText();
}

/*!
* \brief Component::deleteMe
* Deletes the Component from the current view.
Expand Down Expand Up @@ -1226,6 +1252,10 @@ void Component::duplicate()
}
}

/*!
* \brief Component::rotateClockwise
* Rotates the component clockwise.
*/
void Component::rotateClockwise()
{
qreal oldRotation = StringHandler::getNormalizedAngle(mTransformation.getRotateAngle());
Expand All @@ -1235,6 +1265,10 @@ void Component::rotateClockwise()
showResizerItems();
}

/*!
* \brief Component::rotateAntiClockwise
* Rotates the Component anti clockwise.
*/
void Component::rotateAntiClockwise()
{
qreal oldRotation = StringHandler::getNormalizedAngle(mTransformation.getRotateAngle());
Expand All @@ -1250,6 +1284,7 @@ void Component::rotateAntiClockwise()
*/
void Component::flipHorizontal()
{
Transformation oldTransformation = mTransformation;
setOriginAndExtents();
QPointF extent1 = mTransformation.getExtent1();
QPointF extent2 = mTransformation.getExtent2();
Expand All @@ -1262,8 +1297,7 @@ void Component::flipHorizontal()
mTransformation.setExtent1(QPointF(extent1.x(), extent2.y()));
mTransformation.setExtent2(QPointF(extent2.x(), extent1.y()));
}
setTransform(mTransformation.getTransformationMatrix());
emit transformHasChanged();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateComponentCommand(this, oldTransformation, mTransformation, mpGraphicsView));
showResizerItems();
}

Expand All @@ -1273,6 +1307,7 @@ void Component::flipHorizontal()
*/
void Component::flipVertical()
{
Transformation oldTransformation = mTransformation;
setOriginAndExtents();
QPointF extent1 = mTransformation.getExtent1();
QPointF extent2 = mTransformation.getExtent2();
Expand All @@ -1285,8 +1320,7 @@ void Component::flipVertical()
mTransformation.setExtent1(QPointF(extent2.x(), extent1.y()));
mTransformation.setExtent2(QPointF(extent1.x(), extent2.y()));
}
setTransform(mTransformation.getTransformationMatrix());
emit transformHasChanged();
mpGraphicsView->getModelWidget()->getUndoStack()->push(new UpdateComponentCommand(this, oldTransformation, mTransformation, mpGraphicsView));
showResizerItems();
}

Expand Down Expand Up @@ -1604,8 +1638,8 @@ QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
connect(mpGraphicsView->getDuplicateAction(), SIGNAL(triggered()), this, SLOT(duplicate()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(mouseRotateClockwise()), this, SLOT(rotateClockwise()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(mouseRotateAntiClockwise()), this, SLOT(rotateAntiClockwise()), Qt::UniqueConnection);
connect(mpGraphicsView->getFlipHorizontalAction(), SIGNAL(triggered()), this, SLOT(flipHorizontal()), Qt::UniqueConnection);
connect(mpGraphicsView->getFlipVerticalAction(), SIGNAL(triggered()), this, SLOT(flipVertical()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(mouseFlipHorizontal()), this, SLOT(flipHorizontal()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(mouseFlipVertical()), this, SLOT(flipVertical()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(keyPressDelete()), this, SLOT(deleteMe()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(keyPressDuplicate()), this, SLOT(duplicate()), Qt::UniqueConnection);
connect(mpGraphicsView, SIGNAL(keyPressRotateClockwise()), this, SLOT(rotateClockwise()), Qt::UniqueConnection);
Expand Down Expand Up @@ -1639,8 +1673,8 @@ QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
disconnect(mpGraphicsView->getDuplicateAction(), SIGNAL(triggered()), this, SLOT(duplicate()));
disconnect(mpGraphicsView, SIGNAL(mouseRotateClockwise()), this, SLOT(rotateClockwise()));
disconnect(mpGraphicsView, SIGNAL(mouseRotateAntiClockwise()), this, SLOT(rotateAntiClockwise()));
disconnect(mpGraphicsView->getFlipHorizontalAction(), SIGNAL(triggered()), this, SLOT(flipHorizontal()));
disconnect(mpGraphicsView->getFlipVerticalAction(), SIGNAL(triggered()), this, SLOT(flipVertical()));
disconnect(mpGraphicsView, SIGNAL(mouseFlipHorizontal()), this, SLOT(flipHorizontal()));
disconnect(mpGraphicsView, SIGNAL(mouseFlipVertical()), this, SLOT(flipVertical()));
disconnect(mpGraphicsView, SIGNAL(keyPressDelete()), this, SLOT(deleteMe()));
disconnect(mpGraphicsView, SIGNAL(keyPressDuplicate()), this, SLOT(duplicate()));
disconnect(mpGraphicsView, SIGNAL(keyPressRotateClockwise()), this, SLOT(rotateClockwise()));
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Component/Component.h
Expand Up @@ -190,6 +190,7 @@ class Component : public QObject, public QGraphicsItem
void renameInterfacePoint(TLMInterfacePointInfo *pTLMInterfacePointInfo, QString interfacePoint);

Transformation mTransformation;
Transformation mOldTransformation;
private:
Component *mpReferenceComponent;
Component *mpParentComponent;
Expand Down Expand Up @@ -248,6 +249,7 @@ public slots:
void prepareResizeComponent(ResizerItem *pResizerItem);
void resizeComponent(QPointF newPosition);
void finishResizeComponent();
void resizedComponent();
void deleteMe();
void duplicate();
void rotateClockwise();
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -176,7 +176,7 @@ AddComponentCommand::AddComponentCommand(QString name, LibraryTreeItem *pLibrary
mpIconGraphicsView = pGraphicsView->getModelWidget()->getIconGraphicsView();
mpDiagramGraphicsView = pGraphicsView->getModelWidget()->getDiagramGraphicsView();
mpGraphicsView = pGraphicsView;
setText(QString("Added Component %1").arg(name));
setText(QString("Add Component %1").arg(name));

ModelWidget *pModelWidget = mpGraphicsView->getModelWidget();
// if component is of connector type && containing class is Modelica type.
Expand Down Expand Up @@ -277,6 +277,7 @@ UpdateComponentCommand::UpdateComponentCommand(Component *pComponent, const Tran
mOldTransformation = oldTransformation;
mNewTransformation = newTransformation;
mpGraphicsView = pGraphicsView;
setText(QString("Update Component %1").arg(mpComponent->getName()));
}

/*!
Expand Down
28 changes: 28 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -1020,10 +1020,12 @@ void GraphicsView::createActions()
mpFlipHorizontalAction = new QAction(QIcon(":/Resources/icons/flip-horizontal.svg"), tr("Flip Horizontal"), this);
mpFlipHorizontalAction->setStatusTip(tr("Flips the item horizontally"));
mpFlipHorizontalAction->setDisabled(isSystemLibrary);
connect(mpFlipHorizontalAction, SIGNAL(triggered()), SLOT(flipHorizontal()));
// Flip Vertical Action
mpFlipVerticalAction = new QAction(QIcon(":/Resources/icons/flip-vertical.svg"), tr("Flip Vertical"), this);
mpFlipVerticalAction->setStatusTip(tr("Flips the item vertically"));
mpFlipVerticalAction->setDisabled(isSystemLibrary);
connect(mpFlipVerticalAction, SIGNAL(triggered()), SLOT(flipVertical()));
}

/*!
Expand Down Expand Up @@ -1352,6 +1354,32 @@ void GraphicsView::rotateAntiClockwise()
mpModelWidget->getUndoStack()->endMacro();
}

/*!
* \brief GraphicsView::flipHorizontal
* Flips the selected items horizontally emitting GraphicsView::mouseFlipHorizontal() SIGNAL.
*/
void GraphicsView::flipHorizontal()
{
mpModelWidget->getUndoStack()->beginMacro("Flip horizontal by mouse");
emit mouseFlipHorizontal();
mpModelWidget->updateClassAnnotationIfNeeded();
mpModelWidget->updateModelicaText();
mpModelWidget->getUndoStack()->endMacro();
}

/*!
* \brief GraphicsView::flipVertical
* Flips the selected items vertically emitting GraphicsView::mouseFlipVertical() SIGNAL.
*/
void GraphicsView::flipVertical()
{
mpModelWidget->getUndoStack()->beginMacro("Flip vertical by mouse");
emit mouseFlipVertical();
mpModelWidget->updateClassAnnotationIfNeeded();
mpModelWidget->updateModelicaText();
mpModelWidget->getUndoStack()->endMacro();
}

/*!
* \brief GraphicsView::dragMoveEvent
* Defines what happens when dragged and moved an object in a GraphicsView.
Expand Down
4 changes: 4 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -233,6 +233,8 @@ class GraphicsView : public QGraphicsView
void mouseDelete();
void mouseRotateClockwise();
void mouseRotateAntiClockwise();
void mouseFlipHorizontal();
void mouseFlipVertical();
void keyPressDelete();
void keyPressRotateClockwise();
void keyPressRotateAntiClockwise();
Expand Down Expand Up @@ -264,6 +266,8 @@ public slots:
void deleteItems();
void rotateClockwise();
void rotateAntiClockwise();
void flipHorizontal();
void flipVertical();
protected:
virtual void dragMoveEvent(QDragMoveEvent *event);
virtual void dropEvent(QDropEvent *event);
Expand Down

0 comments on commit d56f65a

Please sign in to comment.