Skip to content
Permalink
Browse files

API fixes to use Legend instead of Label where it makes more sense

  • Loading branch information
ccrook committed Sep 30, 2014
1 parent 21ddf12 commit a5f0de9dd78bc1830b3fc6bf5065ba9577d2622f
@@ -39,17 +39,19 @@ class QgsRendererRangeV2

typedef QList<QgsRendererRangeV2> QgsRangeList;

class QgsRendererRangeV2LabelFormat

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

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

QString format() const;
void setFormat( QString format );
@@ -61,8 +63,9 @@ class QgsRendererRangeV2LabelFormat
void setTrimTrailingZeroes( bool trimTrailingZeroes );

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

void setFromDomElement( QDomElement &element );
void saveToDomElement( QDomElement &element );
@@ -153,17 +156,17 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

//! Return the label format used to generate default classification labels
//! @note Added in 2.6
const QgsRendererRangeV2LabelFormat &labelFormat();
const QgsRendererRangeV2LegendFormat &legendFormat() 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 setLegendFormat( const QgsRendererRangeV2LegendFormat &legendFormat, 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,
@@ -173,7 +176,7 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
QgsSymbolV2* symbol /Transfer/,
QgsVectorColorRampV2* ramp /Transfer/,
bool inverted = false,
QgsRendererRangeV2LabelFormat labelFormat=QgsRendererRangeV2LabelFormat()
QgsRendererRangeV2LegendFormat labelFormat=QgsRendererRangeV2LegendFormat()
);

//! create renderer from XML element
@@ -169,10 +169,10 @@ void QgsRendererRangeV2::toSld( QDomDocument &doc, QDomElement &element, QgsStri

///////////

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

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat():
QgsRendererRangeV2LegendFormat::QgsRendererRangeV2LegendFormat():
mFormat( " %1 - %2 " ),
mPrecision( 4 ),
mTrimTrailingZeroes( false ),
@@ -182,7 +182,7 @@ QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat():
{
}

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int precision, bool trimTrailingZeroes ):
QgsRendererRangeV2LegendFormat::QgsRendererRangeV2LegendFormat( QString format, int precision, bool trimTrailingZeroes ):
mReTrailingZeroes( "[.,]?0*$" )
{
setFormat( format );
@@ -191,20 +191,20 @@ QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, in
}


bool QgsRendererRangeV2LabelFormat::operator==( const QgsRendererRangeV2LabelFormat &other ) const
bool QgsRendererRangeV2LegendFormat::operator==( const QgsRendererRangeV2LegendFormat &other ) const
{
return
format() == other.format() &&
precision() == other.precision() &&
trimTrailingZeroes() == other.trimTrailingZeroes();
}

bool QgsRendererRangeV2LabelFormat::operator!=( const QgsRendererRangeV2LabelFormat &other ) const
bool QgsRendererRangeV2LegendFormat::operator!=( const QgsRendererRangeV2LegendFormat &other ) const
{
return !( *this == other );
}

void QgsRendererRangeV2LabelFormat::setPrecision( int precision )
void QgsRendererRangeV2LegendFormat::setPrecision( int precision )
{
// Limit the range of decimal places to a reasonable range
if ( precision < MinPrecision ) precision = MinPrecision;
@@ -220,12 +220,12 @@ void QgsRendererRangeV2LabelFormat::setPrecision( int precision )
}
}

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

QString QgsRendererRangeV2LabelFormat::formatNumber( double value ) const
QString QgsRendererRangeV2LegendFormat::formatNumber( double value ) const
{
if( mPrecision > 0 )
{
@@ -241,7 +241,7 @@ QString QgsRendererRangeV2LabelFormat::formatNumber( double value ) const
}
}

QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper ) const
QString QgsRendererRangeV2LegendFormat::legendForRange( double lower, double upper ) const
{
QString lowerStr=formatNumber(lower);
QString upperStr=formatNumber(upper);
@@ -250,7 +250,7 @@ QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper
return legend.replace( "%1",lowerStr).replace("%2",upperStr );
}

void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
void QgsRendererRangeV2LegendFormat::setFromDomElement( QDomElement &element )
{
mFormat = element.attribute( "format",
element.attribute( "prefix", " " ) + "%1" +
@@ -261,7 +261,7 @@ void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
mTrimTrailingZeroes = element.attribute( "trimtrailingzeroes", "false" ) == "true";
}

void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
void QgsRendererRangeV2LegendFormat::saveToDomElement( QDomElement &element )
{
element.setAttribute( "format", mFormat );
element.setAttribute( "decimalplaces", mPrecision );
@@ -450,9 +450,9 @@ bool QgsGraduatedSymbolRendererV2::updateRangeUpperValue( int rangeIndex, double
if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
return false;
QgsRendererRangeV2 &range = mRanges[rangeIndex];
bool isDefaultLabel = range.label() == mLabelFormat.labelForRange( range );
bool isDefaultLabel = range.label() == mLabelFormat.legendForRange( range );
range.setUpperValue( value );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.labelForRange( range ) );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.legendForRange( range ) );
return true;
}

@@ -461,9 +461,9 @@ bool QgsGraduatedSymbolRendererV2::updateRangeLowerValue( int rangeIndex, double
if ( rangeIndex < 0 || rangeIndex >= mRanges.size() )
return false;
QgsRendererRangeV2 &range = mRanges[rangeIndex];
bool isDefaultLabel = range.label() == mLabelFormat.labelForRange( range );
bool isDefaultLabel = range.label() == mLabelFormat.legendForRange( range );
range.setLowerValue( value );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.labelForRange( range ) );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.legendForRange( range ) );
return true;
}

@@ -498,7 +498,7 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone() const
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
r->setLabelFormat( labelFormat() );
r->setLegendFormat( legendFormat() );
return r;
}

@@ -926,7 +926,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted,
QgsRendererRangeV2LabelFormat labelFormat
QgsRendererRangeV2LegendFormat labelFormat
)
{
QgsRangeList ranges;
@@ -935,7 +935,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( inverted );
r->setMode( mode );
r->setLabelFormat( labelFormat, false );
r->setLegendFormat( labelFormat, false );
r->updateClasses( vlayer, mode, classes );
return r;
}
@@ -1076,7 +1076,7 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
}
else
{
label = mLabelFormat.labelForRange( lower, upper );
label = mLabelFormat.legendForRange( lower, upper );
}
QgsSymbolV2* newSymbol = mSourceSymbol->clone();
addClass( QgsRendererRangeV2( lower, upper, newSymbol, label ) );
@@ -1176,9 +1176,9 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element
QDomElement labelFormatElem = element.firstChildElement( "labelformat" );
if ( ! labelFormatElem.isNull() )
{
QgsRendererRangeV2LabelFormat labelFormat;
QgsRendererRangeV2LegendFormat labelFormat;
labelFormat.setFromDomElement( labelFormatElem );
r->setLabelFormat( labelFormat );
r->setLegendFormat( labelFormat );
}
// TODO: symbol levels
return r;
@@ -1422,7 +1422,7 @@ void QgsGraduatedSymbolRendererV2::addClass( QgsSymbolV2* symbol )
void QgsGraduatedSymbolRendererV2::addClass( double lower, double upper )
{
QgsSymbolV2* newSymbol = mSourceSymbol->clone();
QString label = mLabelFormat.labelForRange( lower, upper );
QString label = mLabelFormat.legendForRange( lower, upper );
mRanges.append( QgsRendererRangeV2( lower, upper, newSymbol, label ) );
}

@@ -1441,20 +1441,20 @@ void QgsGraduatedSymbolRendererV2::deleteAllClasses()
mRanges.clear();
}

void QgsGraduatedSymbolRendererV2::setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges )
void QgsGraduatedSymbolRendererV2::setLegendFormat( const QgsRendererRangeV2LegendFormat &labelFormat, bool updateRanges )
{
if ( updateRanges && labelFormat != mLabelFormat )
{
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
it->setLabel( labelFormat.labelForRange( *it ) );
it->setLabel( labelFormat.legendForRange( *it ) );
}
}
mLabelFormat = labelFormat;
}


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

void QgsGraduatedSymbolRendererV2::moveClass( int from, int to )
@@ -67,14 +67,14 @@ typedef QList<QgsRendererRangeV2> QgsRangeList;


// @note added in 2.6
class CORE_EXPORT QgsRendererRangeV2LabelFormat
class CORE_EXPORT QgsRendererRangeV2LegendFormat
{
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
QgsRendererRangeV2LegendFormat();
QgsRendererRangeV2LegendFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );

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

QString format() const { return mFormat; }
void setFormat( QString format ) { mFormat = format; }
@@ -86,8 +86,8 @@ class CORE_EXPORT QgsRendererRangeV2LabelFormat
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 legendForRange( double lower, double upper ) const;
QString legendForRange( const QgsRendererRangeV2 &range ) const;
QString formatNumber( double value ) const;

void setFromDomElement( QDomElement &element );
@@ -190,17 +190,17 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2

//! Return the label format used to generate default classification labels
//! @note Added in 2.6
const QgsRendererRangeV2LabelFormat &labelFormat() const { return mLabelFormat; }
const QgsRendererRangeV2LegendFormat &legendFormat() const { return mLabelFormat; }
//! 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 setLegendFormat( const QgsRendererRangeV2LegendFormat &legendFormat, 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,
@@ -210,7 +210,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted = false,
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat()
QgsRendererRangeV2LegendFormat legendFormat = QgsRendererRangeV2LegendFormat()
);

//! create renderer from XML element
@@ -287,7 +287,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QScopedPointer<QgsSymbolV2> mSourceSymbol;
QScopedPointer<QgsVectorColorRampV2> mSourceColorRamp;
bool mInvertedColorRamp;
QgsRendererRangeV2LabelFormat mLabelFormat;
QgsRendererRangeV2LegendFormat mLabelFormat;
QScopedPointer<QgsExpression> mRotation;
QScopedPointer<QgsExpression> mSizeScale;
QgsSymbolV2::ScaleMethod mScaleMethod;
@@ -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 a5f0de9

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