From 5dbb9f28def60d3f6bc21a81d1cd5c2e5562bc6e Mon Sep 17 00:00:00 2001 From: Levi Jetzer Date: Wed, 12 Aug 2026 08:43:42 +0200 Subject: [PATCH] Fix numeric field validation gaps found in review Address feedback on the width/height/depth elementInformation fields: - The regex accepted "." alone as a complete value ([0-9]* permits zero digits on both sides), meaning a field could be committed with a literal "." saved to the XML. Require at least one digit either before or after the separator. - The same pattern was duplicated verbatim in elementinfopartwidget.cpp and elementpropertieseditorwidget.cpp's EditorDelegate. Factored into QETInformation::numericInfoPattern(), a single shared definition both call sites now use. - Tightened the pattern to at most 2 decimal places (down from 4) to match the precision actually meaningful for these fields. Not changed, by design: - Storage stays a plain string, consistent with every other numeric elementInformation field (quantity etc.) in this codebase -- values round-trip through XML text regardless, so a long/micron representation wouldn't avoid the string<->number conversion, only relocate it. - No decimal-comma normalization needed: with the fixed pattern, only digits and "." are ever accepted at the keystroke level, so an alternate separator can't enter the field in the first place. --- .../editor/ui/elementpropertieseditorwidget.cpp | 2 +- sources/qetinformation.cpp | 12 ++++++++++++ sources/qetinformation.h | 14 +++++++++----- sources/ui/elementinfopartwidget.cpp | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/sources/editor/ui/elementpropertieseditorwidget.cpp b/sources/editor/ui/elementpropertieseditorwidget.cpp index e04b0bc01..e63f8338f 100644 --- a/sources/editor/ui/elementpropertieseditorwidget.cpp +++ b/sources/editor/ui/elementpropertieseditorwidget.cpp @@ -70,7 +70,7 @@ class EditorDelegate : public QItemDelegate { auto *line_edit = new QLineEdit(parent); auto *validator = new QRegularExpressionValidator( - QRegularExpression(QStringLiteral(R"(^[0-9]*\.?[0-9]{0,4}$)")), line_edit); + QETInformation::numericInfoPattern(), line_edit); line_edit->setValidator(validator); line_edit->setPlaceholderText(tr("ex. 80.5")); line_edit->setToolTip(tr("Nombre décimal avec un point comme séparateur (ex. 80.5)")); diff --git a/sources/qetinformation.cpp b/sources/qetinformation.cpp index d5df1cea1..d6c2f769b 100644 --- a/sources/qetinformation.cpp +++ b/sources/qetinformation.cpp @@ -216,6 +216,18 @@ QString QETInformation::elementInfoToVar(const QString &info) return (QString ("%{void}")); } +/** + * @brief QETInformation::numericInfoPattern + * @return the pattern used to validate numeric elementInformation + * fields (currently width/height/depth): digits with an optional "." + * as decimal separator, requiring at least one digit overall so a + * lone "." can never be a complete, acceptable value on its own. + */ +QRegularExpression QETInformation::numericInfoPattern() +{ + return QRegularExpression(QStringLiteral(R"(^[0-9]+\.?[0-9]{0,2}$|^[0-9]*\.[0-9]{1,2}$)")); +} + /** * @brief QETInformation::infoToVar * @param info diff --git a/sources/qetinformation.h b/sources/qetinformation.h index 569d9021b..492258b17 100644 --- a/sources/qetinformation.h +++ b/sources/qetinformation.h @@ -18,17 +18,19 @@ #ifndef QETINFORMATION_H #define QETINFORMATION_H -#include #include +#include +#include /** * Inside this namespace you will find all information used in QElectrotech for * element, conductor and diagram. * Each information have 3 values : - * #1 the info key = the key of an information as a QString used in the code (example : label) - * #2 the info key to variable = the key in form of a variable. - * This is used by the user to replace a variable by the string of this variable (example : %{label}) - * #3 the info key translated to the current local (example label in dutch = Betriebsmittelkennzeichen) + * #1 the info key = the key of an information as a QString used in the code + * (example : label) #2 the info key to variable = the key in form of a + * variable. This is used by the user to replace a variable by the string of + * this variable (example : %{label}) #3 the info key translated to the current + * local (example label in dutch = Betriebsmittelkennzeichen) */ namespace QETInformation { @@ -158,6 +160,8 @@ namespace QETInformation QStringList elementEditorElementInfoKeys(); QString elementInfoToVar(const QString &info); + QRegularExpression numericInfoPattern(); + QStringList terminalElementInfoKeys(); QString infoToVar(const QString &info); diff --git a/sources/ui/elementinfopartwidget.cpp b/sources/ui/elementinfopartwidget.cpp index dcb5f6842..2c44e51bd 100644 --- a/sources/ui/elementinfopartwidget.cpp +++ b/sources/ui/elementinfopartwidget.cpp @@ -48,7 +48,7 @@ ElementInfoPartWidget::ElementInfoPartWidget( if (key_ == QETInformation::ELMT_WIDTH || key_ == QETInformation::ELMT_HEIGHT || key_ == QETInformation::ELMT_DEPTH) { auto *validator = new QRegularExpressionValidator( - QRegularExpression(QStringLiteral(R"(^[0-9]*\.?[0-9]{0,4}$)")), ui->line_edit); + QETInformation::numericInfoPattern(), ui->line_edit); ui->line_edit->setValidator(validator); ui->line_edit->setPlaceholderText(tr("ex. 80.5")); ui->line_edit->setToolTip(tr("Nombre décimal avec un point comme séparateur (ex. 80.5)"));