Skip to content
Permalink
Browse files

Nicer sorting of numeric format choices

  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent 591e1a2 commit 6b9cafb71fc29efa3002fd8568dd046be847c5be
Showing with 131 additions and 8 deletions.
  1. +2 −0 python/core/auto_generated/numericformats/qgsbasicnumericformat.sip.in
  2. +2 −0 python/core/auto_generated/numericformats/qgsbearingnumericformat.sip.in
  3. +2 −0 python/core/auto_generated/numericformats/qgscurrencynumericformat.sip.in
  4. +2 −0 python/core/auto_generated/numericformats/qgsfallbacknumericformat.sip.in
  5. +7 −0 python/core/auto_generated/numericformats/qgsnumericformat.sip.in
  6. +5 −0 python/core/auto_generated/numericformats/qgsnumericformatregistry.sip.in
  7. +2 −0 python/core/auto_generated/numericformats/qgspercentagenumericformat.sip.in
  8. +2 −0 python/core/auto_generated/numericformats/qgsscientificnumericformat.sip.in
  9. +7 −0 python/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in
  10. +5 −0 src/core/numericformats/qgsbasicnumericformat.cpp
  11. +1 −0 src/core/numericformats/qgsbasicnumericformat.h
  12. +5 −0 src/core/numericformats/qgsbearingnumericformat.cpp
  13. +1 −0 src/core/numericformats/qgsbearingnumericformat.h
  14. +5 −0 src/core/numericformats/qgscurrencynumericformat.cpp
  15. +1 −0 src/core/numericformats/qgscurrencynumericformat.h
  16. +5 −0 src/core/numericformats/qgsfallbacknumericformat.cpp
  17. +1 −0 src/core/numericformats/qgsfallbacknumericformat.h
  18. +5 −0 src/core/numericformats/qgsnumericformat.cpp
  19. +7 −0 src/core/numericformats/qgsnumericformat.h
  20. +8 −0 src/core/numericformats/qgsnumericformatregistry.cpp
  21. +5 −0 src/core/numericformats/qgsnumericformatregistry.h
  22. +6 −1 src/core/numericformats/qgspercentagenumericformat.cpp
  23. +1 −0 src/core/numericformats/qgspercentagenumericformat.h
  24. +5 −0 src/core/numericformats/qgsscientificnumericformat.cpp
  25. +1 −0 src/core/numericformats/qgsscientificnumericformat.h
  26. +31 −7 src/gui/numericformats/qgsnumericformatselectorwidget.cpp
  27. +7 −0 src/gui/numericformats/qgsnumericformatselectorwidget.h
@@ -29,6 +29,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -36,6 +36,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -29,6 +29,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -28,6 +28,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -120,6 +120,13 @@ This id is used to identify this numeric format in the registry with :py:func:`Q
virtual QString visibleName() const = 0;
%Docstring
Returns the translated, user-visible name for this format.
%End

virtual int sortKey();
%Docstring
Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.

Generally, subclasses should return QgsNumericFormat.sortKey() as their sorting key.
%End

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const = 0;
@@ -85,6 +85,11 @@ The caller takes ownership of the returned object.
QString visibleName( const QString &id ) const;
%Docstring
Returns the translated, user-visible name for the format with matching ``id``.
%End

int sortKey( const QString &id ) const;
%Docstring
Returns the sorting key for the format with matching ``id``.
%End

};
@@ -35,6 +35,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -29,6 +29,8 @@ Default constructor

virtual QString visibleName() const;

virtual int sortKey();

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const;

virtual QgsNumericFormat *clone() const /Factory/;
@@ -33,6 +33,13 @@ Sets the format to show in the widget.
Returns a new format object representing the settings currently configured in the widget.

The caller takes ownership of the returned object.
%End

signals:

void changed();
%Docstring
Emitted whenever the format configured in the widget is changed.
%End

};
@@ -51,6 +51,11 @@ QString QgsBasicNumericFormat::visibleName() const
return QObject::tr( "Number" );
}

int QgsBasicNumericFormat::sortKey()
{
return 1;
}

QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
std::ostringstream os;
@@ -39,6 +39,7 @@ class CORE_EXPORT QgsBasicNumericFormat : public QgsNumericFormat

QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -32,6 +32,11 @@ QString QgsBearingNumericFormat::visibleName() const
return QObject::tr( "Bearing" );
}

int QgsBearingNumericFormat::sortKey()
{
return QgsNumericFormat::sortKey();
}

QString QgsBearingNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
switch ( mDirectionFormat )
@@ -47,6 +47,7 @@ class CORE_EXPORT QgsBearingNumericFormat : public QgsBasicNumericFormat

QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -33,6 +33,11 @@ QString QgsCurrencyNumericFormat::visibleName() const
return QObject::tr( "Currency" );
}

int QgsCurrencyNumericFormat::sortKey()
{
return QgsNumericFormat::sortKey();
}

QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
QString res = QgsBasicNumericFormat::formatDouble( value, context );
@@ -36,6 +36,7 @@ class CORE_EXPORT QgsCurrencyNumericFormat : public QgsBasicNumericFormat

QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -27,6 +27,11 @@ QString QgsFallbackNumericFormat::visibleName() const
return QObject::tr( "General" );
}

int QgsFallbackNumericFormat::sortKey()
{
return 0;
}

QString QgsFallbackNumericFormat::formatDouble( double value, const QgsNumericFormatContext & ) const
{
return QString::number( value );
@@ -35,6 +35,7 @@ class CORE_EXPORT QgsFallbackNumericFormat : public QgsNumericFormat
QgsFallbackNumericFormat() = default;
QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -26,6 +26,11 @@ QgsNumericFormatContext::QgsNumericFormatContext()
mDecimalSep = l.decimalPoint();
}

int QgsNumericFormat::sortKey()
{
return 100;
}

void QgsNumericFormat::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
const QVariantMap config = configuration( context );
@@ -152,6 +152,13 @@ class CORE_EXPORT QgsNumericFormat
*/
virtual QString visibleName() const = 0;

/**
* Returns a sorting key value, where formats with a lower sort key will be shown earlier in lists.
*
* Generally, subclasses should return QgsNumericFormat::sortKey() as their sorting key.
*/
virtual int sortKey();

/**
* Returns a formatted string representation of a numeric double value.
*/
@@ -98,3 +98,11 @@ QString QgsNumericFormatRegistry::visibleName( const QString &id ) const

return QString();
}

int QgsNumericFormatRegistry::sortKey( const QString &id ) const
{
if ( mFormats.contains( id ) )
return mFormats.value( id )->sortKey();

return 0;
}
@@ -102,6 +102,11 @@ class CORE_EXPORT QgsNumericFormatRegistry
*/
QString visibleName( const QString &id ) const;

/**
* Returns the sorting key for the format with matching \a id.
*/
int sortKey( const QString &id ) const;

private:
QHash<QString, QgsNumericFormat *> mFormats;
};
@@ -29,7 +29,12 @@ QString QgsPercentageNumericFormat::id() const

QString QgsPercentageNumericFormat::visibleName() const
{
return QObject::tr( "Perctange" );
return QObject::tr( "Percentage" );
}

int QgsPercentageNumericFormat::sortKey()
{
return QgsNumericFormat::sortKey();
}

QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
@@ -43,6 +43,7 @@ class CORE_EXPORT QgsPercentageNumericFormat : public QgsBasicNumericFormat

QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -33,6 +33,11 @@ QString QgsScientificNumericFormat::visibleName() const
return QObject::tr( "Scientific" );
}

int QgsScientificNumericFormat::sortKey()
{
return QgsNumericFormat::sortKey();
}

QString QgsScientificNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
return QgsBasicNumericFormat::formatDouble( value, context );
@@ -36,6 +36,7 @@ class CORE_EXPORT QgsScientificNumericFormat : public QgsBasicNumericFormat

QString id() const override;
QString visibleName() const override;
int sortKey() override;
QString formatDouble( double value, const QgsNumericFormatContext &context ) const override;
QgsNumericFormat *clone() const override SIP_FACTORY;
QgsNumericFormat *create( const QVariantMap &configuration, const QgsReadWriteContext &context ) const override SIP_FACTORY;
@@ -17,24 +17,27 @@
#include "qgsapplication.h"
#include "qgsnumericformatregistry.h"
#include "qgsnumericformat.h"
#include "qgis.h"
#include <mutex>


static void _initWidgetFunctions()
{
static bool sInitialized = false;
if ( sInitialized )
return;
static std::once_flag initialized;
std::call_once( initialized, [ = ]
{

sInitialized = true;
} );
}


QgsNumericFormatSelectorWidget::QgsNumericFormatSelectorWidget( QWidget *parent )
: QgsPanelWidget( parent )
{
setupUi( this );
_initWidgetFunctions();

populateTypes();
mCategoryCombo->setCurrentIndex( 0 );
}

void QgsNumericFormatSelectorWidget::setFormat( const QgsNumericFormat *format )
@@ -44,10 +47,14 @@ void QgsNumericFormatSelectorWidget::setFormat( const QgsNumericFormat *format )

const QString id = format->id();
const int index = mCategoryCombo->findData( id );
const QString prevId = mCategoryCombo->currentData().toString();
if ( index < 0 )
mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( QStringLiteral( "fallback" ) ) );
else
mCategoryCombo->setCurrentIndex( index );

if ( prevId != id )
emit changed();
}

QgsNumericFormat *QgsNumericFormatSelectorWidget::format() const
@@ -57,8 +64,25 @@ QgsNumericFormat *QgsNumericFormatSelectorWidget::format() const

void QgsNumericFormatSelectorWidget::populateTypes()
{
const QStringList ids = QgsApplication::numericFormatRegistry()->formats();
QStringList ids = QgsApplication::numericFormatRegistry()->formats();

std::sort( ids.begin(), ids.end(), [ = ]( const QString & a, const QString & b )->bool
{
if ( QgsApplication::numericFormatRegistry()->sortKey( a ) < QgsApplication::numericFormatRegistry()->sortKey( b ) )
return true;
else if ( QgsApplication::numericFormatRegistry()->sortKey( a ) > QgsApplication::numericFormatRegistry()->sortKey( b ) )
return false;
else
{
int res = QString::localeAwareCompare( QgsApplication::numericFormatRegistry()->visibleName( a ), QgsApplication::numericFormatRegistry()->visibleName( b ) );
if ( res < 0 )
return true;
else if ( res > 0 )
return false;
}
return false;
} );

for ( const QString &id : ids )
for ( const QString &id : qgis::as_const( ids ) )
mCategoryCombo->addItem( QgsApplication::numericFormatRegistry()->visibleName( id ), id );
}
@@ -50,6 +50,13 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private
*/
QgsNumericFormat *format() const SIP_TRANSFERBACK;

signals:

/**
* Emitted whenever the format configured in the widget is changed.
*/
void changed();

private:

void populateTypes();

0 comments on commit 6b9cafb

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