Skip to content

Commit

Permalink
Allow undo/redo for CoOrdinateSystem changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Nov 13, 2015
1 parent 9defa27 commit 0d17a04
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 45 deletions.
1 change: 0 additions & 1 deletion OMEdit/OMEditGUI/Modeling/CoOrdinateSystem.cpp
Expand Up @@ -85,4 +85,3 @@ qreal CoOrdinateSystem::getVerticalGridStep()
}
return mGrid.y();
}

69 changes: 69 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -904,3 +904,72 @@ void UpdateClassExperimentAnnotationCommand::undo()
{
mpMainWindow->getOMCProxy()->addClassAnnotation(mpLibraryTreeItem->getNameStructure(), mOldExperimentAnnotation);
}

UpdateCoOrdinateSystemCommand::UpdateCoOrdinateSystemCommand(GraphicsView *pGraphicsView, CoOrdinateSystem oldCoOrdinateSystem,
CoOrdinateSystem newCoOrdinateSystem, bool copyProperties, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpGraphicsView = pGraphicsView;
mOldCoOrdinateSystem = oldCoOrdinateSystem;
mNewCoOrdinateSystem = newCoOrdinateSystem;
mCopyProperties = copyProperties;
setText(QString("Update %1 CoOrdinate System").arg(mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getNameStructure()));
}

/*!
* \brief UpdateClassCoOrdinateSystemCommand::redo
* Redo the UpdateClassCoOrdinateSystemCommand.
*/
void UpdateCoOrdinateSystemCommand::redo()
{
mpGraphicsView->mCoOrdinateSystem = mNewCoOrdinateSystem;
qreal left = mNewCoOrdinateSystem.getExtent().at(0).x();
qreal bottom = mNewCoOrdinateSystem.getExtent().at(0).y();
qreal right = mNewCoOrdinateSystem.getExtent().at(1).x();
qreal top = mNewCoOrdinateSystem.getExtent().at(1).y();
mpGraphicsView->setExtentRectangle(left, bottom, right, top);
mpGraphicsView->addClassAnnotation();
mpGraphicsView->scene()->update();
// if copy properties is true
if (mCopyProperties) {
GraphicsView *pGraphicsView;
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
pGraphicsView = mpGraphicsView->getModelWidget()->getDiagramGraphicsView();
} else {
pGraphicsView = mpGraphicsView->getModelWidget()->getIconGraphicsView();
}
pGraphicsView->mCoOrdinateSystem = mNewCoOrdinateSystem;
pGraphicsView->setExtentRectangle(left, bottom, right, top);
pGraphicsView->addClassAnnotation();
pGraphicsView->scene()->update();
}
}

/*!
* \brief UpdateClassCoOrdinateSystemCommand::undo
* Undo the UpdateClassCoOrdinateSystemCommand.
*/
void UpdateCoOrdinateSystemCommand::undo()
{
mpGraphicsView->mCoOrdinateSystem = mOldCoOrdinateSystem;
qreal left = mOldCoOrdinateSystem.getExtent().at(0).x();
qreal bottom = mOldCoOrdinateSystem.getExtent().at(0).y();
qreal right = mOldCoOrdinateSystem.getExtent().at(1).x();
qreal top = mOldCoOrdinateSystem.getExtent().at(1).y();
mpGraphicsView->setExtentRectangle(left, bottom, right, top);
mpGraphicsView->addClassAnnotation();
mpGraphicsView->scene()->update();
// if copy properties is true
if (mCopyProperties) {
GraphicsView *pGraphicsView;
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
pGraphicsView = mpGraphicsView->getModelWidget()->getDiagramGraphicsView();
} else {
pGraphicsView = mpGraphicsView->getModelWidget()->getIconGraphicsView();
}
pGraphicsView->mCoOrdinateSystem = mOldCoOrdinateSystem;
pGraphicsView->setExtentRectangle(left, bottom, right, top);
pGraphicsView->addClassAnnotation();
pGraphicsView->scene()->update();
}
}
14 changes: 14 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -183,6 +183,20 @@ class DeleteConnectionCommand : public QUndoCommand
GraphicsView *mpGraphicsView;
};

class UpdateCoOrdinateSystemCommand : public QUndoCommand
{
public:
UpdateCoOrdinateSystemCommand(GraphicsView *pGraphicsView, CoOrdinateSystem oldCoOrdinateSystem, CoOrdinateSystem newCoOrdinateSystem,
bool copyProperties, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
GraphicsView *mpGraphicsView;
CoOrdinateSystem mOldCoOrdinateSystem;
CoOrdinateSystem mNewCoOrdinateSystem;
bool mCopyProperties;
};

class UpdateClassExperimentAnnotationCommand : public QUndoCommand
{
public:
Expand Down
18 changes: 9 additions & 9 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -1204,9 +1204,12 @@ void GraphicsView::clearSelection()
}
}

//! Adds the annotation string of Icon and Diagram layer to the model. Also creates the model icon in the tree.
//! If some custom models are cross referenced then update them accordingly.
void GraphicsView::addClassAnnotation(bool updateModelicaText)
/*!
* \brief GraphicsView::addClassAnnotation
* Adds the annotation string of Icon and Diagram layer to the model. Also creates the model icon in the tree.
* If some custom models are cross referenced then update them accordingly.
*/
void GraphicsView::addClassAnnotation()
{
if (mpModelWidget->getLibraryTreeItem()->isSystemLibrary()) {
return;
Expand Down Expand Up @@ -1260,9 +1263,6 @@ void GraphicsView::addClassAnnotation(bool updateModelicaText)
}
// add the class annotation to model through OMC
if (pMainWindow->getOMCProxy()->addClassAnnotation(mpModelWidget->getLibraryTreeItem()->getNameStructure(), annotationString)) {
if (updateModelicaText) {
mpModelWidget->updateModelicaText();
}
/* When something is added/changed in the icon layer then update the LibraryTreeItem in the Library Browser */
if (mViewType == StringHandler::Icon) {
mpModelWidget->getLibraryTreeItem()->handleIconUpdated();
Expand Down Expand Up @@ -1679,7 +1679,7 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
}
}
if (hasShapeMoved) {
addClassAnnotation(!hasComponentMoved);
addClassAnnotation();
}
if (hasComponentMoved || hasShapeMoved) {
mpModelWidget->updateModelicaText();
Expand Down Expand Up @@ -2787,11 +2787,11 @@ void ModelWidget::clearSelection()
void ModelWidget::updateClassAnnotationIfNeeded()
{
if (mpIconGraphicsView && mpIconGraphicsView->isAddClassAnnotationNeeded()) {
mpIconGraphicsView->addClassAnnotation(false);
mpIconGraphicsView->addClassAnnotation();
mpIconGraphicsView->setAddClassAnnotationNeeded(false);
}
if (mpDiagramGraphicsView && mpDiagramGraphicsView->isAddClassAnnotationNeeded()) {
mpDiagramGraphicsView->addClassAnnotation(false);
mpDiagramGraphicsView->addClassAnnotation();
mpDiagramGraphicsView->setAddClassAnnotationNeeded(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -239,7 +239,7 @@ public slots:
void zoomOut();
void selectAll();
void clearSelection();
void addClassAnnotation(bool updateModelicaText = true);
void addClassAnnotation();
void showGraphicsViewProperties();
void manhattanizeItems();
void deleteItems();
Expand Down
62 changes: 28 additions & 34 deletions OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -41,6 +41,7 @@
#include "ModelicaClassDialog.h"
#include "StringHandler.h"
#include "ModelWidgetContainer.h"
#include "Commands.h"

LibraryBrowseDialog::LibraryBrowseDialog(QString title, QLineEdit *pLineEdit, LibraryWidget *pLibraryWidget)
: QDialog(0, Qt::WindowTitleHint)
Expand Down Expand Up @@ -360,8 +361,8 @@ void ModelicaClassDialog::createModelicaClass()
// show the ModelWidget
pLibraryTreeModel->showModelWidget(pLibraryTreeItem, "", true);
if (pLibraryTreeItem->getModelWidget()) {
pLibraryTreeItem->getModelWidget()->getIconGraphicsView()->addClassAnnotation(false);
pLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->addClassAnnotation(false);
pLibraryTreeItem->getModelWidget()->getIconGraphicsView()->addClassAnnotation();
pLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->addClassAnnotation();
pLibraryTreeItem->getModelWidget()->updateModelicaText();
}
accept();
Expand Down Expand Up @@ -961,13 +962,13 @@ void InformationDialog::closeEvent(QCloseEvent *event)
}

/*!
\class GraphicsViewProperties
\brief Creates a dialog that shows the icon/diagram GraphicsView properties.
*/

* \class GraphicsViewProperties
* \brief Creates a dialog that shows the icon/diagram GraphicsView properties.
*/
/*!
\param pGraphicsView - pointer to GraphicsView
*/
* \brief GraphicsViewProperties::GraphicsViewProperties
* \param pGraphicsView - pointer to GraphicsView
*/
GraphicsViewProperties::GraphicsViewProperties(GraphicsView *pGraphicsView)
: QDialog(pGraphicsView, Qt::WindowTitleHint)
{
Expand Down Expand Up @@ -1049,17 +1050,19 @@ GraphicsViewProperties::GraphicsViewProperties(GraphicsView *pGraphicsView)
mpComponentGroupBox->setLayout(pComponentLayout);
// copy properties check box
mpCopyProperties = new QCheckBox;
if (mpGraphicsView->getViewType() == StringHandler::Icon)
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
mpCopyProperties->setText(tr("Copy properties to Diagram layer"));
else
} else {
mpCopyProperties->setText(tr("Copy properties to Icon layer"));
}
mpCopyProperties->setChecked(true);
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(saveGraphicsViewProperties()));
if (mpGraphicsView->getModelWidget()->getLibraryTreeItem()->isSystemLibrary())
if (mpGraphicsView->getModelWidget()->getLibraryTreeItem()->isSystemLibrary()) {
mpOkButton->setDisabled(true);
}
mpCancelButton = new QPushButton(Helper::cancel);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
// create buttons box
Expand All @@ -1082,37 +1085,28 @@ GraphicsViewProperties::GraphicsViewProperties(GraphicsView *pGraphicsView)
*/
void GraphicsViewProperties::saveGraphicsViewProperties()
{
// save the old CoOrdinateSystem
CoOrdinateSystem oldCoOrdinateSystem = mpGraphicsView->mCoOrdinateSystem;
// construct a new CoOrdinateSystem
CoOrdinateSystem newCoOrdinateSystem;
qreal left = qMin(mpLeftSpinBox->value(), mpRightSpinBox->value());
qreal bottom = qMin(mpBottomSpinBox->value(), mpTopSpinBox->value());
qreal right = qMax(mpLeftSpinBox->value(), mpRightSpinBox->value());
qreal top = qMax(mpBottomSpinBox->value(), mpTopSpinBox->value());
QList<QPointF> extent;
extent << QPointF(left, bottom) << QPointF(right, top);
mpGraphicsView->mCoOrdinateSystem.setExtent(extent);
mpGraphicsView->mCoOrdinateSystem.setPreserveAspectRatio(mpPreserveAspectRatioCheckBox->isChecked());
mpGraphicsView->mCoOrdinateSystem.setInitialScale(mpScaleFactorSpinBox->value());
newCoOrdinateSystem.setExtent(extent);
newCoOrdinateSystem.setPreserveAspectRatio(mpPreserveAspectRatioCheckBox->isChecked());
newCoOrdinateSystem.setInitialScale(mpScaleFactorSpinBox->value());
qreal horizontal = mpHorizontalSpinBox->value();
qreal vertical = mpVerticalSpinBox->value();
mpGraphicsView->mCoOrdinateSystem.setGrid(QPointF(horizontal, vertical));
mpGraphicsView->setExtentRectangle(left, bottom, right, top);
mpGraphicsView->resize(mpGraphicsView->size());
mpGraphicsView->addClassAnnotation();
// if copy properties is true
if (mpCopyProperties->isChecked()) {
GraphicsView *pGraphicsView;
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
pGraphicsView = mpGraphicsView->getModelWidget()->getDiagramGraphicsView();
} else {
pGraphicsView = mpGraphicsView->getModelWidget()->getIconGraphicsView();
}
pGraphicsView->mCoOrdinateSystem.setExtent(extent);
pGraphicsView->mCoOrdinateSystem.setPreserveAspectRatio(mpPreserveAspectRatioCheckBox->isChecked());
pGraphicsView->mCoOrdinateSystem.setInitialScale(mpScaleFactorSpinBox->value());
pGraphicsView->mCoOrdinateSystem.setGrid(QPointF(horizontal, vertical));
pGraphicsView->setExtentRectangle(left, bottom, right, top);
pGraphicsView->resize(pGraphicsView->size());
pGraphicsView->addClassAnnotation();
}
newCoOrdinateSystem.setGrid(QPointF(horizontal, vertical));
// push the CoOrdinateSystem change to undo stack
UpdateCoOrdinateSystemCommand *pUpdateCoOrdinateSystemCommand = new UpdateCoOrdinateSystemCommand(mpGraphicsView, oldCoOrdinateSystem,
newCoOrdinateSystem,
mpCopyProperties->isChecked());
mpGraphicsView->getModelWidget()->getUndoStack()->push(pUpdateCoOrdinateSystemCommand);
mpGraphicsView->getModelWidget()->updateModelicaText();
accept();
}

Expand Down

0 comments on commit 0d17a04

Please sign in to comment.