Skip to content

Commit

Permalink
[NewAPI] Use storeAST/restoreAST APIs (#9309)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Aug 19, 2022
1 parent 11f19cc commit 6aebcc4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
8 changes: 5 additions & 3 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Expand Up @@ -1701,6 +1701,7 @@ void ElementParameters::updateElementParameters()
{
if (MainWindow::instance()->isNewApi()) {
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
int oldASTID = pOMCProxy->storeAST();
QString className = mpElement->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
bool valueChanged = false;
// any parameter changed
Expand Down Expand Up @@ -1795,7 +1796,7 @@ void ElementParameters::updateElementParameters()
// create OMCUndoCommand
const QJsonObject oldModelInstance = mpElement->getGraphicsView()->getModelWidget()->getModelInstance()->getModelJson();
ModelWidget *pModelWidget = mpElement->getGraphicsView()->getModelWidget();
pModelWidget->getUndoStack()->push(new OMCUndoCommand(pModelWidget->getLibraryTreeItem(), oldModelInstance, QString("Update Element %1 Parameters").arg(mpElement->getName())));
pModelWidget->getUndoStack()->push(new OMCUndoCommand(pModelWidget->getLibraryTreeItem(), oldASTID, oldModelInstance, QString("Update Element %1 Parameters").arg(mpElement->getName())));
pModelWidget->updateModelText();
}
} else {
Expand Down Expand Up @@ -2181,6 +2182,8 @@ void ElementAttributes::updateElementAttributes()
causality = "";
}
if (MainWindow::instance()->isNewApi()) {
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
int oldASTID = pOMCProxy->storeAST();
QString modelName = mpElement->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
bool attributesChanged = false;
attributesChanged |= mpElement->getModelElement()->isFinal() != mpFinalCheckBox->isChecked();
Expand All @@ -2198,7 +2201,6 @@ void ElementAttributes::updateElementAttributes()
QString isInner = mpInnerCheckBox->isChecked() ? "true" : "false";
QString isOuter = mpOuterCheckBox->isChecked() ? "true" : "false";

OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
// update component attributes if needed
if (attributesChanged) {
if (!pOMCProxy->setComponentProperties(modelName, mpElement->getName(), isFinal, flow, isProtected, isReplaceAble, variability, isInner, isOuter, causality)) {
Expand Down Expand Up @@ -2242,7 +2244,7 @@ void ElementAttributes::updateElementAttributes()
if (attributesChanged) {
const QJsonObject oldModelInstance = mpElement->getGraphicsView()->getModelWidget()->getModelInstance()->getModelJson();
ModelWidget *pModelWidget = mpElement->getGraphicsView()->getModelWidget();
pModelWidget->getUndoStack()->push(new OMCUndoCommand(pModelWidget->getLibraryTreeItem(), oldModelInstance, QString("Update Component %1 Attributes").arg(mpElement->getName())));
pModelWidget->getUndoStack()->push(new OMCUndoCommand(pModelWidget->getLibraryTreeItem(), oldASTID, oldModelInstance, QString("Update Component %1 Attributes").arg(mpElement->getName())));
pModelWidget->updateModelText();
}
} else {
Expand Down
11 changes: 9 additions & 2 deletions OMEdit/OMEditLIB/Modeling/Commands.cpp
Expand Up @@ -1736,7 +1736,6 @@ void OMSimulatorUndoCommand::redoInternal()
restoreClosedModelWidgets();
// switch to the ModelWidget where the change happened
switchToEditedModelWidget();

}

/*!
Expand Down Expand Up @@ -1792,24 +1791,32 @@ void OMSimulatorUndoCommand::switchToEditedModelWidget()
}
}

OMCUndoCommand::OMCUndoCommand(LibraryTreeItem *pLibraryTreeItem, const QJsonObject &oldModelInstanceJson, const QString &commandText, UndoCommand *pParent)
OMCUndoCommand::OMCUndoCommand(LibraryTreeItem *pLibraryTreeItem, int oldASTID, const QJsonObject &oldModelInstanceJson, const QString &commandText, UndoCommand *pParent)
: UndoCommand(pParent)
{
mpLibraryTreeItem = pLibraryTreeItem;
mOldASTID = oldASTID;
mNewASTID = MainWindow::instance()->getOMCProxy()->storeAST();
mUndoDoneOnce = false;
mOldModelInstanceJson = oldModelInstanceJson;
mNewModelInstanceJson = MainWindow::instance()->getOMCProxy()->getModelInstance(mpLibraryTreeItem->getNameStructure(), true);
setText(commandText);
}

void OMCUndoCommand::redoInternal()
{
if (mUndoDoneOnce) {
MainWindow::instance()->getOMCProxy()->restoreAST(mNewASTID);
}
if (mpLibraryTreeItem && mpLibraryTreeItem->getModelWidget()) {
mpLibraryTreeItem->getModelWidget()->reDrawModelWidget(mNewModelInstanceJson);
}
}

void OMCUndoCommand::undo()
{
MainWindow::instance()->getOMCProxy()->restoreAST(mOldASTID);
mUndoDoneOnce = true;
if (mpLibraryTreeItem && mpLibraryTreeItem->getModelWidget()) {
mpLibraryTreeItem->getModelWidget()->reDrawModelWidget(mOldModelInstanceJson);
}
Expand Down
5 changes: 4 additions & 1 deletion OMEdit/OMEditLIB/Modeling/Commands.h
Expand Up @@ -446,13 +446,16 @@ class OMSimulatorUndoCommand : public UndoCommand
class OMCUndoCommand : public UndoCommand
{
public:
OMCUndoCommand(LibraryTreeItem *pLibraryTreeItem, const QJsonObject &oldModelInstanceJson, const QString &commandText, UndoCommand *pParent = 0);
OMCUndoCommand(LibraryTreeItem *pLibraryTreeItem, int oldASTID, const QJsonObject &oldModelInstanceJson, const QString &commandText, UndoCommand *pParent = 0);
void redoInternal();
void undo();
private:
LibraryTreeItem *mpLibraryTreeItem;
QJsonObject mOldModelInstanceJson;
QJsonObject mNewModelInstanceJson;
int mOldASTID;
int mNewASTID;
bool mUndoDoneOnce;
};

#endif // COMMANDS_H
6 changes: 5 additions & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -718,6 +718,10 @@ QString getComponentName(const QString &qualifiedComponentName)
*/
void GraphicsView::deleteElement(Element *pElement)
{
int oldASTID;
if (MainWindow::instance()->isNewApi() && mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::Modelica) {
oldASTID = MainWindow::instance()->getOMCProxy()->storeAST();
}
// First remove the connections associated to this element
int i = 0;
while(i != mConnectionsList.size()) {
Expand Down Expand Up @@ -775,7 +779,7 @@ void GraphicsView::deleteElement(Element *pElement)
} else {
if (MainWindow::instance()->isNewApi()) {
deleteElementFromClass(pElement);
mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), mpModelWidget->getModelInstance()->getModelJson(), ""));
mpModelWidget->getUndoStack()->push(new OMCUndoCommand(mpModelWidget->getLibraryTreeItem(), oldASTID, mpModelWidget->getModelInstance()->getModelJson(), ""));
} else {
mpModelWidget->getUndoStack()->push(new DeleteComponentCommand(pElement, this));
}
Expand Down
25 changes: 25 additions & 0 deletions OMEdit/OMEditLIB/OMC/OMCProxy.cpp
Expand Up @@ -3449,6 +3449,31 @@ QJsonObject OMCProxy::getModelInstance(const QString &className, bool prettyPrin
return QJsonObject();
}

/*!
* \brief OMCProxy::storeAST
* Stores the AST and return an id handle.
* \return
*/
int OMCProxy::storeAST()
{
int id = mpOMCInterface->storeAST();
printMessagesStringInternal();
return id;
}

/*!
* \brief OMCProxy::restoreAST
* Restores the AST to a specific id handle.
* \param id
* \return
*/
bool OMCProxy::restoreAST(int id)
{
bool result = mpOMCInterface->restoreAST(id);
printMessagesStringInternal();
return result;
}

/*!
\class CustomExpressionBox
\brief A text box for executing OMC commands.
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/OMC/OMCProxy.h
Expand Up @@ -283,6 +283,8 @@ class OMCProxy : public QObject
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);
int storeAST();
bool restoreAST(int id);
signals:
void commandFinished();
public slots:
Expand Down

0 comments on commit 6aebcc4

Please sign in to comment.