Skip to content

Commit

Permalink
Switch to using flags for render context settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 14, 2015
1 parent 824fd7b commit 8ddc909
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 39 deletions.
48 changes: 48 additions & 0 deletions python/core/qgsrendercontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ class QgsRenderContext
QgsRenderContext();
~QgsRenderContext();

//! Enumeration of flags that affect rendering operations
enum Flag
{
DrawEditingInfo, //!< Enable drawing of vertex markers for layers in editing mode
ForceVectorOutput, //!< Vector graphics should not be cached and drawn as raster images
UseAdvancedEffects, //!< Enable layer transparency and blending effects
UseRenderingOptimization, //!< Enable vector simplification and other rendering optimizations
DrawSelection, //!< Whether vector selections should be shown in the rendered map
};
typedef QFlags<QgsRenderContext::Flag> Flags;

//! Set combination of flags that will be used for rendering
void setFlags( const QgsRenderContext::Flags& flags );
//! Enable or disable a particular flag (other flags are not affected)
void setFlag( Flag flag, bool on = true );
//! Return combination of flags used for rendering
Flags flags() const;
//! Check whether a particular flag is enabled
bool testFlag( Flag flag ) const;

//! create initialized QgsRenderContext instance from given QgsMapSettings
//! @note added in 2.4
static QgsRenderContext fromMapSettings( const QgsMapSettings& mapSettings );
Expand All @@ -35,6 +55,7 @@ class QgsRenderContext

/** Returns true if advanced effects such as blend modes such be used */
bool useAdvancedEffects() const;

/** Used to enable or disable advanced effects such as blend modes */
void setUseAdvancedEffects( bool enabled );

Expand All @@ -60,13 +81,17 @@ class QgsRenderContext
void setCoordinateTransform( const QgsCoordinateTransform* t );
void setMapToPixel( const QgsMapToPixel& mtp );
void setExtent( const QgsRectangle& extent );

void setDrawEditingInformation( bool b );

void setRenderingStopped( bool stopped );
void setScaleFactor( double factor );
void setRasterScaleFactor( double factor );
void setRendererScale( double scale );
void setPainter( QPainter* p );

void setForceVectorOutput( bool force );

void setLabelingEngine( QgsLabelingEngineInterface* iface );
void setSelectionColor( const QColor& color );

Expand All @@ -80,12 +105,35 @@ class QgsRenderContext

/** Returns true if the rendering optimization (geometry simplification) can be executed*/
bool useRenderingOptimization() const;

void setUseRenderingOptimization( bool enabled );

//! Added in QGIS v2.4
const QgsVectorSimplifyMethod& vectorSimplifyMethod() const;
void setVectorSimplifyMethod( const QgsVectorSimplifyMethod& 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 );

/** 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();

/** 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
* @note not available in Python bindings
*/
//const QgsExpressionContext& expressionContext() const;

/** Returns pointer to the unsegmentized geometry
@return the geometry*/
const QgsAbstractGeometryV2* geometry() const;
Expand Down
91 changes: 80 additions & 11 deletions src/core/qgsrendercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@
#include "qgsmapsettings.h"

QgsRenderContext::QgsRenderContext()
: mPainter( 0 )
: mFlags( DrawEditingInfo | UseAdvancedEffects | DrawSelection | UseRenderingOptimization )
, mPainter( 0 )
, mCoordTransform( 0 )
, mDrawEditingInformation( true )
, mForceVectorOutput( false )
, mUseAdvancedEffects( true )
, mRenderingStopped( false )
, mScaleFactor( 1.0 )
, mRasterScaleFactor( 1.0 )
, mRendererScale( 1.0 )
, mLabelingEngine( NULL )
, mLabelingEngine2( 0 )
, mShowSelection( true )
, mUseRenderingOptimization( true )
, mGeometry( 0 )
{
mVectorSimplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
Expand All @@ -43,18 +39,41 @@ QgsRenderContext::~QgsRenderContext()
{
}

void QgsRenderContext::setFlags( const QgsRenderContext::Flags& flags )
{
mFlags = flags;
}

void QgsRenderContext::setFlag( QgsRenderContext::Flag flag, bool on )
{
if ( on )
mFlags |= flag;
else
mFlags &= ~flag;
}

QgsRenderContext::Flags QgsRenderContext::flags() const
{
return mFlags;
}

bool QgsRenderContext::testFlag( QgsRenderContext::Flag flag ) const
{
return mFlags.testFlag( flag );
}

QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings& mapSettings )
{
QgsRenderContext ctx;
ctx.setMapToPixel( mapSettings.mapToPixel() );
ctx.setExtent( mapSettings.visibleExtent() );
ctx.setDrawEditingInformation( mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
ctx.setForceVectorOutput( mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
ctx.setUseAdvancedEffects( mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) );
ctx.setUseRenderingOptimization( mapSettings.testFlag( QgsMapSettings::UseRenderingOptimization ) );
ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
ctx.setFlag( UseAdvancedEffects, mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) );
ctx.setFlag( UseRenderingOptimization, mapSettings.testFlag( QgsMapSettings::UseRenderingOptimization ) );
ctx.setCoordinateTransform( 0 );
ctx.setSelectionColor( mapSettings.selectionColor() );
ctx.setShowSelection( mapSettings.testFlag( QgsMapSettings::DrawSelection ) );
ctx.setFlag( DrawSelection, mapSettings.testFlag( QgsMapSettings::DrawSelection ) );
ctx.setRasterScaleFactor( 1.0 );
ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm
ctx.setRendererScale( mapSettings.scale() );
Expand All @@ -67,7 +86,57 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings& mapSet
return ctx;
}

bool QgsRenderContext::forceVectorOutput() const
{
return mFlags.testFlag( ForceVectorOutput );
}

bool QgsRenderContext::useAdvancedEffects() const
{
return mFlags.testFlag( UseAdvancedEffects );
}

void QgsRenderContext::setUseAdvancedEffects( bool enabled )
{
setFlag( UseAdvancedEffects, enabled );
}

bool QgsRenderContext::drawEditingInformation() const
{
return mFlags.testFlag( DrawEditingInfo );
}

bool QgsRenderContext::showSelection() const
{
return mFlags.testFlag( DrawSelection );
}

void QgsRenderContext::setCoordinateTransform( const QgsCoordinateTransform* t )
{
mCoordTransform = t;
}

void QgsRenderContext::setDrawEditingInformation( bool b )
{
setFlag( DrawEditingInfo, b );
}

void QgsRenderContext::setForceVectorOutput( bool force )
{
setFlag( ForceVectorOutput, force );
}

void QgsRenderContext::setShowSelection( const bool showSelection )
{
setFlag( DrawSelection, showSelection );
}

bool QgsRenderContext::useRenderingOptimization() const
{
return mFlags.testFlag( UseRenderingOptimization );
}

void QgsRenderContext::setUseRenderingOptimization( bool enabled )
{
setFlag( UseRenderingOptimization, enabled );
}
88 changes: 60 additions & 28 deletions src/core/qgsrendercontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ class CORE_EXPORT QgsRenderContext
QgsRenderContext();
~QgsRenderContext();

/** Enumeration of flags that affect rendering operations.
* @note added in QGIS 2.14
*/
enum Flag
{
DrawEditingInfo = 0x01, //!< Enable drawing of vertex markers for layers in editing mode
ForceVectorOutput = 0x02, //!< Vector graphics should not be cached and drawn as raster images
UseAdvancedEffects = 0x04, //!< Enable layer transparency and blending effects
UseRenderingOptimization = 0x08, //!< Enable vector simplification and other rendering optimizations
DrawSelection = 0x10, //!< Whether vector selections should be shown in the rendered map
};
Q_DECLARE_FLAGS( Flags, Flag )

/** Set combination of flags that will be used for rendering.
* @note added in QGIS 2.14
*/
void setFlags( const QgsRenderContext::Flags& flags );

/** Enable or disable a particular flag (other flags are not affected)
* @note added in QGIS 2.14
*/
void setFlag( Flag flag, bool on = true );

/** Return combination of flags used for rendering.
* @note added in QGIS 2.14
*/
Flags flags() const;

/** Check whether a particular flag is enabled.
* @note added in QGIS 2.14
*/
bool testFlag( Flag flag ) const;

//! create initialized QgsRenderContext instance from given QgsMapSettings
//! @note added in 2.4
static QgsRenderContext fromMapSettings( const QgsMapSettings& mapSettings );
Expand All @@ -66,14 +99,17 @@ class CORE_EXPORT QgsRenderContext

bool renderingStopped() const {return mRenderingStopped;}

bool forceVectorOutput() const {return mForceVectorOutput;}
bool forceVectorOutput() const;

/** Returns true if advanced effects such as blend modes such be used
*/
bool useAdvancedEffects() const;

/** Returns true if advanced effects such as blend modes such be used */
bool useAdvancedEffects() const {return mUseAdvancedEffects;}
/** Used to enable or disable advanced effects such as blend modes */
void setUseAdvancedEffects( bool enabled ) { mUseAdvancedEffects = enabled; }
/** Used to enable or disable advanced effects such as blend modes
*/
void setUseAdvancedEffects( bool enabled );

bool drawEditingInformation() const {return mDrawEditingInformation;}
bool drawEditingInformation() const;

double rendererScale() const {return mRendererScale;}

Expand All @@ -90,21 +126,25 @@ class CORE_EXPORT QgsRenderContext
* @see selectionColor
* @note Added in QGIS v2.4
*/
bool showSelection() const { return mShowSelection; }
bool showSelection() const;

//setters

/** Sets coordinate transformation. QgsRenderContext does not take ownership*/
void setCoordinateTransform( const QgsCoordinateTransform* t );
void setMapToPixel( const QgsMapToPixel& mtp ) {mMapToPixel = mtp;}
void setExtent( const QgsRectangle& extent ) {mExtent = extent;}
void setDrawEditingInformation( bool b ) {mDrawEditingInformation = b;}

void setDrawEditingInformation( bool b );

void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
void setScaleFactor( double factor ) {mScaleFactor = factor;}
void setRasterScaleFactor( double factor ) {mRasterScaleFactor = factor;}
void setRendererScale( double scale ) {mRendererScale = scale;}
void setPainter( QPainter* p ) {mPainter = p;}
void setForceVectorOutput( bool force ) {mForceVectorOutput = force;}

void setForceVectorOutput( bool force );

void setLabelingEngine( QgsLabelingEngineInterface* iface ) { mLabelingEngine = iface; }
//! Assign new labeling engine
void setLabelingEngineV2( QgsLabelingEngineV2* engine2 ) { mLabelingEngine2 = engine2; }
Expand All @@ -116,11 +156,13 @@ class CORE_EXPORT QgsRenderContext
* @see setSelectionColor
* @note Added in QGIS v2.4
*/
void setShowSelection( const bool showSelection ) { mShowSelection = showSelection; }
void setShowSelection( const bool showSelection );

/** Returns true if the rendering optimization (geometry simplification) can be executed*/
bool useRenderingOptimization() const { return mUseRenderingOptimization; }
void setUseRenderingOptimization( bool enabled ) { mUseRenderingOptimization = enabled; }
/** Returns true if the rendering optimization (geometry simplification) can be executed
*/
bool useRenderingOptimization() const;

void setUseRenderingOptimization( bool enabled );

//! Added in QGIS v2.4
const QgsVectorSimplifyMethod& vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
Expand All @@ -144,6 +186,7 @@ class CORE_EXPORT QgsRenderContext
* associated with this render context.
* @see setExpressionContext()
* @note added in QGIS 2.12
* @note not available in Python bindings
*/
const QgsExpressionContext& expressionContext() const { return mExpressionContext; }

Expand All @@ -154,23 +197,16 @@ class CORE_EXPORT QgsRenderContext

private:

Flags mFlags;

/** Painter for rendering operations*/
QPainter* mPainter;

/** For transformation between coordinate systems. Can be 0 if on-the-fly reprojection is not used*/
const QgsCoordinateTransform* mCoordTransform;

/** True if vertex markers for editing should be drawn*/
bool mDrawEditingInformation;

QgsRectangle mExtent;

/** If true then no rendered vector elements should be cached as image*/
bool mForceVectorOutput;

/** Flag if advanced visual effects such as blend modes should be used. True by default*/
bool mUseAdvancedEffects;

QgsMapToPixel mMapToPixel;

/** True if the rendering has been canceled*/
Expand All @@ -191,15 +227,9 @@ class CORE_EXPORT QgsRenderContext
/** Newer labeling engine implementation (can be NULL) */
QgsLabelingEngineV2* mLabelingEngine2;

/** Whether selection should be shown*/
bool mShowSelection;

/** Color used for features that are marked as selected */
QColor mSelectionColor;

/** True if the rendering optimization (geometry simplification) can be executed*/
bool mUseRenderingOptimization;

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

Expand All @@ -210,4 +240,6 @@ class CORE_EXPORT QgsRenderContext
const QgsAbstractGeometryV2* mGeometry;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRenderContext::Flags )

#endif

0 comments on commit 8ddc909

Please sign in to comment.