Skip to content

Commit

Permalink
Allow editing parameter record via the parameters dialog (#12319)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Apr 26, 2024
1 parent 17e1427 commit d4fe8e1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
62 changes: 44 additions & 18 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,11 @@ Parameter::Parameter(ModelInstance::Element *pElement, bool defaultValue, Elemen
mValueType = Parameter::ReplaceableComponent;
}
if (mpModelInstanceElement->getModel() || mpModelInstanceElement->isShortClassDefinition()) {
mpEditRedeclareClassButton = new QToolButton;
mpEditRedeclareClassButton->setIcon(QIcon(":/Resources/icons/edit-icon.svg"));
mpEditRedeclareClassButton->setToolTip(tr("Edit"));
mpEditRedeclareClassButton->setAutoRaise(true);
connect(mpEditRedeclareClassButton, SIGNAL(clicked()), SLOT(editRedeclareClassButtonClicked()));
createEditClassButton();
}
} else if (mpModelInstanceElement->getModel() && mpModelInstanceElement->getModel()->isRecord()) {
mValueType = Parameter::Record;
createEditClassButton();
} else if (mpModelInstanceElement->getAnnotation()->getChoices() && !mpModelInstanceElement->getAnnotation()->getChoices()->getChoices().isEmpty()) {
mValueType = Parameter::Choices;
} else if (mpModelInstanceElement->getAnnotation()->isChoicesAllMatching()) {
Expand Down Expand Up @@ -504,6 +503,7 @@ void Parameter::setValueWidget(QString value, bool defaultValue, QString fromUni
mpValueCheckBox->blockSignals(signalsState);
mValueCheckBoxModified = valueModified && !defaultValue;
break;
case Parameter::Record:
case Parameter::Normal:
default:
if (defaultValue) {
Expand Down Expand Up @@ -541,6 +541,7 @@ QWidget* Parameter::getValueWidget()
return mpValueComboBox;
case Parameter::CheckBox:
return mpValueCheckBox;
case Parameter::Record:
case Parameter::Normal:
default:
return mpValueTextBox;
Expand All @@ -564,6 +565,7 @@ bool Parameter::isValueModified()
return mpValueComboBox->lineEdit()->isModified() || isValueModifiedHelper();
case Parameter::CheckBox:
return mValueCheckBoxModified || isValueModifiedHelper();
case Parameter::Record:
case Parameter::Normal:
default:
return mpValueTextBox->isModified() || isValueModifiedHelper();
Expand All @@ -587,6 +589,7 @@ QString Parameter::getValue()
return mpValueComboBox->lineEdit()->text().trimmed();
case Parameter::CheckBox:
return mpValueCheckBox->isChecked() ? "true" : "false";
case Parameter::Record:
case Parameter::Normal:
default:
return mpValueTextBox->text().trimmed();
Expand Down Expand Up @@ -626,6 +629,7 @@ void Parameter::setEnabled(bool enable)
case Parameter::CheckBox:
mpValueCheckBox->setEnabled(enable);
break;
case Parameter::Record:
case Parameter::Normal:
default:
mpValueTextBox->setEnabled(enable);
Expand All @@ -636,8 +640,8 @@ void Parameter::setEnabled(bool enable)
if (enable) {
enableDisableUnitComboBox(getValue());
}
if (mpEditRedeclareClassButton) {
mpEditRedeclareClassButton->setEnabled(enable);
if (mpEditClassButton) {
mpEditClassButton->setEnabled(enable);
}
}

Expand All @@ -651,6 +655,19 @@ void Parameter::update()
setEnabled(mEnable);
}

/*!
* \brief Parameter::createEditClassButton
* Creates the edit class button.
*/
void Parameter::createEditClassButton()
{
mpEditClassButton = new QToolButton;
mpEditClassButton->setIcon(QIcon(":/Resources/icons/edit-icon.svg"));
mpEditClassButton->setToolTip(tr("Edit"));
mpEditClassButton->setAutoRaise(true);
connect(mpEditClassButton, SIGNAL(clicked()), SLOT(editClassButtonClicked()));
}

void Parameter::createValueWidget()
{
int i;
Expand Down Expand Up @@ -792,7 +809,7 @@ void Parameter::createValueWidget()

connect(mpValueComboBox, SIGNAL(currentIndexChanged(int)), SLOT(valueComboBoxChanged(int)));
break;

case Parameter::Record:
case Parameter::Normal:
default:
mpValueTextBox = new QLineEdit;
Expand Down Expand Up @@ -871,16 +888,23 @@ bool Parameter::isValueModifiedHelper() const
}

/*!
* \brief Parameter::editRedeclareClassButtonClicked
* Slot activate when mpEditRedeclareClassButton clicked signal is raised.
* \brief Parameter::editClassButtonClicked
* Slot activate when mpEditClassButton clicked signal is raised.
* Opens ElementParameters dialog for the redeclare class.
*/
void Parameter::editRedeclareClassButtonClicked()
void Parameter::editClassButtonClicked()
{
QString type;
const QString value = mpValueComboBox->lineEdit()->text();
QString value;
QString defaultValue;
if (mValueType == Parameter::Record) {
value = mpValueTextBox->text();
defaultValue = mpValueTextBox->placeholderText();
} else {
value = mpValueComboBox->lineEdit()->text();
defaultValue = mpValueComboBox->lineEdit()->placeholderText();
}
QString modifier = value;
const QString defaultValue = mpValueComboBox->lineEdit()->placeholderText();
QString defaultModifier = defaultValue;
ModelInstance::Modifier *pReplaceableConstrainedByModifier = 0;
QString comment;
Expand All @@ -891,6 +915,8 @@ void Parameter::editRedeclareClassButtonClicked()
auto pReplaceableClass = dynamic_cast<ModelInstance::ReplaceableClass*>(mpModelInstanceElement);
type = pReplaceableClass->getBaseClass();
pReplaceableConstrainedByModifier = pReplaceableClass->getReplaceable()->getModifier();
} else if (mValueType == Parameter::Record) {
type = mpModelInstanceElement->getType();
}
// parse the Modelica code of element redeclaration to get the type and modifiers
if (value.startsWith("redeclare")) {
Expand All @@ -900,7 +926,7 @@ void Parameter::editRedeclareClassButtonClicked()
FlatModelica::Parser::getShortClassTypeFromElementRedeclaration(value, type, modifier, comment);
}
}
// get type as qualified path

if ((mpModelInstanceElement == NULL) ||
(mpModelInstanceElement->getTopLevelExtendElement() == NULL) ||
(mpModelInstanceElement->getTopLevelExtendElement()->getParentModel() == NULL) ||
Expand All @@ -909,7 +935,7 @@ void Parameter::editRedeclareClassButtonClicked()
QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
tr("Unable to find the redeclare class."), Helper::ok);
}

// get type as qualified path
const QString qualifiedType = MainWindow::instance()->getOMCProxy()->qualifyPath(mpModelInstanceElement->getTopLevelExtendElement()->getParentModel()->getName(), type);
// if we fail to find the type
if (qualifiedType.isEmpty()) {
Expand All @@ -935,7 +961,7 @@ void Parameter::editRedeclareClassButtonClicked()
MainWindow::instance()->getStatusBar()->clearMessage();
if (pElementParameters->exec() == QDialog::Accepted) {
const QString modification = pElementParameters->getModification();
if (mValueType == Parameter::ReplaceableComponent) {
if (mValueType == Parameter::ReplaceableComponent || mValueType == Parameter::Record) {
if (value.startsWith("redeclare")) {
QString elementRedeclaration = "redeclare " % type % " " % mpModelInstanceElement->getName() % modification;
if (!comment.isEmpty()) {
Expand Down Expand Up @@ -1542,8 +1568,8 @@ void ElementParameters::setUpDialog()
}
pGroupBoxGridLayout->addWidget(pParameter->getValueWidget(), layoutIndex, columnIndex++);

if (pParameter->getEditRedeclareClassButton()) {
pGroupBoxGridLayout->addWidget(pParameter->getEditRedeclareClassButton(), layoutIndex, columnIndex++);
if (pParameter->getEditClassButton()) {
pGroupBoxGridLayout->addWidget(pParameter->getEditClassButton(), layoutIndex, columnIndex++);
} else {
pGroupBoxGridLayout->addItem(new QSpacerItem(1, 1), layoutIndex, columnIndex++);
}
Expand Down
8 changes: 5 additions & 3 deletions OMEdit/OMEditLIB/Element/ElementProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Parameter : public QObject
Enumeration,
ReplaceableComponent,
ReplaceableClass,
Record,
Choices,
ChoicesAllMatching
};
Expand Down Expand Up @@ -107,7 +108,7 @@ class Parameter : public QObject
QWidget* getValueWidget();
bool isValueModified();
QString getValue();
QToolButton *getEditRedeclareClassButton() const {return mpEditRedeclareClassButton;}
QToolButton *getEditClassButton() const {return mpEditClassButton;}
FinalEachToolButton *getFinalEachMenu() const {return mpFinalEachMenuButton;}
QToolButton* getFileSelectorButton() {return mpFileSelectorButton;}
void setLoadSelectorFilter(QString loadSelectorFilter) {mLoadSelectorFilter = loadSelectorFilter;}
Expand Down Expand Up @@ -162,7 +163,7 @@ class Parameter : public QObject
QComboBox *mpValueComboBox;
QLineEdit *mpValueTextBox;
QCheckBox *mpValueCheckBox;
QToolButton *mpEditRedeclareClassButton = 0;
QToolButton *mpEditClassButton = 0;
FinalEachToolButton *mpFinalEachMenuButton = 0;
QToolButton *mpFileSelectorButton;
bool mHasDisplayUnit = false;
Expand All @@ -173,12 +174,13 @@ class Parameter : public QObject
FinalEachToolButton *mpDisplayUnitFinalEachMenuButton = 0;
Label *mpCommentLabel;

void createEditClassButton();
void createValueWidget();
void enableDisableUnitComboBox(const QString &value);
void updateValueBinding(const FlatModelica::Expression expression);
bool isValueModifiedHelper() const;
public slots:
void editRedeclareClassButtonClicked();
void editClassButtonClicked();
void fileSelectorButtonClicked();
void unitComboBoxChanged(int index);
void valueComboBoxChanged(int index);
Expand Down
5 changes: 5 additions & 0 deletions OMEdit/OMEditLIB/Modeling/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,11 @@ namespace ModelInstance
return getRootType() == QLatin1String("enumeration");
}

bool Model::isRecord() const
{
return (mRestriction.compare(QStringLiteral("record")) == 0);
}

bool Model::isType() const
{
return (mRestriction.compare(QStringLiteral("type")) == 0);
Expand Down
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Modeling/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ namespace ModelInstance
bool isConnector() const;
bool isExpandableConnector() const;
bool isEnumeration() const;
bool isRecord() const;
bool isType() const;
bool isDerivedType() const;
bool isPartial() const;
Expand Down

0 comments on commit d4fe8e1

Please sign in to comment.