Skip to content
Permalink
Browse files

Reverting to preferred QgsRenderRangeV2LabelFormat

  • Loading branch information
ccrook committed Oct 3, 2014
1 parent 0d1f719 commit ba0f9d40c911fbbbc765c0fdf971eddde1b2d8ce
@@ -41,17 +41,17 @@ typedef QList<QgsRendererRangeV2> QgsRangeList;


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

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

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

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

void setFromDomElement( QDomElement &element );
@@ -160,12 +160,12 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

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

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

///////////

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

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

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


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

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

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

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

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

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

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

void QgsRendererRangeV2LegendFormat::saveToDomElement( QDomElement &element )
void QgsRendererRangeV2LabelFormat::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.legendForRange( range );
bool isDefaultLabel = range.label() == mLabelFormat.labelForRange( range );
range.setUpperValue( value );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.legendForRange( range ) );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.labelForRange( 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.legendForRange( range );
bool isDefaultLabel = range.label() == mLabelFormat.labelForRange( range );
range.setLowerValue( value );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.legendForRange( range ) );
if ( isDefaultLabel ) range.setLabel( mLabelFormat.labelForRange( range ) );
return true;
}

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

@@ -926,7 +926,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted,
QgsRendererRangeV2LegendFormat labelFormat
QgsRendererRangeV2LabelFormat labelFormat
)
{
QgsRangeList ranges;
@@ -935,7 +935,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
r->setSourceColorRamp( ramp->clone() );
r->setInvertedColorRamp( inverted );
r->setMode( mode );
r->setLegendFormat( labelFormat, false );
r->setLabelFormat( labelFormat, false );
r->updateClasses( vlayer, mode, classes );
return r;
}
@@ -1076,7 +1076,7 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
}
else
{
label = mLabelFormat.legendForRange( lower, upper );
label = mLabelFormat.labelForRange( 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() )
{
QgsRendererRangeV2LegendFormat labelFormat;
QgsRendererRangeV2LabelFormat labelFormat;
labelFormat.setFromDomElement( labelFormatElem );
r->setLegendFormat( labelFormat );
r->setLabelFormat( 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.legendForRange( lower, upper );
QString label = mLabelFormat.labelForRange( lower, upper );
mRanges.append( QgsRendererRangeV2( lower, upper, newSymbol, label ) );
}

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

void QgsGraduatedSymbolRendererV2::setLegendFormat( const QgsRendererRangeV2LegendFormat &labelFormat, bool updateRanges )
void QgsGraduatedSymbolRendererV2::setLabelFormat( const QgsRendererRangeV2LabelFormat &labelFormat, bool updateRanges )
{
if ( updateRanges && labelFormat != mLabelFormat )
{
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
it->setLabel( labelFormat.legendForRange( *it ) );
it->setLabel( labelFormat.labelForRange( *it ) );
}
}
mLabelFormat = labelFormat;
@@ -1477,7 +1477,7 @@ void QgsGraduatedSymbolRendererV2::calculateLabelPrecision( bool updateRanges )
nextDpMinRange *= 10.0;
}
mLabelFormat.setPrecision( ndp );
if ( updateRanges ) setLegendFormat( mLabelFormat, true );
if ( updateRanges ) setLabelFormat( 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 QgsRendererRangeV2LegendFormat
class CORE_EXPORT QgsRendererRangeV2LabelFormat
{
public:
QgsRendererRangeV2LegendFormat();
QgsRendererRangeV2LegendFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString format, int precision = 4, bool trimTrailingZeroes = false );

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

QString format() const { return mFormat; }
void setFormat( QString format ) { mFormat = format; }
@@ -86,8 +86,8 @@ class CORE_EXPORT QgsRendererRangeV2LegendFormat
void setTrimTrailingZeroes( bool trimTrailingZeroes ) { mTrimTrailingZeroes = trimTrailingZeroes; }

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

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

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

//! create renderer from XML element
@@ -287,7 +287,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QScopedPointer<QgsSymbolV2> mSourceSymbol;
QScopedPointer<QgsVectorColorRampV2> mSourceColorRamp;
bool mInvertedColorRamp;
QgsRendererRangeV2LegendFormat mLabelFormat;
QgsRendererRangeV2LabelFormat mLabelFormat;
QScopedPointer<QgsExpression> mRotation;
QScopedPointer<QgsExpression> mSizeScale;
QgsSymbolV2::ScaleMethod mScaleMethod;
@@ -129,7 +129,7 @@ QVariant QgsGraduatedSymbolRendererV2Model::data( const QModelIndex &index, int
{
case 1:
{
int decimalPlaces=mRenderer->legendFormat().precision()+2;
int decimalPlaces=mRenderer->labelFormat().precision()+2;
if( decimalPlaces < 0 ) decimalPlaces=0;
return QString::number( range.lowerValue(), 'f', decimalPlaces ) + " - " + QString::number( range.upperValue(), 'f', decimalPlaces );
}
@@ -397,8 +397,8 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto

cboGraduatedColorRamp->populate( mStyle );

spinPrecision->setMinimum( QgsRendererRangeV2LegendFormat::MinPrecision);
spinPrecision->setMaximum( QgsRendererRangeV2LegendFormat::MaxPrecision);
spinPrecision->setMinimum( QgsRendererRangeV2LabelFormat::MinPrecision);
spinPrecision->setMaximum( QgsRendererRangeV2LabelFormat::MaxPrecision);

// set project default color ramp
QString defaultColorRamp = QgsProject::instance()->readEntry( "DefaultStyles", "/ColorRamp", "" );
@@ -521,7 +521,7 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer( bool updateCount
cbxInvertedColorRamp->setChecked( mRenderer->invertedColorRamp() );
}

QgsRendererRangeV2LegendFormat labelFormat = mRenderer->legendFormat();
QgsRendererRangeV2LabelFormat labelFormat = mRenderer->labelFormat();
txtFormat->setText( labelFormat.format() );
spinPrecision->setValue( labelFormat.precision() );
cbxTrimTrailingZeroes->setChecked( labelFormat.trimTrailingZeroes() );
@@ -742,7 +742,7 @@ void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
const QgsRendererRangeV2& range = mRenderer->ranges()[rangeIdx];
// Add arbitrary 2 to number of decimal places to retain a bit extra.
// Ensures users can see if legend is not completely honest!
int decimalPlaces = mRenderer->legendFormat().precision()+2;
int decimalPlaces = mRenderer->labelFormat().precision()+2;
if( decimalPlaces < 0 ) decimalPlaces=0;
dialog.setLowerValue( QString::number( range.lowerValue(), 'f', decimalPlaces ) );
dialog.setUpperValue( QString::number( range.upperValue(), 'f', decimalPlaces ) );
@@ -858,11 +858,11 @@ void QgsGraduatedSymbolRendererV2Widget::scaleMethodChanged( QgsSymbolV2::ScaleM

void QgsGraduatedSymbolRendererV2Widget::labelFormatChanged()
{
QgsRendererRangeV2LegendFormat labelFormat = QgsRendererRangeV2LegendFormat(
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat(
txtFormat->text(),
spinPrecision->value(),
cbxTrimTrailingZeroes->isChecked() );
mRenderer->setLegendFormat( labelFormat, true );
mRenderer->setLabelFormat( labelFormat, true );
mModel->updateLabels();
}

0 comments on commit ba0f9d4

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