Skip to content

Commit

Permalink
Adapt the create connector to instance API (#12169)
Browse files Browse the repository at this point in the history
Fixes #12163
  • Loading branch information
adeas31 committed Mar 28, 2024
1 parent a5a3ab3 commit e216ac0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
32 changes: 26 additions & 6 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -3160,8 +3160,9 @@ Element* GraphicsView::getConnectorElement(ModelInstance::Connector *pConnector)
* \brief GraphicsView::addConnection
* Adds the connection to GraphicsView.
* \param pElement
* \param createConnector true when function is called from GraphicsView::createConnector
*/
void GraphicsView::addConnection(Element *pElement)
void GraphicsView::addConnection(Element *pElement, bool createConnector)
{
// When clicking the start element
if (!isCreatingConnection()) {
Expand Down Expand Up @@ -3315,17 +3316,23 @@ void GraphicsView::addConnection(Element *pElement)
} else {
if (mpModelWidget->isNewApi()) {
if (!connectionExists(startElementName, endElementName, false)) {
if (mpModelWidget->getModelInstance()->isValidConnection(startElementName, endElementName)) {
/* Issue #12163. Do not check conneciton validity when called from GraphicsView::createConnector
* GraphicsView::createConnector creates an incomplete connector. We do this for performance reasons. Avoid calling getModelInstance API.
* We know for sure that both connectors are compatible in this case so its okay not to check for validity.
*/
if (createConnector || mpModelWidget->getModelInstance()->isValidConnection(startElementName, endElementName)) {
mpConnectionLineAnnotation->setLine(new ModelInstance::Line(mpModelWidget->getModelInstance()));
mpConnectionLineAnnotation->updateLine();
mpConnectionLineAnnotation->drawCornerItems();
mpConnectionLineAnnotation->setCornerItemsActiveOrPassive();
ModelInfo oldModelInfo = mpModelWidget->createModelInfo();
addConnectionToView(mpConnectionLineAnnotation, false);
addConnectionToClass(mpConnectionLineAnnotation);
ModelInfo newModelInfo = mpModelWidget->createModelInfo();
mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), oldModelInfo, newModelInfo, "Add Connection"));
mpModelWidget->updateModelText();
if (!createConnector) {
ModelInfo newModelInfo = mpModelWidget->createModelInfo();
mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), oldModelInfo, newModelInfo, "Add Connection"));
mpModelWidget->updateModelText();
}
} else {
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
GUIMessages::getMessage(GUIMessages::MISMATCHED_CONNECTORS_IN_CONNECT).arg(startElementName, endElementName), Helper::ok);
Expand Down Expand Up @@ -4381,14 +4388,27 @@ void GraphicsView::createConnector()
{
if (mpConnectionLineAnnotation && mpConnectionLineAnnotation->getStartElement()) {
Element *pConnectorElement = mpConnectionLineAnnotation->getStartElement();
if (pConnectorElement->getLibraryTreeItem()) {
if (mpModelWidget->isNewApi()) {
QString defaultName;
QString name = getUniqueElementName(pConnectorElement->getClassName(), pConnectorElement->getName(), &defaultName);
ModelInfo oldModelInfo = mpModelWidget->createModelInfo();
bool connector = pConnectorElement->getModel() ? pConnectorElement->getModel()->isConnector() : false;
ModelInstance::Component *pComponent = GraphicsView::createModelInstanceComponent(mpModelWidget->getModelInstance(), name, pConnectorElement->getClassName(), connector);
addElementToView(pComponent, false, true, true, mapToScene(mapFromGlobal(QCursor::pos())), "", true);
addConnection(mElementsList.last(), true);
ModelInfo newModelInfo = mpModelWidget->createModelInfo();
mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), oldModelInfo, newModelInfo, "Add connector"));
mpModelWidget->updateModelText();
} else if (pConnectorElement->getLibraryTreeItem()) {
mpModelWidget->beginMacro("Add connector");
QString defaultName;
QString name = getUniqueElementName(pConnectorElement->getLibraryTreeItem()->getNameStructure(), pConnectorElement->getLibraryTreeItem()->getName(), &defaultName);
ElementInfo *pElementInfo = new ElementInfo;
addComponentToView(name, pConnectorElement->getLibraryTreeItem(), "", mapToScene(mapFromGlobal(QCursor::pos())), pElementInfo, true, true, true);
addConnection(mElementsList.last());
mpModelWidget->endMacro();
} else {
qDebug() << "GraphicsView::createConnector() should never be reached.";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -423,7 +423,7 @@ class GraphicsView : public QGraphicsView
void updateDynamicSelect(double time);
void resetDynamicSelect();
public slots:
void addConnection(Element *pComponent);
void addConnection(Element *pComponent, bool createConnector = false);
void removeCurrentConnection();
void deleteConnection(LineAnnotation *pConnectionLineAnnotation);
void addTransition(Element *pComponent);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Util/Helper.cpp
Expand Up @@ -757,7 +757,7 @@ QString GUIMessages::getMessage(int type)
case SAME_COMPONENT_NAME:
return tr("A component with the name <b>%1</b> already exists or is a Modelica keyword. Please choose another name.");
case MISMATCHED_CONNECTORS_IN_CONNECT:
return tr("Connectors %1 and %2 are not compatible.");
return tr("Connectors <b>%1</b> and <b>%2</b> are not compatible.");
case SAME_COMPONENT_CONNECT:
return tr("You cannot connect a component to itself.");
case NO_MODELICA_CLASS_OPEN:
Expand Down

0 comments on commit e216ac0

Please sign in to comment.