Skip to content

Commit

Permalink
Update the Libraries Browser with CLI commands (#8100)
Browse files Browse the repository at this point in the history
Fixes #8052
For now only the addition and deletion commands related to models are handled.
  • Loading branch information
adeas31 committed Nov 8, 2021
1 parent 257a47e commit bf6b3cd
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
2 changes: 0 additions & 2 deletions OMEdit/OMEditLIB/Annotations/TextAnnotation.cpp
Expand Up @@ -35,8 +35,6 @@
#include "TextAnnotation.h"
#include "Modeling/Commands.h"

#include "FlatModelica/Expression.h"

/*!
* \class TextAnnotation
* \brief Draws the text shapes.
Expand Down
15 changes: 15 additions & 0 deletions OMEdit/OMEditLIB/FlatModelica/Expression.cpp
Expand Up @@ -542,6 +542,7 @@ namespace FlatModelica

value_t type() const override { return value_t::call; }
bool isLiteral() const override { return false; }
const std::string& name() const { return _name; }
bool isNamed(const std::string &name) const { return _name == name; }
const std::vector<Expression>& args() const { return _args; }
void setArg(size_t index, const Expression &e);
Expand Down Expand Up @@ -1767,6 +1768,20 @@ namespace FlatModelica
return QString::fromStdString(dynamic_cast<const String&>(*_value).value());
}

/*!
* \brief Expression::functionName
* Checks if the Expression is a function call and then returns the function name
* otherwise return empty string.
* \return The QString value of the Expression function name.
*/
QString Expression::functionName() const
{
if (_value && _value->type() == ExpressionBase::value_t::call) {
return QString::fromStdString(dynamic_cast<const Call&>(*_value).name());
}
return QString("");
}

/*!
* \brief Expression::toString
* Unparses the Expression into a string.
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/FlatModelica/Expression.h
Expand Up @@ -88,6 +88,7 @@ namespace FlatModelica
bool boolValue() const;
std::string stringValue() const;
QString QStringValue() const;
QString functionName() const;

std::string toString() const;
QString toQString() const;
Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1872,9 +1872,10 @@ void LibraryTreeModel::showHideProtectedClasses()
* Unloads/deletes the Modelica class.
* \param pLibraryTreeItem
* \param askQuestion
* \param doDeleteClass
* \return
*/
bool LibraryTreeModel::unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQuestion)
bool LibraryTreeModel::unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQuestion, bool doDeleteClass)
{
if (askQuestion) {
QMessageBox *pMessageBox = new QMessageBox(MainWindow::instance());
Expand Down Expand Up @@ -1904,7 +1905,7 @@ bool LibraryTreeModel::unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQu
/* Delete the class in OMC.
* If deleteClass is successful remove the class from Library Browser and delete the corresponding ModelWidget.
*/
if (MainWindow::instance()->getOMCProxy()->deleteClass(pLibraryTreeItem->getNameStructure())) {
if (!doDeleteClass || MainWindow::instance()->getOMCProxy()->deleteClass(pLibraryTreeItem->getNameStructure())) {
removeLibraryTreeItem(pLibraryTreeItem);
if (!pLibraryTreeItem->isTopLevel()) {
LibraryTreeItem *pContainingFileParentLibraryTreeItem = getContainingFileParentLibraryTreeItem(pLibraryTreeItem);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h
Expand Up @@ -313,7 +313,7 @@ class LibraryTreeModel : public QAbstractItemModel
LibraryTreeItem* getLibraryTreeItemFromFile(QString fileName, int lineNumber);
void showModelWidget(LibraryTreeItem *pLibraryTreeItem, bool show = true);
void showHideProtectedClasses();
bool unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
bool unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true, bool doDeleteClass = true);
bool unloadCompositeModelOrTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
bool unloadOMSModel(LibraryTreeItem *pLibraryTreeItem, bool doDelete = true, bool askQuestion = true);
void getExpandedLibraryTreeItemsList(LibraryTreeItem *pLibraryTreeItem, QStringList *pExpandedLibraryTreeItemsList);
Expand Down
45 changes: 45 additions & 0 deletions OMEdit/OMEditLIB/OMC/OMCProxy.cpp
Expand Up @@ -56,6 +56,7 @@ void omc_Main_setWindowsPaths(threadData_t *threadData, void* _inOMHome);
#include "Modeling/MessagesWidget.h"
#include "simulation_options.h"
#include "omc_error.h"
#include "FlatModelica/Expression.h"

#include <QMessageBox>

Expand Down Expand Up @@ -124,6 +125,18 @@ OMCProxy::OMCProxy(threadData_t* threadData, QWidget *pParent)
mUnitConversionList.clear();
mDerivedUnitsMap.clear();
setLoggingEnabled(true);
mLibrariesBrowserAdditionCommandsList << "loadFile"
<< "loadFiles"
<< "loadEncryptedPackage"
<< "loadString"
<< "loadFileInteractive"
<< "loadFileInteractiveQualified"
<< "loadModel"
<< "newModel"
<< "createModel";
mLibrariesBrowserDeletionCommandsList << "deleteClass"
<< "clear"
<< "clearProgram";
//start the server
if(!initializeOMC(threadData)) { // if we are unable to start OMC. Exit the application.
MainWindow::instance()->setExitApplicationStatus(true);
Expand Down Expand Up @@ -309,6 +322,38 @@ void OMCProxy::sendCommand(const QString expression, bool saveToHistory)
double elapsed = (double)commandTime.elapsed() / 1000.0;
logResponse(expression, mResult.trimmed(), elapsed, saveToHistory);

/* Check if any custom command updates the program.
* saveToHistory is true for custom commands.
* Fixes issuse #8052
*/
if (saveToHistory) {
FlatModelica::Expression exp = FlatModelica::Expression::parse(expression);

if (mLibrariesBrowserAdditionCommandsList.contains(exp.functionName())) {
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->loadDependentLibraries(getClassNames());
} else if (mLibrariesBrowserDeletionCommandsList.contains(exp.functionName())) {
if (exp.functionName().compare(QStringLiteral("deleteClass")) == 0) {
if (exp.args().size() > 0) {
LibraryTreeItem *pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(exp.arg(0).toQString());
if (pLibraryTreeItem) {
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->unloadClass(pLibraryTreeItem, false, false);
}
}
} else {
int i = 0;
while (i < MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->getRootLibraryTreeItem()->childrenSize()) {
LibraryTreeItem *pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->getRootLibraryTreeItem()->child(i);
if (pLibraryTreeItem && pLibraryTreeItem->getLibraryType() == LibraryTreeItem::Modelica) {
MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->unloadClass(pLibraryTreeItem, false, false);
i = 0; //Restart iteration
} else {
i++;
}
}
}
}
}

MMC_ELSE()
mResult = "";
fprintf(stderr, "Stack overflow detected and was not caught.\nSend us a bug report at https://trac.openmodelica.org/OpenModelica/newticket\n Include the following trace:\n");
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/OMC/OMCProxy.h
Expand Up @@ -80,6 +80,8 @@ class OMCProxy : public QObject
QMap<QString, QList<QString> > mDerivedUnitsMap;
OMCInterface *mpOMCInterface;
bool mIsLoggingEnabled;
QStringList mLibrariesBrowserAdditionCommandsList;
QStringList mLibrariesBrowserDeletionCommandsList;
public:
OMCProxy(threadData_t *threadData, QWidget *pParent = 0);
~OMCProxy();
Expand Down

0 comments on commit bf6b3cd

Please sign in to comment.