Skip to content

Commit

Permalink
update start values with FocusOut event (#6857)
Browse files Browse the repository at this point in the history
* update start values with FocusOut event

* rename function name

* use return statement

* improve code readability

* improve variable naming

* some simplifications :D

* Remove superfluous return

* Use a reference!

Co-authored-by: Lennart Ochel <lennart.ochel@liu.se>
  • Loading branch information
arun3688 and lochel committed Oct 24, 2020
1 parent 00c75fd commit 5060eea
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
91 changes: 91 additions & 0 deletions OMEdit/OMEditLIB/OMS/ElementPropertiesDialog.cpp
Expand Up @@ -181,6 +181,7 @@ ElementPropertiesDialog::ElementPropertiesDialog(Element *pComponent, QWidget *p
QString nameStructure = QString("%1.%2").arg(mpComponent->getLibraryTreeItem()->getNameStructure(), name);
mParameterLabels.append(pNameLabel);
QLineEdit *pParameterLineEdit = new QLineEdit;
pParameterLineEdit->installEventFilter(this);
bool status = false;
if (pInterfaces[i]->type == oms_signal_type_real) {
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
Expand Down Expand Up @@ -250,6 +251,7 @@ ElementPropertiesDialog::ElementPropertiesDialog(Element *pComponent, QWidget *p
pNameLabel->setToolTip(nameStructure);
mInputLabels.append(pNameLabel);
QLineEdit *pInputLineEdit = new QLineEdit;
pInputLineEdit->installEventFilter(this);
bool status = false;
if (pInterfaces[i]->type == oms_signal_type_real) {
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
Expand Down Expand Up @@ -403,3 +405,92 @@ void ElementPropertiesDialog::updateProperties()
// accept the dialog
accept();
}

/*
* event filter for mParameterLineEdits and mInputLineEdit
* to detect the focus out event, and update
* the default start values from modeldesctiption.xml
*/
bool ElementPropertiesDialog::eventFilter(QObject *pObject, QEvent *pEvent)
{
QLineEdit *pLineEdit = qobject_cast<QLineEdit*>(pObject);

if (pLineEdit && pEvent->type() != QEvent::FocusOut) {
return QWidget::eventFilter(pObject, pEvent);
}

if (!pLineEdit->text().isEmpty()) {
return QWidget::eventFilter(pObject, pEvent);
}

if (!mpComponent->getLibraryTreeItem()->getOMSElement() || !mpComponent->getLibraryTreeItem()->getOMSElement()->connectors) {
return QWidget::eventFilter(pObject, pEvent);
}

// search the lineEdit index in parameters
int parameterIndex = mParameterLineEdits.indexOf(pLineEdit);
if (parameterIndex != -1) {
QString parameterLabelText = mParameterLabels.at(parameterIndex)->text();
deleteStartValueAndRestoreDefault(parameterLabelText, pLineEdit);
}

// search the lineEdit index in inputs
int inputIndex = mInputLineEdits.indexOf(pLineEdit);
if (inputIndex != -1) {
QString inputLabelText = mInputLabels.at(inputIndex)->text();
deleteStartValueAndRestoreDefault(inputLabelText, pLineEdit);
}

return QWidget::eventFilter(pObject, pEvent);
}

/*
* helper function to restore default start values read from modeldescription.xml for fmus
* and 0 for other systems
*/
void ElementPropertiesDialog::deleteStartValueAndRestoreDefault(const QString name, QLineEdit * pLineEdit)
{
oms_connector_t** pConnectors = mpComponent->getLibraryTreeItem()->getOMSElement()->connectors;
int i=0;
while (pConnectors[i] && QString(pConnectors[i]->name).compare(name) != 0) {
i++;
}

// no element found
if (!pConnectors[i]) {
return;
}

auto& pConnector = pConnectors[i];

// only considering parameters and inputs
if (oms_causality_parameter != pConnector->causality && oms_causality_input != pConnector->causality) {
return;
}

QString nameStructure = QString("%1.%2").arg(mpComponent->getLibraryTreeItem()->getNameStructure(), QString(pConnector->name));
OMSProxy::instance()->omsDelete(nameStructure + ":start");

bool status = false;
if (pConnector->type == oms_signal_type_real) {
double value;
if ((status = OMSProxy::instance()->getReal(nameStructure, &value))) {
pLineEdit->setText(QString::number(value));
}
} else if (pConnector->type == oms_signal_type_integer) {
int value;
if ((status = OMSProxy::instance()->getInteger(nameStructure, &value))) {
pLineEdit->setText(QString::number(value));
}
} else if (pConnector->type == oms_signal_type_boolean) {
bool value;
if ((status = OMSProxy::instance()->getBoolean(nameStructure, &value))) {
pLineEdit->setText(QString::number(value));
}
} else {
qDebug() << "ElementPropertiesDialog::deleteStartValueAndRestoreDefault() unknown signal type";
}
if (!status) {
pLineEdit->setPlaceholderText("unknown");
}
}
2 changes: 2 additions & 0 deletions OMEdit/OMEditLIB/OMS/ElementPropertiesDialog.h
Expand Up @@ -41,6 +41,7 @@ class ElementPropertiesDialog : public QDialog
Q_OBJECT
public:
ElementPropertiesDialog(Element *pComponent, QWidget *pParent = 0);
bool eventFilter(QObject *pObject, QEvent *pEvent);
private:
Element *mpComponent;
Label *mpHeading;
Expand Down Expand Up @@ -91,6 +92,7 @@ class ElementPropertiesDialog : public QDialog
QPushButton *mpOkButton;
QPushButton *mpCancelButton;
QDialogButtonBox *mpButtonBox;
void deleteStartValueAndRestoreDefault(const QString name, QLineEdit * pLineEdit);
private slots:
void updateProperties();
};
Expand Down

0 comments on commit 5060eea

Please sign in to comment.