Skip to content

Commit

Permalink
Use regular expression to validate the parameter value (#11877)
Browse files Browse the repository at this point in the history
Fixes #11840
  • Loading branch information
adeas31 committed Jan 25, 2024
1 parent e0950ab commit c19b34d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
28 changes: 17 additions & 11 deletions OMEdit/OMEditLIB/Element/ElementProperties.cpp
Expand Up @@ -772,7 +772,7 @@ void Parameter::createValueWidget()
case Parameter::Normal:
default:
mpValueTextBox = new QLineEdit;
connect(mpValueTextBox, SIGNAL(textEdited(QString)), SLOT(valueTextBoxEdited(QString)));
mpValueTextBox->installEventFilter(this);
break;
}
}
Expand Down Expand Up @@ -1049,16 +1049,6 @@ void Parameter::valueCheckBoxChanged(bool toggle)
updateValueBinding(FlatModelica::Expression(toggle));
}

/*!
* \brief Parameter::valueTextBoxEdited
* This slot is only called when user manually edits the text.\n
* \param text
*/
void Parameter::valueTextBoxEdited(const QString &text)
{
enableDisableUnitComboBox(text);
}

void Parameter::showFixedMenu()
{
// create a menu
Expand Down Expand Up @@ -1115,6 +1105,22 @@ void Parameter::inheritedFixedClicked()
mpFixedCheckBox->setTickState(true, mpFixedCheckBox->getInheritedValue());
}

/*!
* \brief Parameter::eventFilter
* Handles the FocusOut event of value textbox.
* \param pWatched
* \param pEvent
* \return
*/
bool Parameter::eventFilter(QObject *pWatched, QEvent *pEvent)
{
if (mpValueTextBox == pWatched && pEvent->type() == QEvent::FocusOut) {
enableDisableUnitComboBox(mpValueTextBox->text());
}

return QObject::eventFilter(pWatched, pEvent);
}

/*!
\class GroupBox
\brief Creates a group for parameters.
Expand Down
4 changes: 3 additions & 1 deletion OMEdit/OMEditLIB/Element/ElementProperties.h
Expand Up @@ -178,11 +178,13 @@ public slots:
void unitComboBoxChanged(int index);
void valueComboBoxChanged(int index);
void valueCheckBoxChanged(bool toggle);
void valueTextBoxEdited(const QString &text);
void showFixedMenu();
void trueFixedClicked();
void falseFixedClicked();
void inheritedFixedClicked();
// QObject interface
public:
virtual bool eventFilter(QObject *pWatched, QEvent *pEvent) override;
};

class GroupBox : public QGroupBox
Expand Down
20 changes: 4 additions & 16 deletions OMEdit/OMEditLIB/Util/Utilities.cpp
Expand Up @@ -640,23 +640,11 @@ qreal Utilities::convertUnit(qreal value, qreal offset, qreal scaleFactor)
bool Utilities::isValueLiteralConstant(QString value)
{
/* Issue #11795. Allow setting negative values for parameters.
* In future we should use proper regular expressions for this. Maybe when we switch to Qt 6.
* Issue #11840. Allow setting array of values.
* The following regular expression allows decimal values and array of decimal values. The values can be negative.
*/
if (value.compare(QStringLiteral("-")) == 0) {
return true;
}

bool ok = true;
value.toDouble(&ok);
if (ok) {
return true;
}

QStringList valuesArray = StringHandler::removeFirstLastCurlBrackets(value).split(",");
foreach (QString valueElement, valuesArray) {
valueElement.toDouble(&ok);
}
return ok;
QRegExp rx("\\{?\\s*-?\\d+(\\.\\d+)?(?:\\s*,\\s*-?\\d+(\\.\\d+)?)*\\s*\\}?");
return rx.exactMatch(value);
}

/*!
Expand Down

0 comments on commit c19b34d

Please sign in to comment.