Skip to content
Permalink
Browse files

Numerous enhancements to graduated and categorized symbol renderers

  • Loading branch information
ccrook committed Sep 20, 2014
1 parent 1622bed commit abd9e3cd215ae591f01a5ed0ac751373c86eb8b0
@@ -120,6 +120,10 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
bool invertedColorRamp();
void setInvertedColorRamp( bool inverted );

// Update the color ramp used and all symbols colors.
//! @note added in 2.5
void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );

//! @note added in 1.6
void setRotationField( QString fieldOrExpression );
//! @note added in 1.6
@@ -147,6 +151,10 @@ class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
// @note added in 2.5
virtual void checkLegendSymbolItem( QString key, bool state = true );

//! If supported by the renderer, return classification attribute for the use in legend
//! @note added in 2.6
virtual QString legendClassificationAttribute() const;

//! creates a QgsCategorizedSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
//! @returns a new renderer if the conversion was possible, otherwise 0.
@@ -80,6 +80,8 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
bool updateRangeLowerValue( int rangeIndex, double value );
//! @note added in 2.5
bool updateRangeRenderState( int rangeIndex, bool render );
QString defaultRangeLabel( const QgsRendererRangeV2 &range );


void addClass( QgsSymbolV2* symbol );
//! @note available in python bindings as addClassRange
@@ -106,14 +108,22 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
Mode mode() const;
void setMode( Mode mode );

QString units();
void setUnits( QString units, bool updateRanges=true );

int decimalPlaces() const;
void setDecimalPlaces( int decimalPlaces, bool updateRanges=true );

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
QString attrName,
int classes,
Mode mode,
QgsSymbolV2* symbol /Transfer/,
QgsVectorColorRampV2* ramp /Transfer/,
bool inverted = false ) /Factory/;
bool inverted = false,
QString units = "",
int decimalPlaces = 4 ) /Factory/;

//! create renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
@@ -141,7 +151,7 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
/** Update the color ramp used. Also updates all symbols colors.
* Doesn't alter current breaks.
*/
void updateColorRamp( QgsVectorColorRampV2* ramp /Transfer/ );
void updateColorRamp( QgsVectorColorRampV2* ramp /Transfer/, bool inverted = false );

/** Update the all symbols but leave breaks and colors. */
void updateSymbols( QgsSymbolV2* sym /Transfer/ );
@@ -178,6 +188,10 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
//! @returns a new renderer if the conversion was possible, otherwise 0.
static QgsGraduatedSymbolRendererV2* convertFromRenderer( const QgsFeatureRendererV2 *renderer ) /Factory/;

//! If supported by the renderer, return classification attribute for the use in legend
//! @note added in 2.6
virtual QString legendClassificationAttribute();

protected:
QgsSymbolV2* symbolForValue( double value );

@@ -18,6 +18,7 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
void categoriesDoubleClicked( const QModelIndex & idx );
void addCategory();
void addCategories();
void applyColorRamp();
void deleteCategories();
void deleteAllCategories();

@@ -49,6 +50,8 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget

void changeCategorySymbol();

QgsVectorColorRampV2* getColorRamp();

QList<QgsSymbolV2*> selectedSymbols();
QgsCategoryList selectedCategoryList();
void refreshSymbolView();
@@ -26,10 +26,14 @@ class QgsGraduatedSymbolRendererV2Widget : QgsRendererV2Widget
void deleteClasses();
/**Removes all classes from the classification*/
void deleteAllClasses();
/**Toggle the link between classes boundaries */
void toggleBoundariesLink( bool linked );

void rotationFieldChanged( QString fldName );
void sizeScaleFieldChanged( QString fldName );
void scaleMethodChanged( QgsSymbolV2::ScaleMethod scaleMethod );
void decimalPlacesChanged();
void unitsChanged( QString units );

void showSymbolLevels();

@@ -168,7 +168,7 @@ void QgsCategorizedSymbolRendererV2::rebuildHash()
{
mSymbolHash.clear();

for ( int i = 0; i < mCategories.count(); ++i )
for ( int i = 0; i < mCategories.size(); ++i )
{
QgsRendererCategoryV2& cat = mCategories[i];
mSymbolHash.insert( cat.value().toString(), ( cat.renderState() || mCounting ) ? cat.symbol() : &sSkipRender );
@@ -181,7 +181,7 @@ QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForValue( QVariant value )
QHash<QString, QgsSymbolV2*>::iterator it = mSymbolHash.find( value.toString() );
if ( it == mSymbolHash.end() )
{
if ( mSymbolHash.count() == 0 )
if ( mSymbolHash.size() == 0 )
{
QgsDebugMsg( "there are no hashed symbols!!!" );
}
@@ -689,11 +689,27 @@ QgsVectorColorRampV2* QgsCategorizedSymbolRendererV2::sourceColorRamp()
{
return mSourceColorRamp.data();
}

void QgsCategorizedSymbolRendererV2::setSourceColorRamp( QgsVectorColorRampV2* ramp )
{
mSourceColorRamp.reset( ramp );
}

void QgsCategorizedSymbolRendererV2::updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted )
{
setSourceColorRamp(ramp);
setInvertedColorRamp(inverted);
double num=mCategories.count()-1;
double count = 0;
foreach ( const QgsRendererCategoryV2 &cat, mCategories )
{
double value=count/num;
if( mInvertedColorRamp ) value=1.0-value;
cat.symbol()->setColor(mSourceColorRamp->color(value));
count += 1;
}
}

void QgsCategorizedSymbolRendererV2::setRotationField( QString fieldOrExpression )
{
mRotation.reset( QgsSymbolLayerV2Utils::fieldOrExpressionToExpression( fieldOrExpression ) );
@@ -777,5 +793,16 @@ QgsCategorizedSymbolRendererV2* QgsCategorizedSymbolRendererV2::convertFromRende
const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
}
return 0;

// If not one of the specifically handled renderers, then just grab the symbol from the renderer
// Could have applied this to specific renderer types (singleSymbol, graduatedSymbo)

QgsCategorizedSymbolRendererV2* r =new QgsCategorizedSymbolRendererV2( "", QgsCategoryList() );
QgsSymbolV2List symbols=const_cast<QgsFeatureRendererV2 *>(renderer)->symbols();
if( symbols.size() > 0 )
{
r->setSourceSymbol(symbols.at(0)->clone());
}
return r;

}
@@ -142,8 +142,6 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2

QgsSymbolV2* sourceSymbol();
void setSourceSymbol( QgsSymbolV2* sym );
//! @note added in 2.5
void updateSymbols( QgsSymbolV2* sym );

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp( QgsVectorColorRampV2* ramp );
@@ -152,9 +150,10 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
bool invertedColorRamp() { return mInvertedColorRamp; }
void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = inverted; }

// Update the color ramp used. Also updates all symbols colors.
// @note added in 2.5
// Update the color ramp used and all symbols colors.
//! @note added in 2.5
void updateColorRamp( QgsVectorColorRampV2* ramp, bool inverted = false );

//! @note added in 1.6
void setRotationField( QString fieldOrExpression );
//! @note added in 1.6
@@ -164,6 +164,7 @@ QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2( QString attrName, Qg
mMode( Custom ),
mInvertedColorRamp( false ),
mUnits(""),
mDecimalPlaces(4),
mScaleMethod( DEFAULT_SCALE_METHOD )
{
// TODO: check ranges for sanity (NULL symbols, invalid ranges)
@@ -360,7 +361,9 @@ bool QgsGraduatedSymbolRendererV2::updateRangeRenderState( int rangeIndex, bool

QString QgsGraduatedSymbolRendererV2::defaultRangeLabel(const QgsRendererRangeV2 & range )
{
return QString::number( range.lowerValue(), 'f', 4 ) + " - " + QString::number( range.upperValue(), 'f', 4 ) + mUnits;
int ndp=mDecimalPlaces;
return QString::number( range.lowerValue(), 'f', mDecimalPlaces )
+ " - " + QString::number( range.upperValue(), 'f', mDecimalPlaces ) + mUnits;
}

QString QgsGraduatedSymbolRendererV2::dump() const
@@ -387,6 +390,7 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone() const
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
r->setUnits( units(), false );
r->setDecimalPlaces( decimalPlaces(), false );
return r;
}

@@ -814,7 +818,8 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted,
QString units )
QString units,
int decimalPlaces )
{
if ( classes < 1 )
return NULL;
@@ -910,6 +915,7 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
r->setInvertedColorRamp( inverted );
r->setMode( mode );
r->setUnits( units );
r->setDecimalPlaces( decimalPlaces );

// "breaks" list contains all values at class breaks plus maximum as last break
int i = 0;
@@ -1043,11 +1049,13 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );

QDomElement unitsElem = element.firstChildElement( "units" );
if( ! unitsElem.isNull() )
QDomElement labelSettingsElem = element.firstChildElement( "labelsettings" );
if( ! labelSettingsElem.isNull() )
{
QString unitString=unitsElem.attribute("name");
QString unitString=labelSettingsElem.attribute("units");
r->setUnits(unitString, false);
int decimalPlaces=labelSettingsElem.attribute("decimalplaces").toInt();
r->setDecimalPlaces(decimalPlaces,false);
}
// TODO: symbol levels
return r;
@@ -1136,9 +1144,10 @@ QDomElement QgsGraduatedSymbolRendererV2::save( QDomDocument& doc )
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );

QDomElement unitsElem=doc.createElement("units");
unitsElem.setAttribute("name",mUnits);
rendererElem.appendChild( unitsElem );
QDomElement labelSettingsElem=doc.createElement("labelsettings");
labelSettingsElem.setAttribute("units",mUnits);
labelSettingsElem.setAttribute("decimalplaces",mDecimalPlaces);
rendererElem.appendChild( labelSettingsElem );

return rendererElem;
}
@@ -1297,7 +1306,7 @@ void QgsGraduatedSymbolRendererV2::deleteAllClasses()

void QgsGraduatedSymbolRendererV2::setUnits( QString units, bool updateRanges )
{
if( updateRanges )
if( updateRanges && units != mUnits )
{
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
@@ -1312,6 +1321,29 @@ void QgsGraduatedSymbolRendererV2::setUnits( QString units, bool updateRanges )
mUnits=units;
}

void QgsGraduatedSymbolRendererV2::setDecimalPlaces( int decimalPlaces, bool updateRanges )
{
if( decimalPlaces < 0 ) decimalPlaces=0;
if( decimalPlaces > 10 ) decimalPlaces=10;

if( updateRanges && decimalPlaces != mDecimalPlaces )
{
int saveDecimalPlaces=mDecimalPlaces;
for ( QgsRangeList::iterator it = mRanges.begin(); it != mRanges.end(); ++it )
{
QString label=it->label();
QString defaultLabel=defaultRangeLabel(*it);
if( label == defaultLabel )
{
mDecimalPlaces=decimalPlaces;
it->setLabel(defaultRangeLabel(*it));
mDecimalPlaces=saveDecimalPlaces;
}
}
}
mDecimalPlaces=decimalPlaces;
}

void QgsGraduatedSymbolRendererV2::moveClass( int from, int to )
{
if ( from < 0 || from >= mRanges.size() || to < 0 || to >= mRanges.size() ) return;
@@ -1379,5 +1411,16 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::convertFromRenderer(
const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
}
return 0;

// If not one of the specifically handled renderers, then just grab the symbol from the renderer
// Could have applied this to specific renderer types (singleSymbol, graduatedSymbo)

QgsGraduatedSymbolRendererV2* r =new QgsGraduatedSymbolRendererV2( "", QgsRangeList() );
QgsSymbolV2List symbols=const_cast<QgsFeatureRendererV2 *>(renderer)->symbols();
if( symbols.size() > 0 )
{
r->setSourceSymbol(symbols.at(0)->clone());
}
return r;

}
@@ -135,6 +135,9 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QString units() const { return mUnits; }
void setUnits( QString units, bool updateRanges=true );

int decimalPlaces() const { return mDecimalPlaces; };
void setDecimalPlaces( int decimalPlaces, bool updateRanges=true );

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
QString attrName,
@@ -143,7 +146,8 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QgsSymbolV2* symbol,
QgsVectorColorRampV2* ramp,
bool inverted = false,
QString units = "");
QString units = "",
int decimalPlaces = 4);

//! create renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& element );
@@ -215,6 +219,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
protected:
QString mAttrName;
QString mUnits;
int mDecimalPlaces;
QgsRangeList mRanges;
Mode mMode;
QScopedPointer<QgsSymbolV2> mSourceSymbol;
@@ -393,5 +393,11 @@ QgsSingleSymbolRendererV2* QgsSingleSymbolRendererV2::convertFromRenderer( const
return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );

}

QgsSymbolV2List symbols=const_cast<QgsFeatureRendererV2 *>(renderer)->symbols();
if( symbols.size() > 0 )
{
return new QgsSingleSymbolRendererV2(symbols.at(0)->clone());
}
return 0;
}

0 comments on commit abd9e3c

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