Created a new attribute to fieldformtext for storing the locale of the current text, defined it as en_US for calculation event and before passing it to JS for execution always cast it to system locale.

This commit is contained in:
Pratham Gandhi 2024-01-16 21:47:04 +05:30
parent ebc2a33667
commit 532980dd13
5 changed files with 49 additions and 4 deletions

View File

@ -1203,6 +1203,7 @@ void DocumentPrivate::recalculateForms()
// Update text field from calculate
m_scripter->setEvent(nullptr);
const QString newVal = event->value().toString();
fft->setTextLocale(QStringLiteral("en_US"));
if (newVal != oldVal) {
fft->setText(newVal);
fft->setAppearanceText(newVal);

View File

@ -220,6 +220,16 @@ int FormFieldText::maximumLength() const
return -1;
}
void FormFieldText::setTextLocale(const QString &locale)
{
m_locale = locale;
}
const QString FormFieldText::textLocale() const
{
return m_locale;
}
Qt::Alignment FormFieldText::textAlignment() const
{
return Qt::AlignVCenter | Qt::AlignLeft;

View File

@ -329,12 +329,24 @@ public:
*/
virtual void setAppearanceText(const QString &text) = 0;
/*
* Sets the locale for the stored text.
*/
void setTextLocale(const QString &locale);
/*
* The locale associated with the text field.
*/
const QString textLocale() const;
protected:
FormFieldText();
private:
Q_DECLARE_PRIVATE(FormFieldText)
Q_DISABLE_COPY(FormFieldText)
QString m_locale;
};
/**

View File

@ -222,7 +222,18 @@ std::shared_ptr<Event> Event::createFormatEvent(FormField *target, Page *targetP
FormFieldText *fft = dynamic_cast<FormFieldText *>(target);
if (fft) {
ret->setValue(QVariant(fft->text()));
QString eventVal = fft->text();
if (!fft->textLocale().isEmpty()) {
// Swap the text presumably of double to system locale to avoid conversion issues in JS
// If not a number, then it will simply pass the original fft text.
QLocale currentTextLocale(fft->textLocale()), systemLocale;
bool ok;
double converted = currentTextLocale.toDouble(fft->text(), &ok);
if (ok) {
eventVal = systemLocale.toString(converted);
}
}
ret->setValue(QVariant(eventVal));
}
return ret;
}

View File

@ -123,13 +123,24 @@ QJSValue JSField::fieldGetValueCore(bool asString) const
}
case FormField::FormText: {
const FormFieldText *text = static_cast<const FormFieldText *>(m_field);
const QLocale locale;
const QLocale systemLocale;
bool ok;
const double textAsNumber = locale.toDouble(text->text(), &ok);
double textAsNumber;
if (!text->textLocale().isEmpty()) {
const QLocale currentTextLocale(text->textLocale());
textAsNumber = currentTextLocale.toDouble(text->text(), &ok);
} else {
textAsNumber = systemLocale.toDouble(text->text(), &ok);
}
if (ok && !asString) {
result = textAsNumber;
} else {
result = text->text();
// Converting back and forth between locales here to avoid conversion issues between locales
if (ok) {
result = systemLocale.toString(textAsNumber);
} else {
result = text->text();
}
}
break;
}