Skip to content

Commit

Permalink
Allow deleting the submodel icon
Browse files Browse the repository at this point in the history
Restrict few actions on the submodel signals.
Respective context menus
  • Loading branch information
adeas31 committed May 28, 2018
1 parent 04d958a commit f545a43
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 68 deletions.
22 changes: 21 additions & 1 deletion OMEdit/OMEditGUI/Annotations/ShapeAnnotation.cpp
Expand Up @@ -446,6 +446,10 @@ void ShapeAnnotation::createActions()
mpEditTransitionAction = new QAction(Helper::editTransition, mpGraphicsView);
mpEditTransitionAction->setStatusTip(tr("Edits the transition"));
connect(mpEditTransitionAction, SIGNAL(triggered()), SLOT(editTransition()));
// delete submodel icon action
mpDeleteSubModelIconAction = new QAction(QIcon(":/Resources/icons/delete.svg"), tr("Delete SubModel Icon"), mpGraphicsView);
mpDeleteSubModelIconAction->setStatusTip(tr("Deletes the submodel icon"));
connect(mpDeleteSubModelIconAction, SIGNAL(triggered()), SLOT(deleteSubModelIcon()));
}

/*!
Expand Down Expand Up @@ -1594,6 +1598,16 @@ void ShapeAnnotation::editTransition()
pCreateOrEditTransitionDialog->exec();
}

/*!
* \brief ShapeAnnotation::deleteSubModelIcon
* Slot activated when delete submodel icon option is chosen from context menu of the shape.
*/
void ShapeAnnotation::deleteSubModelIcon()
{
mpGraphicsView->getModelWidget()->getUndoStack()->push(new DeleteSubModelIconCommand(mOriginalFileName, mpGraphicsView));
mpGraphicsView->getModelWidget()->updateModelText();
}

/*!
* \brief ShapeAnnotation::alignInterfaces
* Slot activated when Align Interfaces option is chosen from context menu of the shape.
Expand Down Expand Up @@ -1680,7 +1694,13 @@ void ShapeAnnotation::contextMenuEvent(QGraphicsSceneContextMenuEvent *pEvent)
menu.addAction(mpEditTransitionAction);
}
} else if (mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::OMS) {
return;
BitmapAnnotation *pBitmapAnnotation = dynamic_cast<BitmapAnnotation*>(this);
if (pBitmapAnnotation && mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getOMSElement()
&& mpGraphicsView->getModelWidget()->getLibraryTreeItem()->getOMSElement()->type == oms_component_fmu) {
menu.addAction(mpDeleteSubModelIconAction);
} else {
return;
}
}
menu.exec(pEvent->screenPos());
}
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Annotations/ShapeAnnotation.h
Expand Up @@ -113,6 +113,7 @@ class ShapeAnnotation : public QObject, public QGraphicsItem, public GraphicItem
QAction *mpAlignInterfacesAction;
QAction *mpShapeAttributesAction;
QAction *mpEditTransitionAction;
QAction *mpDeleteSubModelIconAction;
public:
enum LineGeometryType {VerticalLine, HorizontalLine};
Transformation mTransformation;
Expand Down Expand Up @@ -228,6 +229,7 @@ public slots:
void alignInterfaces();
void showShapeAttributes();
void editTransition();
void deleteSubModelIcon();
void manhattanizeShape(bool addToStack = true);
void referenceShapeAdded();
void referenceShapeChanged();
Expand Down
80 changes: 52 additions & 28 deletions OMEdit/OMEditGUI/Component/Component.cpp
Expand Up @@ -1657,15 +1657,7 @@ void Component::drawOMSComponent()
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->showModelWidget(mpLibraryTreeItem, false);
}
// draw shapes first
foreach (ShapeAnnotation *pShapeAnnotation, mpLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getShapesList()) {
if (dynamic_cast<RectangleAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new RectangleAnnotation(pShapeAnnotation, this));
} else if (dynamic_cast<TextAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new TextAnnotation(pShapeAnnotation, this));
} else if (dynamic_cast<BitmapAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new BitmapAnnotation(pShapeAnnotation, this));
}
}
drawOMSComponentShapes();
// draw components now
foreach (Component *pComponent, mpLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getComponentsList()) {
Component *pNewComponent = new Component(pComponent, this, getRootParentComponent());
Expand All @@ -1684,6 +1676,23 @@ void Component::drawOMSComponent()
}
}

/*!
* \brief Component::drawOMSComponentShapes
* Draws the shapes for OMSimulator component.
*/
void Component::drawOMSComponentShapes()
{
foreach (ShapeAnnotation *pShapeAnnotation, mpLibraryTreeItem->getModelWidget()->getDiagramGraphicsView()->getShapesList()) {
if (dynamic_cast<RectangleAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new RectangleAnnotation(pShapeAnnotation, this));
} else if (dynamic_cast<TextAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new TextAnnotation(pShapeAnnotation, this));
} else if (dynamic_cast<BitmapAnnotation*>(pShapeAnnotation)) {
mShapesList.append(new BitmapAnnotation(pShapeAnnotation, this));
}
}
}

/*!
* \brief Component::drawComponent
* Draws the Component.
Expand Down Expand Up @@ -2158,22 +2167,25 @@ void Component::updatePlacementAnnotation()
getTransformationOrigin(), getTransformationExtent(),
QString::number(mTransformation.getRotateAngle()));
} else if (pLibraryTreeItem->getLibraryType()== LibraryTreeItem::OMS) {
ssd_element_geometry_t elementGeometry;
QPointF extent1 = mTransformation.getExtent1();
QPointF extent2 = mTransformation.getExtent2();
if (mTransformation.hasOrigin()) {
extent1.setX(extent1.x() + mTransformation.getOrigin().x());
extent1.setY(extent1.y() + mTransformation.getOrigin().y());
extent2.setX(extent2.x() + mTransformation.getOrigin().x());
extent2.setY(extent2.y() + mTransformation.getOrigin().y());
if (mpLibraryTreeItem && mpLibraryTreeItem->getOMSElement()) {
ssd_element_geometry_t *pElementGeometry = mpLibraryTreeItem->getOMSElement()->geometry;
QPointF extent1 = mTransformation.getExtent1();
QPointF extent2 = mTransformation.getExtent2();
if (mTransformation.hasOrigin()) {
extent1.setX(extent1.x() + mTransformation.getOrigin().x());
extent1.setY(extent1.y() + mTransformation.getOrigin().y());
extent2.setX(extent2.x() + mTransformation.getOrigin().x());
extent2.setY(extent2.y() + mTransformation.getOrigin().y());
}
pElementGeometry->x1 = extent1.x();
pElementGeometry->y1 = extent1.y();
pElementGeometry->x2 = extent2.x();
pElementGeometry->y2 = extent2.y();
pElementGeometry->rotation = mTransformation.getRotateAngle();
OMSProxy::instance()->setElementGeometry(mpLibraryTreeItem->getNameStructure(), pElementGeometry);
} else {
qDebug() << "Component::updatePlacementAnnotation() expected to have the OMS Element.";
}
elementGeometry.x1 = extent1.x();
elementGeometry.y1 = extent1.y();
elementGeometry.x2 = extent2.x();
elementGeometry.y2 = extent2.y();
elementGeometry.rotation = mTransformation.getRotateAngle();
elementGeometry.iconSource = NULL;
OMSProxy::instance()->setElementGeometry(mpLibraryTreeItem->getNameStructure(), &elementGeometry);
} else {
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
pOMCProxy->updateComponent(mpComponentInfo->getName(), mpComponentInfo->getClassName(),
Expand Down Expand Up @@ -2231,10 +2243,22 @@ void Component::handleUnloaded()
void Component::handleShapeAdded()
{
Component *pComponent = getRootParentComponent();
pComponent->removeChildren();
pComponent->drawComponent();
pComponent->emitChanged();
pComponent->updateConnections();
if (mpLibraryTreeItem && mpLibraryTreeItem->getLibraryType() == LibraryTreeItem::OMS) {
// remove all shapes
foreach (ShapeAnnotation *pShapeAnnotation, mShapesList) {
pShapeAnnotation->setParentItem(0);
mpGraphicsView->removeItem(pShapeAnnotation);
delete pShapeAnnotation;
}
mShapesList.clear();
// draw shapes
pComponent->drawOMSComponentShapes();
} else {
pComponent->removeChildren();
pComponent->drawComponent();
pComponent->emitChanged();
pComponent->updateConnections();
}
}

void Component::handleComponentAdded()
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Component/Component.h
Expand Up @@ -302,6 +302,7 @@ class Component : public QObject, public QGraphicsItem
void createStateComponent();
void drawInterfacePoints();
void drawOMSComponent();
void drawOMSComponentShapes();
void drawComponent();
void drawInheritedComponentsAndShapes();
void showNonExistingOrDefaultComponentIfNeeded();
Expand Down
86 changes: 86 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -2060,6 +2060,7 @@ void AddSubModelIconCommand::redo()
mpGraphicsView->addShapeToList(pBitmapAnnotation);
mpGraphicsView->addItem(pBitmapAnnotation);
pElementLibraryTreeItem->handleIconUpdated();
pElementLibraryTreeItem->emitShapeAdded(pBitmapAnnotation, mpGraphicsView);
}
}
}
Expand Down Expand Up @@ -2099,6 +2100,7 @@ void AddSubModelIconCommand::undo()
mpGraphicsView->addShapeToList(pTextAnnotation);
mpGraphicsView->addItem(pTextAnnotation);
pElementLibraryTreeItem->handleIconUpdated();
pElementLibraryTreeItem->emitShapeAdded(pRectangleAnnotation, mpGraphicsView);
}
}
}
Expand Down Expand Up @@ -2169,3 +2171,87 @@ void UpdateSubModelIconCommand::undo()
}
}
}

DeleteSubModelIconCommand::DeleteSubModelIconCommand(QString icon, GraphicsView *pGraphicsView, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mIcon = icon;
mpGraphicsView = pGraphicsView;
setText(QString("Delete SubModel Icon"));
}

/*!
* \brief DeleteSubModelIconCommand::redo
* Redo the DeleteSubModelIconCommand
*/
void DeleteSubModelIconCommand::redo()
{
// update element ssd_element_geometry_t
LibraryTreeItem *pElementLibraryTreeItem = mpGraphicsView->getModelWidget()->getLibraryTreeItem();
if (pElementLibraryTreeItem && pElementLibraryTreeItem->getOMSElement() && pElementLibraryTreeItem->getOMSElement()->geometry) {
ssd_element_geometry_t *pElementGeometry = pElementLibraryTreeItem->getOMSElement()->geometry;
if (pElementGeometry->iconSource) {
delete[] pElementGeometry->iconSource;
}
pElementGeometry->iconSource = NULL;
if (OMSProxy::instance()->setElementGeometry(pElementLibraryTreeItem->getNameStructure(), pElementGeometry)) {
// clear all shapes of the submodel first
foreach (ShapeAnnotation *pShapeAnnotation, mpGraphicsView->getShapesList()) {
mpGraphicsView->deleteShapeFromList(pShapeAnnotation);
mpGraphicsView->removeItem(pShapeAnnotation);
}
// Rectangle shape as base
RectangleAnnotation *pRectangleAnnotation = new RectangleAnnotation(mpGraphicsView);
pRectangleAnnotation->initializeTransformation();
pRectangleAnnotation->drawCornerItems();
pRectangleAnnotation->setCornerItemsActiveOrPassive();
mpGraphicsView->addShapeToList(pRectangleAnnotation);
mpGraphicsView->addItem(pRectangleAnnotation);
// Text for name
TextAnnotation *pTextAnnotation = new TextAnnotation(mpGraphicsView);
pTextAnnotation->initializeTransformation();
pTextAnnotation->drawCornerItems();
pTextAnnotation->setCornerItemsActiveOrPassive();
mpGraphicsView->addShapeToList(pTextAnnotation);
mpGraphicsView->addItem(pTextAnnotation);
pElementLibraryTreeItem->handleIconUpdated();
pElementLibraryTreeItem->emitShapeAdded(pRectangleAnnotation, mpGraphicsView);
}
}
}

/*!
* \brief DeleteSubModelIconCommand::undo
* Undo the DeleteSubModelIconCommand
*/
void DeleteSubModelIconCommand::undo()
{
// update element ssd_element_geometry_t
LibraryTreeItem *pElementLibraryTreeItem = mpGraphicsView->getModelWidget()->getLibraryTreeItem();
if (pElementLibraryTreeItem && pElementLibraryTreeItem->getOMSElement() && pElementLibraryTreeItem->getOMSElement()->geometry) {
ssd_element_geometry_t *pElementGeometry = pElementLibraryTreeItem->getOMSElement()->geometry;
QString fileURI = "file:///" + mIcon;
if (pElementGeometry->iconSource) {
delete[] pElementGeometry->iconSource;
}
size_t size = fileURI.toStdString().size() + 1;
pElementGeometry->iconSource = new char[size];
memcpy(pElementGeometry->iconSource, fileURI.toStdString().c_str(), size*sizeof(char));
if (OMSProxy::instance()->setElementGeometry(pElementLibraryTreeItem->getNameStructure(), pElementGeometry)) {
// clear all shapes of the submodel first
foreach (ShapeAnnotation *pShapeAnnotation, mpGraphicsView->getShapesList()) {
mpGraphicsView->deleteShapeFromList(pShapeAnnotation);
mpGraphicsView->removeItem(pShapeAnnotation);
}
// Add new bitmap shape
BitmapAnnotation *pBitmapAnnotation = new BitmapAnnotation(mIcon, mpGraphicsView);
pBitmapAnnotation->initializeTransformation();
pBitmapAnnotation->drawCornerItems();
pBitmapAnnotation->setCornerItemsActiveOrPassive();
mpGraphicsView->addShapeToList(pBitmapAnnotation);
mpGraphicsView->addItem(pBitmapAnnotation);
pElementLibraryTreeItem->handleIconUpdated();
pElementLibraryTreeItem->emitShapeAdded(pBitmapAnnotation, mpGraphicsView);
}
}
}
11 changes: 11 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -465,4 +465,15 @@ class UpdateSubModelIconCommand : public QUndoCommand
ShapeAnnotation *mpShapeAnnotation;
};

class DeleteSubModelIconCommand : public QUndoCommand
{
public:
DeleteSubModelIconCommand(QString icon, GraphicsView *pGraphicsView, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
QString mIcon;
GraphicsView *mpGraphicsView;
};

#endif // COMMANDS_H
59 changes: 25 additions & 34 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -3025,16 +3025,15 @@ void LibraryTreeView::createActions()
mpTLMCoSimulationAction = new QAction(QIcon(":/Resources/icons/tlm-simulate.svg"), Helper::tlmCoSimulationSetup, this);
mpTLMCoSimulationAction->setStatusTip(Helper::tlmCoSimulationSetupTip);
connect(mpTLMCoSimulationAction, SIGNAL(triggered()), SLOT(TLMSimulate()));
// simulate OMSimulator model action
mpSimulateOMSModelAction = new QAction(QIcon(":/Resources/icons/tlm-simulate.svg"), Helper::simulate, this);
mpSimulateOMSModelAction->setStatusTip(Helper::OMSSimulationSetupTip);
mpSimulateOMSModelAction->setEnabled(false);
connect(mpSimulateOMSModelAction, SIGNAL(triggered(bool)), SLOT(simulateOMSModel()));
// rename OMSimulator model Action
mpRenameOMSModelAction = new QAction(Helper::rename, this);
mpRenameOMSModelAction->setStatusTip(Helper::renameOMSModelTip);
mpRenameOMSModelAction->setEnabled(false);
connect(mpRenameOMSModelAction, SIGNAL(triggered()), SLOT(renameOMSModel()));
// OMSimulator simulation setup action
mpOMSSimulationSetupAction = new QAction(QIcon(":/Resources/icons/tlm-simulate.svg"), Helper::OMSSimulationSetup, this);
mpOMSSimulationSetupAction->setStatusTip(Helper::OMSSimulationSetupTip);
connect(mpOMSSimulationSetupAction, SIGNAL(triggered(bool)), SLOT(openOMSSimulationDialog()));
// unload OMSimulator model Action
mpUnloadOMSModelAction = new QAction(QIcon(":/Resources/icons/delete.svg"), Helper::unloadClass, this);
mpUnloadOMSModelAction->setShortcut(QKeySequence::Delete);
Expand Down Expand Up @@ -3211,10 +3210,17 @@ void LibraryTreeView::showContextMenu(QPoint point)
menu.addAction(mpUnloadCompositeModelFileAction);
break;
case LibraryTreeItem::OMS:
menu.addAction(mpRenameOMSModelAction);
menu.addAction(mpSimulateOMSModelAction);
menu.addSeparator();
menu.addAction(mpUnloadOMSModelAction);
menu.addAction(mpViewDiagramAction);
if (pLibraryTreeItem->isTopLevel()) {
menu.addSeparator();
menu.addAction(mpSaveAction);
menu.addAction(mpSaveAsAction);
menu.addSeparator();
menu.addAction(mpRenameOMSModelAction);
menu.addAction(mpOMSSimulationSetupAction);
menu.addSeparator();
menu.addAction(mpUnloadOMSModelAction);
}
break;
}
}
Expand Down Expand Up @@ -3719,34 +3725,14 @@ void LibraryTreeView::TLMSimulate()
}

/*!
* \brief LibraryTreeView::simulateOMSModel
* \brief LibraryTreeView::openOMSSimulationDialog
* Opens the OMSimulator Simulation Dialog for the selected LibraryTreeItem.
*/
void LibraryTreeView::simulateOMSModel()
void LibraryTreeView::openOMSSimulationDialog()
{
LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (pLibraryTreeItem) {

// oms_instantiateFMU(pLibraryTreeItem->getOMSimulatorModel(), "C:/Users/adeas31/AppData/Local/Temp/OpenModelica/OMEdit/DualMassOscillator.System1.fmu", "System1");
// oms_instantiateFMU(pLibraryTreeItem->getOMSimulatorModel(), "C:/Users/adeas31/AppData/Local/Temp/OpenModelica/OMEdit/DualMassOscillator.System2.fmu", "System2");

// //if (status1 == oms_status_ok && status2 == oms_status_ok) {
// oms_addConnection(pLibraryTreeItem->getOMSimulatorModel(), "System1.F", "System2.F");
// oms_addConnection(pLibraryTreeItem->getOMSimulatorModel(), "System1.s", "System2.s");
// oms_addConnection(pLibraryTreeItem->getOMSimulatorModel(), "System1.v", "System2.v");
// oms_addConnection(pLibraryTreeItem->getOMSimulatorModel(), "System1.a", "System2.a");

// oms_setResultFile(pLibraryTreeItem->getOMSimulatorModel(), "DualMassOscillator_me.mat");

// oms_setStopTime(pLibraryTreeItem->getOMSimulatorModel(), 0.1);
// oms_setCommunicationInterval(pLibraryTreeItem->getOMSimulatorModel(), 1e-5);

// oms_initialize(pLibraryTreeItem->getOMSimulatorModel());
// oms_simulate(pLibraryTreeItem->getOMSimulatorModel());

// oms_terminate(pLibraryTreeItem->getOMSimulatorModel());

// MainWindow::instance()->openResultFiles(QStringList(Utilities::tempDirectory() + "/DualMassOscillator_me.mat"));
// }
MainWindow::instance()->OMSSimulationSetup(pLibraryTreeItem);
}
}

Expand Down Expand Up @@ -4365,7 +4351,12 @@ bool LibraryWidget::saveLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem)
} else if (pLibraryTreeItem->getLibraryType() == LibraryTreeItem::Text) {
result = saveTextLibraryTreeItem(pLibraryTreeItem);
} else if (pLibraryTreeItem->getLibraryType() == LibraryTreeItem::OMS) {
result = saveOMSLibraryTreeItem(pLibraryTreeItem);
if (pLibraryTreeItem->isTopLevel()) {
result = saveOMSLibraryTreeItem(pLibraryTreeItem);
} else {
result = saveLibraryTreeItem(pLibraryTreeItem->parent());
return result;
}
} else {
QMessageBox::information(this, Helper::applicationName + " - " + Helper::error, GUIMessages::getMessage(GUIMessages::ERROR_OCCURRED)
.arg(tr("Unable to save the file, unknown library type.")), Helper::ok);
Expand Down

0 comments on commit f545a43

Please sign in to comment.