Skip to content

Commit

Permalink
Fixed redo/undo for submodel parameters in composite models.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbr48 authored and adeas31 committed Feb 1, 2017
1 parent b219433 commit 46db737
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 26 deletions.
38 changes: 26 additions & 12 deletions OMEdit/OMEditGUI/Component/ComponentProperties.cpp
Expand Up @@ -1448,9 +1448,9 @@ void MetaModelSubModelAttributes::setUpDialog()
mpParametersLayout->addWidget(mpParametersLabel,0,0,1,2);
mpParametersScrollArea = new QScrollArea;
mpParametersScrollArea->setWidgetResizable(true);
QWidget *pScrollWidget = new QWidget;
pScrollWidget->setLayout(mpParametersLayout);
mpParametersScrollArea->setWidget(pScrollWidget);
mpParametersScrollWidget = new QWidget;
mpParametersScrollWidget->setLayout(mpParametersLayout);
mpParametersScrollArea->setWidget(mpParametersScrollWidget);
// Create the buttons
mpOkButton = new QPushButton(Helper::ok);
mpOkButton->setAutoDefault(true);
Expand Down Expand Up @@ -1510,6 +1510,7 @@ void MetaModelSubModelAttributes::initializeDialog()
mpParametersLayout->addWidget(mParameterLabels.last(),i+1,0);
mpParametersLayout->addWidget(mParameterLineEdits.last(),i+1,1);
}
mpParametersScrollWidget->setVisible(!parameters.isEmpty());
mpParametersLabel->setVisible(!parameters.isEmpty());
}

Expand Down Expand Up @@ -1564,22 +1565,35 @@ void MetaModelSubModelAttributes::updateSubModelParameters()
newComponentInfo.setStartCommand(mpStartCommandTextBox->text());
newComponentInfo.setExactStep(mpExactStepCheckBox->isChecked());
newComponentInfo.setGeometryFile(mpGeometryFileTextBox->text());


QStringList parameterNames, oldParameterValues, newParameterValues;
if(mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
BaseEditor *pBaseEditor = mpComponent->getGraphicsView()->getModelWidget()->getEditor();
MetaModelEditor *pEditor = qobject_cast<MetaModelEditor*>(pBaseEditor);

parameterNames = pEditor->getParameterNames(mpComponent->getName()); //Assume submodel; otherwise returned list is empty
foreach(QString parName, parameterNames) {
oldParameterValues.append(pEditor->getParameterValue(mpComponent->getName(), parName));
}

for(int i=0; i<mParameterLineEdits.size(); ++i) {
newParameterValues.append(mParameterLineEdits[i]->text());
}
}

// If user has really changed the Component's attributes then push that change on the stack.
if (oldComponentInfo != newComponentInfo) {
if (oldComponentInfo != newComponentInfo || oldParameterValues != newParameterValues) {
UpdateSubModelAttributesCommand *pUpdateSubModelAttributesCommand = new UpdateSubModelAttributesCommand(mpComponent, oldComponentInfo,
newComponentInfo);
newComponentInfo,
parameterNames,
oldParameterValues,
newParameterValues);
ModelWidget *pModelWidget = mpComponent->getGraphicsView()->getModelWidget();
pModelWidget->getUndoStack()->push(pUpdateSubModelAttributesCommand);
pModelWidget->updateModelText();
}

for(int i=0; i<mParameterLabels.size(); ++i) {
QString parameterName = mParameterLabels[i]->text();
QString parameterValue = mParameterLineEdits[i]->text();
MetaModelEditor *pEditor = dynamic_cast<MetaModelEditor*>(mpComponent->getGraphicsView()->getModelWidget()->getEditor());
pEditor->setParameterValue(mpComponent->getName(), parameterName, parameterValue);
}

accept();
}

Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Component/ComponentProperties.h
Expand Up @@ -241,6 +241,7 @@ class MetaModelSubModelAttributes : public QDialog
QLineEdit *mpGeometryFileTextBox;
QPushButton *mpGeometryFileBrowseButton;
QScrollArea *mpParametersScrollArea;
QWidget *mpParametersScrollWidget;
QGridLayout *mpParametersLayout;
QLabel *mpParametersLabel;
QList<QLabel*> mParameterLabels;
Expand Down
31 changes: 19 additions & 12 deletions OMEdit/OMEditGUI/Editors/MetaModelEditor.cpp
Expand Up @@ -163,21 +163,28 @@ QString MetaModelEditor::getParameterValue(QString subModelName, QString paramet

void MetaModelEditor::setParameterValue(QString subModelName, QString parameterName, QString value)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModelElement = subModelList.at(i).toElement();
if (subModelElement.attribute("Name").compare(subModelName) == 0) {
QDomElement parameterElement = subModelElement.firstChildElement("Parameter");
while(!parameterElement.isNull()) {
if(parameterElement.attribute("Name").compare(parameterName) == 0) {
parameterElement.setAttribute("Value",value);
}
parameterElement = parameterElement.nextSiblingElement("Parameter");
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModelElement = subModelList.at(i).toElement();
if (subModelElement.attribute("Name").compare(subModelName) == 0) {
QDomElement parameterElement = subModelElement.firstChildElement("Parameter");
while(!parameterElement.isNull()) {
if(parameterElement.attribute("Name").compare(parameterName) == 0) {
parameterElement.setAttribute("Value",value);
setPlainText(mXmlDocument.toString());
return;
}
setPlainText(mXmlDocument.toString());
return;
parameterElement = parameterElement.nextSiblingElement("Parameter");
}
//Parameter not found, insert it
QDomElement parameterPoint = mXmlDocument.createElement("Parameter");
parameterPoint.setAttribute("Name", parameterName);
parameterPoint.setAttribute("Value", value);
subModelElement.appendChild(parameterPoint);
setPlainText(mXmlDocument.toString());
return;
}
}
}

/*!
Expand Down
38 changes: 37 additions & 1 deletion OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -675,6 +675,15 @@ DeleteComponentCommand::DeleteComponentCommand(Component *pComponent, GraphicsVi
mpIconGraphicsView = pGraphicsView->getModelWidget()->getIconGraphicsView();
mpDiagramGraphicsView = pGraphicsView->getModelWidget()->getDiagramGraphicsView();
mpGraphicsView = pGraphicsView;

//Save sub-model parameters for meta models
if(pGraphicsView->getModelWidget()->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
MetaModelEditor *pEditor = qobject_cast<MetaModelEditor*>(pGraphicsView->getModelWidget()->getEditor());
mParameterNames = pEditor->getParameterNames(pComponent->getName()); //Assume submodel; otherwise returned list is empty
foreach(QString parName, mParameterNames) {
mParameterValues.append(pEditor->getParameterValue(pComponent->getName(), parName));
}
}
}

/*!
Expand Down Expand Up @@ -743,6 +752,14 @@ void DeleteComponentCommand::undo()
mpComponent->emitAdded();
}
mpGraphicsView->addComponentToClass(mpComponent);

//Restore sub-model parameters for meta models
if(pModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
MetaModelEditor *pEditor = qobject_cast<MetaModelEditor*>(pModelWidget->getEditor());
for(int i=0; i<mParameterNames.size(); ++i) {
pEditor->setParameterValue(mpComponent->getName(),mParameterNames[i],mParameterValues[i]);
}
}
}

AddConnectionCommand::AddConnectionCommand(LineAnnotation *pConnectionLineAnnotation, bool addConnection, QUndoCommand *pParent)
Expand Down Expand Up @@ -1141,13 +1158,20 @@ void UpdateClassSimulationFlagsAnnotationCommand::undo()
}

UpdateSubModelAttributesCommand::UpdateSubModelAttributesCommand(Component *pComponent, const ComponentInfo &oldComponentInfo,
const ComponentInfo &newComponentInfo, QUndoCommand *pParent)
const ComponentInfo &newComponentInfo,
QStringList &parameterNames, QStringList &oldParameterValues,
QStringList &newParameterValues, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpComponent = pComponent;
mOldComponentInfo.updateComponentInfo(&oldComponentInfo);
mNewComponentInfo.updateComponentInfo(&newComponentInfo);
setText(QString("Update SubModel %1 Attributes").arg(mpComponent->getName()));

//Save sub-model parameters for meta models
mParameterNames = parameterNames;
mOldParameterValues = oldParameterValues;
mNewParameterValues = newParameterValues;
}

/*!
Expand All @@ -1162,6 +1186,12 @@ void UpdateSubModelAttributesCommand::redo()
mpComponent->getComponentInfo()->setStartCommand(mNewComponentInfo.getStartCommand());
mpComponent->getComponentInfo()->setExactStep(mNewComponentInfo.getExactStep());
mpComponent->getComponentInfo()->setGeometryFile(mNewComponentInfo.getGeometryFile());

if(mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
for(int i=0; i<mParameterNames.size(); ++i) {
pMetaModelEditor->setParameterValue(mpComponent->getName(), mParameterNames[i], mNewParameterValues[i]);
}
}
}

/*!
Expand All @@ -1176,6 +1206,12 @@ void UpdateSubModelAttributesCommand::undo()
mpComponent->getComponentInfo()->setStartCommand(mOldComponentInfo.getStartCommand());
mpComponent->getComponentInfo()->setExactStep(mOldComponentInfo.getExactStep());
mpComponent->getComponentInfo()->setGeometryFile(mOldComponentInfo.getGeometryFile());

if(mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
for(int i=0; i<mParameterNames.size(); ++i) {
pMetaModelEditor->setParameterValue(mpComponent->getName(), mParameterNames[i], mOldParameterValues[i]);
}
}
}

UpdateSimulationParamsCommand::UpdateSimulationParamsCommand(LibraryTreeItem *pLibraryTreeItem, QString oldStartTime, QString newStartTime, QString oldStopTime,
Expand Down
8 changes: 7 additions & 1 deletion OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -145,6 +145,8 @@ class DeleteComponentCommand : public QUndoCommand
GraphicsView *mpIconGraphicsView;
GraphicsView *mpDiagramGraphicsView;
GraphicsView *mpGraphicsView;
QStringList mParameterNames;
QStringList mParameterValues;
};

class AddConnectionCommand : public QUndoCommand
Expand Down Expand Up @@ -244,13 +246,17 @@ class UpdateSubModelAttributesCommand : public QUndoCommand
{
public:
UpdateSubModelAttributesCommand(Component *pComponent, const ComponentInfo &oldComponentInfo, const ComponentInfo &newComponentInfo,
QUndoCommand *pParent = 0);
QStringList &parameterNames, QStringList &oldParameterValues,
QStringList &newParameterValues, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
Component *mpComponent;
ComponentInfo mOldComponentInfo;
ComponentInfo mNewComponentInfo;
QStringList mParameterNames;
QStringList mOldParameterValues;
QStringList mNewParameterValues;
};

class UpdateSimulationParamsCommand : public QUndoCommand
Expand Down

0 comments on commit 46db737

Please sign in to comment.