Skip to content

Commit 5c5ef3b

Browse files
authored
Merge pull request #6246 from elpaso/rangeformatter
[bugfix] Range formatter for doubles and ints
2 parents b7e19d8 + 31de324 commit 5c5ef3b

File tree

14 files changed

+274
-2
lines changed

14 files changed

+274
-2
lines changed

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
%Include geometry/qgswkbtypes.sip
288288
%Include ./3d/qgs3drendererregistry.sip
289289
%Include ./3d/qgsabstract3drenderer.sip
290+
%Include fieldformatter/qgsrangefieldformatter.sip
290291
%Include fieldformatter/qgsdatetimefieldformatter.sip
291292
%Include fieldformatter/qgsfallbackfieldformatter.sip
292293
%Include fieldformatter/qgskeyvaluefieldformatter.sip
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/fieldformatter/qgsrangefieldformatter.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
class QgsRangeFieldFormatter : QgsFieldFormatter
11+
{
12+
%Docstring
13+
Field formatter for a range (double) field with precision and locale
14+
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgsrangefieldformatter.h"
20+
%End
21+
public:
22+
23+
QgsRangeFieldFormatter();
24+
%Docstring
25+
Default constructor of field formatter for a range (double)field.
26+
%End
27+
28+
virtual QString id() const;
29+
30+
31+
virtual QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const;
32+
33+
34+
};
35+
36+
/************************************************************************
37+
* This file has been generated automatically from *
38+
* *
39+
* src/core/fieldformatter/qgsrangefieldformatter.h *
40+
* *
41+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
42+
************************************************************************/

python/gui/editorwidgets/qgsdoublespinbox.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ Defines if the clear value should be the minimum or maximum values of the widget
117117
Returns the value used when clear() is called.
118118

119119
.. seealso:: :py:func:`setClearValue`
120+
%End
121+
122+
void setLineEditAlignment( Qt::Alignment alignment );
123+
%Docstring
124+
Set alignment in the embedded line edit widget
125+
126+
:param alignment:
120127
%End
121128

122129
virtual double valueFromText( const QString &text ) const;

python/gui/editorwidgets/qgsspinbox.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ Defines if the clear value should be the minimum or maximum values of the widget
117117
Returns the value used when clear() is called.
118118

119119
.. seealso:: :py:func:`setClearValue`
120+
%End
121+
122+
void setLineEditAlignment( Qt::Alignment alignment );
123+
%Docstring
124+
Set alignment in the embedded line edit widget
125+
126+
:param alignment:
120127
%End
121128

122129
virtual int valueFromText( const QString &text ) const;

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ SET(QGIS_CORE_SRCS
473473
3d/qgs3drendererregistry.cpp
474474
3d/qgsabstract3drenderer.cpp
475475

476+
fieldformatter/qgsrangefieldformatter.cpp
476477
fieldformatter/qgsdatetimefieldformatter.cpp
477478
fieldformatter/qgsfallbackfieldformatter.cpp
478479
fieldformatter/qgskeyvaluefieldformatter.cpp
@@ -1110,6 +1111,7 @@ SET(QGIS_CORE_HDRS
11101111
3d/qgs3drendererregistry.h
11111112
3d/qgsabstract3drenderer.h
11121113

1114+
fieldformatter/qgsrangefieldformatter.h
11131115
fieldformatter/qgsdatetimefieldformatter.h
11141116
fieldformatter/qgsfallbackfieldformatter.h
11151117
fieldformatter/qgskeyvaluefieldformatter.h
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***************************************************************************
2+
qgsrangefieldformatter.cpp - QgsRangeFieldFormatter
3+
4+
---------------------
5+
begin : 01/02/2018
6+
copyright : (C) 2018 by Alessandro Pasotti
7+
email : elpaso at itopen dot it
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#include <QLocale>
18+
19+
#include "qgsrangefieldformatter.h"
20+
21+
#include "qgssettings.h"
22+
#include "qgsfield.h"
23+
#include "qgsvectorlayer.h"
24+
25+
26+
QString QgsRangeFieldFormatter::id() const
27+
{
28+
return QStringLiteral( "Range" );
29+
}
30+
31+
QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
32+
{
33+
Q_UNUSED( cache )
34+
Q_UNUSED( config )
35+
36+
if ( value.isNull() )
37+
{
38+
return QgsApplication::nullRepresentation();
39+
}
40+
41+
QString result;
42+
43+
// Prepare locale
44+
std::function<QLocale()> f_locale = [ ]
45+
{
46+
QLocale locale( QgsApplication::instance()->locale() );
47+
QLocale::NumberOptions options( locale.numberOptions() );
48+
options |= QLocale::NumberOption::OmitGroupSeparator;
49+
locale.setNumberOptions( options );
50+
return locale;
51+
};
52+
53+
const QgsField field = layer->fields().at( fieldIndex );
54+
55+
if ( field.type() == QVariant::Double &&
56+
config.contains( QStringLiteral( "Precision" ) ) &&
57+
value.isValid( ) )
58+
{
59+
bool ok;
60+
double val( value.toDouble( &ok ) );
61+
if ( ok )
62+
{
63+
int precision( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
64+
if ( ok )
65+
{
66+
// TODO: make the format configurable!
67+
result = f_locale().toString( val, 'f', precision );
68+
}
69+
}
70+
}
71+
else if ( field.type() == QVariant::Int &&
72+
value.isValid( ) )
73+
{
74+
bool ok;
75+
double val( value.toInt( &ok ) );
76+
if ( ok )
77+
{
78+
result = f_locale().toString( val );
79+
}
80+
}
81+
else
82+
{
83+
result = value.toString();
84+
}
85+
return result;
86+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgsrangefieldformatter.h - QgsRangeFieldFormatter
3+
4+
---------------------
5+
begin : 01/02/2018
6+
copyright : (C) 2018 by Alessandro Pasotti
7+
email : elpaso at itopen dot it
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSRANGEFIELDFORMATTER_H
17+
#define QGSRANGEFIELDFORMATTER_H
18+
19+
#include "qgis_core.h"
20+
#include "qgsfieldformatter.h"
21+
22+
/**
23+
* \ingroup core
24+
* Field formatter for a range (double) field with precision and locale
25+
*
26+
* \since QGIS 3.0
27+
*/
28+
class CORE_EXPORT QgsRangeFieldFormatter : public QgsFieldFormatter
29+
{
30+
public:
31+
32+
/**
33+
* Default constructor of field formatter for a range (double)field.
34+
*/
35+
QgsRangeFieldFormatter() = default;
36+
37+
QString id() const override;
38+
39+
QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;
40+
41+
};
42+
43+
#endif // QGSRANGEFIELDFORMATTER_H

src/core/qgsfieldformatterregistry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsrelationreferencefieldformatter.h"
2323
#include "qgskeyvaluefieldformatter.h"
2424
#include "qgslistfieldformatter.h"
25+
#include "qgsrangefieldformatter.h"
2526
#include "qgsfallbackfieldformatter.h"
2627

2728

@@ -34,6 +35,7 @@ QgsFieldFormatterRegistry::QgsFieldFormatterRegistry( QObject *parent )
3435
addFieldFormatter( new QgsKeyValueFieldFormatter() );
3536
addFieldFormatter( new QgsListFieldFormatter() );
3637
addFieldFormatter( new QgsDateTimeFieldFormatter() );
38+
addFieldFormatter( new QgsRangeFieldFormatter() );
3739

3840
mFallbackFieldFormatter = new QgsFallbackFieldFormatter();
3941
}

src/gui/editorwidgets/qgsdoublespinbox.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
3131
{
3232
mLineEdit = new QgsSpinBoxLineEdit();
3333

34+
// By default, group separator is off
35+
setLocale( QLocale( QgsApplication::locale( ) ) );
3436
setLineEdit( mLineEdit );
3537

3638
QSize msz = minimumSizeHint();
@@ -134,6 +136,11 @@ double QgsDoubleSpinBox::clearValue() const
134136
return mCustomClearValue;
135137
}
136138

139+
void QgsDoubleSpinBox::setLineEditAlignment( Qt::Alignment alignment )
140+
{
141+
mLineEdit->setAlignment( alignment );
142+
}
143+
137144
QString QgsDoubleSpinBox::stripped( const QString &originalText ) const
138145
{
139146
//adapted from QAbstractSpinBoxPrivate::stripped

src/gui/editorwidgets/qgsdoublespinbox.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ class GUI_EXPORT QgsDoubleSpinBox : public QDoubleSpinBox
126126
*/
127127
double clearValue() const;
128128

129+
/**
130+
* Set alignment in the embedded line edit widget
131+
* \param alignment
132+
*/
133+
void setLineEditAlignment( Qt::Alignment alignment );
134+
129135
double valueFromText( const QString &text ) const override;
130136
QValidator::State validate( QString &input, int &pos ) const override;
131137
void paintEvent( QPaintEvent *e ) override;

0 commit comments

Comments
 (0)