Skip to content
Permalink
Browse files

Merge pull request #1587 from ccrook/CategorizedRendererUpdate

Categorized and graduated renderer enhancements
  • Loading branch information
NathanW2 committed Sep 26, 2014
2 parents b734e87 + b558c08 commit 0c6576c4236c0d81e69046cd8cca7227c93de957
@@ -122,6 +122,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
@@ -149,6 +153,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.
@@ -5,7 +5,8 @@ class QgsRendererRangeV2
%End

public:
QgsRendererRangeV2( double lowerValue, double upperValue, QgsSymbolV2* symbol /Transfer/, QString label, bool render = true );
QgsRendererRangeV2();
QgsRendererRangeV2( double lowerValue, double upperValue, QgsSymbolV2* symbol, QString label, bool render = true );
QgsRendererRangeV2( const QgsRendererRangeV2& range );

// default dtor is ok
@@ -38,6 +39,42 @@ class QgsRendererRangeV2

typedef QList<QgsRendererRangeV2> QgsRangeList;

class QgsRendererRangeV2LabelFormat
{
%TypeHeaderCode
#include <qgsgraduatedsymbolrendererv2.h>
%End
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString prefix, QString separator, QString suffix, int decimalPlaces=4, bool trimTrailingZeroes=false );

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

QString prefix() const;
void setPrefix( QString prefix );

QString separator() const;
void setSeparator( QString separator );

QString suffix() const;
void setSuffix( QString suffix );

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

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 );

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


class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
{
%TypeHeaderCode
@@ -86,6 +123,8 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
void addClass( QgsSymbolV2* symbol );
//! @note available in python bindings as addClassRange
void addClass( QgsRendererRangeV2 range ) /PyName=addClassRange/;
//! @note available in python bindings as addClassLowerUpper
void addClass( double lower, double upper ) /PyName=addClassLowerUpper/;
void deleteClass( int idx );
void deleteAllClasses();

@@ -107,6 +146,30 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2

Mode mode() const;
void setMode( Mode mode );
//! Recalculate classes for a layer
//! @param vlayer The layer being rendered (from which data values are calculated)
//! @param mode The calculation mode
//! @param nclasses The number of classes to calculate (approximate for some modes)
//! @note Added in 2.6
void updateClasses( QgsVectorLayer *vlayer, Mode mode, int nclasses );
//! Evaluates the data expression and returns the list of values from the layer
//! @param vlayer The layer for which to evaluate the expression
//! @note Added in 2.6
QList<double> getDataValues( QgsVectorLayer *vlayer );

//! Return the label format used to generate default classification labels
//! @note Added in 2.6
const QgsRendererRangeV2LabelFormat &labelFormat();
//! 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 );

//! 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 );

static QgsGraduatedSymbolRendererV2* createRenderer(
QgsVectorLayer* vlayer,
@@ -115,7 +178,9 @@ class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
Mode mode,
QgsSymbolV2* symbol /Transfer/,
QgsVectorColorRampV2* ramp /Transfer/,
bool inverted = false ) /Factory/;
bool inverted = false,
QgsRendererRangeV2LabelFormat labelFormat=QgsRendererRangeV2LabelFormat()
);

//! create renderer from XML element
static QgsFeatureRendererV2* create( QDomElement& element ) /Factory/;
@@ -143,9 +208,9 @@ 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/ = 0, bool inverted = false );

/** Update the all symbols but leave breaks and colors. */
/** Update all the symbols but leave breaks and colors. */
void updateSymbols( QgsSymbolV2* sym /Transfer/ );

//! @note added in 1.6
@@ -175,6 +240,10 @@ class QgsGraduatedSymbolRendererV2 : 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();

//! creates a QgsGraduatedSymbolRendererV2 from an existing renderer.
//! @note added in 2.5
//! @returns a new renderer if the conversion was possible, otherwise 0.
@@ -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,17 +26,24 @@ 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 labelFormatChanged();

void showSymbolLevels();

void rowsMoved();
void modelDataChanged();

protected:
void updateUiFromRenderer();
void updateUiFromRenderer( bool updateCount=true );
void connectUpdateHandlers();
void disconnectUpdateHandlers();
bool rowsOrdered();

void updateGraduatedSymbolIcon();

@@ -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!!!" );
}
@@ -700,11 +700,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 ) );
@@ -788,5 +804,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;

}
@@ -152,6 +152,10 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
bool invertedColorRamp() { return mInvertedColorRamp; }
void setInvertedColorRamp( bool inverted ) { mInvertedColorRamp = 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

0 comments on commit 0c6576c

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