Skip to content

Commit 2bb1c8a

Browse files
committedAug 22, 2015
Start on implementing contexts for render operations
1 parent a0e35a3 commit 2bb1c8a

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed
 

‎python/core/qgsmapsettings.sip

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ class QgsMapSettings
118118
//! Return the calculated scale of the map
119119
double scale() const;
120120

121+
/** Sets the expression context. This context is used for all expression evaluation
122+
* associated with this map settings.
123+
* @see expressionContext()
124+
* @note added in QGIS 2.12
125+
*/
126+
void setExpressionContext( const QgsExpressionContext& context );
127+
128+
/** Gets the expression context. This context should be used for all expression evaluation
129+
* associated with this map settings.
130+
* @see setExpressionContext()
131+
* @note added in QGIS 2.12
132+
*/
133+
const QgsExpressionContext& expressionContext() const;
121134

122135
// -- utility functions --
123136

‎src/core/composer/qgscomposermap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
232232
jobMapSettings.setFlag( QgsMapSettings::UseRenderingOptimization, false );
233233
}
234234

235+
QgsExpressionContext* context = createExpressionContext();
236+
jobMapSettings.setExpressionContext( *context );
237+
delete context;
238+
235239
//update $map variable. Use QgsComposerItem's id since that is user-definable
236240
QgsExpression::setSpecialColumn( "$map", QgsComposerItem::id() );
237241

‎src/core/qgsmapsettings.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsmaptopixel.h"
2727
#include "qgsrectangle.h"
2828
#include "qgsscalecalculator.h"
29+
#include "qgsexpressioncontext.h"
2930

3031
class QPainter;
3132

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

168+
/** Sets the expression context. This context is used for all expression evaluation
169+
* associated with this map settings.
170+
* @see expressionContext()
171+
* @note added in QGIS 2.12
172+
*/
173+
void setExpressionContext( const QgsExpressionContext& context ) { mExpressionContext = context; }
174+
175+
/** Gets the expression context. This context should be used for all expression evaluation
176+
* associated with this map settings.
177+
* @see setExpressionContext()
178+
* @note added in QGIS 2.12
179+
*/
180+
const QgsExpressionContext& expressionContext() const { return mExpressionContext; }
167181

168182
// -- utility functions --
169183

@@ -240,6 +254,7 @@ class CORE_EXPORT QgsMapSettings
240254

241255
QStringList mLayers;
242256
QMap<QString, QString> mLayerStyleOverrides;
257+
QgsExpressionContext mExpressionContext;
243258

244259
bool mProjectionsEnabled;
245260
QgsCoordinateReferenceSystem mDestCRS;

‎src/core/qgsrendercontext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings& mapSet
5656
ctx.setRasterScaleFactor( 1.0 );
5757
ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm
5858
ctx.setRendererScale( mapSettings.scale() );
59+
ctx.setExpressionContext( mapSettings.expressionContext() );
5960

6061
//this flag is only for stopping during the current rendering progress,
6162
//so must be false at every new render operation

‎src/core/qgsrendercontext.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsmaptopixel.h"
2525
#include "qgsrectangle.h"
2626
#include "qgsvectorsimplifymethod.h"
27+
#include "qgsexpressioncontext.h"
2728

2829
class QPainter;
2930

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

122+
/** Sets the expression context. This context is used for all expression evaluation
123+
* associated with this render context.
124+
* @see expressionContext()
125+
* @note added in QGIS 2.12
126+
*/
127+
void setExpressionContext( const QgsExpressionContext& context ) { mExpressionContext = context; }
128+
129+
/** Gets the expression context. This context should be used for all expression evaluation
130+
* associated with this render context.
131+
* @see setExpressionContext()
132+
* @note added in QGIS 2.12
133+
*/
134+
QgsExpressionContext& expressionContext() { return mExpressionContext; }
135+
136+
/** Gets the expression context (const version). This context should be used for all expression evaluation
137+
* associated with this render context.
138+
* @see setExpressionContext()
139+
* @note added in QGIS 2.12
140+
*/
141+
const QgsExpressionContext& expressionContext() const { return mExpressionContext; }
142+
121143
private:
122144

123145
/** Painter for rendering operations*/
@@ -165,6 +187,9 @@ class CORE_EXPORT QgsRenderContext
165187

166188
/** Simplification object which holds the information about how to simplify the features for fast rendering */
167189
QgsVectorSimplifyMethod mVectorSimplifyMethod;
190+
191+
/** Expression context */
192+
QgsExpressionContext mExpressionContext;
168193
};
169194

170195
#endif

‎src/core/qgsvectorlayerrenderer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ QgsVectorLayerRenderer::QgsVectorLayerRenderer( QgsVectorLayer* layer, QgsRender
9292
mRendererV2->setVertexMarkerAppearance( mVertexMarkerStyle, mVertexMarkerSize );
9393
}
9494

95+
mContext.expressionContext() << QgsExpressionContextUtils::layerScope( layer );
96+
9597
mAttrNames = mRendererV2->usedAttributes();
9698

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

285+
mContext.expressionContext().setFeature( fet );
286+
283287
bool sel = mContext.showSelection() && mSelectedFeatureIds.contains( fet.id() );
284288
bool drawMarker = ( mDrawVertexMarkers && mContext.drawEditingInformation() && ( !mVertexMarkerOnlyForSelection || sel ) );
285289

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

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

428+
mContext.expressionContext().setFeature( *fit );
429+
423430
try
424431
{
425432
mRendererV2->renderFeature( *fit, mContext, layer, sel, drawMarker );

0 commit comments

Comments
 (0)
Please sign in to comment.