Skip to content

Commit

Permalink
[NewAPI] Show replaceable choices in parameters window (#10090)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Jan 23, 2023
1 parent 84c3c16 commit f92baa2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
54 changes: 24 additions & 30 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Expand Up @@ -548,12 +548,11 @@ void Parameter::createValueWidget()
case Parameter::ReplaceableComponent:
case Parameter::ReplaceableClass:
if (MainWindow::instance()->isNewApi()) {
//! @todo constrainedBy is missing in the instance api. See #9559
// constrainedByClassName = mpElement->getElementInfo()->getConstrainedByClassName();
//! @todo choices are missing in the instance api. See #9380
// if (mpElement->hasChoices()) {
// choices = mpElement->getChoices();
// }
constrainedByClassName = mpModelInstanceElement->getReplaceable().getConstrainedby();
if (constrainedByClassName.isEmpty()) {
constrainedByClassName = mpModelInstanceElement->getType();
}
choices = mpModelInstanceElement->getChoices().getChoices();
parentClassName = mpModelInstanceElement->getParentModel()->getName();
if (mpModelInstanceElement->getModel()) {
restriction = mpModelInstanceElement->getModel()->getRestriction();
Expand Down Expand Up @@ -1777,6 +1776,7 @@ void ElementParameters::updateElementParameters()
QString className = pModelWidget->getLibraryTreeItem()->getNameStructure();
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
bool valueChanged = false;
QMap<QString, QString> elementModifiersMap;
// any parameter changed
foreach (Parameter *pParameter, mParametersList) {
QString elementModifierKey = pParameter->getNameLabel()->text();
Expand All @@ -1798,23 +1798,13 @@ void ElementParameters::updateElementParameters()
}
if (pParameter->isValueModified()) {
valueChanged = true;
/* If the element is inherited then add the modifier value into the extends. */
if (mpElement->isInheritedElement()) {
pOMCProxy->setExtendsModifierValue(className, mpElement->getModelElement()->getParentModel()->getName(), mpElement->getName() % "." % elementModifierKey, elementModifierValue);
} else {
pOMCProxy->setComponentModifierValue(className, mpElement->getName() % "." % elementModifierKey, elementModifierValue);
}
elementModifiersMap.insert(elementModifierKey, elementModifierValue);
}
if (pParameter->isShowStartAttribute() && (pParameter->getFixedState().compare(pParameter->getOriginalFixedValue()) != 0)) {
valueChanged = true;
elementModifierKey = elementModifierKey.replace(".start", ".fixed");
elementModifierValue = pParameter->getFixedState();
/* If the element is inherited then add the modifier value into the extends. */
if (mpElement->isInheritedElement()) {
pOMCProxy->setExtendsModifierValue(className, mpElement->getModelElement()->getParentModel()->getName(), mpElement->getName() % "." % elementModifierKey, elementModifierValue);
} else {
pOMCProxy->setComponentModifierValue(className, mpElement->getName() % "." % elementModifierKey, elementModifierValue);
}
elementModifiersMap.insert(elementModifierKey, elementModifierValue);
}
// remove the .start or .fixed from modifier key
if (pParameter->isShowStartAttribute()) {
Expand All @@ -1829,13 +1819,7 @@ void ElementParameters::updateElementParameters()
const QString unit = pParameter->getUnitComboBox()->itemData(pParameter->getUnitComboBox()->currentIndex()).toString();
if (pParameter->getUnitComboBox()->isEnabled() && !unit.isEmpty() && pParameter->getDisplayUnit().compare(unit) != 0) {
valueChanged = true;
/* If the element is inherited then add the modifier value into the extends. */
if (mpElement->isInheritedElement()) {
pOMCProxy->setExtendsModifierValue(className, mpElement->getModelElement()->getParentModel()->getName(), mpElement->getName() % "." % elementModifierKey % ".displayUnit",
"\"" + unit + "\"");
} else {
pOMCProxy->setComponentModifierValue(className, mpElement->getName() % "." % elementModifierKey % ".displayUnit", "\"" + unit + "\"");
}
elementModifiersMap.insert(elementModifierKey % ".displayUnit", "\"" + unit + "\"");
}
}
// any new modifier is added
Expand All @@ -1853,11 +1837,7 @@ void ElementParameters::updateElementParameters()
valueChanged = true;
QString elementModifierKey = modifier.mid(0, modifier.indexOf("("));
QString elementModifierValue = modifier.mid(modifier.indexOf("("));
if (mpElement->isInheritedElement()) {
pOMCProxy->setExtendsModifierValue(className, mpElement->getModelElement()->getParentModel()->getName(), mpElement->getName() % "." % elementModifierKey, elementModifierValue);
} else {
pOMCProxy->setComponentModifierValue(className, mpElement->getName() % "." % elementModifierKey, elementModifierValue);
}
elementModifiersMap.insert(elementModifierKey, elementModifierValue);
} else {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, GUIMessages::getMessage(GUIMessages::WRONG_MODIFIER).arg(modifier),
Helper::scriptingKind, Helper::errorLevel));
Expand All @@ -1866,6 +1846,20 @@ void ElementParameters::updateElementParameters()
}
// if valueChanged is true then put the change in the undo stack.
if (valueChanged) {
// remove all the modifiers of a component.
pOMCProxy->removeComponentModifiers(className, mpElement->getName());
// apply the new Component modifiers if any
QMap<QString, QString>::iterator newElementModifier;
for (newElementModifier = elementModifiersMap.begin(); newElementModifier != elementModifiersMap.end(); ++newElementModifier) {
QString modifierValue = newElementModifier.value();
QString modifierKey = QString(mpElement->getName() % "." % newElementModifier.key());
// if the element is inherited then add the modifier value into the extends.
if (mpElement->isInheritedElement()) {
pOMCProxy->setExtendsModifierValue(className, mpElement->getModelElement()->getParentModel()->getName(), modifierKey, modifierValue);
} else {
pOMCProxy->setComponentModifierValue(className, modifierKey, modifierValue);
}
}
ModelInfo newModelInfo = pModelWidget->createModelInfo();
pModelWidget->getUndoStack()->push(new OMCUndoCommand(pModelWidget->getLibraryTreeItem(), oldModelInfo, newModelInfo, QString("Update Element %1 Parameters").arg(mpElement->getName())));
pModelWidget->updateModelText();
Expand Down
42 changes: 32 additions & 10 deletions OMEdit/OMEditLIB/Modeling/Model.cpp
Expand Up @@ -567,6 +567,23 @@ namespace ModelInstance
return "";
}

Replaceable::Replaceable()
{
mIsReplaceable = false;
mConstrainedby = "";
}

void Replaceable::deserialize(const QJsonValue &jsonValue)
{
if (jsonValue.isObject()) {
mIsReplaceable = true;
QJsonObject replaceable = jsonValue.toObject();
mConstrainedby = replaceable.value("constrainedby").toString();
} else {
mIsReplaceable = jsonValue.toBool();
}
}

Model::Model()
{
initialize();
Expand Down Expand Up @@ -1138,6 +1155,20 @@ namespace ModelInstance
if (jsonObject.contains("__Dymola_checkBox")) {
mDymolaCheckBox.deserialize(jsonObject.value("__Dymola_checkBox"));
}

if (jsonObject.contains("choice")) {
QJsonArray choices = jsonObject.value("choice").toArray();
foreach (auto choice, choices) {
if (choice.isObject()) {
QJsonObject choiceObject = choice.toObject();
if (choiceObject.contains("$value")) {
mChoice.append(choiceObject.value("$value").toString());
}
} else {
mChoice.append(choice.toString());
}
}
}
}

Element::Element(Model *pParentModel)
Expand Down Expand Up @@ -1170,7 +1201,6 @@ namespace ModelInstance
mFinal = false;
mInner = false;
mOuter = false;
mReplaceable = false;
mRedeclare = false;
mConnector = "";
mVariability = "";
Expand Down Expand Up @@ -1261,14 +1291,7 @@ namespace ModelInstance
}

if (prefixes.contains("replaceable")) {
auto replaceable = prefixes.value("replaceble");

if (replaceable.isObject()) {
mReplaceable = true;
// constrainedby stuff goes here
} else {
mReplaceable = replaceable.toBool();
}
mReplaceable.deserialize(prefixes.value("replaceable"));
}

if (prefixes.contains("redeclare")) {
Expand Down Expand Up @@ -1621,7 +1644,6 @@ namespace ModelInstance
Model::setModelJson(jsonObject.value("baseClass").toObject());
Model::deserialize();
}

}

}
25 changes: 23 additions & 2 deletions OMEdit/OMEditLIB/Modeling/Model.h
Expand Up @@ -301,6 +301,23 @@ namespace ModelInstance
static QString getModifierValue(const Modifier &modifier, const QString &modifierName, QStringList qualifiedModifierName);
};

class Replaceable
{
public:
Replaceable();
void deserialize(const QJsonValue &jsonValue);

bool isReplaceable() const {return mIsReplaceable;}
void setReplaceable(bool replaceable) {mIsReplaceable = replaceable;}
QString getConstrainedby() const {return mConstrainedby;}
void setConstrainedby(const QString &constrainedby) {mConstrainedby = constrainedby;}
private:
bool mIsReplaceable;
QString mConstrainedby;
Modifier mModifier;
QString mComment;
};

class Extend;
class Element;
class Connection;
Expand Down Expand Up @@ -477,11 +494,14 @@ namespace ModelInstance
public:
Choices();
void deserialize(const QJsonObject &jsonObject);

bool isCheckBox() const {return mCheckBox;}
bool isDymolaCheckBox() const {return mDymolaCheckBox;}
QStringList getChoices() const {return mChoice;}
private:
BooleanAnnotation mCheckBox;
BooleanAnnotation mDymolaCheckBox;
QStringList mChoice;
};

class Element
Expand Down Expand Up @@ -512,7 +532,8 @@ namespace ModelInstance
bool isFinal() const {return mFinal;}
bool isInner() const {return mInner;}
bool isOuter() const {return mOuter;}
bool isReplaceable() const {return mReplaceable;}
bool isReplaceable() const {return mReplaceable.isReplaceable();}
Replaceable getReplaceable() const {return mReplaceable;}
bool isRedeclare() const {return mRedeclare;}
QString getConnector() const {return mConnector;}
QString getVariability() const {return mVariability;}
Expand All @@ -537,7 +558,7 @@ namespace ModelInstance
bool mFinal;
bool mInner;
bool mOuter;
bool mReplaceable;
Replaceable mReplaceable;
bool mRedeclare;
QString mConnector;
QString mVariability;
Expand Down

0 comments on commit f92baa2

Please sign in to comment.