Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Also respect other locale specific characters
  • Loading branch information
nyalldawson committed Jan 8, 2020
1 parent 545edcf commit 664ba23
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 4 deletions.
70 changes: 70 additions & 0 deletions python/core/auto_generated/numericformats/qgsnumericformat.sip.in
Expand Up @@ -57,6 +57,76 @@ Returns the decimal separator character.
Returns the decimal separator character.

.. seealso:: :py:func:`setDecimalSeparator`
%End

QChar percent() const;
%Docstring
Returns the percent character.

.. seealso:: :py:func:`setPercent`
%End

void setPercent( const QChar &character );
%Docstring
Sets the percent ``character``.

.. seealso:: :py:func:`percent`
%End

QChar zeroDigit() const;
%Docstring
Returns the zero digit character.

.. seealso:: :py:func:`setZeroDigit`
%End

void setZeroDigit( const QChar &character );
%Docstring
Returns the zero digit ``character``.

.. seealso:: :py:func:`zeroDigit`
%End

QChar negativeSign() const;
%Docstring
Returns the negative sign character.

.. seealso:: :py:func:`setNegativeSign`
%End

void setNegativeSign( const QChar &character );
%Docstring
Sets the negative sign ``character``.

.. seealso:: :py:func:`negativeSign`
%End

QChar positiveSign() const;
%Docstring
Returns the positive sign character.

.. seealso:: :py:func:`setPositiveSign`
%End

void setPositiveSign( const QChar &character );
%Docstring
Sets the positive sign ``character``.

.. seealso:: :py:func:`positiveSign`
%End

QChar exponential() const;
%Docstring
Returns the exponential character.

.. seealso:: :py:func:`setExponential`
%End

void setExponential( const QChar &character );
%Docstring
Sets the exponential ``character``.

.. seealso:: :py:func:`exponential`
%End

};
Expand Down
6 changes: 3 additions & 3 deletions src/core/numericformats/qgsbasicnumericformat.cpp
Expand Up @@ -70,21 +70,21 @@ QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericForma
QString res = QString::fromStdString( os.str() );

if ( mShowPlusSign && value > 0 )
res.prepend( '+' );
res.prepend( context.positiveSign() );

if ( !mShowTrailingZeros && res.contains( context.decimalSeparator() ) )
{
int trimPoint = res.length() - 1;
int ePoint = 0;
if ( mUseScientific )
{
while ( res.at( trimPoint ) != 'e' && res.at( trimPoint ) != 'E' )
while ( res.at( trimPoint ).toUpper() != context.exponential().toUpper() )
trimPoint--;
ePoint = trimPoint;
trimPoint--;
}

while ( res.at( trimPoint ) == '0' )
while ( res.at( trimPoint ) == context.zeroDigit() )
trimPoint--;

if ( res.at( trimPoint ) == context.decimalSeparator() )
Expand Down
5 changes: 5 additions & 0 deletions src/core/numericformats/qgsnumericformat.cpp
Expand Up @@ -24,6 +24,11 @@ QgsNumericFormatContext::QgsNumericFormatContext()
QLocale l;
mThousandsSep = l.groupSeparator();
mDecimalSep = l.decimalPoint();
mPercent = l.percent();
mZeroDigit = l.zeroDigit();
mNegativeSign = l.negativeSign();
mPositiveSign = l.positiveSign();
mExponential = l.exponential();
}

int QgsNumericFormat::sortKey()
Expand Down
105 changes: 105 additions & 0 deletions src/core/numericformats/qgsnumericformat.h
Expand Up @@ -83,9 +83,114 @@ class CORE_EXPORT QgsNumericFormatContext
mDecimalSep = separator;
}

/**
* Returns the percent character.
*
* \see setPercent()
*/
QChar percent() const
{
return mPercent;
}

/**
* Sets the percent \a character.
*
* \see percent()
*/
void setPercent( const QChar &character )
{
mPercent = character;
}

/**
* Returns the zero digit character.
*
* \see setZeroDigit()
*/
QChar zeroDigit() const
{
return mZeroDigit;
}

/**
* Returns the zero digit \a character.
*
* \see zeroDigit()
*/
void setZeroDigit( const QChar &character )
{
mZeroDigit = character;
}

/**
* Returns the negative sign character.
*
* \see setNegativeSign()
*/
QChar negativeSign() const
{
return mNegativeSign;
}

/**
* Sets the negative sign \a character.
*
* \see negativeSign()
*/
void setNegativeSign( const QChar &character )
{
mNegativeSign = character;
}

/**
* Returns the positive sign character.
*
* \see setPositiveSign()
*/
QChar positiveSign() const
{
return mPositiveSign;
}

/**
* Sets the positive sign \a character.
*
* \see positiveSign()
*/
void setPositiveSign( const QChar &character )
{
mPositiveSign = character;
}

/**
* Returns the exponential character.
*
* \see setExponential()
*/
QChar exponential() const
{
return mExponential;
}

/**
* Sets the exponential \a character.
*
* \see exponential()
*/
void setExponential( const QChar &character )
{
mExponential = character;
}

private:
QChar mThousandsSep;
QChar mDecimalSep;
QChar mPercent;
QChar mZeroDigit;
QChar mNegativeSign;
QChar mPositiveSign;
QChar mExponential;
};

#ifdef SIP_RUN
Expand Down
2 changes: 1 addition & 1 deletion src/core/numericformats/qgspercentagenumericformat.cpp
Expand Up @@ -62,7 +62,7 @@ QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumeric
break;
}

return QgsBasicNumericFormat::formatDouble( value, context ) + '%';
return QgsBasicNumericFormat::formatDouble( value, context ) + context.percent();
}

QgsNumericFormat *QgsPercentageNumericFormat::clone() const
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/test_qgsnumericformat.py
Expand Up @@ -124,6 +124,10 @@ def testBasicFormat(self):
self.assertEqual(f.formatDouble(55555555.123456, context), '+55555555.123')
self.assertEqual(f.formatDouble(-5.5, context), '-5.500')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555.500')
context.setPositiveSign('w')
self.assertEqual(f.formatDouble(5, context), 'w5.000')
self.assertEqual(f.formatDouble(-5, context), '-5.000')
self.assertEqual(f.formatDouble(5.5, context), 'w5.500')

f2 = f.clone()
self.assertIsInstance(f2, QgsBasicNumericFormat)
Expand Down Expand Up @@ -465,6 +469,11 @@ def testPercentageFormat(self):
self.assertEqual(f.formatDouble(5.5, context), '+550.000%')
self.assertEqual(f.formatDouble(-5.5, context), '-550.000%')

context.setPercent('p')
self.assertEqual(f.formatDouble(0, context), '0.000p')
self.assertEqual(f.formatDouble(5, context), '+500.000p')
self.assertEqual(f.formatDouble(-5, context), '-500.000p')

f2 = f.clone()
self.assertIsInstance(f2, QgsPercentageNumericFormat)

Expand Down

0 comments on commit 664ba23

Please sign in to comment.