Skip to content
Permalink
Browse files

Add percentage numeric formatter

  • Loading branch information
nyalldawson committed Jan 6, 2020
1 parent d0b5a01 commit 8bcba4780e787a4e8f05c3cfd3deb892f9a2894d
@@ -28,6 +28,9 @@ Default constructor

virtual QString id() const;

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

virtual QgsNumericFormat *clone() const /Factory/;
@@ -63,6 +63,7 @@ Returns the decimal separator character.
#include <qgsbasicnumericformat.h>
#include <qgsbearingnumericformat.h>
#include <qgsfallbacknumericformat.h>
#include <qgspercentagenumericformat.h>
%End

class QgsNumericFormat
@@ -87,6 +88,8 @@ This is an abstract base class and will always need to be subclassed.
sipType = sipType_QgsBearingNumericFormat;
else if ( dynamic_cast< QgsFallbackNumericFormat * >( sipCpp ) )
sipType = sipType_QgsFallbackNumericFormat;
else if ( dynamic_cast< QgsPercentageNumericFormat * >( sipCpp ) )
sipType = sipType_QgsPercentageNumericFormat;
else
sipType = NULL;
%End
@@ -0,0 +1,73 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/numericformats/qgspercentagenumericformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


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

.. versionadded:: 3.12
%End

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

enum InputValues
{
ValuesArePercentage,
ValuesAreFractions,
};

QgsPercentageNumericFormat();
%Docstring
Default constructor
%End


virtual QString id() const;

%Docstring
QgsPercentageNumericFormat 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;


InputValues inputValues() const;
%Docstring
Returns the format of the incoming values.

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

void setInputValues( const InputValues &format );
%Docstring
Sets the ``format`` of the incoming values.

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

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

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/numericformats/qgspercentagenumericformat.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -400,6 +400,7 @@
%Include auto_generated/numericformats/qgsfallbacknumericformat.sip
%Include auto_generated/numericformats/qgsnumericformat.sip
%Include auto_generated/numericformats/qgsnumericformatregistry.sip
%Include auto_generated/numericformats/qgspercentagenumericformat.sip
%Include auto_generated/processing/models/qgsprocessingmodelalgorithm.sip
%Include auto_generated/processing/models/qgsprocessingmodelchildalgorithm.sip
%Include auto_generated/processing/models/qgsprocessingmodelchildparametersource.sip
@@ -109,6 +109,7 @@ SET(QGIS_CORE_SRCS
numericformats/qgsfallbacknumericformat.cpp
numericformats/qgsnumericformat.cpp
numericformats/qgsnumericformatregistry.cpp
numericformats/qgspercentagenumericformat.cpp

auth/qgsauthcertutils.cpp
auth/qgsauthconfig.cpp
@@ -1137,6 +1138,7 @@ SET(QGIS_CORE_HDRS
numericformats/qgsfallbacknumericformat.h
numericformats/qgsnumericformat.h
numericformats/qgsnumericformatregistry.h
numericformats/qgspercentagenumericformat.h

processing/models/qgsprocessingmodelalgorithm.h
processing/models/qgsprocessingmodelchildalgorithm.h
@@ -37,7 +37,9 @@ class CORE_EXPORT QgsBasicNumericFormat : public QgsNumericFormat
*/
QgsBasicNumericFormat();

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

QString id() const override;
@@ -89,6 +89,7 @@ class CORE_EXPORT QgsNumericFormatContext
#include <qgsbasicnumericformat.h>
#include <qgsbearingnumericformat.h>
#include <qgsfallbacknumericformat.h>
#include <qgspercentagenumericformat.h>
% End
#endif

@@ -113,6 +114,8 @@ class CORE_EXPORT QgsNumericFormat
sipType = sipType_QgsBearingNumericFormat;
else if ( dynamic_cast< QgsFallbackNumericFormat * >( sipCpp ) )
sipType = sipType_QgsFallbackNumericFormat;
else if ( dynamic_cast< QgsPercentageNumericFormat * >( sipCpp ) )
sipType = sipType_QgsPercentageNumericFormat;
else
sipType = NULL;
SIP_END
@@ -18,12 +18,13 @@
#include "qgsfallbacknumericformat.h"
#include "qgsbasicnumericformat.h"
#include "qgsbearingnumericformat.h"

#include "qgspercentagenumericformat.h"

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

QgsNumericFormatRegistry::~QgsNumericFormatRegistry()
@@ -0,0 +1,73 @@
/***************************************************************************
qgspercentagenumericformat.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 "qgspercentagenumericformat.h"
#include "qgis.h"


QgsPercentageNumericFormat::QgsPercentageNumericFormat()
{
}

QString QgsPercentageNumericFormat::id() const
{
return QStringLiteral( "percentage" );
}

QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
switch ( mInputValues )
{
case ValuesArePercentage:
break;

case ValuesAreFractions:
value *= 100;
break;
}

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

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

QgsNumericFormat *QgsPercentageNumericFormat::create( const QVariantMap &configuration ) const
{
std::unique_ptr< QgsPercentageNumericFormat > res = qgis::make_unique< QgsPercentageNumericFormat >();
res->setConfiguration( configuration );
res->mInputValues = static_cast< InputValues >( configuration.value( QStringLiteral( "input_values" ), static_cast< int >( ValuesArePercentage ) ).toInt() );
return res.release();
}

QVariantMap QgsPercentageNumericFormat::configuration() const
{
QVariantMap res = QgsBasicNumericFormat::configuration();
res.insert( QStringLiteral( "input_values" ), static_cast< int >( mInputValues ) );
return res;
}

QgsPercentageNumericFormat::InputValues QgsPercentageNumericFormat::inputValues() const
{
return mInputValues;
}

void QgsPercentageNumericFormat::setInputValues( const InputValues &inputValues )
{
mInputValues = inputValues;
}
@@ -0,0 +1,79 @@
/***************************************************************************
qgspercentagenumericformat.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 QGSPERCENTAGENUMERICFORMAT_H
#define QGSPERCENTAGENUMERICFORMAT_H

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

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

//! Input value format, which specifies the format of the incoming values
enum InputValues
{
ValuesArePercentage, //!< Incoming values are percentage values (e.g. 50 for 50%)
ValuesAreFractions, //!< Incoming values are numeric fractions (e.g. 0.5 for 50%)
};

/**
* Default constructor
*/
QgsPercentageNumericFormat();

//! QgsPercentageNumericFormat cannot be copied
QgsPercentageNumericFormat( const QgsPercentageNumericFormat & ) = delete;
//! QgsPercentageNumericFormat cannot be copied
QgsPercentageNumericFormat &operator=( const QgsPercentageNumericFormat & ) = 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 format of the incoming values.
*
* \see setInputValues()
*/
InputValues inputValues() const;

/**
* Sets the \a format of the incoming values.
*
* \see inputValues()
*/
void setInputValues( const InputValues &format );

private:

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

InputValues mInputValues = ValuesArePercentage;

};

#endif // QGSPERCENTAGENUMERICFORMAT_H

0 comments on commit 8bcba47

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