From ea459d0afaeb7953e6804747f2b17d3c90ea52e2 Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Thu, 8 Feb 2024 12:11:12 +0100 Subject: [PATCH] Copy the connector to both icon and diagram layer (#11966) Fix for #11961 Only paste the connections on diagram layer --- OMEdit/OMEditLIB/Element/Element.cpp | 7 +++--- OMEdit/OMEditLIB/Modeling/Model.h | 1 + .../Modeling/ModelWidgetContainer.cpp | 25 +++++++++++++------ .../OMEditLIB/Modeling/ModelWidgetContainer.h | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/OMEdit/OMEditLIB/Element/Element.cpp b/OMEdit/OMEditLIB/Element/Element.cpp index 91c7d2f97b5..d861da26770 100644 --- a/OMEdit/OMEditLIB/Element/Element.cpp +++ b/OMEdit/OMEditLIB/Element/Element.cpp @@ -1233,7 +1233,6 @@ QString Element::getTransformationAnnotation(bool ModelicaSyntax) // add the origin if (mTransformation.getOrigin().isDynamicSelectExpression() || mTransformation.getOrigin().toQString().compare(QStringLiteral("{0,0}")) != 0) { annotationStringList.append(QString("origin=%1").arg(mTransformation.getOrigin().toQString())); - } // add extent points if (mTransformation.getExtent().isDynamicSelectExpression() || mTransformation.getExtent().size() > 1) { @@ -1261,7 +1260,7 @@ QString Element::getPlacementAnnotation(bool ModelicaSyntax) placementAnnotationString.append(QString("visible=%1,").arg(mTransformation.getVisible().toQString())); } } - if ((mpLibraryTreeItem && mpLibraryTreeItem->isConnector()) || (mpGraphicsView->getModelWidget()->isNewApi() && mpModel->isConnector())) { + if ((mpLibraryTreeItem && mpLibraryTreeItem->isConnector()) || (mpGraphicsView->getModelWidget()->isNewApi() && mpModel && mpModel->isConnector())) { if (mpGraphicsView->getViewType() == StringHandler::Icon) { // first get the component from diagram view and get the transformations Element *pElement = mpGraphicsView->getModelWidget()->getDiagramGraphicsView()->getElementObject(getName()); @@ -1327,7 +1326,7 @@ QString Element::getOMCPlacementAnnotation(QPointF position) if (mTransformation.isValid()) { placementAnnotationString.append(mTransformation.getVisible() ? "true" : "false"); } - if (mpLibraryTreeItem && mpLibraryTreeItem->isConnector()) { + if ((mpLibraryTreeItem && mpLibraryTreeItem->isConnector()) || (mpGraphicsView->getModelWidget()->isNewApi() && mpModel && mpModel->isConnector())) { if (mpGraphicsView->getViewType() == StringHandler::Icon) { // first get the component from diagram view and get the transformations Element *pElement; @@ -3585,7 +3584,7 @@ void Element::duplicate() QPointF gridStep(mpGraphicsView->mMergedCoOrdinateSystem.getHorizontalGridStep() * 5, mpGraphicsView->mMergedCoOrdinateSystem.getVerticalGridStep() * 5); // add component if (mpGraphicsView->getModelWidget()->isNewApi()) { - ModelInstance::Component *pModelInstanceComponent = GraphicsView::createModelInstanceComponent(mpGraphicsView->getModelWidget()->getModelInstance(), name, getClassName()); + ModelInstance::Component *pModelInstanceComponent = GraphicsView::createModelInstanceComponent(mpGraphicsView->getModelWidget()->getModelInstance(), name, getClassName(), false); mpGraphicsView->addElementToView(pModelInstanceComponent, false, true, false, QPointF(0, 0), getOMCPlacementAnnotation(gridStep), false); // set modifiers if (mpModelComponent->getModifier()) { diff --git a/OMEdit/OMEditLIB/Modeling/Model.h b/OMEdit/OMEditLIB/Modeling/Model.h index 934886cd1a8..67580327f20 100644 --- a/OMEdit/OMEditLIB/Modeling/Model.h +++ b/OMEdit/OMEditLIB/Modeling/Model.h @@ -582,6 +582,7 @@ namespace ModelInstance const QString &getName() const {return mName;} const QString &getRootType() const; bool isMissing() const {return mMissing;} + void setRestriction(const QString &restriction) {mRestriction = restriction;} const QString &getRestriction() const {return mRestriction;} bool isConnector() const; bool isExpandableConnector() const; diff --git a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp index 9fa257fbfde..99908198462 100644 --- a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp +++ b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp @@ -879,17 +879,20 @@ bool GraphicsView::performElementCreationChecks(LibraryTreeItem *pLibraryTreeIte * \param pModelInstance * \param name * \param className + * \param isConnector * \return */ -ModelInstance::Component *GraphicsView::createModelInstanceComponent(ModelInstance::Model *pModelInstance, const QString &name, const QString &className) +ModelInstance::Component *GraphicsView::createModelInstanceComponent(ModelInstance::Model *pModelInstance, const QString &name, const QString &className, bool isConnector) { ModelInstance::Component *pComponent = new ModelInstance::Component(pModelInstance); pComponent->setName(name); pComponent->setType(className); - /* We use getModelInstanceAnnotation here for bettter performance + /* We use getModelInstance with icon flag for bettter performance * This model will be updated right after this so it doesn't matter if the Component has complete model or not. */ - pComponent->setModel(new ModelInstance::Model(MainWindow::instance()->getOMCProxy()->getModelInstance(className, "", false, true))); + ModelInstance::Model *pModel = new ModelInstance::Model(MainWindow::instance()->getOMCProxy()->getModelInstance(className, "", false, true)); + pModel->setRestriction(isConnector ? "connector" : "model"); + pComponent->setModel(pModel); pModelInstance->addElement(pComponent); return pComponent; } @@ -963,7 +966,7 @@ bool GraphicsView::addComponent(QString className, QPointF position) || (mViewType == StringHandler::Icon && (type == StringHandler::Connector || type == StringHandler::ExpandableConnector))) { if (mpModelWidget->isNewApi()) { ModelInfo oldModelInfo = mpModelWidget->createModelInfo(); - ModelInstance::Component *pComponent = GraphicsView::createModelInstanceComponent(mpModelWidget->getModelInstance(), name, pLibraryTreeItem->getNameStructure()); + ModelInstance::Component *pComponent = GraphicsView::createModelInstanceComponent(mpModelWidget->getModelInstance(), name, pLibraryTreeItem->getNameStructure(), pLibraryTreeItem->isConnector()); addElementToView(pComponent, false, true, true, position, "", true); ModelInfo newModelInfo = mpModelWidget->createModelInfo(); mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), oldModelInfo, newModelInfo, "Add Element")); @@ -3631,6 +3634,7 @@ void GraphicsView::copyItems(bool cut) QJsonObject componentJsonObject; componentJsonObject.insert(QLatin1String("classname"), pElement->getClassName()); componentJsonObject.insert(QLatin1String("name"), pElement->getName()); + componentJsonObject.insert(QLatin1String("connector"), pElement->getModel() ? pElement->getModel()->isConnector() : false); componentJsonObject.insert(QLatin1String("placement"), pElement->getOMCPlacementAnnotation(QPointF(0, 0))); componentsJsonArray.append(componentJsonObject); } else if (ShapeAnnotation *pShapeAnnotation = dynamic_cast(itemsList.at(i))) { @@ -4049,8 +4053,11 @@ void GraphicsView::pasteItems() QJsonObject componentObject = componentsArray.at(i).toObject(); const QString className = componentObject.value("classname").toString(); const QString name = componentObject.value("name").toString(); + const bool connector = componentObject.value("connector").toBool(); const QString placement = componentObject.value("placement").toString(); - addElementToView(GraphicsView::createModelInstanceComponent(mpModelWidget->getModelInstance(), name, className), false, false, false, QPointF(0, 0), placement, false); + const int numberOfElements = mElementsList.size(); + addElementToView(GraphicsView::createModelInstanceComponent(mpModelWidget->getModelInstance(), name, className, connector), false, false, false, QPointF(0, 0), placement, false); + assert(mElementsList.size() > numberOfElements); mElementsList.last()->setSelected(true); } } @@ -4073,8 +4080,12 @@ void GraphicsView::pasteItems() LineAnnotation *pConnectionLineAnnotation = new LineAnnotation(lineShape, 0, 0, this); pConnectionLineAnnotation->setStartElementName(connectionObject.value("from").toString()); pConnectionLineAnnotation->setEndElementName(connectionObject.value("to").toString()); - addConnectionToView(pConnectionLineAnnotation, false); - mConnectionsList.last()->setSelected(true); + // always add the connections to diagram layer. + GraphicsView *pDiagramGraphicsView = mpModelWidget->getDiagramGraphicsView(); + pDiagramGraphicsView->addConnectionToView(pConnectionLineAnnotation, false); + if (mViewType == StringHandler::Diagram) { + mConnectionsList.last()->setSelected(true); + } } } // add shapes diff --git a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h index 35951b1034e..f47bf1032e0 100644 --- a/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h +++ b/OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h @@ -249,7 +249,7 @@ class GraphicsView : public QGraphicsView QAction* getFlipHorizontalAction() {return mpFlipHorizontalAction;} QAction* getFlipVerticalAction() {return mpFlipVerticalAction;} bool performElementCreationChecks(LibraryTreeItem *pLibraryTreeItem, QString *name, QString *defaultPrefix); - static ModelInstance::Component* createModelInstanceComponent(ModelInstance::Model *pModelInstance, const QString &name, const QString &className); + static ModelInstance::Component* createModelInstanceComponent(ModelInstance::Model *pModelInstance, const QString &name, const QString &className, bool isConnector); bool addComponent(QString className, QPointF position); void addComponentToView(QString name, LibraryTreeItem *pLibraryTreeItem, QString annotation, QPointF position, ElementInfo *pComponentInfo, bool addObject, bool openingClass, bool emitComponentAdded);