Skip to content
Permalink
Browse files

Add display of sample values to numeric format selector widget

  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent 0fc7abb commit 124df2d78df6c740aa587a8d901749388c37d217
@@ -38,6 +38,8 @@ Default constructor

virtual int sortKey();

virtual double suggestSampleValue() const;

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

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

virtual int sortKey();

virtual double suggestSampleValue() const;

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

virtual QgsNumericFormat *clone() const /Factory/;
@@ -127,6 +127,11 @@ Returns the translated, user-visible name for this format.
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 double suggestSampleValue() const;
%Docstring
Returns a suggested sample value which nicely represents the current format configuration.
%End

virtual QString formatDouble( double value, const QgsNumericFormatContext &context ) const = 0;
@@ -37,6 +37,8 @@ Default constructor

virtual int sortKey();

virtual double suggestSampleValue() const;

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

virtual QgsNumericFormat *clone() const /Factory/;
@@ -55,6 +55,11 @@ Emitted whenever the configuration of the numeric format is changed.

class QgsBasicNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring
A widget which allow control over the properties of a :py:class:`QgsBasicNumericFormat`.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsnumericformatwidget.h"
@@ -76,6 +81,11 @@ class QgsBasicNumericFormatWidget : QgsNumericFormatWidget

class QgsBearingNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring
A widget which allow control over the properties of a :py:class:`QgsBearingNumericFormat`.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsnumericformatwidget.h"
@@ -98,6 +108,11 @@ class QgsBearingNumericFormatWidget : QgsNumericFormatWidget

class QgsCurrencyNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring
A widget which allow control over the properties of a :py:class:`QgsCurrencyNumericFormat`.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsnumericformatwidget.h"
@@ -120,6 +135,11 @@ class QgsCurrencyNumericFormatWidget : QgsNumericFormatWidget

class QgsPercentageNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring
A widget which allow control over the properties of a :py:class:`QgsPercentageNumericFormat`.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsnumericformatwidget.h"
@@ -143,6 +163,11 @@ class QgsPercentageNumericFormatWidget : QgsNumericFormatWidget

class QgsScientificNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring
A widget which allow control over the properties of a :py:class:`QgsScientificNumericFormat`.

.. versionadded:: 3.12
%End

%TypeHeaderCode
#include "qgsnumericformatwidget.h"
@@ -37,6 +37,11 @@ int QgsBearingNumericFormat::sortKey()
return QgsNumericFormat::sortKey();
}

double QgsBearingNumericFormat::suggestSampleValue() const
{
return 270.123;
}

QString QgsBearingNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
switch ( mDirectionFormat )
@@ -48,6 +48,7 @@ class CORE_EXPORT QgsBearingNumericFormat : public QgsBasicNumericFormat
QString id() const override;
QString visibleName() const override;
int sortKey() override;
double suggestSampleValue() const 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;
@@ -39,6 +39,11 @@ int QgsCurrencyNumericFormat::sortKey()
return QgsNumericFormat::sortKey();
}

double QgsCurrencyNumericFormat::suggestSampleValue() const
{
return 1234.56;
}

QString QgsCurrencyNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
QString res = QgsBasicNumericFormat::formatDouble( value, context );
@@ -37,6 +37,7 @@ class CORE_EXPORT QgsCurrencyNumericFormat : public QgsBasicNumericFormat
QString id() const override;
QString visibleName() const override;
int sortKey() override;
double suggestSampleValue() const 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;
@@ -31,6 +31,11 @@ int QgsNumericFormat::sortKey()
return 100;
}

double QgsNumericFormat::suggestSampleValue() const
{
return 1234.56789123456;
}

void QgsNumericFormat::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
const QVariantMap config = configuration( context );
@@ -159,6 +159,11 @@ class CORE_EXPORT QgsNumericFormat
*/
virtual int sortKey();

/**
* Returns a suggested sample value which nicely represents the current format configuration.
*/
virtual double suggestSampleValue() const;

/**
* Returns a formatted string representation of a numeric double value.
*/
@@ -37,6 +37,19 @@ int QgsPercentageNumericFormat::sortKey()
return QgsNumericFormat::sortKey();
}

double QgsPercentageNumericFormat::suggestSampleValue() const
{
switch ( mInputValues )
{
case ValuesArePercentage:
return 50.1234;

case ValuesAreFractions:
return 0.501234;
}
return 50.1234; // no warnings
}

QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
switch ( mInputValues )
@@ -44,6 +44,7 @@ class CORE_EXPORT QgsPercentageNumericFormat : public QgsBasicNumericFormat
QString id() const override;
QString visibleName() const override;
int sortKey() override;
double suggestSampleValue() const 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;
@@ -22,6 +22,7 @@
#include "qgsgui.h"
#include "qgsnumericformatguiregistry.h"
#include "qgsreadwritecontext.h"
#include "qgsbasicnumericformat.h"
#include <mutex>


@@ -32,6 +33,12 @@ QgsNumericFormatSelectorWidget::QgsNumericFormatSelectorWidget( QWidget *parent

mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->fallbackFormat() );

mPreviewFormat = qgis::make_unique< QgsBasicNumericFormat >();
mPreviewFormat->setShowThousandsSeparator( false );
mPreviewFormat->setShowPlusSign( false );
mPreviewFormat->setShowTrailingZeros( false );
mPreviewFormat->setNumberDecimalPlaces( 12 );

populateTypes();
mCategoryCombo->setCurrentIndex( mCategoryCombo->findData( mCurrentFormat->id() ) );

@@ -81,6 +88,7 @@ void QgsNumericFormatSelectorWidget::formatTypeChanged()
mCurrentFormat.reset( QgsApplication::numericFormatRegistry()->create( newId, props, QgsReadWriteContext() ) );

updateFormatWidget();
updateSampleText();
emit changed();
}

@@ -89,6 +97,7 @@ void QgsNumericFormatSelectorWidget::formatChanged()
if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
mCurrentFormat.reset( w->format() );

updateSampleText();
emit changed();
}

@@ -139,4 +148,14 @@ void QgsNumericFormatSelectorWidget::updateFormatWidget()
{
stackedWidget->setCurrentWidget( pageDummy );
}

updateSampleText();
}

void QgsNumericFormatSelectorWidget::updateSampleText()
{
const double sampleValue = mCurrentFormat->suggestSampleValue();
mSampleLabel->setText( QStringLiteral( "%1 %2 %3" ).arg( mPreviewFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) )
.arg( QChar( 0x2192 ) )
.arg( mCurrentFormat->formatDouble( sampleValue, QgsNumericFormatContext() ) ) );
}
@@ -22,6 +22,7 @@
#include <memory>

class QgsNumericFormat;
class QgsBasicNumericFormat;


/**
@@ -68,8 +69,10 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private

void populateTypes();
void updateFormatWidget();
void updateSampleText();

std::unique_ptr< QgsNumericFormat > mCurrentFormat;
std::unique_ptr< QgsBasicNumericFormat > mPreviewFormat;
};

#endif //QGSNUMERICFORMATSELECTORWIDGET_H
@@ -68,6 +68,12 @@ class GUI_EXPORT QgsNumericFormatWidget : public QgsPanelWidget

class QgsBasicNumericFormat;

/**
* \ingroup gui
* \class QgsBasicNumericFormatWidget
* A widget which allow control over the properties of a QgsBasicNumericFormat.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsBasicNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsBasicNumericFormatWidgetBase
{
Q_OBJECT
@@ -91,6 +97,12 @@ class GUI_EXPORT QgsBasicNumericFormatWidget : public QgsNumericFormatWidget, pr

class QgsBearingNumericFormat;

/**
* \ingroup gui
* \class QgsBearingNumericFormatWidget
* A widget which allow control over the properties of a QgsBearingNumericFormat.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsBearingNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsBearingNumericFormatWidgetBase
{
Q_OBJECT
@@ -115,6 +127,12 @@ class GUI_EXPORT QgsBearingNumericFormatWidget : public QgsNumericFormatWidget,

class QgsCurrencyNumericFormat;

/**
* \ingroup gui
* \class QgsCurrencyNumericFormatWidget
* A widget which allow control over the properties of a QgsCurrencyNumericFormat.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsCurrencyNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsCurrencyNumericFormatWidgetBase
{
Q_OBJECT
@@ -139,6 +157,12 @@ class GUI_EXPORT QgsCurrencyNumericFormatWidget : public QgsNumericFormatWidget,

class QgsPercentageNumericFormat;

/**
* \ingroup gui
* \class QgsPercentageNumericFormatWidget
* A widget which allow control over the properties of a QgsPercentageNumericFormat.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsPercentageNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsPercentageNumericFormatWidgetBase
{
Q_OBJECT
@@ -164,6 +188,12 @@ class GUI_EXPORT QgsPercentageNumericFormatWidget : public QgsNumericFormatWidge

class QgsScientificNumericFormat;

/**
* \ingroup gui
* \class QgsScientificNumericFormatWidget
* A widget which allow control over the properties of a QgsScientificNumericFormat.
* \since QGIS 3.12
*/
class GUI_EXPORT QgsScientificNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsScientificNumericFormatWidgetBase
{
Q_OBJECT
@@ -68,8 +68,32 @@
</item>
</layout>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="mSampleGroupBox">
<property name="title">
<string>Sample</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="mSampleLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsPanelWidget</class>
<extends>QWidget</extends>
<header>qgspanelwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 124df2d

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