Compare commits

...

6 Commits

Author SHA1 Message Date
Pratham Gandhi
ba1a68361a Merge branch 'work/pgandhi/form-calculation-fix' into 'master'
Convert to system locale string before passing to Javascript for execution

See merge request graphics/okular!914
2024-06-24 20:55:01 +00:00
Pratham Gandhi
e5900f9a4f created global qobject 2024-06-24 10:39:38 +05:30
Pratham Gandhi
19fdf818cc Moved m_locale to pimpl. Now recording the format stored after AFNumber_Format is called 2024-01-21 21:46:44 +05:30
Pratham Gandhi
262e2faadd Increased toString precision 2024-01-21 19:04:01 +05:30
Pratham Gandhi
e4e43a8f91 Output of AFSimple_Calculate shall be in en_US 2024-01-21 19:04:01 +05:30
Pratham Gandhi
532980dd13 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. 2024-01-21 19:04:01 +05:30
11 changed files with 120 additions and 10 deletions

View File

@ -487,6 +487,7 @@ if (TARGET Qt6::Qml)
core/script/js_util.cpp
core/script/js_event.cpp
core/script/js_ocg.cpp
core/script/js_global.cpp
)
target_link_libraries(okularcore PRIVATE Qt6::Qml)
endif()

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

@ -20,6 +20,7 @@
#include "js_event_p.h"
#include "js_field_p.h"
#include "js_fullscreen_p.h"
#include "js_global_p.h"
#include "js_ocg_p.h"
#include "js_spell_p.h"
#include "js_util_p.h"
@ -71,6 +72,7 @@ void ExecutorJSPrivate::initTypes()
m_interpreter.globalObject().setProperty(QStringLiteral("display"), m_interpreter.newQObject(new JSDisplay));
m_interpreter.globalObject().setProperty(QStringLiteral("spell"), m_interpreter.newQObject(new JSSpell));
m_interpreter.globalObject().setProperty(QStringLiteral("util"), m_interpreter.newQObject(new JSUtil));
m_interpreter.globalObject().setProperty(QStringLiteral("global"), m_interpreter.newQObject(new JSGlobal));
}
ExecutorJS::ExecutorJS(DocumentPrivate *doc)

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);

16
core/script/js_global.cpp Normal file
View File

@ -0,0 +1,16 @@
/*
SPDX-FileCopyrightText: 2024 Pratham Gandhi <ppg.1382@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "js_global_p.h"
using namespace Okular;
JSGlobal::JSGlobal(QObject *parent)
: QObject(parent)
{
}
JSGlobal::~JSGlobal() = default;

25
core/script/js_global_p.h Normal file
View File

@ -0,0 +1,25 @@
/*
SPDX-FileCopyrightText: 2024 Pratham Gandhi <ppg.1382@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef OKULAR_SCRIPT_JS_GLOBAL_P_H
#define OKULAR_SCRIPT_JS_GLOBAL_P_H
#include <QObject>
namespace Okular
{
class JSGlobal : public QObject
{
Q_OBJECT
public:
explicit JSGlobal(QObject *parent = nullptr);
~JSGlobal() override;
};
}
#endif