Skip to content

Commit

Permalink
Undo/redo for Component parameters modifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Nov 2, 2015
1 parent c790357 commit 760f38b
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 32 deletions.
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Component/Component.h
Expand Up @@ -142,6 +142,7 @@ class Component : public QObject, public QGraphicsItem
LibraryTreeItem* getLibraryTreeItem() {return mpLibraryTreeItem;}
QString getName() {return mpComponentInfo->getName();}
GraphicsView* getGraphicsView() {return mpGraphicsView;}
Component *getReferenceComponent() {return mpReferenceComponent;}
Component* getParentComponent() {return mpParentComponent;}
Component* getRootParentComponent();
ComponentType getComponentType() {return mComponentType;}
Expand Down
80 changes: 48 additions & 32 deletions OMEdit/OMEditGUI/Component/ComponentProperties.cpp
Expand Up @@ -59,6 +59,7 @@ Parameter::Parameter(Component *pComponent, bool showStartAttribute, QString tab
mpNameLabel = new Label;
mpFixedCheckBox = new FixedCheckBox;
connect(mpFixedCheckBox, SIGNAL(clicked()), SLOT(showFixedMenu()));
mOriginalFixedValue = "";
// set the value type based on component type.
OMCProxy *pOMCProxy = mpComponent->getGraphicsView()->getModelWidget()->getModelWidgetContainer()->getMainWindow()->getOMCProxy();
if (mpComponent->getComponentInfo()->getClassName().compare("Boolean") == 0) {
Expand Down Expand Up @@ -166,6 +167,7 @@ QString Parameter::getValue()

void Parameter::setFixedState(QString fixed, bool defaultValue)
{
mOriginalFixedValue = fixed;
if (fixed.compare("true") == 0) {
mpFixedCheckBox->setTickState(defaultValue, true);
} else {
Expand Down Expand Up @@ -758,73 +760,87 @@ Parameter* ComponentParameters::findParameter(const QString &parameter, Qt::Case
}

/*!
Slot activated when mpOkButton clicked signal is raised.\n
Checks the list of parameters i.e mParametersList and if the value is changed then sets the new value.
*/
* \brief ComponentParameters::updateComponentParameters
* Slot activated when mpOkButton clicked signal is raised.\n
* Checks the list of parameters i.e mParametersList and if the value is changed then sets the new value.
*/
void ComponentParameters::updateComponentParameters()
{
bool valueChanged = false;
bool modifierValueChanged = false;
OMCProxy *pOMCProxy = mpMainWindow->getOMCProxy();
QMap<QString, QString> newComponentModifiersMap;
QMap<QString, QString> newComponentExtendsModifiersMap;
// any parameter changed
foreach (Parameter *pParameter, mParametersList) {
QString className = mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
QString componentModifier = QString(mpComponent->getName()).append(".").append(pParameter->getNameLabel()->text());
QString componentModifierKey = QString(mpComponent->getName()).append(".").append(pParameter->getNameLabel()->text());
QString componentModifierValue = pParameter->getValue();
if (pParameter->isValueModified()) {
valueChanged = true;
QString componentModifierValue = pParameter->getValue();
/* If the component is inherited then add the modifier value into the extends. */
if (mpComponent->isInheritedComponent()) {
if (pOMCProxy->setExtendsModifierValue(className, mpComponent->getInheritedClassName(), componentModifier,
componentModifierValue.prepend("=")))
modifierValueChanged = true;
newComponentExtendsModifiersMap.insert(componentModifierKey, componentModifierValue);
} else {
if (pOMCProxy->setComponentModifierValue(className, componentModifier, componentModifierValue.prepend("="))) {
modifierValueChanged = true;
}
newComponentModifiersMap.insert(componentModifierKey, componentModifierValue);
}
}
if (pParameter->isShowStartAttribute()) {
if (pParameter->isShowStartAttribute() && (pParameter->getFixedState().compare(pParameter->getOriginalFixedValue()) != 0)) {
valueChanged = true;
componentModifier = componentModifier.replace(".start", ".fixed");
QString componentModifierValue = pParameter->getFixedState();
componentModifierKey = componentModifierKey.replace(".start", ".fixed");
componentModifierValue = pParameter->getFixedState();
/* If the component is inherited then add the modifier value into the extends. */
if (mpComponent->isInheritedComponent()) {
if (pOMCProxy->setExtendsModifierValue(className, mpComponent->getInheritedClassName(), componentModifier,
componentModifierValue.prepend("=")))
modifierValueChanged = true;
newComponentExtendsModifiersMap.insert(componentModifierKey, componentModifierValue);
} else {
if (pOMCProxy->setComponentModifierValue(className, componentModifier, componentModifierValue.prepend("="))) {
modifierValueChanged = true;
}
newComponentModifiersMap.insert(componentModifierKey, componentModifierValue);
}
}
}
// add modifiers
// any new modifier is added
if (!mpModifiersTextBox->text().isEmpty()) {
QString regexp ("\\s*([A-Za-z0-9]+\\s*)\\(\\s*([A-Za-z0-9]+)\\s*=\\s*([A-Za-z0-9]+)\\s*\\)$");
QRegExp modifierRegExp (regexp);
QStringList modifiers = mpModifiersTextBox->text().split(",", QString::SkipEmptyParts);
foreach (QString modifier, modifiers) {
modifier = modifier.trimmed();
if (modifierRegExp.exactMatch(modifier)) {
QString className = mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
QString componentModifier = QString(mpComponent->getName()).append(".").append(modifier.mid(0, modifier.indexOf("(")));
QString componentModifierValue = modifier.mid(modifier.indexOf("("));
pOMCProxy->setComponentModifierValue(className, componentModifier, componentModifierValue);
valueChanged = true;
QString componentModifierKey = QString(mpComponent->getName()).append(".").append(modifier.mid(0, modifier.indexOf("(")));
QString componentModifierValue = modifier.mid(modifier.indexOf("("));
newComponentModifiersMap.insert(componentModifierKey, componentModifierValue);
} else {
mpMainWindow->getMessagesWidget()->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0,
GUIMessages::getMessage(GUIMessages::WRONG_MODIFIER).arg(modifier),
Helper::scriptingKind, Helper::errorLevel));
}
}
}
// if valueChanged is true then set the model modified.
// if valueChanged is true then put the change in the undo stack.
if (valueChanged) {
mpComponent->getGraphicsView()->getModelWidget()->updateModelicaText();
if (modifierValueChanged) {
mpComponent->componentParameterHasChanged();
// save the Component modifiers
QString className = mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
QMap<QString, QString> oldComponentModifiersMap;
QStringList componentModifiersList = mpMainWindow->getOMCProxy()->getComponentModifierNames(className, mpComponent->getName());
foreach (QString componentModifier, componentModifiersList) {
QString originalModifierName = QString(mpComponent->getName()).append(".").append(componentModifier);
QString componentModifierValue = mpMainWindow->getOMCProxy()->getComponentModifierValue(className, originalModifierName);
oldComponentModifiersMap.insert(componentModifier, componentModifierValue);
}
// save the Component extends modifiers
QMap<QString, QString> oldComponentExtendsModifiersMap;
if (mpComponent->getReferenceComponent()) {
QString inheritedClassName = mpComponent->getReferenceComponent()->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
QStringList extendsModifiersList = mpMainWindow->getOMCProxy()->getExtendsModifierNames(className, inheritedClassName);
foreach (QString extendsModifier, extendsModifiersList) {
QString componentModifierValue = mpMainWindow->getOMCProxy()->getExtendsModifierValue(className, inheritedClassName, extendsModifier);
oldComponentExtendsModifiersMap.insert(extendsModifier, componentModifierValue);
}
}
// create UpdateComponentParametersCommand
UpdateComponentParametersCommand *pUpdateComponentParametersCommand;
pUpdateComponentParametersCommand = new UpdateComponentParametersCommand(mpComponent, oldComponentModifiersMap,
oldComponentExtendsModifiersMap, newComponentModifiersMap,
newComponentExtendsModifiersMap);
mpComponent->getGraphicsView()->getModelWidget()->getUndoStack()->push(pUpdateComponentParametersCommand);
mpComponent->getGraphicsView()->getModelWidget()->updateModelicaText();
}
accept();
}
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Component/ComponentProperties.h
Expand Up @@ -61,6 +61,7 @@ class Parameter : public QObject
void updateNameLabel();
Label* getNameLabel() {return mpNameLabel;}
FixedCheckBox* getFixedCheckBox() {return mpFixedCheckBox;}
QString getOriginalFixedValue() {return mOriginalFixedValue;}
void setValueType(ValueType valueType) {mValueType = valueType;}
void setValueWidget(QString value, bool defaultValue);
ValueType getValueType() {return mValueType;}
Expand All @@ -80,6 +81,7 @@ class Parameter : public QObject
bool mShowStartAttribute;
Label *mpNameLabel;
FixedCheckBox *mpFixedCheckBox;
QString mOriginalFixedValue;
ValueType mValueType;
QComboBox *mpValueComboBox;
QLineEdit *mpValueTextBox;
Expand Down
72 changes: 72 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -32,6 +32,7 @@
*/

#include "Commands.h"
#include "ComponentProperties.h"

AddShapeCommand::AddShapeCommand(ShapeAnnotation *pShapeAnnotation, QUndoCommand *pParent)
: QUndoCommand(pParent)
Expand Down Expand Up @@ -552,6 +553,77 @@ void UpdateComponentAttributesCommand::undo()
}
}

UpdateComponentParametersCommand::UpdateComponentParametersCommand(Component *pComponent, QMap<QString, QString> oldComponentModifiersMap,
QMap<QString, QString> oldComponentExtendsModifiersMap,
QMap<QString, QString> newComponentModifiersMap,
QMap<QString, QString> newComponentExtendsModifiersMap,
QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpComponent = pComponent;
mOldComponentModifiersMap = oldComponentModifiersMap;
mOldComponentExtendsModifiersMap = oldComponentExtendsModifiersMap;
mNewComponentModifiersMap = newComponentModifiersMap;
mNewComponentExtendsModifiersMap = newComponentExtendsModifiersMap;
setText(QString("Update Component %1 Parameters").arg(mpComponent->getName()));
}

/*!
* \brief UpdateComponentParametersCommand::redo
* Redo the UpdateComponentParametersCommand.
*/
void UpdateComponentParametersCommand::redo()
{
OMCProxy *pOMCProxy = mpComponent->getGraphicsView()->getModelWidget()->getModelWidgetContainer()->getMainWindow()->getOMCProxy();
QString className = mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
// apply the new Component modifiers if any
QMap<QString, QString>::iterator componentModifier;
for (componentModifier = mNewComponentModifiersMap.begin(); componentModifier != mNewComponentModifiersMap.end(); ++componentModifier) {
QString modifierValue = componentModifier.value();
pOMCProxy->setComponentModifierValue(className, componentModifier.key(), modifierValue.prepend("="));
}
// apply the new Component extends modifiers if any
if (mpComponent->getReferenceComponent()) {
QString inheritedClassName = mpComponent->getReferenceComponent()->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
QMap<QString, QString>::iterator componentExtendsModifier;
for (componentExtendsModifier = mNewComponentExtendsModifiersMap.begin(); componentExtendsModifier != mNewComponentExtendsModifiersMap.end(); ++componentExtendsModifier) {
QString modifierValue = componentExtendsModifier.value();
pOMCProxy->setExtendsModifierValue(className, inheritedClassName, componentExtendsModifier.key(), modifierValue.prepend("="));
}
}
mpComponent->componentParameterHasChanged();
}

/*!
* \brief UpdateComponentParametersCommand::undo
* Undo the UpdateComponentParametersCommand.
*/
void UpdateComponentParametersCommand::undo()
{
OMCProxy *pOMCProxy = mpComponent->getGraphicsView()->getModelWidget()->getModelWidgetContainer()->getMainWindow()->getOMCProxy();
QString className = mpComponent->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
// remove all the modifiers of a component.
pOMCProxy->removeComponentModifiers(className, mpComponent->getName());
// apply the old Component modifiers if any
QMap<QString, QString>::iterator componentModifier;
for (componentModifier = mOldComponentModifiersMap.begin(); componentModifier != mOldComponentModifiersMap.end(); ++componentModifier) {
QString modifierValue = componentModifier.value();
pOMCProxy->setComponentModifierValue(className, componentModifier.key(), modifierValue.prepend("="));
}
if (mpComponent->getReferenceComponent()) {
QString inheritedClassName = mpComponent->getReferenceComponent()->getGraphicsView()->getModelWidget()->getLibraryTreeItem()->getNameStructure();
// remove all the extends modifiers.
pOMCProxy->removeExtendsModifiers(className, inheritedClassName);
// apply the new Component extends modifiers if any
QMap<QString, QString>::iterator componentExtendsModifier;
for (componentExtendsModifier = mOldComponentExtendsModifiersMap.begin(); componentExtendsModifier != mOldComponentExtendsModifiersMap.end(); ++componentExtendsModifier) {
QString modifierValue = componentExtendsModifier.value();
pOMCProxy->setExtendsModifierValue(className, inheritedClassName, componentExtendsModifier.key(), modifierValue.prepend("="));
}
}
mpComponent->componentParameterHasChanged();
}

DeleteComponentCommand::DeleteComponentCommand(Component *pComponent, GraphicsView *pGraphicsView, QUndoCommand *pParent)
: QUndoCommand(pParent)
{
Expand Down
17 changes: 17 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -117,6 +117,23 @@ class UpdateComponentAttributesCommand : public QUndoCommand
QMap<QString, QString> mComponentModifiersMap;
};

class Parameter;
class UpdateComponentParametersCommand : public QUndoCommand
{
public:
UpdateComponentParametersCommand(Component *pComponent, QMap<QString, QString> oldComponentModifiersMap,
QMap<QString, QString> oldComponentExtendsModifiersMap, QMap<QString, QString> newComponentModifiersMap,
QMap<QString, QString> newComponentExtendsModifiersMap, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
Component *mpComponent;
QMap<QString, QString> mOldComponentModifiersMap;
QMap<QString, QString> mOldComponentExtendsModifiersMap;
QMap<QString, QString> mNewComponentModifiersMap;
QMap<QString, QString> mNewComponentExtendsModifiersMap;
};

class DeleteComponentCommand : public QUndoCommand
{
public:
Expand Down
24 changes: 24 additions & 0 deletions OMEdit/OMEditGUI/OMC/OMCProxy.cpp
Expand Up @@ -1003,6 +1003,18 @@ bool OMCProxy::setComponentModifierValue(QString className, QString modifierName
}
}

/*!
* \brief OMCProxy::removeComponentModifiers
* Removes all the modifiers of a component.
* \param className
* \param name
* \return
*/
bool OMCProxy::removeComponentModifiers(QString className, QString name)
{
return mpOMCInterface->removeComponentModifiers(className, name);
}

QStringList OMCProxy::getExtendsModifierNames(QString className, QString extendsClassName)
{
sendCommand("getExtendsModifierNames(" + className + "," + extendsClassName + ", useQuotes = true)");
Expand Down Expand Up @@ -1050,6 +1062,18 @@ bool OMCProxy::isExtendsModifierFinal(QString className, QString extendsClassNam
return StringHandler::unparseBool(getResult());
}

/*!
* \brief OMCProxy::removeExtendsModifiers
* Removes the extends modifier of a class.
* \param className
* \param extendsClassName
* \return
*/
bool OMCProxy::removeExtendsModifiers(QString className, QString extendsClassName)
{
return mpOMCInterface->removeExtendsModifiers(className, extendsClassName);
}

/*!
Gets the Icon Annotation of a specified class from OMC.
\param className - is the name of the class.
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/OMC/OMCProxy.h
Expand Up @@ -131,10 +131,12 @@ class OMCProxy : public QObject
QStringList getComponentModifierNames(QString className, QString name);
QString getComponentModifierValue(QString className, QString name);
bool setComponentModifierValue(QString className, QString name, QString modifierValue);
bool removeComponentModifiers(QString className, QString name);
QStringList getExtendsModifierNames(QString className, QString extendsClassName);
QString getExtendsModifierValue(QString className, QString extendsClassName, QString modifierName);
bool setExtendsModifierValue(QString className, QString extendsClassName, QString modifierName, QString modifierValue);
bool isExtendsModifierFinal(QString className, QString extendsClassName, QString modifierName);
bool removeExtendsModifiers(QString className, QString extendsClassName);
QString getIconAnnotation(QString className);
QString getDiagramAnnotation(QString className);
int getConnectionCount(QString className);
Expand Down

0 comments on commit 760f38b

Please sign in to comment.