Compare commits

...

5 Commits

7 changed files with 76 additions and 10 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

@ -171,6 +171,9 @@ void FormFieldButton::setIcon(Okular::FormField *)
class Okular::FormFieldTextPrivate : public Okular::FormFieldPrivate
{
private:
QString m_locale;
public:
FormFieldTextPrivate()
: FormFieldPrivate(FormField::FormText)
@ -220,6 +223,18 @@ int FormFieldText::maximumLength() const
return -1;
}
void FormFieldText::setTextLocale(const QString &locale)
{
Q_D(FormFieldText);
d->m_locale = locale;
}
const QString FormFieldText::textLocale() const
{
Q_D(const FormFieldText);
return d->m_locale;
}
Qt::Alignment FormFieldText::textAlignment() const
{
return Qt::AlignVCenter | Qt::AlignLeft;

View File

@ -329,6 +329,16 @@ 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();

View File

@ -81,7 +81,7 @@ function AFSimple_Calculate( cFunction, cFields )
ret /= cFields.length;
}
event.value = util.numberToString( ret, "g", 32 );
event.value = util.numberToString( ret, "g", 32, 'en_US');
}
@ -126,7 +126,7 @@ function AFNumber_Format( nDec, sepStyle, negStyle, currStyle, strCurrency, bCur
{
// Use de_DE as the locale for the dot separator format
ret = util.numberToString( localized, "f", nDec, 'de_DE' );
event.target.locale = 'de_DE';
if ( sepStyle === 3 )
{
// No thousands separators. Remove all dots from the DE format.
@ -137,6 +137,7 @@ function AFNumber_Format( nDec, sepStyle, negStyle, currStyle, strCurrency, bCur
{
// Otherwise US
ret = util.numberToString( localized, "f", nDec, 'en_US' );
event.target.locale = 'en_US';
if ( sepStyle === 1 )
{

View File

@ -250,7 +250,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, 'g', 32);
}
}
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, 'g', 32);
} else {
result = text->text();
}
}
break;
}
@ -213,6 +224,20 @@ void JSField::setHidden(bool hidden)
updateField(m_field);
}
// Field.locale (getter)
QString JSField::locale() const
{
FormFieldText *fft = dynamic_cast<FormFieldText *>(m_field);
return fft->textLocale();
}
// Field.locale (setter)
void JSField::setLocale(const QString &locale)
{
FormFieldText *fft = dynamic_cast<FormFieldText *>(m_field);
fft->setTextLocale(locale);
}
// Field.display (getter)
int JSField::display() const
{

View File

@ -23,10 +23,11 @@ class JSField : public QObject
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(bool readonly READ readonly WRITE setReadonly) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QString type READ type CONSTANT)
Q_PROPERTY(QJSValue value READ value WRITE setValue) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QJSValue valueAsString READ valueAsString) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(bool hidden READ hidden WRITE setHidden) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(int display READ display WRITE setDisplay) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QJSValue value READ value WRITE setValue) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QJSValue valueAsString READ valueAsString) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(bool hidden READ hidden WRITE setHidden) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(int display READ display WRITE setDisplay) // clazy:exclude=qproperty-without-notify
Q_PROPERTY(QString locale READ locale WRITE setLocale) // clazy:exclude=qproperty-without-notify
public:
explicit JSField(FormField *field, QObject *parent = nullptr);
@ -47,6 +48,8 @@ public:
QJSValue valueAsString() const;
bool hidden() const;
void setHidden(bool hidden);
QString locale() const;
void setLocale(const QString &locale);
Q_INVOKABLE QJSValue buttonGetIcon(int nFace = 0) const;
Q_INVOKABLE void buttonSetIcon(const QJSValue &oIcon, int nFace = 0);