mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-02 16:44:16 +02:00
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).
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
#include <QSplitter>
|
||||
#include <QShortcut>
|
||||
#include <QMenu>
|
||||
#include <QRegularExpressionValidator>
|
||||
|
||||
/**
|
||||
@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)"));
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <QHash>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionValidator>
|
||||
#include <QStringList>
|
||||
|
||||
/**
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user