Skip to content
Permalink
Browse files
Add expression to graduated renderer
  • Loading branch information
NathanW2 committed Sep 15, 2013
1 parent 6cf3b9f commit a0ff6d5c7caa354f8fe1b4d3711364f70d9a8b82
@@ -22,7 +22,7 @@
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgsvectordataprovider.h"

#include "qgsexpression.h"
#include <QDomDocument>
#include <QDomElement>
#include <QSettings> // for legend
@@ -184,18 +184,22 @@ QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForValue( double value )
QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForFeature( QgsFeature& feature )
{
const QgsAttributes& attrs = feature.attributes();
QVariant value;
if ( mAttrNum < 0 || mAttrNum >= attrs.count() )
{
QgsDebugMsg( "attribute required by renderer not found: " + mAttrName + "(index " + QString::number( mAttrNum ) + ")" );
return NULL;
value = mExpression->evaluate( &feature );
}
else
{
value = attrs[mAttrNum];
}

// Null values should not be categorized
if ( attrs[mAttrNum].isNull() )
if ( value.isNull() )
return NULL;

// find the right category
QgsSymbolV2* symbol = symbolForValue( attrs[mAttrNum].toDouble() );
QgsSymbolV2* symbol = symbolForValue( value.toDouble() );
if ( symbol == NULL )
return NULL;

@@ -237,6 +241,12 @@ void QgsGraduatedSymbolRendererV2::startRender( QgsRenderContext& context, const
// find out classification attribute index from name
mAttrNum = vlayer ? vlayer->fieldNameIndex( mAttrName ) : -1;

if ( mAttrNum == -1 )
{
mExpression = new QgsExpression( mAttrName );
mExpression->prepare( vlayer->pendingFields() );
}

mRotationFieldIdx = ( mRotationField.isEmpty() ? -1 : vlayer->fieldNameIndex( mRotationField ) );
mSizeScaleFieldIdx = ( mSizeScaleField.isEmpty() ? -1 : vlayer->fieldNameIndex( mSizeScaleField ) );

@@ -279,7 +289,11 @@ void QgsGraduatedSymbolRendererV2::stopRender( QgsRenderContext& context )
QList<QString> QgsGraduatedSymbolRendererV2::usedAttributes()
{
QSet<QString> attributes;
attributes.insert( mAttrName );
QgsExpression exp( mAttrName );
foreach (QString attr, exp.referencedColumns() )
{
attributes << attr;
}
if ( !mRotationField.isEmpty() )
{
attributes.insert( mRotationField );
@@ -780,11 +794,29 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
return NULL;

int attrNum = vlayer->fieldNameIndex( attrName );
double minimum;
double maximum;
if ( attrNum == -1 )
{
QList<double> values;
QgsFeatureIterator fit = vlayer->getFeatures();
QgsFeature feature;
QgsExpression expression( attrName );
while ( fit.nextFeature( feature ) )
{
values << expression.evaluate( feature ).toDouble();
}
qSort( values );
minimum = values.first();
maximum = values.last();
}
else
{
minimum = vlayer->minimumValue( attrNum ).toDouble();
maximum = vlayer->maximumValue( attrNum ).toDouble();
}

double minimum = vlayer->minimumValue( attrNum ).toDouble();
double maximum = vlayer->maximumValue( attrNum ).toDouble();
QgsDebugMsg( QString( "min %1 // max %2" ).arg( minimum ).arg( maximum ) );

QList<double> breaks;
QList<int> labels;
if ( mode == EqualInterval )
@@ -17,6 +17,7 @@

#include "qgssymbolv2.h"
#include "qgsrendererv2.h"
#include "qgsexpression.h"

class CORE_EXPORT QgsRendererRangeV2
{
@@ -177,7 +178,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QString mRotationField;
QString mSizeScaleField;
QgsSymbolV2::ScaleMethod mScaleMethod;

QgsExpression* mExpression;
//! attribute index (derived from attribute name in startRender)
int mAttrNum;
int mRotationFieldIdx, mSizeScaleFieldIdx;
@@ -22,6 +22,7 @@
#include "qgsvectorlayer.h"

#include "qgssymbolv2selectordialog.h"
#include "qgsexpressionbuilderdialog.h"

#include "qgsludialog.h"

@@ -359,6 +360,7 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto
mGraduatedSymbol = QgsSymbolV2::defaultSymbol( mLayer->geometryType() );

connect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
connect( btnExpression, SIGNAL( clicked() ), this, SLOT( setExpression() ) );
connect( viewGraduated, SIGNAL( doubleClicked( const QModelIndex & ) ), this, SLOT( rangesDoubleClicked( const QModelIndex & ) ) );
connect( viewGraduated, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( rangesClicked( const QModelIndex & ) ) );
connect( viewGraduated, SIGNAL( customContextMenuRequested( const QPoint& ) ), this, SLOT( contextMenuViewCategories( const QPoint& ) ) );
@@ -417,7 +419,12 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer()
disconnect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
QString attrName = mRenderer->classAttribute();
int idx = cboGraduatedColumn->findText( attrName, Qt::MatchExactly );
cboGraduatedColumn->setCurrentIndex( idx >= 0 ? idx : 0 );
if ( idx == -1 )
{
cboGraduatedColumn->addItem( attrName );
idx = cboGraduatedColumn->count() - 1;
}
cboGraduatedColumn->setCurrentIndex( idx );
connect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );

// set source symbol
@@ -453,6 +460,22 @@ void QgsGraduatedSymbolRendererV2Widget::graduatedColumnChanged()
}


void QgsGraduatedSymbolRendererV2Widget::setExpression()
{
QgsExpressionBuilderDialog dlg( mLayer, cboGraduatedColumn->currentText(), this );
dlg.setWindowTitle( "Set column expression" );
if ( dlg.exec() )
{
QString expression = dlg.expressionText();
if ( !expression.isEmpty() )
{
cboGraduatedColumn->addItem( expression );
cboGraduatedColumn->setCurrentIndex( cboGraduatedColumn->count() - 1 );
}
}

}

void QgsGraduatedSymbolRendererV2Widget::classifyGraduated()
{
QString attrName = cboGraduatedColumn->currentText();
@@ -80,6 +80,7 @@ class GUI_EXPORT QgsGraduatedSymbolRendererV2Widget : public QgsRendererV2Widget
public slots:
void changeGraduatedSymbol();
void graduatedColumnChanged();
void setExpression();
void classifyGraduated();
void reapplyColorRamp();
void rangesDoubleClicked( const QModelIndex & idx );
Loading

0 comments on commit a0ff6d5

Please sign in to comment.