From 7c941e4697dc3268a7a595ef09e5da9008335d55 Mon Sep 17 00:00:00 2001 From: Levi Jetzer Date: Fri, 21 Aug 2026 15:16:36 +0200 Subject: [PATCH] Guard numeric elementInformation fields against "." and normalize decimal comma - ElementInfoWidget::currentInfo() now skips a field whose validator hasn't accepted its text (e.g. a lone "." mid-typing), which previously stored and later parsed to 0. - New QETInformation::NumericInfoValidator rewrites "," to "." before validating, so 80,5 on a German/French keyboard no longer silently becomes 805. Used at both existing call sites. - Restored the header's #1/#2/#3 doc comment (was reflowed into a run-on paragraph by a previous edit). --- .../ui/elementpropertieseditorwidget.cpp | 4 +-- sources/qetinformation.cpp | 27 +++++++++++++++++++ sources/qetinformation.h | 17 +++++++++--- sources/ui/elementinfopartwidget.cpp | 12 +++++++-- sources/ui/elementinfopartwidget.h | 4 +-- sources/ui/elementinfowidget.cpp | 3 +++ 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/sources/editor/ui/elementpropertieseditorwidget.cpp b/sources/editor/ui/elementpropertieseditorwidget.cpp index e63f8338f..296831450 100644 --- a/sources/editor/ui/elementpropertieseditorwidget.cpp +++ b/sources/editor/ui/elementpropertieseditorwidget.cpp @@ -44,6 +44,7 @@ #include #include #include +#include /** @brief The EditorDelegate class @@ -69,8 +70,7 @@ class EditorDelegate : public QItemDelegate if (key == QETInformation::ELMT_WIDTH || key == QETInformation::ELMT_HEIGHT || key == QETInformation::ELMT_DEPTH) { auto *line_edit = new QLineEdit(parent); - auto *validator = new QRegularExpressionValidator( - QETInformation::numericInfoPattern(), line_edit); + auto *validator = new QETInformation::NumericInfoValidator(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 d6c2f769b..83dadd0c1 100644 --- a/sources/qetinformation.cpp +++ b/sources/qetinformation.cpp @@ -228,6 +228,33 @@ QRegularExpression QETInformation::numericInfoPattern() return QRegularExpression(QStringLiteral(R"(^[0-9]+\.?[0-9]{0,2}$|^[0-9]*\.[0-9]{1,2}$)")); } +/** + @brief QETInformation::NumericInfoValidator::NumericInfoValidator + @param parent +*/ +QETInformation::NumericInfoValidator::NumericInfoValidator(QObject *parent) : + QRegularExpressionValidator(numericInfoPattern(), parent) +{ +} + +/** + @brief QETInformation::NumericInfoValidator::validate + Rewrites any "," in @a input to "." in place, then delegates to + the base class for the actual numericInfoPattern() check. @a pos + is left untouched by the rewrite itself -- replacing "," with "." + never changes the string's length, so the cursor position the + caller already tracked stays correct. + @param input the text being validated; may be rewritten + @param pos the cursor position within @a input + @return the resulting validation state +*/ +QValidator::State QETInformation::NumericInfoValidator::validate(QString &input, int &pos) const +{ + if (input.contains(QLatin1Char(','))) + input.replace(QLatin1Char(','), QLatin1Char('.')); + return QRegularExpressionValidator::validate(input, pos); +} + /** * @brief QETInformation::infoToVar * @param info diff --git a/sources/qetinformation.h b/sources/qetinformation.h index 492258b17..5ed893311 100644 --- a/sources/qetinformation.h +++ b/sources/qetinformation.h @@ -20,6 +20,7 @@ #include #include +#include #include /** @@ -27,10 +28,12 @@ * 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) + * (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 { @@ -161,6 +164,12 @@ namespace QETInformation QString elementInfoToVar(const QString &info); QRegularExpression numericInfoPattern(); + class NumericInfoValidator : public QRegularExpressionValidator + { + public: + explicit NumericInfoValidator(QObject *parent = nullptr); + State validate(QString &input, int &pos) const override; + }; QStringList terminalElementInfoKeys(); diff --git a/sources/ui/elementinfopartwidget.cpp b/sources/ui/elementinfopartwidget.cpp index 2c44e51bd..d880f3b96 100644 --- a/sources/ui/elementinfopartwidget.cpp +++ b/sources/ui/elementinfopartwidget.cpp @@ -47,8 +47,7 @@ ElementInfoPartWidget::ElementInfoPartWidget( if (key_ == QETInformation::ELMT_WIDTH || key_ == QETInformation::ELMT_HEIGHT || key_ == QETInformation::ELMT_DEPTH) { - auto *validator = new QRegularExpressionValidator( - QETInformation::numericInfoPattern(), ui->line_edit); + auto *validator = new QETInformation::NumericInfoValidator(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)")); @@ -78,6 +77,15 @@ QString ElementInfoPartWidget::text() const return (ui->line_edit->text()); } +/** + @brief ElementInfoPartWidget::hasAcceptableInput + @return whether the line edit's current text satisfies its validator +*/ +bool ElementInfoPartWidget::hasAcceptableInput() const +{ + return ui->line_edit->hasAcceptableInput(); +} + /** @brief ElementInfoPartWidget::setText Set text to line edit diff --git a/sources/ui/elementinfopartwidget.h b/sources/ui/elementinfopartwidget.h index 5e52a066e..a79eb48d4 100644 --- a/sources/ui/elementinfopartwidget.h +++ b/sources/ui/elementinfopartwidget.h @@ -41,9 +41,9 @@ class ElementInfoPartWidget : public QWidget QWidget *parent = nullptr); ~ElementInfoPartWidget() override; - QString key () const -{return key_;} + QString key () const {return key_;} QString text () const; + bool hasAcceptableInput() const; void setText (const QString &); void setPlaceHolderText (const QString &text); void setFocusTolineEdit(); diff --git a/sources/ui/elementinfowidget.cpp b/sources/ui/elementinfowidget.cpp index 0b8a41111..ce34f34e1 100644 --- a/sources/ui/elementinfowidget.cpp +++ b/sources/ui/elementinfowidget.cpp @@ -387,6 +387,9 @@ DiagramContext ElementInfoWidget::currentInfo() const for (const auto &eipw : std::as_const(m_eipw_list)) { + if (!eipw->hasAcceptableInput()) + continue; + //add value only if they're something to store if (!eipw->text().isEmpty()) {