Skip to content
Permalink
Browse files

Add numeric formatter for currency values

  • Loading branch information
nyalldawson committed Jan 6, 2020
1 parent d445487 commit 7affde66245b0084fac5b286449603b91d1b7074
@@ -0,0 +1,81 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/numericformats/qgscurrencynumericformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


class QgsCurrencyNumericFormat : QgsBasicNumericFormat
{
%Docstring
A numeric formatter which returns a text representation of a currency value.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgscurrencynumericformat.h"
%End
public:

QgsCurrencyNumericFormat();
%Docstring
Default constructor
%End


virtual QString id() const;

%Docstring
QgsCurrencyNumericFormat cannot be copied
%End
virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;

virtual QgsNumericFormat *create( const QVariantMap &configuration ) const /Factory/;

virtual QVariantMap configuration() const;


QString prefix() const;
%Docstring
Returns the currency prefix, e.g. "$".

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

void setPrefix( const QString &prefix );
%Docstring
Sets the currency ``prefix``, e.g. "$".

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

QString suffix() const;
%Docstring
Returns the currency suffix, e.g. "AUD".

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

void setSuffix( const QString &suffix );
%Docstring
Sets the currency ``suffix``, e.g. "AUD".

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

private:
QgsCurrencyNumericFormat( const QgsCurrencyNumericFormat &other );
};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/numericformats/qgscurrencynumericformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -62,6 +62,7 @@ Returns the decimal separator character.
%ModuleHeaderCode
#include <qgsbasicnumericformat.h>
#include <qgsbearingnumericformat.h>
#include <qgscurrencynumericformat.h>
#include <qgsfallbacknumericformat.h>
#include <qgspercentagenumericformat.h>
#include <qgsscientificnumericformat.h>
@@ -93,6 +94,8 @@ This is an abstract base class and will always need to be subclassed.
sipType = sipType_QgsPercentageNumericFormat;
else if ( dynamic_cast< QgsScientificNumericFormat * >( sipCpp ) )
sipType = sipType_QgsScientificNumericFormat;
else if ( dynamic_cast< QgsCurrencyNumericFormat * >( sipCpp ) )
sipType = sipType_QgsCurrencyNumericFormat;
else
sipType = NULL;
%End
@@ -397,6 +397,7 @@
%Include auto_generated/metadata/qgsprojectmetadata.sip
%Include auto_generated/numericformats/qgsbasicnumericformat.sip
%Include auto_generated/numericformats/qgsbearingnumericformat.sip
%Include auto_generated/numericformats/qgscurrencynumericformat.sip
%Include auto_generated/numericformats/qgsfallbacknumericformat.sip
%Include auto_generated/numericformats/qgsnumericformat.sip
%Include auto_generated/numericformats/qgsnumericformatregistry.sip
@@ -106,6 +106,7 @@ SET(QGIS_CORE_SRCS

numericformats/qgsbasicnumericformat.cpp
numericformats/qgsbearingnumericformat.cpp
numericformats/qgscurrencynumericformat.cpp
numericformats/qgsfallbacknumericformat.cpp
numericformats/qgsnumericformat.cpp
numericformats/qgsnumericformatregistry.cpp
@@ -1136,6 +1137,7 @@ SET(QGIS_CORE_HDRS

numericformats/qgsbasicnumericformat.h
numericformats/qgsbearingnumericformat.h
numericformats/qgscurrencynumericformat.h
numericformats/qgsfallbacknumericformat.h
numericformats/qgsnumericformat.h
numericformats/qgsnumericformatregistry.h
@@ -0,0 +1,80 @@
/***************************************************************************
qgscurrencynumericformat.cpp
----------------------------
begin : January 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgscurrencynumericformat.h"
#include "qgis.h"


QgsCurrencyNumericFormat::QgsCurrencyNumericFormat()
: mPrefix( QStringLiteral( "$" ) )
{
}

QString QgsCurrencyNumericFormat::id() const
{
return QStringLiteral( "currency" );
}

QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
QString res = QgsBasicNumericFormat::formatDouble( value, context );
if ( value < 0 || ( value > 0 && showPlusSign() ) )
return res.at( 0 ) + mPrefix + res.mid( 1 ) + mSuffix;
else
return mPrefix + res + mSuffix;
}

QgsNumericFormat *QgsCurrencyNumericFormat::clone() const
{
return create( configuration() );
}

QgsNumericFormat *QgsCurrencyNumericFormat::create( const QVariantMap &configuration ) const
{
std::unique_ptr< QgsCurrencyNumericFormat > res = qgis::make_unique< QgsCurrencyNumericFormat >();
res->setConfiguration( configuration );
res->mPrefix = configuration.value( QStringLiteral( "prefix" ), QStringLiteral( "$" ) ).toString();
res->mSuffix = configuration.value( QStringLiteral( "suffix" ), QString() ).toString();
return res.release();
}

QVariantMap QgsCurrencyNumericFormat::configuration() const
{
QVariantMap res = QgsBasicNumericFormat::configuration();
res.insert( QStringLiteral( "prefix" ), mPrefix );
res.insert( QStringLiteral( "suffix" ), mSuffix );
return res;
}

QString QgsCurrencyNumericFormat::prefix() const
{
return mPrefix;
}

void QgsCurrencyNumericFormat::setPrefix( const QString &prefix )
{
mPrefix = prefix;
}

QString QgsCurrencyNumericFormat::suffix() const
{
return mSuffix;
}

void QgsCurrencyNumericFormat::setSuffix( const QString &suffix )
{
mSuffix = suffix;
}
@@ -0,0 +1,83 @@
/***************************************************************************
qgscurrencynumericformat.h
----------------------------
begin : January 2020
copyright : (C) 2020 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSCURRENCYNUMERICFORMAT_H
#define QGSCURRENCYNUMERICFORMAT_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsbasicnumericformat.h"

/**
* \ingroup core
* A numeric formatter which returns a text representation of a currency value.
*
* \since QGIS 3.12
*/
class CORE_EXPORT QgsCurrencyNumericFormat : public QgsBasicNumericFormat
{
public:

/**
* Default constructor
*/
QgsCurrencyNumericFormat();

//! QgsCurrencyNumericFormat cannot be copied
QgsCurrencyNumericFormat( const QgsCurrencyNumericFormat & ) = delete;
//! QgsCurrencyNumericFormat cannot be copied
QgsCurrencyNumericFormat &operator=( const QgsCurrencyNumericFormat & ) = delete;

QString id() const override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration ) const override SIP_FACTORY;
QVariantMap configuration() const override;

/**
* Returns the currency prefix, e.g. "$".
* \see setPrefix()
*/
QString prefix() const;

/**
* Sets the currency \a prefix, e.g. "$".
* \see prefix()
*/
void setPrefix( const QString &prefix );

/**
* Returns the currency suffix, e.g. "AUD".
* \see setSuffix()
*/
QString suffix() const;

/**
* Sets the currency \a suffix, e.g. "AUD".
* \see suffix()
*/
void setSuffix( const QString &suffix );

private:

#ifdef SIP_RUN
QgsCurrencyNumericFormat( const QgsCurrencyNumericFormat &other );
#endif

QString mPrefix;
QString mSuffix;

};

#endif // QGSCURRENCYNUMERICFORMAT_H
@@ -88,6 +88,7 @@ class CORE_EXPORT QgsNumericFormatContext
% ModuleHeaderCode
#include <qgsbasicnumericformat.h>
#include <qgsbearingnumericformat.h>
#include <qgscurrencynumericformat.h>
#include <qgsfallbacknumericformat.h>
#include <qgspercentagenumericformat.h>
#include <qgsscientificnumericformat.h>
@@ -119,6 +120,8 @@ class CORE_EXPORT QgsNumericFormat
sipType = sipType_QgsPercentageNumericFormat;
else if ( dynamic_cast< QgsScientificNumericFormat * >( sipCpp ) )
sipType = sipType_QgsScientificNumericFormat;
else if ( dynamic_cast< QgsCurrencyNumericFormat * >( sipCpp ) )
sipType = sipType_QgsCurrencyNumericFormat;
else
sipType = NULL;
SIP_END
@@ -18,13 +18,15 @@
#include "qgsfallbacknumericformat.h"
#include "qgsbasicnumericformat.h"
#include "qgsbearingnumericformat.h"
#include "qgscurrencynumericformat.h"
#include "qgspercentagenumericformat.h"
#include "qgsscientificnumericformat.h"

QgsNumericFormatRegistry::QgsNumericFormatRegistry()
{
addFormat( new QgsBasicNumericFormat() );
addFormat( new QgsBearingNumericFormat() );
addFormat( new QgsCurrencyNumericFormat() );
addFormat( new QgsPercentageNumericFormat() );
addFormat( new QgsScientificNumericFormat() );
}

0 comments on commit 7affde6

Please sign in to comment.
You can’t perform that action at this time.