Skip to content
Permalink
Browse files

Port diagrams to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent f82c641 commit 430303dfc95f3f5408fa0f4a45c0dc12d76ac326
@@ -10,7 +10,15 @@ class QgsDiagram
virtual QgsDiagram* clone() const = 0;

void clearCache();
QgsExpression* getExpression( const QString& expression, const QgsFields* fields );
QgsExpression* getExpression( const QString& expression, const QgsFields* fields ) /Deprecated/;

/** Returns a prepared expression for the specified context.
* @param expression expression string
* @param context expression context
* @note added in QGIS 2.12
*/
QgsExpression* getExpression( const QString& expression, const QgsExpressionContext& context );

/** @deprecated `void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )` should be used instead */
virtual void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position ) /Deprecated/;
/** Draws the diagram at the given position (in pixel coordinates)*/
@@ -46,13 +46,26 @@ void QgsDiagram::clearCache()

QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFields* fields )
{
Q_NOWARN_DEPRECATED_PUSH
if ( !mExpressions.contains( expression ) )
{
QgsExpression* expr = new QgsExpression( expression );
expr->prepare( *fields );
mExpressions[expression] = expr;
}
return mExpressions[expression];
Q_NOWARN_DEPRECATED_POP
}

QgsExpression *QgsDiagram::getExpression( const QString &expression, const QgsExpressionContext &context )
{
if ( !mExpressions.contains( expression ) )
{
QgsExpression* expr = new QgsExpression( expression );
expr->prepare( &context );
mExpressions[expression] = expr;
}
return mExpressions[expression];
}

void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
@@ -16,6 +16,7 @@
#define QGSDIAGRAM_H

#include "qgsfeature.h"
#include "qgsexpressioncontext.h"
#include <QPen>
#include <QBrush>

@@ -40,7 +41,16 @@ class CORE_EXPORT QgsDiagram
virtual QgsDiagram* clone() const = 0;

void clearCache();
QgsExpression* getExpression( const QString& expression, const QgsFields* fields );

Q_DECL_DEPRECATED QgsExpression* getExpression( const QString& expression, const QgsFields* fields );

/** Returns a prepared expression for the specified context.
* @param expression expression string
* @param context expression context
* @note added in QGIS 2.12
*/
QgsExpression* getExpression( const QString& expression, const QgsExpressionContext& context );

/** @deprecated `void renderDiagram( const QgsFeature& feature, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position )` should be used instead */
virtual Q_DECL_DEPRECATED void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
/** Draws the diagram at the given position (in pixel coordinates)*/
@@ -37,7 +37,6 @@ QgsDiagram* QgsHistogramDiagram::clone() const

QSizeF QgsHistogramDiagram::diagramSize( const QgsFeature& feature, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
{
Q_UNUSED( c );
QSizeF size;
if ( feature.attributes().count() == 0 )
{
@@ -49,10 +48,15 @@ QSizeF QgsHistogramDiagram::diagramSize( const QgsFeature& feature, const QgsRen

double maxValue = 0;

QgsExpressionContext expressionContext = c.expressionContext();
expressionContext.setFeature( feature );
if ( feature.fields() )
expressionContext.setFields( *feature.fields() );

foreach ( QString cat, s.categoryAttributes )
{
QgsExpression* expression = getExpression( cat, feature.fields() );
maxValue = qMax( expression->evaluate( feature ).toDouble(), maxValue );
QgsExpression* expression = getExpression( cat, expressionContext );
maxValue = qMax( expression->evaluate( &expressionContext ).toDouble(), maxValue );
}

// Scale, if extension is smaller than the specified minimum
@@ -126,10 +130,15 @@ void QgsHistogramDiagram::renderDiagram( const QgsFeature& feature, QgsRenderCon
QList<double> values;
double maxValue = 0;

QgsExpressionContext expressionContext = c.expressionContext();
expressionContext.setFeature( feature );
if ( feature.fields() )
expressionContext.setFields( *feature.fields() );

foreach ( QString cat, s.categoryAttributes )
{
QgsExpression* expression = getExpression( cat, feature.fields() );
double currentVal = expression->evaluate( feature ).toDouble();
QgsExpression* expression = getExpression( cat, expressionContext );
double currentVal = expression->evaluate( &expressionContext ).toDouble();
values.push_back( currentVal );
maxValue = qMax( currentVal, maxValue );
}
@@ -42,8 +42,13 @@ QSizeF QgsPieDiagram::diagramSize( const QgsFeature& feature, const QgsRenderCon
QVariant attrVal;
if ( is.classificationAttributeIsExpression )
{
QgsExpression* expression = getExpression( is.classificationAttributeExpression, feature.fields() );
attrVal = expression->evaluate( feature );
QgsExpressionContext expressionContext = c.expressionContext();
if ( feature.fields() )
expressionContext.setFields( *feature.fields() );
expressionContext.setFeature( feature );

QgsExpression* expression = getExpression( is.classificationAttributeExpression, expressionContext );
attrVal = expression->evaluate( &expressionContext );
}
else
{
@@ -121,11 +126,16 @@ void QgsPieDiagram::renderDiagram( const QgsFeature& feature, QgsRenderContext&
double valSum = 0;
int valCount = 0;

QgsExpressionContext expressionContext = c.expressionContext();
expressionContext.setFeature( feature );
if ( feature.fields() )
expressionContext.setFields( *feature.fields() );

QList<QString>::const_iterator catIt = s.categoryAttributes.constBegin();
for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
{
QgsExpression* expression = getExpression( *catIt, feature.fields() );
currentVal = expression->evaluate( feature ).toDouble();
QgsExpression* expression = getExpression( *catIt, expressionContext );
currentVal = expression->evaluate( &expressionContext ).toDouble();
values.push_back( currentVal );
valSum += currentVal;
if ( currentVal ) valCount++;
@@ -520,7 +520,7 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
QList<QString>::const_iterator attIt = att.constBegin();
for ( ; attIt != att.constEnd(); ++attIt )
{
QgsExpression* expression = diagRenderer->diagram()->getExpression( *attIt, &mFields );
QgsExpression* expression = diagRenderer->diagram()->getExpression( *attIt, mContext.expressionContext() );
QStringList columns = expression->referencedColumns();
QStringList::const_iterator columnsIterator = columns.constBegin();
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
@@ -535,7 +535,7 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
{
if ( linearlyInterpolatedDiagramRenderer->classificationAttributeIsExpression() )
{
QgsExpression* expression = diagRenderer->diagram()->getExpression( linearlyInterpolatedDiagramRenderer->classificationAttributeExpression(), &mFields );
QgsExpression* expression = diagRenderer->diagram()->getExpression( linearlyInterpolatedDiagramRenderer->classificationAttributeExpression(), mContext.expressionContext() );
QStringList columns = expression->referencedColumns();
QStringList::const_iterator columnsIterator = columns.constBegin();
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )

0 comments on commit 430303d

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