Skip to content

Commit

Permalink
add replaceSubModel() to GUI (#9469)
Browse files Browse the repository at this point in the history
* add replaceSubModel() to GUI

* remove signals

* move all actions and slot to Element class

* minor fixes

* remove header
  • Loading branch information
arun3688 committed Oct 17, 2022
1 parent dd49a40 commit 12071b1
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 0 deletions.
13 changes: 13 additions & 0 deletions OMEdit/OMEditLIB/Element/Element.cpp
Expand Up @@ -2746,6 +2746,19 @@ void Element::createActions()
mpElementPropertiesAction = new QAction(Helper::properties, mpGraphicsView);
mpElementPropertiesAction->setStatusTip(tr("Shows the Properties dialog"));
connect(mpElementPropertiesAction, SIGNAL(triggered()), SLOT(showElementPropertiesDialog()));
// ReplaceSubModel Action
mpReplaceSubModelAction = new QAction(ResourceCache::getIcon(":/Resources/icons/import-fmu.svg"), tr("Replace SubModel"), this);
mpReplaceSubModelAction->setStatusTip(tr("Replaces the SubModel, but retains the connections and parameters if valid"));
connect(mpReplaceSubModelAction, SIGNAL(triggered()), SLOT(showReplaceSubModelDialog()));
}

/*!
* \brief Element::showReplaceSubModelDialog
* Slot that opens up the ReplaceSubModelDialog Dialog from GraphicsView.
*/
void Element::showReplaceSubModelDialog()
{
mpGraphicsView->showReplaceSubModelDialog(this->getName());
}

void Element::createResizerItems()
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditLIB/Element/Element.h
Expand Up @@ -243,6 +243,7 @@ class Element : public QObject, public QGraphicsItem
QAction* getOpenClassAction() {return mpOpenClassAction;}
QAction* getSubModelAttributesAction() {return mpSubModelAttributesAction;}
QAction* getElementPropertiesAction() {return mpElementPropertiesAction;}
QAction* getReplaceSubModelAction() {return mpReplaceSubModelAction;}
ElementInfo* getElementInfo() {return mpElementInfo;}
QList<ShapeAnnotation*> getShapesList() {return mShapesList;}
QList<Element*> getInheritedElementsList() {return mInheritedElementsList;}
Expand Down Expand Up @@ -335,6 +336,7 @@ class Element : public QObject, public QGraphicsItem
QAction *mpOpenClassAction;
QAction *mpSubModelAttributesAction;
QAction *mpElementPropertiesAction;
QAction *mpReplaceSubModelAction;
ResizerItem *mpBottomLeftResizerItem;
ResizerItem *mpTopLeftResizerItem;
ResizerItem *mpTopRightResizerItem;
Expand Down Expand Up @@ -432,6 +434,7 @@ public slots:
void openClass();
void showSubModelAttributes();
void showElementPropertiesDialog();
void showReplaceSubModelDialog();
void updateDynamicSelect(double time);
void resetDynamicSelect();
protected:
Expand Down
12 changes: 12 additions & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -3734,6 +3734,8 @@ void GraphicsView::omsOneComponentContextMenu(Element *pComponent, QMenu *pMenu)
pMenu->addAction(mpRotateAntiClockwiseAction);
pMenu->addAction(mpFlipHorizontalAction);
pMenu->addAction(mpFlipVerticalAction);
pMenu->addSeparator();
pMenu->addAction(pComponent->getReplaceSubModelAction());
}

/*!
Expand Down Expand Up @@ -4070,6 +4072,16 @@ void GraphicsView::flipVertical()
mpModelWidget->endMacro();
}

/*!
* \brief GraphicsView::showReplaceSubModelDialog
* function that opens up the ReplaceSubModelDialog Dialog.
*/
void GraphicsView::showReplaceSubModelDialog(QString name)
{
ReplaceSubModelDialog *pReplaceFMUDialog = new ReplaceSubModelDialog(this, name);
pReplaceFMUDialog->exec();
}

/*!
* \brief GraphicsView::createConnector
* Creates a connector while making a connection.\n
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -350,6 +350,7 @@ class GraphicsView : public QGraphicsView
void removeItem(QGraphicsItem *pGraphicsItem);
void fitInViewInternal();
void emitResetDynamicSelect();
void showReplaceSubModelDialog(QString name);
private:
void createActions();
bool isClassDroppedOnItself(LibraryTreeItem *pLibraryTreeItem);
Expand Down
127 changes: 127 additions & 0 deletions OMEdit/OMEditLIB/OMS/ModelDialog.cpp
Expand Up @@ -440,6 +440,133 @@ void AddSubModelDialog::addSubModel()
}
}

/*!
* \class ReplaceSubModelDialog
* \Creates a dialog to allow users to replace a submodel to OMSimulator model.
* \brief ReplaceSubModelDialog::ReplaceSubModelDialog
* \param pGraphicsView
* \param path
* \param name
*/
ReplaceSubModelDialog::ReplaceSubModelDialog(GraphicsView *pGraphicsView, QString pName)
: QDialog(pGraphicsView)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1 - %2").arg(Helper::applicationName).arg("replaceSubModel"));
setMinimumWidth(400);
mpGraphicsView = pGraphicsView;
mpElementName = pName;
// set heading
mpHeading = Utilities::getHeadingLabel("replaceSubModel");
// set separator line
mpHorizontalLine = Utilities::getHeadingLine();

// path
mpPathLabel = new Label(Helper::path);
mpPathTextBox = new QLineEdit();
mpBrowsePathButton = new QPushButton(Helper::browse);
mpBrowsePathButton->setAutoDefault(false);
connect(mpBrowsePathButton, SIGNAL(clicked()), SLOT(browseSubModelPath()));

// dryRun
mpDryRunLabel = new Label("dryRun:");
mpDryRunLabel->setToolTip( tr("dryRun = true will not replace the subModel, you can see the list of warnings and dryRun = false will replace the SubModel"));
mpDryRunComboBox = new QComboBox;
mpDryRunComboBox->addItem("true");
mpDryRunComboBox->addItem("false");


// buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
connect(mpOkButton, SIGNAL(clicked()), SLOT(replaceSubModel()));
mpCancelButton = new QPushButton(Helper::cancel);
mpCancelButton->setAutoDefault(false);
connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));

// add buttons to the button box
mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);

// set the layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
pMainLayout->addWidget(mpHeading, 0, 0, 1, 3);
pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 3);
pMainLayout->addWidget(mpPathLabel, 2, 0);
pMainLayout->addWidget(mpPathTextBox, 2, 1);
pMainLayout->addWidget(mpBrowsePathButton, 2, 2);
pMainLayout->addWidget(mpDryRunLabel, 3, 0);
pMainLayout->addWidget(mpDryRunComboBox, 3, 1, 1, 2);
pMainLayout->addWidget(mpButtonBox, 4, 0, 1, 3, Qt::AlignRight);
setLayout(pMainLayout);
}

/*!
* \brief ReplaceSubModelDialog::browseSubModelPath
* Allows the user to select the submodel path and returns it.
*/

void ReplaceSubModelDialog::browseSubModelPath()
{
QString path = StringHandler::getOpenFileName(this, QString("%1 - %2").arg(Helper::applicationName, Helper::chooseFile), NULL, Helper::subModelFileTypes, NULL);
if (!path.isEmpty()){
mpPathTextBox->setText(path);
}
}

/*!
* \brief ReplaceSubModelDialog::replaceSubModel
* replaces a submodel to the OMSimulator model, but still retains
* the connection and other variables and parameters if they are valid
*/
void ReplaceSubModelDialog::replaceSubModel()
{
if (mpPathTextBox->text().isEmpty()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error), GUIMessages::getMessage(GUIMessages::ENTER_VALUE).arg(tr("ReplaceSubModel")), Helper::ok);
return;
}

QFileInfo fileInfo(mpPathTextBox->text());
if (!fileInfo.exists()) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error), tr("Unable to find the SubModel file."), Helper::ok);
return;
}

LibraryTreeItem *pParentLibraryTreeItem;
pParentLibraryTreeItem = mpGraphicsView->getModelWidget()->getLibraryTreeItem();

// add the submodel
QString nameStructure = QString("%1.%2").arg(pParentLibraryTreeItem->getNameStructure()).arg(mpElementName);
//qDebug() << "\nnamestructure : " << nameStructure << "==>" << mpDryRunComboBox->currentIndex();

bool dryRun;
if (mpDryRunComboBox->currentIndex() == 0){
dryRun = true;
} else {
dryRun = false;
}

bool failed = false;
int warningCount;

if (OMSProxy::instance()->replaceSubModel(nameStructure, fileInfo.absoluteFilePath(), dryRun, &warningCount)) {
mpGraphicsView->getModelWidget()->createOMSimulatorUndoCommand(QString("replace submodel %1").arg(nameStructure));
mpGraphicsView->getModelWidget()->updateModelText();
accept();
} else {
failed = true;
}

// TODO remove the dryRun combo box check and always make the first run dryRun = true and warn users in case warnings exist

if (failed) {
QMessageBox::critical(this, QString("%1 - %2").arg(Helper::applicationName, Helper::error),
tr("Failed to replace submodel. %1").arg(GUIMessages::getMessage(GUIMessages::CHECK_MESSAGES_BROWSER)), Helper::ok);
}
}

/*!
* \class AddOrEditIconDialog
* \brief Creates a dialog to allow users to add or edit icon for system or component.
Expand Down
23 changes: 23 additions & 0 deletions OMEdit/OMEditLIB/OMS/ModelDialog.h
Expand Up @@ -122,6 +122,29 @@ private slots:
void addSubModel();
};

class ReplaceSubModelDialog: public QDialog
{
Q_OBJECT
public:
ReplaceSubModelDialog(GraphicsView *pGraphicsView, QString pName);
private:
GraphicsView *mpGraphicsView;
QString mpElementName;
Label *mpHeading;
QFrame *mpHorizontalLine;
Label *mpPathLabel;
QLineEdit *mpPathTextBox;
Label *mpDryRunLabel;
QComboBox *mpDryRunComboBox;
QPushButton *mpBrowsePathButton;
QPushButton *mpOkButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
private slots:
void browseSubModelPath();
void replaceSubModel();
};

class ShapeAnnotation;
class AddOrEditIconDialog : public QDialog
{
Expand Down
20 changes: 20 additions & 0 deletions OMEdit/OMEditLIB/OMS/OMSProxy.cpp
Expand Up @@ -437,6 +437,26 @@ bool OMSProxy::addSubModel(QString cref, QString fmuPath)
return statusToBool(status);
}

/*!
* \brief OMSProxy::replaceSubModel
* \Adds the submodel to the system
* \param cref
* \param fmupath
* \param dryCount
* \param count
* \return
*/
bool OMSProxy::replaceSubModel(QString cref, QString fmuPath, bool dryCount, int *count)
{
QString command = "oms_replaceSubModel";
QStringList args;
args << "\"" + cref + "\"" << fmuPath << QString::number(dryCount);
LOG_COMMAND(command, args);
oms_status_enu_t status = oms_replaceSubModel(cref.toUtf8().constData(), fmuPath.toUtf8().constData(), dryCount, count);
logResponse(command, status, &commandTime);
return statusToBool(status);
}

/*!
* \brief OMSProxy::createElementGeometryUsingPosition
* Creates the element geometry using position.
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/OMS/OMSProxy.h
Expand Up @@ -78,6 +78,7 @@ class OMSProxy : public QObject
bool addConnectorToBus(QString busCref, QString connectorCref);
bool addConnectorToTLMBus(QString busCref, QString connectorCref, QString type);
bool addSubModel(QString cref, QString fmuPath);
bool replaceSubModel(QString cref, QString fmuPath, bool dryCount, int* count);
void createElementGeometryUsingPosition(const QString &cref, QPointF position);
bool addExternalTLMModel(QString cref, QString startScript, QString modelPath);
bool addSystem(QString cref, oms_system_enu_t type);
Expand Down

0 comments on commit 12071b1

Please sign in to comment.