Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Start on implementing contexts for render operations
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent a0e35a3 commit 2bb1c8a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/core/qgsmapsettings.sip
Expand Up @@ -118,6 +118,19 @@ class QgsMapSettings
//! Return the calculated scale of the map
double scale() const;

/** Sets the expression context. This context is used for all expression evaluation
* associated with this map settings.
* @see expressionContext()
* @note added in QGIS 2.12
*/
void setExpressionContext( const QgsExpressionContext& context );

/** Gets the expression context. This context should be used for all expression evaluation
* associated with this map settings.
* @see setExpressionContext()
* @note added in QGIS 2.12
*/
const QgsExpressionContext& expressionContext() const;

// -- utility functions --

Expand Down
4 changes: 4 additions & 0 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -232,6 +232,10 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
jobMapSettings.setFlag( QgsMapSettings::UseRenderingOptimization, false );
}

QgsExpressionContext* context = createExpressionContext();
jobMapSettings.setExpressionContext( *context );
delete context;

//update $map variable. Use QgsComposerItem's id since that is user-definable
QgsExpression::setSpecialColumn( "$map", QgsComposerItem::id() );

Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsmapsettings.h
Expand Up @@ -26,6 +26,7 @@
#include "qgsmaptopixel.h"
#include "qgsrectangle.h"
#include "qgsscalecalculator.h"
#include "qgsexpressioncontext.h"

class QPainter;

Expand Down Expand Up @@ -164,6 +165,19 @@ class CORE_EXPORT QgsMapSettings
//! Return the calculated scale of the map
double scale() const;

/** Sets the expression context. This context is used for all expression evaluation
* associated with this map settings.
* @see expressionContext()
* @note added in QGIS 2.12
*/
void setExpressionContext( const QgsExpressionContext& context ) { mExpressionContext = context; }

/** Gets the expression context. This context should be used for all expression evaluation
* associated with this map settings.
* @see setExpressionContext()
* @note added in QGIS 2.12
*/
const QgsExpressionContext& expressionContext() const { return mExpressionContext; }

// -- utility functions --

Expand Down Expand Up @@ -240,6 +254,7 @@ class CORE_EXPORT QgsMapSettings

QStringList mLayers;
QMap<QString, QString> mLayerStyleOverrides;
QgsExpressionContext mExpressionContext;

bool mProjectionsEnabled;
QgsCoordinateReferenceSystem mDestCRS;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsrendercontext.cpp
Expand Up @@ -56,6 +56,7 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings& mapSet
ctx.setRasterScaleFactor( 1.0 );
ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm
ctx.setRendererScale( mapSettings.scale() );
ctx.setExpressionContext( mapSettings.expressionContext() );

//this flag is only for stopping during the current rendering progress,
//so must be false at every new render operation
Expand Down
25 changes: 25 additions & 0 deletions src/core/qgsrendercontext.h
Expand Up @@ -24,6 +24,7 @@
#include "qgsmaptopixel.h"
#include "qgsrectangle.h"
#include "qgsvectorsimplifymethod.h"
#include "qgsexpressioncontext.h"

class QPainter;

Expand Down Expand Up @@ -118,6 +119,27 @@ class CORE_EXPORT QgsRenderContext
const QgsVectorSimplifyMethod& vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
void setVectorSimplifyMethod( const QgsVectorSimplifyMethod& simplifyMethod ) { mVectorSimplifyMethod = simplifyMethod; }

/** Sets the expression context. This context is used for all expression evaluation
* associated with this render context.
* @see expressionContext()
* @note added in QGIS 2.12
*/
void setExpressionContext( const QgsExpressionContext& context ) { mExpressionContext = context; }

/** Gets the expression context. This context should be used for all expression evaluation
* associated with this render context.
* @see setExpressionContext()
* @note added in QGIS 2.12
*/
QgsExpressionContext& expressionContext() { return mExpressionContext; }

/** Gets the expression context (const version). This context should be used for all expression evaluation
* associated with this render context.
* @see setExpressionContext()
* @note added in QGIS 2.12
*/
const QgsExpressionContext& expressionContext() const { return mExpressionContext; }

private:

/** Painter for rendering operations*/
Expand Down Expand Up @@ -165,6 +187,9 @@ class CORE_EXPORT QgsRenderContext

/** Simplification object which holds the information about how to simplify the features for fast rendering */
QgsVectorSimplifyMethod mVectorSimplifyMethod;

/** Expression context */
QgsExpressionContext mExpressionContext;
};

#endif
7 changes: 7 additions & 0 deletions src/core/qgsvectorlayerrenderer.cpp
Expand Up @@ -92,6 +92,8 @@ QgsVectorLayerRenderer::QgsVectorLayerRenderer( QgsVectorLayer* layer, QgsRender
mRendererV2->setVertexMarkerAppearance( mVertexMarkerStyle, mVertexMarkerSize );
}

mContext.expressionContext() << QgsExpressionContextUtils::layerScope( layer );

mAttrNames = mRendererV2->usedAttributes();

//register label and diagram layer to the labeling engine
Expand Down Expand Up @@ -280,6 +282,8 @@ void QgsVectorLayerRenderer::drawRendererV2( QgsFeatureIterator& fit )
break;
}

mContext.expressionContext().setFeature( fet );

bool sel = mContext.showSelection() && mSelectedFeatureIds.contains( fet.id() );
bool drawMarker = ( mDrawVertexMarkers && mContext.drawEditingInformation() && ( !mVertexMarkerOnlyForSelection || sel ) );

Expand Down Expand Up @@ -364,6 +368,7 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )

if ( mContext.labelingEngine() )
{
mContext.expressionContext().setFeature( fet );
if ( mLabeling )
{
mContext.labelingEngine()->registerFeature( mLayerID, fet, mContext );
Expand Down Expand Up @@ -420,6 +425,8 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
// maybe vertex markers should be drawn only during the last pass...
bool drawMarker = ( mDrawVertexMarkers && mContext.drawEditingInformation() && ( !mVertexMarkerOnlyForSelection || sel ) );

mContext.expressionContext().setFeature( *fit );

try
{
mRendererV2->renderFeature( *fit, mContext, layer, sel, drawMarker );
Expand Down

0 comments on commit 2bb1c8a

Please sign in to comment.