Skip to content
Permalink
Browse files

Merge pull request #1599 from ccrook/CategorizedRendererFixes

Categorized renderer fixes
  • Loading branch information
NathanW2 committed Oct 3, 2014
2 parents 10d75c0 + ba0f9d4 commit 0d5fb236ba6e780a2921c5446cf4a10d3ba52baa
@@ -39,33 +39,40 @@ class QgsRendererRangeV2

typedef QList<QgsRendererRangeV2> QgsRangeList;


// @note added in 2.6
class QgsRendererRangeV2LabelFormat
{
%TypeHeaderCode
#include <qgsgraduatedsymbolrendererv2.h>
%End
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces=4, bool trimTrailingZeroes=false );
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );

bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;

QString format() const;
void setFormat( QString format );

int decimalPlaces() const;
void setDecimalPlaces( int decimalPlaces );
int precision();
void setPrecision( int precision );

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

//! @note labelForLowerUpper in python bindings
QString labelForRange( double lower, double upper ) /PyName=labelForLowerUpper/;
QString labelForRange( const QgsRendererRangeV2 &range );
QString labelForRange( double lower, double upper ) const;
QString labelForRange( const QgsRendererRangeV2 &range ) const;
QString formatNumber( double value ) const;

void setFromDomElement( QDomElement &element );
void saveToDomElement( QDomElement &element );

static int MaxPrecision;
static int MinPrecision;

};


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

//! Return the label format used to generate default classification labels
//! @note Added in 2.6
const QgsRendererRangeV2LabelFormat &labelFormat();
const QgsRendererRangeV2LabelFormat &labelFormat() const;
//! Set the label format used to generate default classification labels
//! @param labelFormat The string appended to classification labels
//! @param updateRanges If true then ranges ending with the old unit string are updated to the new.
//! @note Added in 2.6
void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges=true );
void setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges = true );

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

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
@@ -169,19 +169,24 @@ void QgsRendererRangeV2::toSld( QDomDocument &doc, QDomElement &element, QgsStri

///////////

int QgsRendererRangeV2LabelFormat::MaxPrecision=15;
int QgsRendererRangeV2LabelFormat::MinPrecision=-6;

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat():
mFormat( " %1 - %2 " ),
mDecimalPlaces( 4 ),
mPrecision( 4 ),
mTrimTrailingZeroes( false ),
mReTrailingZeroes( "\\.?0*$" )
mNumberScale( 1.0 ),
mNumberSuffix( "" ),
mReTrailingZeroes( "[.,]?0*$" )
{
}

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces, bool trimTrailingZeroes ):
mReTrailingZeroes( "\\.?0*$" )
QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int precision, bool trimTrailingZeroes ):
mReTrailingZeroes( "[.,]?0*$" )
{
setFormat( format );
setDecimalPlaces( decimalPlaces );
setPrecision( precision );
setTrimTrailingZeroes( trimTrailingZeroes );
}

@@ -190,7 +195,7 @@ bool QgsRendererRangeV2LabelFormat::operator==( const QgsRendererRangeV2LabelFor
{
return
format() == other.format() &&
decimalPlaces() == other.decimalPlaces() &&
precision() == other.precision() &&
trimTrailingZeroes() == other.trimTrailingZeroes();
}

@@ -199,31 +204,50 @@ bool QgsRendererRangeV2LabelFormat::operator!=( const QgsRendererRangeV2LabelFor
return !( *this == other );
}

void QgsRendererRangeV2LabelFormat::setDecimalPlaces( int decimalPlaces )
void QgsRendererRangeV2LabelFormat::setPrecision( int precision )
{
// Limit the range of decimal places to a reasonable range
if ( decimalPlaces < 0 ) decimalPlaces = 0;
if ( decimalPlaces > 15 ) decimalPlaces = 15;
mDecimalPlaces = decimalPlaces;
if ( precision < MinPrecision ) precision = MinPrecision;
if ( precision > MaxPrecision ) precision = MaxPrecision;
mPrecision = precision;
mNumberScale=1.0;
mNumberSuffix="";
while( precision < 0 )
{
precision++;
mNumberScale /= 10.0;
mNumberSuffix.append('0');
}
}

QString QgsRendererRangeV2LabelFormat::labelForRange( const QgsRendererRangeV2 &range ) const
{
return labelForRange( range.lowerValue(), range.upperValue() );
}

QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
QString QgsRendererRangeV2LabelFormat::formatNumber( double value ) const
{
QString lowerStr = QString::number( lower, 'f', mDecimalPlaces );
QString upperStr = QString::number( upper, 'f', mDecimalPlaces );

if ( mTrimTrailingZeroes )
if( mPrecision > 0 )
{
QString valueStr=QString::number( value, 'f', mPrecision );
if( mTrimTrailingZeroes ) valueStr=valueStr.replace(mReTrailingZeroes,"");
return valueStr;
}
else
{
if ( lowerStr.contains( '.' ) ) lowerStr = lowerStr.replace( mReTrailingZeroes, "" );
if ( upperStr.contains( '.' ) ) upperStr = upperStr.replace( mReTrailingZeroes, "" );
QString valueStr=QString::number( value*mNumberScale, 'f', 0 );
if( valueStr != "0" ) valueStr=valueStr+mNumberSuffix;
return valueStr;
}
}

QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
{
QString lowerStr=formatNumber(lower);
QString upperStr=formatNumber(upper);

return mFormat.arg( lowerStr, upperStr );
QString legend(mFormat);
return legend.replace( "%1",lowerStr).replace("%2",upperStr );
}

void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
@@ -233,14 +257,14 @@ void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
element.attribute( "separator", " - " ) + "%2" +
element.attribute( "suffix", " " )
);
mDecimalPlaces = element.attribute( "decimalplaces", "4" ).toInt();
setPrecision( element.attribute( "decimalplaces", "4" ).toInt());
mTrimTrailingZeroes = element.attribute( "trimtrailingzeroes", "false" ) == "true";
}

void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
{
element.setAttribute( "format", mFormat );
element.setAttribute( "decimalplaces", mDecimalPlaces );
element.setAttribute( "decimalplaces", mPrecision );
element.setAttribute( "trimtrailingzeroes", mTrimTrailingZeroes ? "true" : "false" );
}

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


void QgsGraduatedSymbolRendererV2::calculateLabelDecimalPlaces( bool updateRanges )
void QgsGraduatedSymbolRendererV2::calculateLabelPrecision( bool updateRanges )
{
// Find the minimum size of a class
double minClassRange = 0.0;
@@ -1452,7 +1476,7 @@ void QgsGraduatedSymbolRendererV2::calculateLabelDecimalPlaces( bool updateRange
ndp--;
nextDpMinRange *= 10.0;
}
mLabelFormat.setDecimalPlaces( ndp );
mLabelFormat.setPrecision( ndp );
if ( updateRanges ) setLabelFormat( mLabelFormat, true );
}

@@ -71,31 +71,38 @@ class CORE_EXPORT QgsRendererRangeV2LabelFormat
{
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces = 4, bool trimTrailingZeroes = false );
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );

bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;

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

int decimalPlaces() const { return mDecimalPlaces; }
void setDecimalPlaces( int decimalPlaces );
int precision() const { return mPrecision; }
void setPrecision( int precision );

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

//! @note labelForLowerUpper in python bindings
QString labelForRange( double lower, double upper ) const;
QString labelForRange( const QgsRendererRangeV2 &range ) const;
QString formatNumber( double value ) const;

void setFromDomElement( QDomElement &element );
void saveToDomElement( QDomElement &element );

static int MaxPrecision;
static int MinPrecision;

protected:
QString mFormat;
int mDecimalPlaces;
int mPrecision;
bool mTrimTrailingZeroes;
// values used to manage number formatting - precision and trailing zeroes
double mNumberScale;
QString mNumberSuffix;
QRegExp mReTrailingZeroes;
};

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

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
@@ -203,7 +210,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted = false,
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat()
QgsRendererRangeV2LabelFormat legendFormat = QgsRendererRangeV2LabelFormat()
);

//! create renderer from XML element
@@ -197,7 +197,7 @@ QVariant QgsCategorizedSymbolRendererV2Model::headerData( int section, Qt::Orien
{
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= 0 && section < 3 )
{
QStringList lst; lst << tr( "Symbol" ) << tr( "Value" ) << tr( "Label" );
QStringList lst; lst << tr( "Symbol" ) << tr( "Value" ) << tr( "Legend" );
return lst.value( section );
}
return QVariant();

0 comments on commit 0d5fb23

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