Skip to content

Commit 0d5fb23

Browse files
committed
Merge pull request #1599 from ccrook/CategorizedRendererFixes
Categorized renderer fixes
2 parents 10d75c0 + ba0f9d4 commit 0d5fb23

File tree

6 files changed

+169
-120
lines changed

6 files changed

+169
-120
lines changed

python/core/symbology-ng/qgsgraduatedsymbolrendererv2.sip

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,40 @@ class QgsRendererRangeV2
3939

4040
typedef QList<QgsRendererRangeV2> QgsRangeList;
4141

42+
43+
// @note added in 2.6
4244
class QgsRendererRangeV2LabelFormat
4345
{
4446
%TypeHeaderCode
4547
#include <qgsgraduatedsymbolrendererv2.h>
4648
%End
4749
public:
4850
QgsRendererRangeV2LabelFormat();
49-
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces=4, bool trimTrailingZeroes=false );
51+
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
5052

5153
bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
5254
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;
5355

5456
QString format() const;
5557
void setFormat( QString format );
5658

57-
int decimalPlaces() const;
58-
void setDecimalPlaces( int decimalPlaces );
59+
int precision();
60+
void setPrecision( int precision );
5961

6062
bool trimTrailingZeroes() const;
6163
void setTrimTrailingZeroes( bool trimTrailingZeroes );
6264

6365
//! @note labelForLowerUpper in python bindings
64-
QString labelForRange( double lower, double upper ) /PyName=labelForLowerUpper/;
65-
QString labelForRange( const QgsRendererRangeV2 &range );
66+
QString labelForRange( double lower, double upper ) const;
67+
QString labelForRange( const QgsRendererRangeV2 &range ) const;
68+
QString formatNumber( double value ) const;
6669

6770
void setFromDomElement( QDomElement &element );
6871
void saveToDomElement( QDomElement &element );
72+
73+
static int MaxPrecision;
74+
static int MinPrecision;
75+
6976
};
7077

7178

@@ -153,17 +160,17 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
153160

154161
//! Return the label format used to generate default classification labels
155162
//! @note Added in 2.6
156-
const QgsRendererRangeV2LabelFormat &labelFormat();
163+
const QgsRendererRangeV2LabelFormat &labelFormat() const;
157164
//! Set the label format used to generate default classification labels
158165
//! @param labelFormat The string appended to classification labels
159166
//! @param updateRanges If true then ranges ending with the old unit string are updated to the new.
160167
//! @note Added in 2.6
161-
void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges=true );
168+
void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = true );
162169

163170
//! Reset the label decimal places to a numberbased on the minimum class interval
164171
//! @param updateRanges if true then ranges currently using the default label will be updated
165172
//! @note Added in 2.6
166-
void calculateLabelDecimalPlaces( bool updateRanges=true );
173+
void calculateLabelPrecision( bool updateRanges = true );
167174

168175
static QgsGraduatedSymbolRendererV2* createRenderer(
169176
QgsVectorLayer* vlayer,

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp

Lines changed: 46 additions & 22 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,31 +204,50 @@ 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 )
231+
{
232+
QString valueStr=QString::number( value, 'f', mPrecision );
233+
if( mTrimTrailingZeroes ) valueStr=valueStr.replace(mReTrailingZeroes,"");
234+
return valueStr;
235+
}
236+
else
221237
{
222-
if ( lowerStr.contains( '.' ) ) lowerStr = lowerStr.replace( mReTrailingZeroes, "" );
223-
if ( upperStr.contains( '.' ) ) upperStr = upperStr.replace( mReTrailingZeroes, "" );
238+
QString valueStr=QString::number( value*mNumberScale, 'f', 0 );
239+
if( valueStr != "0" ) valueStr=valueStr+mNumberSuffix;
240+
return valueStr;
224241
}
242+
}
243+
244+
QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
245+
{
246+
QString lowerStr=formatNumber(lower);
247+
QString upperStr=formatNumber(upper);
225248

226-
return mFormat.arg( lowerStr, upperStr );
249+
QString legend(mFormat);
250+
return legend.replace( "%1",lowerStr).replace("%2",upperStr );
227251
}
228252

229253
void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
@@ -233,14 +257,14 @@ void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
233257
element.attribute( "separator", " - " ) + "%2" +
234258
element.attribute( "suffix", " " )
235259
);
236-
mDecimalPlaces = element.attribute( "decimalplaces", "4" ).toInt();
260+
setPrecision( element.attribute( "decimalplaces", "4" ).toInt());
237261
mTrimTrailingZeroes = element.attribute( "trimtrailingzeroes", "false" ) == "true";
238262
}
239263

240264
void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
241265
{
242266
element.setAttribute( "format", mFormat );
243-
element.setAttribute( "decimalplaces", mDecimalPlaces );
267+
element.setAttribute( "decimalplaces", mPrecision );
244268
element.setAttribute( "trimtrailingzeroes", mTrimTrailingZeroes ? "true" : "false" );
245269
}
246270

@@ -1430,7 +1454,7 @@ void QgsGraduatedSymbolRendererV2::setLabelFormat( const QgsRendererRangeV2Label
14301454
}
14311455

14321456

1433-
void QgsGraduatedSymbolRendererV2::calculateLabelDecimalPlaces( bool updateRanges )
1457+
void QgsGraduatedSymbolRendererV2::calculateLabelPrecision( bool updateRanges )
14341458
{
14351459
// Find the minimum size of a class
14361460
double minClassRange = 0.0;
@@ -1452,7 +1476,7 @@ void QgsGraduatedSymbolRendererV2::calculateLabelDecimalPlaces( bool updateRange
14521476
ndp--;
14531477
nextDpMinRange *= 10.0;
14541478
}
1455-
mLabelFormat.setDecimalPlaces( ndp );
1479+
mLabelFormat.setPrecision( ndp );
14561480
if ( updateRanges ) setLabelFormat( mLabelFormat, true );
14571481
}
14581482

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h

Lines changed: 13 additions & 6 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

@@ -193,7 +200,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
193200
//! Reset the label decimal places to a numberbased on the minimum class interval
194201
//! @param updateRanges if true then ranges currently using the default label will be updated
195202
//! @note Added in 2.6
196-
void calculateLabelDecimalPlaces( bool updateRanges = true );
203+
void calculateLabelPrecision( bool updateRanges = true );
197204

198205
static QgsGraduatedSymbolRendererV2* createRenderer(
199206
QgsVectorLayer* vlayer,
@@ -203,7 +210,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
203210
QgsSymbolV2* symbol,
204211
QgsVectorColorRampV2* ramp,
205212
bool inverted = false,
206-
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat()
213+
QgsRendererRangeV2LabelFormat legendFormat = QgsRendererRangeV2LabelFormat()
207214
);
208215

209216
//! create renderer from XML element

src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ QVariant QgsCategorizedSymbolRendererV2Model::headerData( int section, Qt::Orien
197197
{
198198
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
199199
{
200-
QStringList lst; lst << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
200+
QStringList lst; lst << tr( "Symbol" ) << tr( "Value" ) << tr( "Legend" );
201201
return lst.value( section );
202202
}
203203
return QVariant();

0 commit comments

Comments
 (0)