Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add expression to graduated renderer
  • Loading branch information
NathanW2 committed Sep 15, 2013
1 parent 6cf3b9f commit a0ff6d5
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 84 deletions.
50 changes: 41 additions & 9 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -22,7 +22,7 @@
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgsvectordataprovider.h"

#include "qgsexpression.h"
#include <QDomDocument>
#include <QDomElement>
#include <QSettings> // for legend
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) );

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h
Expand Up @@ -17,6 +17,7 @@

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

class CORE_EXPORT QgsRendererRangeV2
{
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 24 additions & 1 deletion src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsvectorlayer.h"

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

#include "qgsludialog.h"

Expand Down Expand Up @@ -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& ) ) );
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.h
Expand Up @@ -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 );
Expand Down

0 comments on commit a0ff6d5

Please sign in to comment.