Skip to content

Commit

Permalink
Use modifierToJSON API to update the element modifier (#10441)
Browse files Browse the repository at this point in the history
Update the modifier with the new value and reset it when not needed
  • Loading branch information
adeas31 committed Mar 24, 2023
1 parent 2d4375a commit c84168c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
20 changes: 14 additions & 6 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Expand Up @@ -752,26 +752,34 @@ void Parameter::editRedeclareClassButtonClicked()
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
tr("Unable to find the redeclare class %1.").arg(type), Helper::ok);
} else {
MainWindow::instance()->getProgressBar()->setRange(0, 0);
MainWindow::instance()->showProgressBar();
ModelInstance::Model *pCurrentModel = mpModelInstanceElement->getModel();
const QJsonObject newModelJSON = MainWindow::instance()->getOMCProxy()->getModelInstance(type);
const QJsonObject newModelJSON = MainWindow::instance()->getOMCProxy()->getModelInstance(type, mpValueComboBox->lineEdit()->text());
if (!newModelJSON.isEmpty()) {
const QJsonObject modifierJSON = MainWindow::instance()->getOMCProxy()->modifierToJSON(mpValueComboBox->lineEdit()->text());
if (!modifierJSON.isEmpty()) {
ModelInstance::Modifier elementModifier;
elementModifier.deserialize(QJsonValue(modifierJSON));
mpModelInstanceElement->setModifier(elementModifier);
}
ModelInstance::Model *pNewModel = new ModelInstance::Model(newModelJSON);
mpModelInstanceElement->setModel(pNewModel);
MainWindow::instance()->getProgressBar()->setRange(0, 0);
MainWindow::instance()->showProgressBar();
ElementParameters *pElementParameters = new ElementParameters(mpModelInstanceElement, mpElementParameters->getGraphicsView(), mpElementParameters->isInherited(), true, mpElementParameters);
MainWindow::instance()->hideProgressBar();
MainWindow::instance()->getStatusBar()->clearMessage();
if (pElementParameters->exec() == QDialog::Accepted) {
if (!pElementParameters->getModification().isEmpty()) {
setValueWidget(pElementParameters->getModification(), false, mUnit, true);
}
}
pElementParameters->deleteLater();
// reset the modifier
mpModelInstanceElement->resetModifier();
// reset the actual model of the element
mpModelInstanceElement->setModel(pCurrentModel);
delete pNewModel;
}
MainWindow::instance()->hideProgressBar();
MainWindow::instance()->getStatusBar()->clearMessage();
}
}

Expand Down Expand Up @@ -1407,7 +1415,7 @@ void ElementParameters::fetchElementModifiers()
{
foreach (auto modifier, mpElement->getModifier().getModifiers()) {
Parameter *pParameter = findParameter(modifier.getName());
ElementParameters::applyStartFixedAndDisplayUnitModifiers(pParameter, modifier, mInherited || mNested);
ElementParameters::applyStartFixedAndDisplayUnitModifiers(pParameter, modifier, mInherited);
}
}

Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/Model.cpp
Expand Up @@ -1553,6 +1553,7 @@ namespace ModelInstance

if (jsonObject.contains("modifiers")) {
mModifier.deserialize(jsonObject.value("modifiers"));
mModifierForReset = mModifier;
}

if (jsonObject.contains("value")) {
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditLIB/Modeling/Model.h
Expand Up @@ -607,6 +607,8 @@ namespace ModelInstance
void setModel(Model *pModel) {mpModel = pModel;}
Model *getModel() const {return mpModel;}
Modifier getModifier() const {return mModifier;}
void setModifier(const Modifier modifier) {mModifier = modifier;}
void resetModifier() {mModifier = mModifierForReset;}
QString getModifierValueFromType(QStringList modifierNames);
Dimensions getDimensions() const {return mDims;}
Prefixes *getPrefixes() const {return mpPrefixes.get();}
Expand All @@ -631,6 +633,7 @@ namespace ModelInstance
Model *mpModel = 0;

Modifier mModifier;
Modifier mModifierForReset;
Dimensions mDims;
std::unique_ptr<Prefixes> mpPrefixes;
QString mComment;
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -959,7 +959,7 @@ bool GraphicsView::addComponent(QString className, QPointF position)
/* We use getModelInstanceIcon here 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(pLibraryTreeItem->getNameStructure(), false, true)));
pComponent->setModel(new ModelInstance::Model(MainWindow::instance()->getOMCProxy()->getModelInstance(pLibraryTreeItem->getNameStructure(), "", false, true)));
pModelInstance->addElement(pComponent);
ModelInfo oldModelInfo = mpModelWidget->createModelInfo();
addElementToView(pComponent, false, true, true, position);
Expand Down Expand Up @@ -5814,7 +5814,7 @@ void ModelWidget::loadModelInstance(bool icon, const ModelInfo &modelInfo)
// save the current ModelInstance pointer so we can delete it later.
ModelInstance::Model *pOldModelInstance = mpModelInstance;
// call getModelInstance
const QJsonObject jsonObject = MainWindow::instance()->getOMCProxy()->getModelInstance(mpLibraryTreeItem->getNameStructure(), false, icon);
const QJsonObject jsonObject = MainWindow::instance()->getOMCProxy()->getModelInstance(mpLibraryTreeItem->getNameStructure(), "", false, icon);

QElapsedTimer timer;
timer.start();
Expand Down
30 changes: 27 additions & 3 deletions OMEdit/OMEditLIB/OMC/OMCProxy.cpp
Expand Up @@ -3403,7 +3403,7 @@ QList<QString> OMCProxy::getAvailablePackageConversionsFrom(const QString &pkg,
* \param icon
* \return
*/
QJsonObject OMCProxy::getModelInstance(const QString &className, bool prettyPrint, bool icon)
QJsonObject OMCProxy::getModelInstance(const QString &className, const QString &modifier, bool prettyPrint, bool icon)
{
QElapsedTimer timer;
timer.start();
Expand All @@ -3420,10 +3420,10 @@ QJsonObject OMCProxy::getModelInstance(const QString &className, bool prettyPrin
} else {
getErrorString();
}
modelInstanceJson = mpOMCInterface->getModelInstance(className, QString(), prettyPrint);
modelInstanceJson = mpOMCInterface->getModelInstance(className, modifier, prettyPrint);
}
} else {
modelInstanceJson = mpOMCInterface->getModelInstance(className, QString(), prettyPrint);
modelInstanceJson = mpOMCInterface->getModelInstance(className, modifier, prettyPrint);
}

if (MainWindow::instance()->isNewApiProfiling()) {
Expand All @@ -3450,6 +3450,30 @@ QJsonObject OMCProxy::getModelInstance(const QString &className, bool prettyPrin
return QJsonObject();
}

/*!
* \brief OMCProxy::modifierToJSON
* Converts the modifier to JSON format.
* \param modifier
* \param prettyPrint
* \return
*/
QJsonObject OMCProxy::modifierToJSON(const QString &modifier, bool prettyPrint)
{
QString modifierJson = mpOMCInterface->modifierToJSON(modifier, prettyPrint);
printMessagesStringInternal();
if (!modifierJson.isEmpty()) {
QJsonParseError jsonParserError;
QJsonDocument doc = QJsonDocument::fromJson(modifierJson.toUtf8(), &jsonParserError);
if (doc.isNull()) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica,
QString("Failed to parse modifier to json with error %1.").arg(jsonParserError.errorString()),
Helper::scriptingKind, Helper::errorLevel));
}
return doc.object();
}
return QJsonObject();
}

/*!
* \brief OMCProxy::storeAST
* Stores the AST and return an id handle.
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/OMC/OMCProxy.h
Expand Up @@ -278,7 +278,8 @@ class OMCProxy : public QObject
QStringList getAvailablePackageVersions(QString pkg, QString version);
bool convertPackageToLibrary(const QString &packageToConvert, const QString &library, const QString &libraryVersion);
QList<QString> getAvailablePackageConversionsFrom(const QString &pkg, const QString &version);
QJsonObject getModelInstance(const QString &className, bool prettyPrint = false, bool icon = false);
QJsonObject getModelInstance(const QString &className, const QString &modifier = QString(""), bool prettyPrint = false, bool icon = false);
QJsonObject modifierToJSON(const QString &modifier, bool prettyPrint = false);
int storeAST();
bool restoreAST(int id);
bool clear();
Expand Down

0 comments on commit c84168c

Please sign in to comment.