Skip to content
Permalink
Browse files

Add methods to store/recreate formats from XML elements, and tweak

serialization API
  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent b9babb7 commit 90184a59ebb1c920790a7f45817a96dc067750e6
Showing with 255 additions and 145 deletions.
  1. +3 −9 python/core/auto_generated/numericformats/qgsbasicnumericformat.sip.in
  2. +2 −5 python/core/auto_generated/numericformats/qgsbearingnumericformat.sip.in
  3. +2 −8 python/core/auto_generated/numericformats/qgscurrencynumericformat.sip.in
  4. +2 −2 python/core/auto_generated/numericformats/qgsfallbacknumericformat.sip.in
  5. +15 −5 python/core/auto_generated/numericformats/qgsnumericformat.sip.in
  6. +9 −1 python/core/auto_generated/numericformats/qgsnumericformatregistry.sip.in
  7. +2 −8 python/core/auto_generated/numericformats/qgspercentagenumericformat.sip.in
  8. +3 −8 python/core/auto_generated/numericformats/qgsscientificnumericformat.sip.in
  9. +5 −5 src/core/numericformats/qgsbasicnumericformat.cpp
  10. +3 −12 src/core/numericformats/qgsbasicnumericformat.h
  11. +5 −5 src/core/numericformats/qgsbearingnumericformat.cpp
  12. +2 −11 src/core/numericformats/qgsbearingnumericformat.h
  13. +5 −5 src/core/numericformats/qgscurrencynumericformat.cpp
  14. +2 −11 src/core/numericformats/qgscurrencynumericformat.h
  15. +2 −2 src/core/numericformats/qgsfallbacknumericformat.cpp
  16. +2 −2 src/core/numericformats/qgsfallbacknumericformat.h
  17. +11 −0 src/core/numericformats/qgsnumericformat.cpp
  18. +16 −5 src/core/numericformats/qgsnumericformat.h
  19. +14 −2 src/core/numericformats/qgsnumericformatregistry.cpp
  20. +11 −1 src/core/numericformats/qgsnumericformatregistry.h
  21. +5 −5 src/core/numericformats/qgspercentagenumericformat.cpp
  22. +2 −11 src/core/numericformats/qgspercentagenumericformat.h
  23. +5 −5 src/core/numericformats/qgsscientificnumericformat.cpp
  24. +2 −12 src/core/numericformats/qgsscientificnumericformat.h
  25. +125 −5 tests/src/python/test_qgsnumericformat.py
@@ -25,22 +25,18 @@ A numeric formatter which returns a simple text representation of a value.
Default constructor
%End


virtual QString id() const;

%Docstring
QgsBasicNumericFormat 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 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const /Factory/;

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


void setConfiguration( const QVariantMap &configuration );
void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );
%Docstring
Sets the format's ``configuration``.
%End
@@ -114,8 +110,6 @@ numberDecimalPlaces()).
protected:


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

/************************************************************************
@@ -38,9 +38,9 @@ Default constructor

virtual QgsNumericFormat *clone() const /Factory/;

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

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


FormatDirectionOption directionFormat() const;
@@ -59,9 +59,6 @@ described in the returned strings.
.. seealso:: :py:func:`directionFormat`
%End


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

/************************************************************************
@@ -25,19 +25,15 @@ A numeric formatter which returns a text representation of a currency value.
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 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const /Factory/;

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


QString prefix() const;
@@ -68,8 +64,6 @@ Sets the currency ``suffix``, e.g. "AUD".
.. seealso:: :py:func:`suffix`
%End

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

/************************************************************************
@@ -30,9 +30,9 @@ Default constructor

virtual QgsNumericFormat *clone() const /Factory/;

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

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;

};

@@ -8,6 +8,8 @@





class QgsNumericFormatContext
{
%Docstring
@@ -84,9 +86,7 @@ This is an abstract base class and will always need to be subclassed.
#include "qgsnumericformat.h"
%End
%ConvertToSubClassCode
if ( dynamic_cast< QgsBasicNumericFormat * >( sipCpp ) )
sipType = sipType_QgsBasicNumericFormat;
else if ( dynamic_cast< QgsBearingNumericFormat * >( sipCpp ) )
if ( dynamic_cast< QgsBearingNumericFormat * >( sipCpp ) )
sipType = sipType_QgsBearingNumericFormat;
else if ( dynamic_cast< QgsFallbackNumericFormat * >( sipCpp ) )
sipType = sipType_QgsFallbackNumericFormat;
@@ -96,6 +96,8 @@ This is an abstract base class and will always need to be subclassed.
sipType = sipType_QgsScientificNumericFormat;
else if ( dynamic_cast< QgsCurrencyNumericFormat * >( sipCpp ) )
sipType = sipType_QgsCurrencyNumericFormat;
else if ( dynamic_cast< QgsBasicNumericFormat * >( sipCpp ) )
sipType = sipType_QgsBasicNumericFormat;
else
sipType = NULL;
%End
@@ -127,18 +129,26 @@ Clones the format, returning a new object.
The caller takes ownership of the returned object.
%End

virtual QgsNumericFormat *create( const QVariantMap &configuration ) const = 0 /Factory/;
virtual QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const = 0 /Factory/;
%Docstring
Creates a new copy of the format, using the supplied ``configuration``.

The caller takes ownership of the returned object.
%End

virtual QVariantMap configuration() const = 0;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const = 0;
%Docstring
Returns the current configuration of the formatter. This value can be used in a call to create()
in order to recreate this formatter in its current state.
%End

void writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
%Docstring
Writes the format to an XML ``element``.

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

};

/************************************************************************
@@ -58,11 +58,19 @@ a default QgsFallbackNumericFormat with a null id will be returned instead.
The caller takes ownership of the returned object.
%End

QgsNumericFormat *create( const QString &id, const QVariantMap &configuration ) const /TransferBack/;
QgsNumericFormat *create( const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context ) const /TransferBack/;
%Docstring
Creates a new numeric format by ``id``, using the supplied ``configuration``. If there is no such ``id`` registered,
a default QgsFallbackNumericFormat with a null id will be returned instead.

The caller takes ownership of the returned object.
%End

QgsNumericFormat *createFromXml( const QDomElement &element, const QgsReadWriteContext &context ) const /TransferBack/;
%Docstring
Creates a new numeric format from an XML ``element``. If there is no matching format ID registered,
a default QgsFallbackNumericFormat will be returned instead.

The caller takes ownership of the returned object.
%End

@@ -31,19 +31,15 @@ A numeric formatter which returns a text representation of a percentage value.
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 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const /Factory/;

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


InputValues inputValues() const;
@@ -60,8 +56,6 @@ Sets the ``format`` of the incoming values.
.. seealso:: :py:func:`inputValues`
%End

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

/************************************************************************
@@ -25,19 +25,15 @@ A numeric formatter which returns a scientific notation representation of a valu
Default constructor
%End


virtual QString id() const;

%Docstring
QgsScientificNumericFormat 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 QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const /Factory/;

virtual QVariantMap configuration() const;
virtual QVariantMap configuration( const QgsReadWriteContext &context ) const;


virtual void setNumberDecimalPlaces( int places );
@@ -52,8 +48,7 @@ The ``places`` argument must be at least 1.
.. seealso:: :py:func:`setShowTrailingZeros`
%End

private:
QgsScientificNumericFormat( const QgsScientificNumericFormat &other );

};

/************************************************************************
@@ -91,17 +91,17 @@ QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericForma

QgsNumericFormat *QgsBasicNumericFormat::clone() const
{
return create( configuration() );
return new QgsBasicNumericFormat( *this );
}

QgsNumericFormat *QgsBasicNumericFormat::create( const QVariantMap &configuration ) const
QgsNumericFormat *QgsBasicNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const
{
std::unique_ptr< QgsBasicNumericFormat > res = qgis::make_unique< QgsBasicNumericFormat >();
res->setConfiguration( configuration );
res->setConfiguration( configuration, context );
return res.release();
}

QVariantMap QgsBasicNumericFormat::configuration() const
QVariantMap QgsBasicNumericFormat::configuration( const QgsReadWriteContext & ) const
{
QVariantMap res;
res.insert( QStringLiteral( "decimals" ), mNumberDecimalPlaces );
@@ -111,7 +111,7 @@ QVariantMap QgsBasicNumericFormat::configuration() const
return res;
}

void QgsBasicNumericFormat::setConfiguration( const QVariantMap &configuration )
void QgsBasicNumericFormat::setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext & )
{
mNumberDecimalPlaces = configuration.value( QStringLiteral( "decimals" ), 6 ).toInt();
mShowThousandsSeparator = configuration.value( QStringLiteral( "show_thousand_separator" ), true ).toBool();
@@ -37,21 +37,16 @@ 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;
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;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
QVariantMap configuration( const QgsReadWriteContext &context ) const override;

/**
* Sets the format's \a configuration.
*/
void setConfiguration( const QVariantMap &configuration );
void setConfiguration( const QVariantMap &configuration, const QgsReadWriteContext &context );

/**
* Returns the maximum number of decimal places to show.
@@ -117,10 +112,6 @@ class CORE_EXPORT QgsBasicNumericFormat : public QgsNumericFormat

private:

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

int mNumberDecimalPlaces = 6;
bool mShowThousandsSeparator = true;
bool mShowPlusSign = false;
@@ -71,20 +71,20 @@ QString QgsBearingNumericFormat::formatDouble( double value, const QgsNumericFor

QgsNumericFormat *QgsBearingNumericFormat::clone() const
{
return create( configuration() );
return new QgsBearingNumericFormat( *this );
}

QgsNumericFormat *QgsBearingNumericFormat::create( const QVariantMap &configuration ) const
QgsNumericFormat *QgsBearingNumericFormat::create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const
{
std::unique_ptr< QgsBearingNumericFormat > res = qgis::make_unique< QgsBearingNumericFormat >();
res->setConfiguration( configuration );
res->setConfiguration( configuration, context );
res->mDirectionFormat = static_cast< FormatDirectionOption >( configuration.value( QStringLiteral( "direction_format" ), 0 ).toInt() );
return res.release();
}

QVariantMap QgsBearingNumericFormat::configuration() const
QVariantMap QgsBearingNumericFormat::configuration( const QgsReadWriteContext &context ) const
{
QVariantMap res = QgsBasicNumericFormat::configuration();
QVariantMap res = QgsBasicNumericFormat::configuration( context );
res.insert( QStringLiteral( "direction_format" ), static_cast< int >( mDirectionFormat ) );
return res;
}
@@ -48,8 +48,8 @@ class CORE_EXPORT QgsBearingNumericFormat : public QgsBasicNumericFormat
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;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
QVariantMap configuration( const QgsReadWriteContext &context ) const override;

/**
* Returns the directional formatting option, which controls how bearing direction is
@@ -67,17 +67,8 @@ class CORE_EXPORT QgsBearingNumericFormat : public QgsBasicNumericFormat
*/
void setDirectionFormat( const FormatDirectionOption &format );

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

private:

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

FormatDirectionOption mDirectionFormat = UseRange0To180WithEWDirectionalSuffix;

};
@@ -39,21 +39,21 @@ QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFo

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

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

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

0 comments on commit 90184a5

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