Commit 387e69b7 authored by Joao Oliveira's avatar Joao Oliveira

Implemented support for AFPercent_Format and AFPercent_Keystroke

parent 241e807b
Pipeline #6754 canceled with stage
in 1 minute and 12 seconds
......@@ -31,6 +31,8 @@ private slots:
void testTimeFormat_data();
void testSpecialFormat();
void testSpecialFormat_data();
void testPercentFormat();
void testPercentFormat_data();
private:
Okular::Document *m_document;
......@@ -136,6 +138,45 @@ void FormatTest::testSpecialFormat_data()
QTest::newRow( "field invalid SSN" ) << QStringLiteral( "CPF" ) << QStringLiteral( "1234567890" ) << false << QStringLiteral( "123-45-6789" );
}
void FormatTest::testPercentFormat()
{
m_formattedText = QStringLiteral( "" );
QFETCH( QString, fieldName );
QFETCH( QString, text );
QFETCH( bool, edited );
QFETCH( QString, result );
Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ fieldName ] );
bool ok = false;
fft->setText( text );
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok, true );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( ok, edited );
QCOMPARE( m_formattedText, result );
}
void FormatTest::testPercentFormat_data()
{
QTest::addColumn< QString >( "fieldName" );
QTest::addColumn< QString >( "text" );
QTest::addColumn< bool >( "edited" );
QTest::addColumn< QString > ( "result" );
// The tests which have invalid edited, keep the same value as when it was formatted before.
QTest::newRow( "normal percent" ) << QStringLiteral( "pct1" ) << QStringLiteral( "1.20" ) << true << QStringLiteral( "120.00 %" );
QTest::newRow( "percent with comma thousands sep" ) << QStringLiteral( "pct1" ) << QStringLiteral( "1234.20" ) << true << QStringLiteral( "123,420.00 %" );
QTest::newRow( "invalid number" ) << QStringLiteral( "pct1" ) << QStringLiteral( "1234,20" ) << false << QStringLiteral( "" );
QTest::newRow( "normal percent 2" ) << QStringLiteral( "pct2" ) << QStringLiteral( "1.20" ) << true << QStringLiteral( "120.00 %" );
QTest::newRow( "percent without comma thousands sep" ) << QStringLiteral( "pct2" ) << QStringLiteral( "1234.20" ) << true << QStringLiteral( "123420.00 %" );
QTest::newRow( "percent with comma dot sep" ) << QStringLiteral( "pct3" ) << QStringLiteral( "1,20" ) << true << QStringLiteral( "120,00 %" );
QTest::newRow( "percent with comma dot sep and thousands dot sep" ) << QStringLiteral( "pct3" ) << QStringLiteral( "1234,20" ) << true << QStringLiteral( "123.420,00 %" );
QTest::newRow( "invalid number with dot sep" ) << QStringLiteral( "pct3" ) << QStringLiteral( "1234.20" ) << false << QStringLiteral( "" );
QTest::newRow( "normal percent 3" ) << QStringLiteral( "pct4" ) << QStringLiteral( "1,20" ) << true << QStringLiteral( "120,00 %" );
QTest::newRow( "normal percent 4 with \' as sep" ) << QStringLiteral( "pct4" ) << QStringLiteral( "1234,20" ) << true << QStringLiteral( "123420,00 %" );
}
void FormatTest::cleanupTestCase()
{
m_document->closeDocument();
......
......@@ -246,4 +246,118 @@ function AFSpecial_Keystroke( psf )
return;
}
}
}
/** AFPercent_Format
*
* Formats event.value based on parameters.
*
* Parameter description based on Acrobat Help:
*
* nDec is the number of places after the decimal point
* sepStyle is an integer denoting the style of the separator, which is one of:
* 0 - 1,234.56
* 1 - 1234.56
* 2 - 1.234,56
* 3 - 1234,56
* 4 - 1'234.56
*/
function AFPercent_Format( nDec, sepStyle )
{
if ( !event.value )
{
return;
}
var ret;
var localized = Number( event.target.value );
if( isNaN( localized ) )
{
event.value = 'NaN';
return;
}
localized = localized * 100;
localized = localized.toFixed( nDec );
var locale = 'de-DE';
var useGrouping = true;
if ( sepStyle === 0 )
{
locale = 'en-US';
}
else if ( sepStyle === 1 )
{
locale = 'en-US'
useGrouping = false;
}
else if ( sepStyle === 3 )
{
useGrouping = false;
}
var decPart = localized.substr( localized.indexOf( /\D/ )-1, nDec );
var intPart = Math.trunc( localized );
if( useGrouping )
{
intPart = intPart.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
if( locale === 'en-US' )
{
ret = intPart.toString().replace( /\./g, '.' ) + '.' + decPart;
}
else
{
ret = intPart.toString().replace( /,/g, (sepStyle === 4 ? '\'' : '.' ) ) + (sepStyle === 4 ? '.' : ',') + decPart;
}
ret = ret + ' %';
event.value = ret;
}
/** AFPercent_Keystroke
*
* Checks if the String in event.value is valid.
*
* Parameter description based on Acrobat Help:
*
* nDec is the number of places after the decimal point
* sepStyle is an integer denoting the style of the separator, which is one of:
* 0 - 1,234.56
* 1 - 1234.56
* 2 - 1.234,56
* 3 - 1234,56
* 4 - 1'234.56
*
* We only support numbers written in the form of 1234,56 or 1234.56, depending on what is sepStyle.
* If sepStyle >= 2, we use commas to separate and don't allow dots.
* If sepStyle <= 2, we use dots to separate and don't allow commas.
*/
function AFPercent_Keystroke( nDec, sepStyle )
{
if ( !event.value )
{
return;
}
var number = event.target.value;
if( sepStyle >= 2 )
{
number = number.replace( '.', 'A' );
number = number.replace( ',', '.' );
}
if( isNaN( number ) ){
event.rc = false;
event.value = 'NaN';
return;
}
event.value = number;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment