Skip to content

Commit

Permalink
Add uses annotation after duplicating a model (#12213)
Browse files Browse the repository at this point in the history
Fixes #8174
  • Loading branch information
adeas31 committed Apr 9, 2024
1 parent 1b2eabe commit 6016ef7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
79 changes: 45 additions & 34 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -1081,45 +1081,15 @@ void GraphicsView::addElementToView(ModelInstance::Component *pComponent, bool i
void GraphicsView::addElementToClass(Element *pElement)
{
if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::Modelica) {
MainWindow *pMainWindow = MainWindow::instance();
// Add the component to model in OMC.
/* Ticket:4132
* Always send the full path so that addComponent API doesn't fail when it makes a call to getDefaultPrefixes.
* I updated the addComponent API to make path relative.
*/
pMainWindow->getOMCProxy()->addComponent(pElement->getName(), pElement->getClassName(),
mpModelWidget->getLibraryTreeItem()->getNameStructure(), pElement->getPlacementAnnotation());
LibraryTreeModel *pLibraryTreeModel = pMainWindow->getLibraryWidget()->getLibraryTreeModel();
// get the toplevel class of dragged component
QString packageName = StringHandler::getFirstWordBeforeDot(pElement->getClassName());
LibraryTreeItem *pPackageLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(packageName);
// get the top level class of current class
QString topLevelClassName = StringHandler::getFirstWordBeforeDot(mpModelWidget->getLibraryTreeItem()->getNameStructure());
LibraryTreeItem *pTopLevelLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(topLevelClassName);
if (pPackageLibraryTreeItem && pTopLevelLibraryTreeItem) {
// get uses annotation of the toplevel class
QList<QList<QString > > usesAnnotation = pMainWindow->getOMCProxy()->getUses(pTopLevelLibraryTreeItem->getNameStructure());
QStringList newUsesAnnotation;
for (int i = 0 ; i < usesAnnotation.size() ; i++) {
if (usesAnnotation.at(i).at(0).compare(packageName) == 0) {
return; // if the package is already in uses annotation of class then simply return without doing anything.
} else {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(usesAnnotation.at(i).at(0)).arg(usesAnnotation.at(i).at(1)));
}
}
// if the package has version only then add the uses annotation
if (!pPackageLibraryTreeItem->mClassInformation.version.isEmpty() &&
// Do not add a uses-annotation to itself
pTopLevelLibraryTreeItem->getNameStructure() != packageName) {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(packageName).arg(pPackageLibraryTreeItem->mClassInformation.version));
QString usesAnnotationString = QString("annotate=$annotation(uses(%1))").arg(newUsesAnnotation.join(","));
pMainWindow->getOMCProxy()->addClassAnnotation(pTopLevelLibraryTreeItem->getNameStructure(), usesAnnotationString);
// if save folder structure then update the parent package
if (pTopLevelLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure) {
pLibraryTreeModel->updateLibraryTreeItemClassText(pTopLevelLibraryTreeItem);
}
}
}
const QString className = pElement->getClassName();
MainWindow::instance()->getOMCProxy()->addComponent(pElement->getName(), className, mpModelWidget->getLibraryTreeItem()->getNameStructure(), pElement->getPlacementAnnotation());
// add uses annotation
addUsesAnnotation(className, mpModelWidget->getLibraryTreeItem()->getNameStructure(), false);
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType()== LibraryTreeItem::CompositeModel) {
// add SubModel Element
CompositeModelEditor *pCompositeModelEditor = dynamic_cast<CompositeModelEditor*>(mpModelWidget->getEditor());
Expand All @@ -1134,6 +1104,47 @@ void GraphicsView::addElementToClass(Element *pElement)
}
}

/*!
* \brief GraphicsView::addUsesAnnotation
* \param insertedClassName - the name of the class that is dragged or duplicated.
* \param containingClassName - the name of the containing class where the component is dropped or duplicated model is added.
* \param updateParentText - update the text of the parent.
*/
void GraphicsView::addUsesAnnotation(const QString &insertedClassName, const QString &containingClassName, bool updateParentText)
{
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
// get the toplevel class of dragged component or duplicated model
QString packageName = StringHandler::getFirstWordBeforeDot(insertedClassName);
LibraryTreeItem *pPackageLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(packageName);
// get the top level class of containing class
QString topLevelClassName = StringHandler::getFirstWordBeforeDot(containingClassName);
LibraryTreeItem *pTopLevelLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(topLevelClassName);
if (pPackageLibraryTreeItem && pTopLevelLibraryTreeItem) {
// get uses annotation of the toplevel class
QList<QList<QString > > usesAnnotation = MainWindow::instance()->getOMCProxy()->getUses(pTopLevelLibraryTreeItem->getNameStructure());
QStringList newUsesAnnotation;
for (int i = 0 ; i < usesAnnotation.size() ; i++) {
if (usesAnnotation.at(i).at(0).compare(packageName) == 0) {
return; // if the package is already in uses annotation of class then simply return without doing anything.
} else {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(usesAnnotation.at(i).at(0)).arg(usesAnnotation.at(i).at(1)));
}
}
// if the package has version only then add the uses annotation
if (!pPackageLibraryTreeItem->mClassInformation.version.isEmpty() &&
// Do not add a uses-annotation to itself
pTopLevelLibraryTreeItem->getNameStructure() != packageName) {
newUsesAnnotation.append(QString("%1(version=\"%2\")").arg(packageName).arg(pPackageLibraryTreeItem->mClassInformation.version));
QString usesAnnotationString = QString("annotate=$annotation(uses(%1))").arg(newUsesAnnotation.join(","));
MainWindow::instance()->getOMCProxy()->addClassAnnotation(pTopLevelLibraryTreeItem->getNameStructure(), usesAnnotationString);
// if save folder structure then update the parent package
if (pTopLevelLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure || updateParentText) {
pLibraryTreeModel->updateLibraryTreeItemClassText(pTopLevelLibraryTreeItem);
}
}
}
}

/*!
* \brief GraphicsView::addElementItem
* Adds the Element and its origin and resizer items to the GraphicsView.
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -253,6 +253,7 @@ class GraphicsView : public QGraphicsView
void addElementToOutOfSceneList(Element *pElement) {mOutOfSceneElementsList.append(pElement);}
void addInheritedElementToList(Element *pElement) {mInheritedElementsList.append(pElement);}
void addElementToClass(Element *pElement);
static void addUsesAnnotation(const QString &insertedClassName, const QString &containingClassName, bool updateParentText);
void addElementItem(Element *pElement);
void removeElementItem(Element *pElement);
void deleteElement(Element *pElement);
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -1160,6 +1160,8 @@ void DuplicateClassDialog::duplicateClass()
syncDuplicatedModelWithOMC(pLibraryTreeItem);
pLibraryTreeModel->checkIfAnyNonExistingClassLoaded();
pLibraryTreeModel->showModelWidget(pLibraryTreeItem);
// add uses annotation
GraphicsView::addUsesAnnotation(mpLibraryTreeItem->getNameStructure(), mpPathTextBox->text(), true);
accept();
} else {
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
Expand Down

0 comments on commit 6016ef7

Please sign in to comment.