Skip to content
Permalink
Browse files

More composer porting

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent cf22025 commit 5cb874c2aa55f73cc2db2c66e062c140e668bf9b
@@ -709,14 +709,24 @@ void QgsComposerItem::setSceneRect( const QRectF& rectangle )
emit sizeChanged();
}

QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOnly )
QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOnly, const QgsExpressionContext* context )
{
QRectF result = newRect;

//TODO QGIS 3.0
//maintain pre 2.12 API. remove when API break allowed
QScopedPointer< QgsExpressionContext > scopedContext;
const QgsExpressionContext* evalContext = context;
if ( !evalContext )
{
scopedContext.reset( createExpressionContext() );
evalContext = scopedContext.data();
}

//data defined position or size set? if so, update rect with data defined values
QVariant exprVal;
//evaulate width and height first, since they may affect position if non-top-left reference point set
if ( dataDefinedEvaluate( QgsComposerObject::ItemWidth, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::ItemWidth, exprVal, *evalContext ) )
{
bool ok;
double width = exprVal.toDouble( &ok );
@@ -726,7 +736,7 @@ QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOn
result.setWidth( width );
}
}
if ( dataDefinedEvaluate( QgsComposerObject::ItemHeight, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::ItemHeight, exprVal, *evalContext ) )
{
bool ok;
double height = exprVal.toDouble( &ok );
@@ -762,7 +772,7 @@ QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOn
x += rect().width();
}
}
if ( dataDefinedEvaluate( QgsComposerObject::PositionX, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::PositionX, exprVal, *evalContext ) )
{
bool ok;
double positionX = exprVal.toDouble( &ok );
@@ -798,7 +808,7 @@ QRectF QgsComposerItem::evalItemRect( const QRectF &newRect, const bool resizeOn
y += rect().height();
}
}
if ( dataDefinedEvaluate( QgsComposerObject::PositionY, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::PositionY, exprVal, *evalContext ) )
{
bool ok;
double positionY = exprVal.toDouble( &ok );
@@ -849,18 +859,7 @@ bool QgsComposerItem::shouldDrawItem() const

QgsExpressionContext* QgsComposerItem::createExpressionContext() const
{
QgsExpressionContext* context = new QgsExpressionContext();
context->appendScope( QgsExpressionContextUtils::globalScope() );
context->appendScope( QgsExpressionContextUtils::projectScope() );
if ( mComposition )
{
context->appendScope( QgsExpressionContextUtils::compositionScope( mComposition ) );
if ( mComposition->atlasComposition().enabled() )
{
context->appendScope( QgsExpressionContextUtils::atlasScope( &mComposition->atlasComposition() ) );
}
}

QgsExpressionContext* context = QgsComposerObject::createExpressionContext();
context->appendScope( QgsExpressionContextUtils::composerItemScope( this ) );
return context;
}
@@ -583,11 +583,7 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
*/
virtual void setCurrentExportLayer( const int layerIdx = -1 ) { mCurrentExportLayer = layerIdx; }

/** Creates an expression context relating to the item's current state. The context includes
* scopes for global, project, composition, atlas and item properties.
* @note added in QGIS 2.12
*/
QgsExpressionContext* createExpressionContext() const;
virtual QgsExpressionContext* createExpressionContext() const override;

public slots:
/** Sets the item rotation
@@ -21,6 +21,7 @@
#include "qgscomposerutils.h"
#include "qgscomposerobject.h"
#include "qgsdatadefined.h"
#include "qgsexpressioncontext.h"

#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter

@@ -151,33 +152,24 @@ void QgsComposerObject::refreshDataDefinedProperty( const DataDefinedProperty pr
//nothing to do in base class for now
}

bool QgsComposerObject::dataDefinedEvaluate( const DataDefinedProperty property, QVariant &expressionValue ) const
bool QgsComposerObject::dataDefinedEvaluate( const DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext& context ) const
{
if ( !mComposition )
{
return false;
}
return mComposition->dataDefinedEvaluate( property, expressionValue, &mDataDefinedProperties );
return mComposition->dataDefinedEvaluate( property, expressionValue, context, &mDataDefinedProperties );
}

void QgsComposerObject::prepareDataDefinedExpressions() const
{
//use atlas coverage layer if set
QgsVectorLayer* atlasLayer = 0;
if ( mComposition )
{
QgsAtlasComposition* atlas = &mComposition->atlasComposition();
if ( atlas && atlas->enabled() )
{
atlasLayer = atlas->coverageLayer();
}
}
QScopedPointer< QgsExpressionContext > context( createExpressionContext() );

//prepare all QgsDataDefineds
QMap< DataDefinedProperty, QgsDataDefined* >::const_iterator it = mDataDefinedProperties.constBegin();
if ( it != mDataDefinedProperties.constEnd() )
{
it.value()->prepareExpression( atlasLayer );
it.value()->prepareExpression( *context.data() );
}
}

@@ -200,3 +192,19 @@ QStringList QgsComposerObject::customProperties() const
{
return mCustomProperties.keys();
}

QgsExpressionContext* QgsComposerObject::createExpressionContext() const
{
QgsExpressionContext* context = 0;
if ( mComposition )
{
context = mComposition->createExpressionContext();
}
else
{
context = new QgsExpressionContext();
context->appendScope( QgsExpressionContextUtils::globalScope() );
context->appendScope( QgsExpressionContextUtils::projectScope() );
}
return context;
}
@@ -25,6 +25,7 @@
class QgsComposition;
class QPainter;
class QgsDataDefined;
class QgsExpressionContext;

/** \ingroup MapComposer
* A base class for objects which belong to a map composition.
@@ -161,6 +162,12 @@ class CORE_EXPORT QgsComposerObject: public QObject
*/
QStringList customProperties() const;

/** Creates an expression context relating to the objects's current state. The context includes
* scopes for global, project and composition properties.
* @note added in QGIS 2.12
*/
virtual QgsExpressionContext* createExpressionContext() const;

public slots:

/** Triggers a redraw for the item*/
@@ -189,9 +196,11 @@ class CORE_EXPORT QgsComposerObject: public QObject
* @returns true if data defined property could be successfully evaluated
* @param property data defined property to evaluate
* @param expressionValue QVariant for storing the evaluated value
* @param context expression context for evaluating expressions. Must have feature and fields set to current
* atlas feature and coverage layer fields prior to calling this method.
* @note this method was added in version 2.5
*/
bool dataDefinedEvaluate( const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue ) const;
bool dataDefinedEvaluate( const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext& context ) const;

signals:
/** Emitted when the item changes. Signifies that the item widgets must update the
@@ -3062,7 +3062,9 @@ QStringList QgsComposition::customProperties() const
return mCustomProperties.keys();
}

bool QgsComposition::dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties )
bool QgsComposition::dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
const QgsExpressionContext& context,
QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties )
{
if ( property == QgsComposerObject::NoProperty || property == QgsComposerObject::AllProperties )
{
@@ -3092,7 +3094,7 @@ bool QgsComposition::dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty
}

//evaluate data defined property using current atlas context
QVariant result = dataDefinedValue( property, useFeature ? &currentFeature : 0, layerFields, dataDefinedProperties );
QVariant result = dataDefinedValue( property, useFeature ? &currentFeature : 0, layerFields, context, dataDefinedProperties );

if ( result.isValid() )
{
@@ -3132,7 +3134,7 @@ bool QgsComposition::dataDefinedActive( const QgsComposerObject::DataDefinedProp
return dd->isActive();
}

QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields& fields, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const
QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields& fields, const QgsExpressionContext& context, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const
{
if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
{
@@ -3168,14 +3170,14 @@ QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedPropert

if ( !dd->expressionIsPrepared() )
{
prepareDataDefinedExpression( dd, dataDefinedProperties );
prepareDataDefinedExpression( dd, dataDefinedProperties, context );
}

if ( useExpression && dd->expressionIsPrepared() )
{
QgsExpression* expr = dd->expression();

result = expr->evaluate( feature );
result = expr->evaluate( &context );
if ( expr->hasEvalError() )
{
QgsDebugMsgLevel( QString( "Evaluate error:" ) + expr->evalErrorString(), 4 );
@@ -3198,34 +3200,42 @@ QVariant QgsComposition::dataDefinedValue( QgsComposerObject::DataDefinedPropert
return result;
}

void QgsComposition::prepareDataDefinedExpression( QgsDataDefined *dd, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const
void QgsComposition::prepareDataDefinedExpression( QgsDataDefined *dd, QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties,
const QgsExpressionContext& context ) const
{
QgsVectorLayer* atlasLayer = 0;

if ( mAtlasComposition.enabled() )
{
atlasLayer = mAtlasComposition.coverageLayer();
}

//if specific QgsDataDefined passed, prepare it
//otherwise prepare all QgsDataDefineds
if ( dd )
{
dd->prepareExpression( atlasLayer );
dd->prepareExpression( context );
}
else
{
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >::const_iterator it = dataDefinedProperties->constBegin();
for ( ; it != dataDefinedProperties->constEnd(); ++it )
{
it.value()->prepareExpression( atlasLayer );
it.value()->prepareExpression( context );
}
}
}

QgsExpressionContext* QgsComposition::createExpressionContext() const
{
QgsExpressionContext* context = new QgsExpressionContext();
context->appendScope( QgsExpressionContextUtils::globalScope() );
context->appendScope( QgsExpressionContextUtils::projectScope() );
context->appendScope( QgsExpressionContextUtils::compositionScope( this ) );
if ( mAtlasComposition.enabled() )
{
context->appendScope( QgsExpressionContextUtils::atlasScope( &mAtlasComposition ) );
}
return context;
}

void QgsComposition::prepareAllDataDefinedExpressions()
{
prepareDataDefinedExpression( 0, &mDataDefinedProperties );
QScopedPointer< QgsExpressionContext > context( createExpressionContext() );
prepareDataDefinedExpression( 0, &mDataDefinedProperties, *context.data() );
}

void QgsComposition::relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter )
@@ -858,10 +858,13 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
* @returns true if data defined property could be successfully evaluated
* @param property data defined property to evaluate
* @param expressionValue QVariant for storing the evaluated value
* @param context expression context for evaluating expressions. Must have feature and fields set
* to corresponding atlas feature and coverage layer fields prior to calling this method.
* @param dataDefinedProperties map of data defined properties to QgsDataDefined
* @note this method was added in version 2.5
*/
bool dataDefinedEvaluate( QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue,
const QgsExpressionContext& context,
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );

/** Returns whether a data defined property has been set and is currently active.
@@ -876,19 +879,29 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
* @param property data defined property to evaluate
* @param feature current atlas feature to evaluate property for
* @param fields fields from atlas layer
* @param context expression context for evaluating expressions (note, must have fields and feature set)
* @param dataDefinedProperties map of data defined properties to QgsDataDefined
* @note this method was added in version 2.5
*/
QVariant dataDefinedValue( QgsComposerObject::DataDefinedProperty property, const QgsFeature *feature, const QgsFields& fields,
const QgsExpressionContext& context,
QMap<QgsComposerObject::DataDefinedProperty, QgsDataDefined *> *dataDefinedProperties ) const;


/** Prepares the expression for a data defined property, using the current atlas layer if set.
/** Prepares the expression for a data defined property.
* @param dd data defined to prepare. If no data defined is set, all data defined expressions will be prepared
* @param dataDefinedProperties map of data defined properties to QgsDataDefined
* @param context expression context for expression preparation. Should have fields set to match the current atlas layer if required prior
* to calling this method.
* @note this method was added in version 2.5
*/
void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties ) const;
void prepareDataDefinedExpression( QgsDataDefined *dd, QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties, const QgsExpressionContext& context ) const;

/** Creates an expression context relating to the compositions's current state. The context includes
* scopes for global, project, composition and atlas properties.
* @note added in QGIS 2.12
*/
QgsExpressionContext* createExpressionContext() const;

/** Check whether any data defined page settings are active.
* @returns true if any data defined page settings are active.
@@ -676,6 +676,7 @@ QgsExpressionContextScope* QgsExpressionContextUtils::atlasScope( const QgsAtlas
{
QgsFeature atlasFeature = atlas->feature();
scope->setFeature( atlasFeature );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_feature", QVariant::fromValue( atlasFeature ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "atlas_featureid", atlasFeature.id(), true ) );
}

0 comments on commit 5cb874c

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