mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-16 00:03:12 -04:00
Port diagrams to expression contexts
This commit is contained in:
parent
f82c641a3d
commit
430303dfc9
@ -10,7 +10,15 @@ class QgsDiagram
|
|||||||
virtual QgsDiagram* clone() const = 0;
|
virtual QgsDiagram* clone() const = 0;
|
||||||
|
|
||||||
void clearCache();
|
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 */
|
/** @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/;
|
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)*/
|
/** Draws the diagram at the given position (in pixel coordinates)*/
|
||||||
|
@ -46,6 +46,7 @@ void QgsDiagram::clearCache()
|
|||||||
|
|
||||||
QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFields* fields )
|
QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFields* fields )
|
||||||
{
|
{
|
||||||
|
Q_NOWARN_DEPRECATED_PUSH
|
||||||
if ( !mExpressions.contains( expression ) )
|
if ( !mExpressions.contains( expression ) )
|
||||||
{
|
{
|
||||||
QgsExpression* expr = new QgsExpression( expression );
|
QgsExpression* expr = new QgsExpression( expression );
|
||||||
@ -53,6 +54,18 @@ QgsExpression* QgsDiagram::getExpression( const QString& expression, const QgsFi
|
|||||||
mExpressions[expression] = expr;
|
mExpressions[expression] = expr;
|
||||||
}
|
}
|
||||||
return mExpressions[expression];
|
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 )
|
void QgsDiagram::setPenWidth( QPen& pen, const QgsDiagramSettings& s, const QgsRenderContext& c )
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#define QGSDIAGRAM_H
|
#define QGSDIAGRAM_H
|
||||||
|
|
||||||
#include "qgsfeature.h"
|
#include "qgsfeature.h"
|
||||||
|
#include "qgsexpressioncontext.h"
|
||||||
#include <QPen>
|
#include <QPen>
|
||||||
#include <QBrush>
|
#include <QBrush>
|
||||||
|
|
||||||
@ -40,7 +41,16 @@ class CORE_EXPORT QgsDiagram
|
|||||||
virtual QgsDiagram* clone() const = 0;
|
virtual QgsDiagram* clone() const = 0;
|
||||||
|
|
||||||
void clearCache();
|
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 */
|
/** @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 );
|
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)*/
|
/** 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 )
|
QSizeF QgsHistogramDiagram::diagramSize( const QgsFeature& feature, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is )
|
||||||
{
|
{
|
||||||
Q_UNUSED( c );
|
|
||||||
QSizeF size;
|
QSizeF size;
|
||||||
if ( feature.attributes().count() == 0 )
|
if ( feature.attributes().count() == 0 )
|
||||||
{
|
{
|
||||||
@ -49,10 +48,15 @@ QSizeF QgsHistogramDiagram::diagramSize( const QgsFeature& feature, const QgsRen
|
|||||||
|
|
||||||
double maxValue = 0;
|
double maxValue = 0;
|
||||||
|
|
||||||
|
QgsExpressionContext expressionContext = c.expressionContext();
|
||||||
|
expressionContext.setFeature( feature );
|
||||||
|
if ( feature.fields() )
|
||||||
|
expressionContext.setFields( *feature.fields() );
|
||||||
|
|
||||||
foreach ( QString cat, s.categoryAttributes )
|
foreach ( QString cat, s.categoryAttributes )
|
||||||
{
|
{
|
||||||
QgsExpression* expression = getExpression( cat, feature.fields() );
|
QgsExpression* expression = getExpression( cat, expressionContext );
|
||||||
maxValue = qMax( expression->evaluate( feature ).toDouble(), maxValue );
|
maxValue = qMax( expression->evaluate( &expressionContext ).toDouble(), maxValue );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale, if extension is smaller than the specified minimum
|
// Scale, if extension is smaller than the specified minimum
|
||||||
@ -126,10 +130,15 @@ void QgsHistogramDiagram::renderDiagram( const QgsFeature& feature, QgsRenderCon
|
|||||||
QList<double> values;
|
QList<double> values;
|
||||||
double maxValue = 0;
|
double maxValue = 0;
|
||||||
|
|
||||||
|
QgsExpressionContext expressionContext = c.expressionContext();
|
||||||
|
expressionContext.setFeature( feature );
|
||||||
|
if ( feature.fields() )
|
||||||
|
expressionContext.setFields( *feature.fields() );
|
||||||
|
|
||||||
foreach ( QString cat, s.categoryAttributes )
|
foreach ( QString cat, s.categoryAttributes )
|
||||||
{
|
{
|
||||||
QgsExpression* expression = getExpression( cat, feature.fields() );
|
QgsExpression* expression = getExpression( cat, expressionContext );
|
||||||
double currentVal = expression->evaluate( feature ).toDouble();
|
double currentVal = expression->evaluate( &expressionContext ).toDouble();
|
||||||
values.push_back( currentVal );
|
values.push_back( currentVal );
|
||||||
maxValue = qMax( currentVal, maxValue );
|
maxValue = qMax( currentVal, maxValue );
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,13 @@ QSizeF QgsPieDiagram::diagramSize( const QgsFeature& feature, const QgsRenderCon
|
|||||||
QVariant attrVal;
|
QVariant attrVal;
|
||||||
if ( is.classificationAttributeIsExpression )
|
if ( is.classificationAttributeIsExpression )
|
||||||
{
|
{
|
||||||
QgsExpression* expression = getExpression( is.classificationAttributeExpression, feature.fields() );
|
QgsExpressionContext expressionContext = c.expressionContext();
|
||||||
attrVal = expression->evaluate( feature );
|
if ( feature.fields() )
|
||||||
|
expressionContext.setFields( *feature.fields() );
|
||||||
|
expressionContext.setFeature( feature );
|
||||||
|
|
||||||
|
QgsExpression* expression = getExpression( is.classificationAttributeExpression, expressionContext );
|
||||||
|
attrVal = expression->evaluate( &expressionContext );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -121,11 +126,16 @@ void QgsPieDiagram::renderDiagram( const QgsFeature& feature, QgsRenderContext&
|
|||||||
double valSum = 0;
|
double valSum = 0;
|
||||||
int valCount = 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();
|
QList<QString>::const_iterator catIt = s.categoryAttributes.constBegin();
|
||||||
for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
|
for ( ; catIt != s.categoryAttributes.constEnd(); ++catIt )
|
||||||
{
|
{
|
||||||
QgsExpression* expression = getExpression( *catIt, feature.fields() );
|
QgsExpression* expression = getExpression( *catIt, expressionContext );
|
||||||
currentVal = expression->evaluate( feature ).toDouble();
|
currentVal = expression->evaluate( &expressionContext ).toDouble();
|
||||||
values.push_back( currentVal );
|
values.push_back( currentVal );
|
||||||
valSum += currentVal;
|
valSum += currentVal;
|
||||||
if ( currentVal ) valCount++;
|
if ( currentVal ) valCount++;
|
||||||
|
@ -520,7 +520,7 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
|
|||||||
QList<QString>::const_iterator attIt = att.constBegin();
|
QList<QString>::const_iterator attIt = att.constBegin();
|
||||||
for ( ; attIt != att.constEnd(); ++attIt )
|
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 columns = expression->referencedColumns();
|
||||||
QStringList::const_iterator columnsIterator = columns.constBegin();
|
QStringList::const_iterator columnsIterator = columns.constBegin();
|
||||||
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
|
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
|
||||||
@ -535,7 +535,7 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
|
|||||||
{
|
{
|
||||||
if ( linearlyInterpolatedDiagramRenderer->classificationAttributeIsExpression() )
|
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 columns = expression->referencedColumns();
|
||||||
QStringList::const_iterator columnsIterator = columns.constBegin();
|
QStringList::const_iterator columnsIterator = columns.constBegin();
|
||||||
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
|
for ( ; columnsIterator != columns.constEnd(); ++columnsIterator )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user