Skip to content

Commit

Permalink
[NewAPI] Merge the CoordinateSystem when reading from JSON (#9457)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Oct 3, 2022
1 parent 9cbaf62 commit 08d940e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 33 deletions.
8 changes: 4 additions & 4 deletions OMEdit/OMEditLIB/Element/Element.cpp
Expand Up @@ -1252,12 +1252,12 @@ ModelInstance::CoordinateSystem Element::getCoOrdinateSystemNew() const
ModelInstance::CoordinateSystem coordinateSystem;
if (mpModel->isConnector()) {
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
coordinateSystem = mpModel->getIconAnnotation()->getCoordinateSystem();
coordinateSystem = mpModel->getIconAnnotation()->getMergedCoordinateSystem();
} else {
coordinateSystem = mpModel->getDiagramAnnotation()->getCoordinateSystem();
coordinateSystem = mpModel->getDiagramAnnotation()->getMergedCoordinateSystem();
}
} else {
coordinateSystem = mpModel->getIconAnnotation()->getCoordinateSystem();
coordinateSystem = mpModel->getIconAnnotation()->getMergedCoordinateSystem();
}
return coordinateSystem;
}
Expand Down Expand Up @@ -3786,7 +3786,7 @@ void Element::fetchInterfaceData()
*/
void Element::openClass()
{
MainWindow::instance()->getLibraryWidget()->openLibraryTreeItem(mpLibraryTreeItem->getNameStructure());
MainWindow::instance()->getLibraryWidget()->openLibraryTreeItem(getClassName());
}

/*!
Expand Down
54 changes: 54 additions & 0 deletions OMEdit/OMEditLIB/Modeling/Model.cpp
Expand Up @@ -629,6 +629,7 @@ namespace ModelInstance
{
if (jsonObject.contains("coordinateSystem")) {
mCoordinateSystem.deserialize(jsonObject.value("coordinateSystem").toObject());
mMergedCoOrdinateSystem = mCoordinateSystem;
}

if (jsonObject.contains("graphics")) {
Expand Down Expand Up @@ -778,13 +779,28 @@ namespace ModelInstance
if (mModelJson.contains("annotation")) {
QJsonObject annotation = mModelJson.value("annotation").toObject();

/* From Modelica Specification Version 3.5-dev
* The coordinate system (including preserveAspectRatio) of a class is defined by the following priority:
* 1. The coordinate system annotation given in the class (if specified).
* 2. The coordinate systems of the first base-class where the extent on the extends-clause specifies a
* null-region (if any). Note that null-region is the default for base-classes, see section 18.6.3.
* 3. The default coordinate system CoordinateSystem(extent={{-100, -100}, {100, 100}}).
*
* Following is the first case.
*/
if (annotation.contains("Icon")) {
mpIconAnnotation->deserialize(annotation.value("Icon").toObject());
}
if (!mpIconAnnotation->getCoordinateSystem().isComplete()) {
readCoordinateSystemFromExtendsClass(true);
}

if (annotation.contains("Diagram")) {
mpDiagramAnnotation->deserialize(annotation.value("Diagram").toObject());
}
if (!mpDiagramAnnotation->getCoordinateSystem().isComplete()) {
readCoordinateSystemFromExtendsClass(false);
}

if (annotation.contains("DocumentationClass")) {
mDocumentationClass = annotation.value("DocumentationClass").toBool();
Expand Down Expand Up @@ -920,6 +936,44 @@ namespace ModelInstance
return (mRestriction.compare(QStringLiteral("enumeration")) == 0);
}

void Model::readCoordinateSystemFromExtendsClass(bool isIcon)
{
/* From Modelica Specification Version 3.5-dev
* The coordinate system (including preserveAspectRatio) of a class is defined by the following priority:
* 1. The coordinate system annotation given in the class (if specified).
* 2. The coordinate systems of the first base-class where the extent on the extends-clause specifies a
* null-region (if any). Note that null-region is the default for base-classes, see section 18.6.3.
* 3. The default coordinate system CoordinateSystem(extent={{-100, -100}, {100, 100}}).
*
* Following is the second case.
*/
foreach (auto pExtend, mExtends) {
ModelInstance::CoordinateSystem coordinateSystem;
IconDiagramAnnotation *pIconDiagramAnnotation = 0;
if (isIcon) {
coordinateSystem = pExtend->getIconAnnotation()->getCoordinateSystem();
pIconDiagramAnnotation = mpIconAnnotation;
} else {
coordinateSystem = pExtend->getDiagramAnnotation()->getCoordinateSystem();
pIconDiagramAnnotation = mpDiagramAnnotation;
}

if (!pIconDiagramAnnotation->mMergedCoOrdinateSystem.hasExtent() && coordinateSystem.hasExtent()) {
pIconDiagramAnnotation->mMergedCoOrdinateSystem.setExtent(coordinateSystem.getExtent());
}
if (!pIconDiagramAnnotation->mMergedCoOrdinateSystem.hasPreserveAspectRatio() && coordinateSystem.hasPreserveAspectRatio()) {
pIconDiagramAnnotation->mMergedCoOrdinateSystem.setPreserveAspectRatio(coordinateSystem.getPreserveAspectRatio());
}
if (!pIconDiagramAnnotation->mMergedCoOrdinateSystem.hasInitialScale() && coordinateSystem.hasInitialScale()) {
pIconDiagramAnnotation->mMergedCoOrdinateSystem.setInitialScale(coordinateSystem.getInitialScale());
}
if (!pIconDiagramAnnotation->mMergedCoOrdinateSystem.hasGrid() && coordinateSystem.hasGrid()) {
pIconDiagramAnnotation->mMergedCoOrdinateSystem.setGrid(coordinateSystem.getGrid());
}
break; // we only check coordinate system of first inherited class. See the comment in start of function i.e., "The coordinate systems of the first base-class ..."
}
}

bool Model::isParameterConnectorSizing(const QString &parameter)
{
foreach (auto pModelElement, mElements) {
Expand Down
4 changes: 3 additions & 1 deletion OMEdit/OMEditLIB/Modeling/Model.h
Expand Up @@ -304,10 +304,12 @@ namespace ModelInstance
~IconDiagramAnnotation();
void deserialize(const QJsonObject &jsonObject);
CoordinateSystem getCoordinateSystem() {return mCoordinateSystem;}
CoordinateSystem getMergedCoordinateSystem() {return mMergedCoOrdinateSystem;}
QList<Shape*> getGraphics() const {return mGraphics;}
bool isGraphicsEmpty() const {return mGraphics.isEmpty();}

CoordinateSystem mCoordinateSystem;
CoordinateSystem mMergedCoOrdinateSystem;
QList<Shape*> mGraphics;

};
Expand All @@ -328,7 +330,6 @@ namespace ModelInstance
void setModelJson(const QJsonObject &modelJson) {mModelJson = modelJson;}
QString getName() const {return mName;}
QStringList getDims() const {return mDims;}
void setRestriction(const QString &restriction) {mRestriction = restriction;}
QString getRestriction() const {return mRestriction;}
bool isConnector() const;
bool isExpandableConnector() const;
Expand All @@ -345,6 +346,7 @@ namespace ModelInstance
QString getComment() const {return mComment;}
IconDiagramAnnotation *getIconAnnotation() const {return mpIconAnnotation;}
IconDiagramAnnotation *getDiagramAnnotation() const {return mpDiagramAnnotation;}
void readCoordinateSystemFromExtendsClass(bool isIcon);
bool isDocumentationClass() const {return mDocumentationClass;}
QString getVersion() const {return mVersion;}
QString getVersionDate() const {return mVersionDate;}
Expand Down
79 changes: 51 additions & 28 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -247,39 +247,64 @@ void GraphicsView::setIsVisualizationView(bool visualizationView)
*/
void GraphicsView::drawCoordinateSystem()
{
/* From Modelica Specification Version 3.5-dev
* The coordinate system (including preserveAspectRatio) of a class is defined by the following priority:
* 1. The coordinate system annotation given in the class (if specified).
* 2. The coordinate systems of the first base-class where the extent on the extends-clause specifies a
* null-region (if any). Note that null-region is the default for base-classes, see section 18.6.3.
* 3. The default coordinate system CoordinateSystem(extent={{-100, -100}, {100, 100}}).
*
* Following is the first case.
*/
ModelInstance::CoordinateSystem coordinateSystem;
if (mViewType == StringHandler::Icon && mpModelWidget->getLibraryTreeItem()->getAccess() >= LibraryTreeItem::icon) {
coordinateSystem = mpModelWidget->getModelInstance()->getIconAnnotation()->getCoordinateSystem();
} else if (mViewType == StringHandler::Diagram && mpModelWidget->getLibraryTreeItem()->getAccess() >= LibraryTreeItem::diagram) {
coordinateSystem = mpModelWidget->getModelInstance()->getDiagramAnnotation()->getCoordinateSystem();
}
ModelInstance::Extent extent = coordinateSystem.getExtent();
ModelInstance::Point leftBottom = extent.getExtent1();
mCoOrdinateSystem.setLeft(leftBottom.x());
mCoOrdinateSystem.setBottom(leftBottom.y());
ModelInstance::Point rightTop = extent.getExtent2();
mCoOrdinateSystem.setRight(rightTop.x());
mCoOrdinateSystem.setTop(rightTop.y());
mCoOrdinateSystem.setPreserveAspectRatio(coordinateSystem.getPreserveAspectRatio());
mCoOrdinateSystem.setInitialScale(coordinateSystem.getInitialScale());
ModelInstance::Point grid = coordinateSystem.getGrid();
mCoOrdinateSystem.setHorizontal(grid.x());
mCoOrdinateSystem.setVertical(grid.y());
// start with the local CoOrdinateSystem

if (coordinateSystem.hasExtent()) {
ModelInstance::Extent extent = coordinateSystem.getExtent();
ModelInstance::Point leftBottom = extent.getExtent1();
mCoOrdinateSystem.setLeft(leftBottom.x());
mCoOrdinateSystem.setBottom(leftBottom.y());
ModelInstance::Point rightTop = extent.getExtent2();
mCoOrdinateSystem.setRight(rightTop.x());
mCoOrdinateSystem.setTop(rightTop.y());
}
if (coordinateSystem.hasPreserveAspectRatio()) {
mCoOrdinateSystem.setPreserveAspectRatio(coordinateSystem.getPreserveAspectRatio());
}
if (coordinateSystem.hasInitialScale()) {
mCoOrdinateSystem.setInitialScale(coordinateSystem.getInitialScale());
}
if (coordinateSystem.hasGrid()) {
ModelInstance::Point grid = coordinateSystem.getGrid();
mCoOrdinateSystem.setHorizontal(grid.x());
mCoOrdinateSystem.setVertical(grid.y());
}
mMergedCoOrdinateSystem = mCoOrdinateSystem;
// if local CoOrdinateSystem is not complete then try to complete the merged CoOrdinateSystem.
// if (!mCoOrdinateSystem.isComplete()) {
// readCoOrdinateSystemFromInheritedClass(this, pGraphicsView);
// }
if (!mCoOrdinateSystem.isComplete()) {
ModelInstance::CoordinateSystem mergedCoordinateSystem;
if (mViewType == StringHandler::Icon && mpModelWidget->getLibraryTreeItem()->getAccess() >= LibraryTreeItem::icon) {
mergedCoordinateSystem = mpModelWidget->getModelInstance()->getIconAnnotation()->getMergedCoordinateSystem();
} else if (mViewType == StringHandler::Diagram && mpModelWidget->getLibraryTreeItem()->getAccess() >= LibraryTreeItem::diagram) {
mergedCoordinateSystem = mpModelWidget->getModelInstance()->getDiagramAnnotation()->getMergedCoordinateSystem();
}

if (mergedCoordinateSystem.hasExtent()) {
ModelInstance::Extent extent = mergedCoordinateSystem.getExtent();
ModelInstance::Point leftBottom = extent.getExtent1();
mMergedCoOrdinateSystem.setLeft(leftBottom.x());
mMergedCoOrdinateSystem.setBottom(leftBottom.y());
ModelInstance::Point rightTop = extent.getExtent2();
mMergedCoOrdinateSystem.setRight(rightTop.x());
mMergedCoOrdinateSystem.setTop(rightTop.y());
}
if (mergedCoordinateSystem.hasPreserveAspectRatio()) {
mMergedCoOrdinateSystem.setPreserveAspectRatio(mergedCoordinateSystem.getPreserveAspectRatio());
}
if (mergedCoordinateSystem.hasInitialScale()) {
mMergedCoOrdinateSystem.setInitialScale(mergedCoordinateSystem.getInitialScale());
}
if (mergedCoordinateSystem.hasGrid()) {
ModelInstance::Point grid = mergedCoordinateSystem.getGrid();
mMergedCoOrdinateSystem.setHorizontal(grid.x());
mMergedCoOrdinateSystem.setVertical(grid.y());
}
}

setExtentRectangle(mMergedCoOrdinateSystem.getExtentRectangle());
resize(size());
Expand Down Expand Up @@ -910,9 +935,7 @@ bool GraphicsView::addComponent(QString className, QPointF position)
ModelInstance::Element *pElement = new ModelInstance::Element(pModelInstance);
pElement->setName(name);
pElement->setType(pLibraryTreeItem->getNameStructure());
ModelInstance::Model *pModel = new ModelInstance::Model;
pModel->setRestriction(StringHandler::getModelicaClassType(type).toLower());
pElement->setModel(pModel);
pElement->setModel(new ModelInstance::Model(MainWindow::instance()->getOMCProxy()->getModelInstance(pLibraryTreeItem->getNameStructure(), true)));
pModelInstance->addElement(pElement);
ModelInfo oldModelInfo = mpModelWidget->createModelInfo();
addElementToView(pElement, false, true, true, position);
Expand Down

0 comments on commit 08d940e

Please sign in to comment.