Skip to content

Commit ac82ac2

Browse files
authored
Use the redeclare class instead of the component type (#10375)
* Use the redeclare class instead of the component type Move the functions related to parser to FlatModelica::Parser Parse the element redeclaration to get the element type * Read the redeclare class type from JSON Use both JSON and the parser to get the element type
1 parent af48bbf commit ac82ac2

File tree

10 files changed

+225
-80
lines changed

10 files changed

+225
-80
lines changed

OMEdit/OMEditLIB/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ set(OMEDITLIB_SOURCES Util/Helper.cpp
127127
Util/NetworkAccessManager.cpp
128128
FlatModelica/Expression.cpp
129129
FlatModelica/ExpressionFuncs.cpp
130+
FlatModelica/Parser.cpp
130131
Animation/OpenGLWidget.cpp
131132
Animation/AbstractAnimationWindow.cpp
132133
Animation/ViewerWidget.cpp
@@ -259,6 +260,7 @@ set(OMEDITLIB_HEADERS Util/Helper.h
259260
Util/NetworkAccessManager.h
260261
FlatModelica/Expression.h
261262
FlatModelica/ExpressionFuncs.h
263+
FlatModelica/Parser.h
262264
Animation/OpenGLWidget.h
263265
Animation/AbstractAnimationWindow.h
264266
Animation/ViewerWidget.h

OMEdit/OMEditLIB/Element/ElementProperties.cpp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "Modeling/Commands.h"
3939
#include "Options/OptionsDialog.h"
4040
#include "OMPlot.h"
41+
#include "FlatModelica/Parser.h"
4142

4243
#include <QApplication>
4344
#include <QMenu>
@@ -200,11 +201,11 @@ Parameter::Parameter(ModelInstance::Component *pComponent, ElementParameters *pE
200201
// For now we only handle replaceable component. Enable the line below once we start handling the replaceable class.
201202
//mValueType = Parameter::ReplaceableClass;
202203
if (mpModelInstanceComponent->getModel()) {
203-
mpModifyReplaceableButton = new QToolButton;
204-
mpModifyReplaceableButton->setIcon( QIcon(":/Resources/icons/edit-icon.svg"));
205-
mpModifyReplaceableButton->setToolTip(tr("Edit"));
206-
mpModifyReplaceableButton->setAutoRaise(true);
207-
connect(mpModifyReplaceableButton, SIGNAL(clicked()), SLOT(modifyReplaceableButtonClicked()));
204+
mpEditRedeclareClassButton = new QToolButton;
205+
mpEditRedeclareClassButton->setIcon( QIcon(":/Resources/icons/edit-icon.svg"));
206+
mpEditRedeclareClassButton->setToolTip(tr("Edit"));
207+
mpEditRedeclareClassButton->setAutoRaise(true);
208+
connect(mpEditRedeclareClassButton, SIGNAL(clicked()), SLOT(editRedeclareClassButtonClicked()));
208209
}
209210
} else if (!mpModelInstanceComponent->getAnnotation()->getChoices().getChoices().isEmpty()) {
210211
mValueType = Parameter::Choices;
@@ -515,8 +516,8 @@ void Parameter::setEnabled(bool enable)
515516
if (enable) {
516517
enableDisableUnitComboBox(getValue());
517518
}
518-
if (mpModifyReplaceableButton) {
519-
mpModifyReplaceableButton->setEnabled(enable);
519+
if (mpEditRedeclareClassButton) {
520+
mpEditRedeclareClassButton->setEnabled(enable);
520521
}
521522
}
522523

@@ -624,7 +625,7 @@ void Parameter::createValueWidget()
624625
if (MainWindow::instance()->isNewApi()) {
625626
comment = choice;
626627
} else {
627-
comment = StringHandler::getModelicaComment(choice);
628+
comment = StringHandler::removeFirstLastQuotes(FlatModelica::Parser::getModelicaComment(choice));
628629
}
629630
mpValueComboBox->addItem(comment, choice);
630631
}
@@ -717,22 +718,50 @@ void Parameter::updateValueBinding(const FlatModelica::Expression expression)
717718
}
718719
}
719720

720-
void Parameter::modifyReplaceableButtonClicked()
721+
/*!
722+
* \brief Parameter::editRedeclareClassButtonClicked
723+
* Slot activate when mpEditRedeclareClassButton clicked signal is raised.
724+
* Opens ElementParameters dialog for the redeclare class.
725+
*/
726+
void Parameter::editRedeclareClassButtonClicked()
721727
{
722-
// QString className = mpModelInstanceComponent->getType();
723-
// qDebug() << className;
724-
MainWindow::instance()->getProgressBar()->setRange(0, 0);
725-
MainWindow::instance()->showProgressBar();
726-
ElementParameters *pElementParameters = new ElementParameters(mpModelInstanceComponent, mpElementParameters->getGraphicsView(),
727-
mpElementParameters->isInherited(), true, mpElementParameters);
728-
MainWindow::instance()->hideProgressBar();
729-
MainWindow::instance()->getStatusBar()->clearMessage();
730-
if (pElementParameters->exec() == QDialog::Accepted) {
731-
if (!pElementParameters->getModification().isEmpty()) {
732-
setValueWidget(pElementParameters->getModification(), false, mUnit, true);
728+
QString type;
729+
if (mpValueComboBox->currentIndex() == 0) {
730+
type = mpModelInstanceComponent->getType();
731+
} else {
732+
type = mpModelInstanceComponent->getAnnotation()->getChoices().getType(mpValueComboBox->currentIndex() - 1);
733+
}
734+
735+
// if type is empty then try to parse the Modelica code of redeclare to get the type
736+
if (type.isEmpty()) {
737+
type = FlatModelica::Parser::getTypeFromElementRedeclaration(mpValueComboBox->lineEdit()->text());
738+
}
739+
740+
if (type.isEmpty()) {
741+
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
742+
tr("Unable to find the redeclare class %1.").arg(type), Helper::ok);
743+
} else {
744+
MainWindow::instance()->getProgressBar()->setRange(0, 0);
745+
MainWindow::instance()->showProgressBar();
746+
ModelInstance::Model *pCurrentModel = mpModelInstanceComponent->getModel();
747+
const QJsonObject newModelJSON = MainWindow::instance()->getOMCProxy()->getModelInstance(type);
748+
if (!newModelJSON.isEmpty()) {
749+
ModelInstance::Model *pNewModel = new ModelInstance::Model(newModelJSON);
750+
mpModelInstanceComponent->setModel(pNewModel);
751+
ElementParameters *pElementParameters = new ElementParameters(mpModelInstanceComponent, mpElementParameters->getGraphicsView(), mpElementParameters->isInherited(), true, mpElementParameters);
752+
MainWindow::instance()->hideProgressBar();
753+
MainWindow::instance()->getStatusBar()->clearMessage();
754+
if (pElementParameters->exec() == QDialog::Accepted) {
755+
if (!pElementParameters->getModification().isEmpty()) {
756+
setValueWidget(pElementParameters->getModification(), false, mUnit, true);
757+
}
758+
}
759+
pElementParameters->deleteLater();
760+
// reset the actual model of the element
761+
mpModelInstanceComponent->setModel(pCurrentModel);
762+
delete pNewModel;
733763
}
734764
}
735-
pElementParameters->deleteLater();
736765
}
737766

738767
/*!
@@ -1201,8 +1230,8 @@ void ElementParameters::setUpDialog()
12011230
}
12021231
pGroupBoxGridLayout->addWidget(pParameter->getValueWidget(), layoutIndex, columnIndex++);
12031232

1204-
if (pParameter->getModifyReplaceableButton()) {
1205-
pGroupBoxGridLayout->addWidget(pParameter->getModifyReplaceableButton(), layoutIndex, columnIndex++);
1233+
if (pParameter->getEditRedeclareClassButton()) {
1234+
pGroupBoxGridLayout->addWidget(pParameter->getEditRedeclareClassButton(), layoutIndex, columnIndex++);
12061235
} else {
12071236
pGroupBoxGridLayout->addItem(new QSpacerItem(1, 1), layoutIndex, columnIndex++);
12081237
}
@@ -1716,12 +1745,6 @@ void ElementParametersOld::setUpDialog()
17161745
}
17171746
pGroupBoxGridLayout->addWidget(pParameter->getValueWidget(), layoutIndex, columnIndex++);
17181747

1719-
if (pParameter->getModifyReplaceableButton()) {
1720-
pGroupBoxGridLayout->addWidget(pParameter->getModifyReplaceableButton(), layoutIndex, columnIndex++);
1721-
} else {
1722-
pGroupBoxGridLayout->addItem(new QSpacerItem(1, 1), layoutIndex, columnIndex++);
1723-
}
1724-
17251748
if (pParameter->getLoadSelectorFilter().compare("-") != 0 || pParameter->getLoadSelectorCaption().compare("-") != 0 ||
17261749
pParameter->getSaveSelectorFilter().compare("-") != 0 || pParameter->getSaveSelectorCaption().compare("-") != 0) {
17271750
pGroupBoxGridLayout->addWidget(pParameter->getFileSelectorButton(), layoutIndex, columnIndex++);

OMEdit/OMEditLIB/Element/ElementProperties.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Parameter : public QObject
8181
QWidget* getValueWidget();
8282
bool isValueModified();
8383
QString getValue();
84-
QToolButton *getModifyReplaceableButton() const {return mpModifyReplaceableButton;}
84+
QToolButton *getEditRedeclareClassButton() const {return mpEditRedeclareClassButton;}
8585
QToolButton* getFileSelectorButton() {return mpFileSelectorButton;}
8686
void setLoadSelectorFilter(QString loadSelectorFilter) {mLoadSelectorFilter = loadSelectorFilter;}
8787
QString getLoadSelectorFilter() {return mLoadSelectorFilter;}
@@ -127,7 +127,7 @@ class Parameter : public QObject
127127
QComboBox *mpValueComboBox;
128128
QLineEdit *mpValueTextBox;
129129
QCheckBox *mpValueCheckBox;
130-
QToolButton *mpModifyReplaceableButton = 0;
130+
QToolButton *mpEditRedeclareClassButton = 0;
131131
QToolButton *mpFileSelectorButton;
132132
QString mUnit;
133133
QString mDisplayUnit;
@@ -139,7 +139,7 @@ class Parameter : public QObject
139139
void enableDisableUnitComboBox(const QString &value);
140140
void updateValueBinding(const FlatModelica::Expression expression);
141141
public slots:
142-
void modifyReplaceableButtonClicked();
142+
void editRedeclareClassButtonClicked();
143143
void fileSelectorButtonClicked();
144144
void unitComboBoxChanged(int index);
145145
void valueComboBoxChanged(int index);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
13+
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
14+
*
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from OSMC, either from the above address,
18+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
19+
* http://www.openmodelica.org, and in the OpenModelica distribution.
20+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
21+
*
22+
* This program is distributed WITHOUT ANY WARRANTY; without
23+
* even the implied warranty of MERCHANTABILITY or FITNESS
24+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
25+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
26+
*
27+
* See the full OSMC Public License conditions for more details.
28+
*
29+
*/
30+
/*
31+
* @author Adeel Asghar <adeel.asghar@liu.se>
32+
*/
33+
34+
#define ANTLR4CPP_STATIC
35+
#include "antlr4-runtime.h"
36+
#include "modelicaLexer.h"
37+
#include "modelicaParser.h"
38+
#include "modelicaBaseListener.h"
39+
40+
#include "Parser.h"
41+
42+
static std::string cmt = "";
43+
44+
class ModelicaCommentListener : public openmodelica::modelicaBaseListener {
45+
void exitComment(openmodelica::modelicaParser::CommentContext *ctx) override {
46+
cmt = ctx->getText();
47+
}
48+
};
49+
50+
/*!
51+
* \brief FlatModelica::Parser::getModelicaComment
52+
* \param element
53+
* \return comment
54+
*/
55+
QString FlatModelica::Parser::getModelicaComment(QString element)
56+
{
57+
cmt = "";
58+
std::string s = element.toStdString();
59+
antlr4::ANTLRInputStream input(s);
60+
openmodelica::modelicaLexer lexer(&input);
61+
antlr4::CommonTokenStream tokens(&lexer);
62+
openmodelica::modelicaParser parser(&tokens);
63+
antlr4::tree::ParseTree* tree = parser.argument();
64+
ModelicaCommentListener listener;
65+
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
66+
if (cmt.size() > 1) {
67+
return QString::fromStdString(cmt);
68+
}
69+
return element;
70+
}
71+
72+
/*!
73+
* \brief FlatModelica::Parser::getTypeFromElementRedeclaration
74+
* \param elmentRedeclaration
75+
* \return
76+
*/
77+
QString FlatModelica::Parser::getTypeFromElementRedeclaration(const QString &elmentRedeclaration)
78+
{
79+
antlr4::ANTLRInputStream input(elmentRedeclaration.toStdString());
80+
openmodelica::modelicaLexer lexer(&input);
81+
antlr4::CommonTokenStream tokens(&lexer);
82+
openmodelica::modelicaParser parser(&tokens);
83+
openmodelica::modelicaParser::Element_redeclarationContext *pElement_redeclarationContext = parser.element_redeclaration();
84+
if (pElement_redeclarationContext && pElement_redeclarationContext->component_clause1()) {
85+
return QString::fromStdString(pElement_redeclarationContext->component_clause1()->type_specifier()->getText());
86+
}
87+
return "";
88+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
13+
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
14+
*
15+
* The OpenModelica software and the Open Source Modelica
16+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17+
* from OSMC, either from the above address,
18+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
19+
* http://www.openmodelica.org, and in the OpenModelica distribution.
20+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
21+
*
22+
* This program is distributed WITHOUT ANY WARRANTY; without
23+
* even the implied warranty of MERCHANTABILITY or FITNESS
24+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
25+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
26+
*
27+
* See the full OSMC Public License conditions for more details.
28+
*
29+
*/
30+
/*
31+
* @author Adeel Asghar <adeel.asghar@liu.se>
32+
*/
33+
#ifndef PARSER_H
34+
#define PARSER_H
35+
36+
#include <QString>
37+
38+
namespace FlatModelica
39+
{
40+
namespace Parser {
41+
QString getModelicaComment(QString element);
42+
QString getTypeFromElementRedeclaration(const QString &elmentRedeclaration);
43+
} // namespace Utilities
44+
} // namespace FlatModelica
45+
46+
#endif // PARSER_H

OMEdit/OMEditLIB/Modeling/Model.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ namespace ModelInstance
13361336
{
13371337
mCheckBox = false;
13381338
mDymolaCheckBox = false;
1339+
mChoices.clear();
13391340
}
13401341

13411342
void Choices::deserialize(const QJsonObject &jsonObject)
@@ -1351,18 +1352,39 @@ namespace ModelInstance
13511352
if (jsonObject.contains("choice")) {
13521353
QJsonArray choices = jsonObject.value("choice").toArray();
13531354
foreach (auto choice, choices) {
1355+
QString type = "";
13541356
if (choice.isObject()) {
13551357
QJsonObject choiceObject = choice.toObject();
1358+
if (choiceObject.contains("$type")) {
1359+
type = choiceObject.value("$type").toString();
1360+
}
13561361
if (choiceObject.contains("$value")) {
1357-
mChoice.append(choiceObject.value("$value").toString());
1362+
mChoices.append(qMakePair(choiceObject.value("$value").toString(), type));
13581363
}
13591364
} else {
1360-
mChoice.append(choice.toString());
1365+
mChoices.append(qMakePair(choice.toString(), type));
13611366
}
13621367
}
13631368
}
13641369
}
13651370

1371+
QStringList Choices::getChoices() const
1372+
{
1373+
QStringList choices;
1374+
foreach (Choice choice, mChoices) {
1375+
choices.append(choice.first);
1376+
}
1377+
return choices;
1378+
}
1379+
1380+
QString Choices::getType(int index) const
1381+
{
1382+
if (0 <= index && index < mChoices.size()) {
1383+
return mChoices.at(index).second;
1384+
}
1385+
return "";
1386+
}
1387+
13661388
Element::Element(Model *pParentModel)
13671389
{
13681390
mpParentModel = pParentModel;

OMEdit/OMEditLIB/Modeling/Model.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ namespace ModelInstance
353353
BooleanAnnotation mConnectorSizing;
354354
};
355355

356+
typedef QPair<QString, QString> Choice;
356357
class Choices
357358
{
358359
public:
@@ -361,11 +362,12 @@ namespace ModelInstance
361362

362363
bool isCheckBox() const {return mCheckBox;}
363364
bool isDymolaCheckBox() const {return mDymolaCheckBox;}
364-
QStringList getChoices() const {return mChoice;}
365+
QStringList getChoices() const;
366+
QString getType(int index) const;
365367
private:
366368
BooleanAnnotation mCheckBox;
367369
BooleanAnnotation mDymolaCheckBox;
368-
QStringList mChoice;
370+
QVector<Choice> mChoices;
369371
};
370372

371373
class IconDiagramMap
@@ -786,7 +788,8 @@ namespace ModelInstance
786788
std::unique_ptr<Connector> mpStartConnector;
787789
std::unique_ptr<Annotation> mpAnnotation;
788790
};
791+
} // namespace ModelInstance
789792

790-
}
793+
Q_DECLARE_METATYPE(ModelInstance::Choice)
791794

792795
#endif // MODEL_H

0 commit comments

Comments
 (0)