Skip to content

Commit 21ddf12

Browse files
committedSep 30, 2014
Changing decimal places to precision, allowing negative precision, improving rounding for locales with , instead of . for decimal point
1 parent 465219a commit 21ddf12

File tree

5 files changed

+88
-43
lines changed

5 files changed

+88
-43
lines changed
 

‎python/core/symbology-ng/qgsgraduatedsymbolrendererv2.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class QgsRendererRangeV2LabelFormat
5454
QString format() const;
5555
void setFormat( QString format );
5656

57-
int decimalPlaces() const;
58-
void setDecimalPlaces( int decimalPlaces );
57+
int precision();
58+
void setPrecision( int precision );
5959

6060
bool trimTrailingZeroes() const;
6161
void setTrimTrailingZeroes( bool trimTrailingZeroes );

‎src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,24 @@ void QgsRendererRangeV2::toSld( QDomDocument &doc, QDomElement &element, QgsStri
169169

170170
///////////
171171

172+
int QgsRendererRangeV2LabelFormat::MaxPrecision=15;
173+
int QgsRendererRangeV2LabelFormat::MinPrecision=-6;
174+
172175
QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat():
173176
mFormat( " %1 - %2 " ),
174-
mDecimalPlaces( 4 ),
177+
mPrecision( 4 ),
175178
mTrimTrailingZeroes( false ),
176-
mReTrailingZeroes( "\\.?0*$" )
179+
mNumberScale( 1.0 ),
180+
mNumberSuffix( "" ),
181+
mReTrailingZeroes( "[.,]?0*$" )
177182
{
178183
}
179184

180-
QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces, bool trimTrailingZeroes ):
181-
mReTrailingZeroes( "\\.?0*$" )
185+
QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int precision, bool trimTrailingZeroes ):
186+
mReTrailingZeroes( "[.,]?0*$" )
182187
{
183188
setFormat( format );
184-
setDecimalPlaces( decimalPlaces );
189+
setPrecision( precision );
185190
setTrimTrailingZeroes( trimTrailingZeroes );
186191
}
187192

@@ -190,7 +195,7 @@ bool QgsRendererRangeV2LabelFormat::operator==( const QgsRendererRangeV2LabelFor
190195
{
191196
return
192197
format() == other.format() &&
193-
decimalPlaces() == other.decimalPlaces() &&
198+
precision() == other.precision() &&
194199
trimTrailingZeroes() == other.trimTrailingZeroes();
195200
}
196201

@@ -199,32 +204,49 @@ bool QgsRendererRangeV2LabelFormat::operator!=( const QgsRendererRangeV2LabelFor
199204
return !( *this == other );
200205
}
201206

202-
void QgsRendererRangeV2LabelFormat::setDecimalPlaces( int decimalPlaces )
207+
void QgsRendererRangeV2LabelFormat::setPrecision( int precision )
203208
{
204209
// Limit the range of decimal places to a reasonable range
205-
if ( decimalPlaces < 0 ) decimalPlaces = 0;
206-
if ( decimalPlaces > 15 ) decimalPlaces = 15;
207-
mDecimalPlaces = decimalPlaces;
210+
if ( precision < MinPrecision ) precision = MinPrecision;
211+
if ( precision > MaxPrecision ) precision = MaxPrecision;
212+
mPrecision = precision;
213+
mNumberScale=1.0;
214+
mNumberSuffix="";
215+
while( precision < 0 )
216+
{
217+
precision++;
218+
mNumberScale /= 10.0;
219+
mNumberSuffix.append('0');
220+
}
208221
}
209222

210223
QString QgsRendererRangeV2LabelFormat::labelForRange( const QgsRendererRangeV2 &range ) const
211224
{
212225
return labelForRange( range.lowerValue(), range.upperValue() );
213226
}
214227

215-
QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
228+
QString QgsRendererRangeV2LabelFormat::formatNumber( double value ) const
216229
{
217-
QString lowerStr = QString::number( lower, 'f', mDecimalPlaces );
218-
QString upperStr = QString::number( upper, 'f', mDecimalPlaces );
219-
220-
if ( mTrimTrailingZeroes )
230+
if( mPrecision > 0 )
221231
{
222-
if ( lowerStr.contains( '.' ) ) lowerStr = lowerStr.replace( mReTrailingZeroes, "" );
223-
if ( upperStr.contains( '.' ) ) upperStr = upperStr.replace( mReTrailingZeroes, "" );
232+
QString valueStr=QString::number( value, 'f', mPrecision );
233+
if( mTrimTrailingZeroes ) valueStr=valueStr.replace(mReTrailingZeroes,"");
234+
return valueStr;
224235
}
236+
else
237+
{
238+
QString valueStr=QString::number( value*mNumberScale, 'f', 0 );
239+
if( valueStr != "0" ) valueStr=valueStr+mNumberSuffix;
240+
return valueStr;
241+
}
242+
}
225243

226-
QString legend(mFormat);
244+
QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
245+
{
246+
QString lowerStr=formatNumber(lower);
247+
QString upperStr=formatNumber(upper);
227248

249+
QString legend(mFormat);
228250
return legend.replace( "%1",lowerStr).replace("%2",upperStr );
229251
}
230252

@@ -235,14 +257,14 @@ void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
235257
element.attribute( "separator", " - " ) + "%2" +
236258
element.attribute( "suffix", " " )
237259
);
238-
mDecimalPlaces = element.attribute( "decimalplaces", "4" ).toInt();
260+
setPrecision( element.attribute( "decimalplaces", "4" ).toInt());
239261
mTrimTrailingZeroes = element.attribute( "trimtrailingzeroes", "false" ) == "true";
240262
}
241263

242264
void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
243265
{
244266
element.setAttribute( "format", mFormat );
245-
element.setAttribute( "decimalplaces", mDecimalPlaces );
267+
element.setAttribute( "decimalplaces", mPrecision );
246268
element.setAttribute( "trimtrailingzeroes", mTrimTrailingZeroes ? "true" : "false" );
247269
}
248270

@@ -1454,7 +1476,7 @@ void QgsGraduatedSymbolRendererV2::calculateLabelDecimalPlaces( bool updateRange
14541476
ndp--;
14551477
nextDpMinRange *= 10.0;
14561478
}
1457-
mLabelFormat.setDecimalPlaces( ndp );
1479+
mLabelFormat.setPrecision( ndp );
14581480
if ( updateRanges ) setLabelFormat( mLabelFormat, true );
14591481
}
14601482

‎src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,38 @@ class CORE_EXPORT QgsRendererRangeV2LabelFormat
7171
{
7272
public:
7373
QgsRendererRangeV2LabelFormat();
74-
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces = 4, bool trimTrailingZeroes = false );
74+
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
7575

7676
bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
7777
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
7878

7979
QString format() const { return mFormat; }
8080
void setFormat( QString format ) { mFormat = format; }
8181

82-
int decimalPlaces() const { return mDecimalPlaces; }
83-
void setDecimalPlaces( int decimalPlaces );
82+
int precision() const { return mPrecision; }
83+
void setPrecision( int precision );
8484

8585
bool trimTrailingZeroes() const { return mTrimTrailingZeroes; }
8686
void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }
8787

8888
//! @note labelForLowerUpper in python bindings
8989
QString labelForRange( double lower, double upper ) const;
9090
QString labelForRange( const QgsRendererRangeV2 &range ) const;
91+
QString formatNumber( double value ) const;
9192

9293
void setFromDomElement( QDomElement &element );
9394
void saveToDomElement( QDomElement &element );
9495

96+
static int MaxPrecision;
97+
static int MinPrecision;
98+
9599
protected:
96100
QString mFormat;
97-
int mDecimalPlaces;
101+
int mPrecision;
98102
bool mTrimTrailingZeroes;
103+
// values used to manage number formatting - precision and trailing zeroes
104+
double mNumberScale;
105+
QString mNumberSuffix;
99106
QRegExp mReTrailingZeroes;
100107
};
101108

‎src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ QVariant QgsGraduatedSymbolRendererV2Model::data( const QModelIndex &index, int
118118
if ( !index.isValid() || !mRenderer ) return QVariant();
119119

120120
const QgsRendererRangeV2 range = mRenderer->ranges().value( index.row() );
121-
QString rangeStr = QString::number( range.lowerValue(), 'f', 4 ) + " - " + QString::number( range.upperValue(), 'f', 4 );
122121

123122
if ( role == Qt::CheckStateRole && index.column() == 0 )
124123
{
@@ -128,7 +127,12 @@ QVariant QgsGraduatedSymbolRendererV2Model::data( const QModelIndex &index, int
128127
{
129128
switch ( index.column() )
130129
{
131-
case 1: return rangeStr;
130+
case 1:
131+
{
132+
int decimalPlaces=mRenderer->labelFormat().precision()+2;
133+
if( decimalPlaces < 0 ) decimalPlaces=0;
134+
return QString::number( range.lowerValue(), 'f', decimalPlaces ) + " - " + QString::number( range.upperValue(), 'f', decimalPlaces );
135+
}
132136
case 2: return range.label();
133137
default: return QVariant();
134138
}
@@ -145,7 +149,7 @@ QVariant QgsGraduatedSymbolRendererV2Model::data( const QModelIndex &index, int
145149
{
146150
switch ( index.column() )
147151
{
148-
case 1: return rangeStr;
152+
// case 1: return rangeStr;
149153
case 2: return range.label();
150154
default: return QVariant();
151155
}
@@ -393,6 +397,9 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto
393397

394398
cboGraduatedColorRamp->populate( mStyle );
395399

400+
spinPrecision->setMinimum( QgsRendererRangeV2LabelFormat::MinPrecision);
401+
spinPrecision->setMaximum( QgsRendererRangeV2LabelFormat::MaxPrecision);
402+
396403
// set project default color ramp
397404
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
398405
if ( defaultColorRamp != "" )
@@ -456,7 +463,7 @@ void QgsGraduatedSymbolRendererV2Widget::connectUpdateHandlers()
456463
connect( cboGraduatedMode, SIGNAL( currentIndexChanged( int ) ) , this, SLOT( classifyGraduated() ) );
457464
connect( cboGraduatedColorRamp, SIGNAL( currentIndexChanged( int ) ) , this, SLOT( reapplyColorRamp() ) );
458465
connect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ) , this, SLOT( reapplyColorRamp() ) );
459-
connect( spinDecimalPlaces, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
466+
connect( spinPrecision, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
460467
connect( cbxTrimTrailingZeroes, SIGNAL( toggled( bool ) ), this, SLOT( labelFormatChanged() ) );
461468
connect( txtFormat, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
462469

@@ -472,7 +479,7 @@ void QgsGraduatedSymbolRendererV2Widget::disconnectUpdateHandlers()
472479
disconnect( cboGraduatedMode, SIGNAL( currentIndexChanged( int ) ) , this, SLOT( classifyGraduated() ) );
473480
disconnect( cboGraduatedColorRamp, SIGNAL( currentIndexChanged( int ) ) , this, SLOT( reapplyColorRamp() ) );
474481
disconnect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ) , this, SLOT( reapplyColorRamp() ) );
475-
disconnect( spinDecimalPlaces, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
482+
disconnect( spinPrecision, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
476483
disconnect( cbxTrimTrailingZeroes, SIGNAL( toggled( bool ) ), this, SLOT( labelFormatChanged() ) );
477484
disconnect( txtFormat, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
478485

@@ -516,7 +523,7 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer( bool updateCount
516523

517524
QgsRendererRangeV2LabelFormat labelFormat = mRenderer->labelFormat();
518525
txtFormat->setText( labelFormat.format() );
519-
spinDecimalPlaces->setValue( labelFormat.decimalPlaces() );
526+
spinPrecision->setValue( labelFormat.precision() );
520527
cbxTrimTrailingZeroes->setChecked( labelFormat.trimTrailingZeroes() );
521528

522529
mModel = new QgsGraduatedSymbolRendererV2Model( this );
@@ -733,11 +740,12 @@ void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
733740
QgsLUDialog dialog( this );
734741

735742
const QgsRendererRangeV2& range = mRenderer->ranges()[rangeIdx];
736-
// Add arbitrary 3 to number of decimal places to retain a bit extra accuracy in
737-
// case we want to??
738-
int decimalPlaces = mRenderer->labelFormat().decimalPlaces();
739-
dialog.setLowerValue( QString::number( range.lowerValue(), 'f', decimalPlaces + 3 ) );
740-
dialog.setUpperValue( QString::number( range.upperValue(), 'f', decimalPlaces + 3 ) );
743+
// Add arbitrary 2 to number of decimal places to retain a bit extra.
744+
// Ensures users can see if legend is not completely honest!
745+
int decimalPlaces = mRenderer->labelFormat().precision()+2;
746+
if( decimalPlaces < 0 ) decimalPlaces=0;
747+
dialog.setLowerValue( QString::number( range.lowerValue(), 'f', decimalPlaces ) );
748+
dialog.setUpperValue( QString::number( range.upperValue(), 'f', decimalPlaces ) );
741749

742750
if ( dialog.exec() == QDialog::Accepted )
743751
{
@@ -852,7 +860,7 @@ void QgsGraduatedSymbolRendererV2Widget::labelFormatChanged()
852860
{
853861
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat(
854862
txtFormat->text(),
855-
spinDecimalPlaces->value(),
863+
spinPrecision->value(),
856864
cbxTrimTrailingZeroes->isChecked() );
857865
mRenderer->setLabelFormat( labelFormat, true );
858866
mModel->updateLabels();

‎src/ui/qgsgraduatedsymbolrendererv2widget.ui

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
<item row="6" column="3">
2323
<layout class="QHBoxLayout" name="horizontalLayout_7">
2424
<item>
25-
<widget class="QSpinBox" name="spinDecimalPlaces">
25+
<widget class="QSpinBox" name="spinPrecision">
2626
<property name="sizePolicy">
2727
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
2828
<horstretch>0</horstretch>
2929
<verstretch>0</verstretch>
3030
</sizepolicy>
3131
</property>
32+
<property name="toolTip">
33+
<string>Precision of upper and lower values in label text.
34+
Positive is number of decimal places
35+
Negative rounds to powers of 10</string>
36+
</property>
3237
<property name="minimum">
3338
<number>0</number>
3439
</property>
@@ -42,6 +47,9 @@
4247
</item>
4348
<item>
4449
<widget class="QCheckBox" name="cbxTrimTrailingZeroes">
50+
<property name="toolTip">
51+
<string>Check to remove trailing zeroes after the decimal point from the upper and lower values in the legend.</string>
52+
</property>
4553
<property name="text">
4654
<string>Trim</string>
4755
</property>
@@ -240,13 +248,13 @@ Use &quot;%1&quot; for the lower bound of the classification, and &quot;%2&quot;
240248
<item row="6" column="2">
241249
<widget class="QLabel" name="label_16">
242250
<property name="text">
243-
<string>Decimal places</string>
251+
<string>Precision</string>
244252
</property>
245253
<property name="alignment">
246254
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
247255
</property>
248256
<property name="buddy">
249-
<cstring>spinDecimalPlaces</cstring>
257+
<cstring>spinPrecision</cstring>
250258
</property>
251259
</widget>
252260
</item>
@@ -361,7 +369,7 @@ Use &quot;%1&quot; for the lower bound of the classification, and &quot;%2&quot;
361369
<tabstop>cbxInvertedColorRamp</tabstop>
362370
<tabstop>cboGraduatedMode</tabstop>
363371
<tabstop>txtFormat</tabstop>
364-
<tabstop>spinDecimalPlaces</tabstop>
372+
<tabstop>spinPrecision</tabstop>
365373
<tabstop>cbxTrimTrailingZeroes</tabstop>
366374
<tabstop>viewGraduated</tabstop>
367375
<tabstop>btnGraduatedClassify</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.