Skip to content

Commit

Permalink
fix more graduate renderer crashes (followup d99d2fd)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 31, 2014
1 parent 6070627 commit e4d1735
Showing 1 changed file with 65 additions and 41 deletions.
106 changes: 65 additions & 41 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void QgsRendererRangeV2::setRenderState( bool render )

QString QgsRendererRangeV2::dump() const
{
return QString( "%1 - %2::%3::%4\n" ).arg( mLowerValue ).arg( mUpperValue ).arg( mLabel ).arg( mSymbol->dump() );
return QString( "%1 - %2::%3::%4\n" ).arg( mLowerValue ).arg( mUpperValue ).arg( mLabel ).arg( mSymbol.data() ? mSymbol->dump() : "(no symbol)" );
}

void QgsRendererRangeV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
Expand Down Expand Up @@ -209,8 +209,7 @@ bool QgsRendererRangeV2LabelFormat::operator!=( const QgsRendererRangeV2LabelFor
void QgsRendererRangeV2LabelFormat::setPrecision( int precision )
{
// Limit the range of decimal places to a reasonable range
if ( precision < MinPrecision ) precision = MinPrecision;
if ( precision > MaxPrecision ) precision = MaxPrecision;
precision = qBound( MinPrecision, precision, MaxPrecision );
mPrecision = precision;
mNumberScale = 1.0;
mNumberSuffix = "";
Expand All @@ -232,15 +231,19 @@ QString QgsRendererRangeV2LabelFormat::formatNumber( double value ) const
if ( mPrecision > 0 )
{
QString valueStr = QString::number( value, 'f', mPrecision );
if ( mTrimTrailingZeroes ) valueStr = valueStr.replace( mReTrailingZeroes, "" );
if ( mReNegativeZero.exactMatch( valueStr ) ) valueStr = valueStr.mid( 1 );
if ( mTrimTrailingZeroes )
valueStr = valueStr.replace( mReTrailingZeroes, "" );
if ( mReNegativeZero.exactMatch( valueStr ) )
valueStr = valueStr.mid( 1 );
return valueStr;
}
else
{
QString valueStr = QString::number( value * mNumberScale, 'f', 0 );
if ( valueStr == "-0" ) valueStr = "0";
if ( valueStr != "0" ) valueStr = valueStr + mNumberSuffix;
if ( valueStr == "-0" )
valueStr = "0";
if ( valueStr != "0" )
valueStr = valueStr + mNumberSuffix;
return valueStr;
}
}
Expand Down Expand Up @@ -275,12 +278,12 @@ void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
///////////

QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2( QString attrName, QgsRangeList ranges )
: QgsFeatureRendererV2( "graduatedSymbol" ),
mAttrName( attrName ),
mRanges( ranges ),
mMode( Custom ),
mInvertedColorRamp( false ),
mScaleMethod( DEFAULT_SCALE_METHOD )
: QgsFeatureRendererV2( "graduatedSymbol" )
, mAttrName( attrName )
, mRanges( ranges )
, mMode( Custom )
, mInvertedColorRamp( false )
, mScaleMethod( DEFAULT_SCALE_METHOD )
{
// TODO: check ranges for sanity (NULL symbols, invalid ranges)
}
Expand Down Expand Up @@ -375,6 +378,9 @@ void QgsGraduatedSymbolRendererV2::startRender( QgsRenderContext& context, const
QgsRangeList::iterator it = mRanges.begin();
for ( ; it != mRanges.end(); ++it )
{
if( !it->symbol() )
continue;

it->symbol()->startRender( context, &fields );

if ( mRotation.data() || mSizeScale.data() )
Expand All @@ -392,7 +398,12 @@ void QgsGraduatedSymbolRendererV2::stopRender( QgsRenderContext& context )
{
QgsRangeList::iterator it = mRanges.begin();
for ( ; it != mRanges.end(); ++it )
{
if( !it->symbol() )
continue;

it->symbol()->stopRender( context );
}

// cleanup mTempSymbols
QHash<QgsSymbolV2*, QgsSymbolV2*>::iterator it2 = mTempSymbols.begin();
Expand Down Expand Up @@ -966,15 +977,15 @@ QList<double> QgsGraduatedSymbolRendererV2::getDataValues( QgsVectorLayer *vlaye
lst = expression->referencedColumns();

QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest()
.setFlags( ( !expression.isNull() && expression->needsGeometry() ) ?
.setFlags( ( expression && expression->needsGeometry() ) ?
QgsFeatureRequest::NoFlags :
QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( lst, vlayer->pendingFields() ) );

// create list of non-null attribute values
while ( fit.nextFeature( f ) )
{
QVariant v = expression.isNull() ? f.attribute( attrNum ) : expression->evaluate( f );
QVariant v = expression ? expression->evaluate( f ) : f.attribute( attrNum );
if ( !v.isNull() )
values.append( v.toDouble() );
}
Expand All @@ -983,25 +994,30 @@ QList<double> QgsGraduatedSymbolRendererV2::getDataValues( QgsVectorLayer *vlaye

void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses )
{
if ( mAttrName.isEmpty() ) return;
if ( mAttrName.isEmpty() )
return;

setMode( mode );
// Custom classes are not recalculated
if ( mode == Custom ) return;
if ( mode == Custom )
return;

if ( nclasses < 1 ) nclasses = 1;
if ( nclasses < 1 )
nclasses = 1;

QList<double> values;
bool valuesLoaded = false;
double minimum;
double maximum;


int attrNum = vlayer->fieldNameIndex( mAttrName );

if ( attrNum == -1 )
{
values = getDataValues( vlayer );
if( values.isEmpty() )
return;

qSort( values );
minimum = values.first();
maximum = values.last();
Expand All @@ -1027,9 +1043,7 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
else if ( mode == Quantile || mode == Jenks || mode == StdDev )
{
// get values from layer


if ( ! valuesLoaded )
if ( !valuesLoaded )
{
values = getDataValues( vlayer );
}
Expand All @@ -1053,7 +1067,6 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
Q_ASSERT( false );
}


double lower, upper = minimum;
QString label;
deleteAllClasses();
Expand Down Expand Up @@ -1086,15 +1099,13 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
{
label = mLabelFormat.labelForRange( lower, upper );
}
QgsSymbolV2* newSymbol = mSourceSymbol->clone();
QgsSymbolV2* newSymbol = mSourceSymbol ? mSourceSymbol->clone() : QgsSymbolV2::defaultSymbol( vlayer->geometryType() );
addClass( QgsRendererRangeV2( lower, upper, newSymbol, label ) );
}
updateColorRamp( 0, mInvertedColorRamp );

}



QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element )
{
QDomElement symbolsElem = element.firstChildElement( "symbols" );
Expand Down Expand Up @@ -1335,19 +1346,24 @@ void QgsGraduatedSymbolRendererV2::updateColorRamp( QgsVectorColorRampV2 *ramp,
int i = 0;
if ( ramp )
{
this->setSourceColorRamp( ramp );
this->setInvertedColorRamp( inverted );
setSourceColorRamp( ramp );
setInvertedColorRamp( inverted );
}

if ( mSourceColorRamp )
{
foreach ( QgsRendererRangeV2 range, mRanges )
{
QgsSymbolV2* symbol = range.symbol()->clone();
double colorValue;
if ( inverted ) colorValue = ( mRanges.count() > 1 ? ( double )( mRanges.count() - i - 1 ) / ( mRanges.count() - 1 ) : 0 );
else colorValue = ( mRanges.count() > 1 ? ( double ) i / ( mRanges.count() - 1 ) : 0 );
symbol->setColor( mSourceColorRamp->color( colorValue ) );
QgsSymbolV2 *symbol = range.symbol() ? range.symbol()->clone() : 0;
if( symbol )
{
double colorValue;
if ( inverted )
colorValue = ( mRanges.count() > 1 ? ( double )( mRanges.count() - i - 1 ) / ( mRanges.count() - 1 ) : 0 );
else
colorValue = ( mRanges.count() > 1 ? ( double ) i / ( mRanges.count() - 1 ) : 0 );
symbol->setColor( mSourceColorRamp->color( colorValue ) );
}
updateRangeSymbol( i, symbol );
++i;
}
Expand All @@ -1357,15 +1373,18 @@ void QgsGraduatedSymbolRendererV2::updateColorRamp( QgsVectorColorRampV2 *ramp,

void QgsGraduatedSymbolRendererV2::updateSymbols( QgsSymbolV2 *sym )
{
if( !sym )
return;

int i = 0;
foreach ( QgsRendererRangeV2 range, mRanges )
{
QgsSymbolV2* symbol = sym->clone();
QgsSymbolV2 *symbol = sym->clone();
symbol->setColor( range.symbol()->color() );
updateRangeSymbol( i, symbol );
++i;
}
this->setSourceSymbol( sym->clone() );
setSourceSymbol( sym->clone() );
}

void QgsGraduatedSymbolRendererV2::setRotationField( QString fieldOrExpression )
Expand Down Expand Up @@ -1393,7 +1412,8 @@ void QgsGraduatedSymbolRendererV2::setScaleMethod( QgsSymbolV2::ScaleMethod scal
mScaleMethod = scaleMethod;
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
setScaleMethodToSymbol( it->symbol(), scaleMethod );
if( it->symbol() )
setScaleMethodToSymbol( it->symbol(), scaleMethod );
}
}

Expand Down Expand Up @@ -1470,10 +1490,13 @@ void QgsGraduatedSymbolRendererV2::calculateLabelPrecision( bool updateRanges )
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
double range = it->upperValue() - it->lowerValue();
if ( range <= 0.0 ) continue;
if ( minClassRange == 0.0 || range < minClassRange ) minClassRange = range;
if ( range <= 0.0 )
continue;
if ( minClassRange == 0.0 || range < minClassRange )
minClassRange = range;
}
if ( minClassRange <= 0.0 ) return;
if ( minClassRange <= 0.0 )
return;

// Now set the number of decimal places to ensure no more than 20% error in
// representing this range (up to 10% at upper and lower end)
Expand All @@ -1491,7 +1514,8 @@ void QgsGraduatedSymbolRendererV2::calculateLabelPrecision( bool updateRanges )

void QgsGraduatedSymbolRendererV2::moveClass( int from, int to )
{
if ( from < 0 || from >= mRanges.size() || to < 0 || to >= mRanges.size() ) return;
if ( from < 0 || from >= mRanges.size() || to < 0 || to >= mRanges.size() )
return;
mRanges.move( from, to );
}

Expand Down Expand Up @@ -1566,6 +1590,6 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::convertFromRenderer(
{
r->setSourceSymbol( symbols.at( 0 )->clone() );
}
return r;

return r;
}

0 comments on commit e4d1735

Please sign in to comment.