Skip to content
Permalink
Browse files

[FEATURE] Data defined symbol layer visibility

Adds a data defined override to control a symbol layer's
visibility. Allows users to disable drawing certain symbol layers
for matching features.
  • Loading branch information
nyalldawson committed Oct 18, 2016
1 parent b8c2e68 commit a6148deb3093af0d076a1ab8ad4b470fdd40159b
@@ -277,16 +277,19 @@ class QgsSymbolLayer
*/
void setPaintEffect( QgsPaintEffect* effect /Transfer/);

protected:
QgsSymbolLayer( QgsSymbol::SymbolType type, bool locked = false );

/** Prepares all data defined property expressions for evaluation. This should
* be called prior to evaluating data defined properties.
* @param context symbol render context
* @note added in QGIS 2.12
*/
virtual void prepareExpressions( const QgsSymbolRenderContext& context );

//! Data defined layer enabled string
static const QString EXPR_LAYER_ENABLED;

protected:
QgsSymbolLayer( QgsSymbol::SymbolType type, bool locked = false );

/** Saves all data defined properties to a string map.
* @param stringMap destination string map
* @see restoreDataDefinedProperties
@@ -1,4 +1,4 @@
class QgsLayerPropertiesWidget : QgsPanelWidget
class QgsLayerPropertiesWidget : QgsPanelWidget, QgsExpressionContextGenerator
{
%TypeHeaderCode
#include <qgslayerpropertieswidget.h>
@@ -353,7 +353,6 @@ void QgsEllipseSymbolLayer::startRender( QgsSymbolRenderContext& context )
mPen.setJoinStyle( mPenJoinStyle );
mPen.setWidthF( QgsSymbolLayerUtils::convertToPainterUnits( context.renderContext(), mOutlineWidth, mOutlineWidthUnit, mOutlineWidthMapUnitScale ) );
mBrush.setColor( mColor );
prepareExpressions( context );
}

void QgsEllipseSymbolLayer::stopRender( QgsSymbolRenderContext & )
@@ -258,7 +258,6 @@ void QgsSimpleFillSymbolLayer::startRender( QgsSymbolRenderContext& context )
mPen.setStyle( mBorderStyle );
mPen.setWidthF( QgsSymbolLayerUtils::convertToPainterUnits( context.renderContext(), mBorderWidth, mBorderWidthUnit, mBorderWidthMapUnitScale ) );
mPen.setJoinStyle( mPenJoinStyle );
prepareExpressions( context );
}

void QgsSimpleFillSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -838,9 +837,6 @@ void QgsGradientFillSymbolLayer::startRender( QgsSymbolRenderContext& context )
QColor selColor = context.renderContext().selectionColor();
if ( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
mSelBrush = QBrush( selColor );

//update mBrush to use a gradient fill with specified properties
prepareExpressions( context );
}

void QgsGradientFillSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -1132,8 +1128,6 @@ void QgsShapeburstFillSymbolLayer::startRender( QgsSymbolRenderContext& context
QColor selColor = context.renderContext().selectionColor();
if ( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
mSelBrush = QBrush( selColor );

prepareExpressions( context );
}

void QgsShapeburstFillSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -1996,8 +1990,6 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolRenderContext& context )
{
mOutline->startRender( context.renderContext(), context.fields() );
}

prepareExpressions( context );
}

void QgsSVGFillSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -2831,8 +2823,6 @@ void QgsLinePatternFillSymbolLayer::startRender( QgsSymbolRenderContext& context
{
mFillLineSymbol->startRender( context.renderContext(), context.fields() );
}

prepareExpressions( context );
}

void QgsLinePatternFillSymbolLayer::stopRender( QgsSymbolRenderContext & )
@@ -3257,7 +3247,6 @@ void QgsPointPatternFillSymbolLayer::startRender( QgsSymbolRenderContext& contex
{
mOutline->startRender( context.renderContext(), context.fields() );
}
prepareExpressions( context );
}

void QgsPointPatternFillSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -3451,6 +3440,8 @@ QgsSymbolLayer* QgsCentroidFillSymbolLayer::create( const QgsStringMap& properti
if ( properties.contains( "point_on_all_parts" ) )
sl->setPointOnAllParts( properties["point_on_all_parts"].toInt() != 0 );

sl->restoreDataDefinedProperties( properties );

return sl;
}

@@ -3533,6 +3524,7 @@ QgsStringMap QgsCentroidFillSymbolLayer::properties() const
QgsStringMap map;
map["point_on_surface"] = QString::number( mPointOnSurface );
map["point_on_all_parts"] = QString::number( mPointOnAllParts );
saveDataDefinedProperties( map );
return map;
}

@@ -3544,6 +3536,7 @@ QgsCentroidFillSymbolLayer* QgsCentroidFillSymbolLayer::clone() const
x->setSubSymbol( mMarker->clone() );
x->setPointOnSurface( mPointOnSurface );
x->setPointOnAllParts( mPointOnAllParts );
copyDataDefinedProperties( x );
copyPaintEffect( x );
return x;
}
@@ -3760,7 +3753,6 @@ void QgsRasterFillSymbolLayer::renderPolygon( const QPolygonF &points, QList<QPo

void QgsRasterFillSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
prepareExpressions( context );
applyPattern( mBrush, mImageFilePath, mWidth, mAlpha, context );
}

@@ -44,6 +44,7 @@ QgsSymbolLayer* QgsGeometryGeneratorSymbolLayer::create( const QgsStringMap& pro
{
symbolLayer->setSubSymbol( QgsFillSymbol::createSimple( properties ) );
}
symbolLayer->restoreDataDefinedProperties( properties );

return symbolLayer;
}
@@ -117,6 +118,7 @@ QgsSymbolLayer* QgsGeometryGeneratorSymbolLayer::clone() const

clone->setSymbolType( mSymbolType );

copyDataDefinedProperties( clone );
copyPaintEffect( clone );

return clone;
@@ -138,6 +140,7 @@ QgsStringMap QgsGeometryGeneratorSymbolLayer::properties() const
props.insert( "SymbolType", "Fill" );
break;
}
saveDataDefinedProperties( props );

return props;
}
@@ -180,7 +183,9 @@ bool QgsGeometryGeneratorSymbolLayer::setSubSymbol( QgsSymbol* symbol )

QSet<QString> QgsGeometryGeneratorSymbolLayer::usedAttributes() const
{
return mSymbol->usedAttributes() + mExpression->referencedColumns();
return QgsSymbolLayer::usedAttributes()
+ mSymbol->usedAttributes()
+ mExpression->referencedColumns();
}

bool QgsGeometryGeneratorSymbolLayer::isCompatibleWithSymbol( QgsSymbol* symbol ) const
@@ -228,9 +228,6 @@ void QgsSimpleLineSymbolLayer::startRender( QgsSymbolRenderContext& context )
if ( ! selectionIsOpaque )
selColor.setAlphaF( context.alpha() );
mSelPen.setColor( selColor );

//prepare expressions for data defined properties
prepareExpressions( context );
}

void QgsSimpleLineSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -815,9 +812,6 @@ void QgsMarkerLineSymbolLayer::startRender( QgsSymbolRenderContext& context )
mMarker->setRenderHints( hints );

mMarker->startRender( context.renderContext(), context.fields() );

//prepare expressions for data defined properties
prepareExpressions( context );
}

void QgsMarkerLineSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -160,8 +160,6 @@ void QgsSimpleMarkerSymbolLayerBase::startRender( QgsSymbolRenderContext &contex
else
mPath = transform.map( mPath );

prepareExpressions( context );

QgsMarkerSymbolLayer::startRender( context );
}

@@ -1909,7 +1907,6 @@ void QgsSvgMarkerSymbolLayer::startRender( QgsSymbolRenderContext& context )
{
QgsMarkerSymbolLayer::startRender( context ); // get anchor point expressions
Q_UNUSED( context );
prepareExpressions( context );
}

void QgsSvgMarkerSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -2574,7 +2571,6 @@ void QgsFontMarkerSymbolLayer::startRender( QgsSymbolRenderContext& context )
mChrWidth = mFontMetrics->width( mChr );
mChrOffset = QPointF( mChrWidth / 2.0, -mFontMetrics->ascent() / 2.0 );
mOrigSize = mSize; // save in case the size would be data defined
prepareExpressions( context );
}

void QgsFontMarkerSymbolLayer::stopRender( QgsSymbolRenderContext& context )
@@ -400,6 +400,7 @@ void QgsSymbol::startRender( QgsRenderContext& context, const QgsFields& fields
if ( !layer->enabled() )
continue;

layer->prepareExpressions( symbolContext );
layer->startRender( symbolContext );
}
}
@@ -610,6 +611,14 @@ void QgsSymbol::renderUsingLayer( QgsSymbolLayer* layer, QgsSymbolRenderContext&
{
Q_ASSERT( layer->type() == Hybrid );

if ( layer->hasDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED ) )
{
bool ok = false;
bool enabled = layer->evaluateDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED, context, QVariant(), &ok ).toBool();
if ( ok && !enabled )
return;
}

QgsGeometryGeneratorSymbolLayer* generatorLayer = static_cast<QgsGeometryGeneratorSymbolLayer*>( layer );

QgsPaintEffect* effect = generatorLayer->paintEffect();
@@ -1403,6 +1412,14 @@ void QgsMarkerSymbol::renderPointUsingLayer( QgsMarkerSymbolLayer* layer, QPoint
{
static QPointF nullPoint( 0, 0 );

if ( layer->hasDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED ) )
{
bool ok = false;
bool enabled = layer->evaluateDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED, context, QVariant(), &ok ).toBool();
if ( ok && !enabled )
return;
}

QgsPaintEffect* effect = layer->paintEffect();
if ( effect && effect->enabled() )
{
@@ -1676,6 +1693,14 @@ void QgsLineSymbol::renderPolyline( const QPolygonF& points, const QgsFeature* f

void QgsLineSymbol::renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const QPolygonF &points, QgsSymbolRenderContext &context )
{
if ( layer->hasDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED ) )
{
bool ok = false;
bool enabled = layer->evaluateDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED, context, QVariant(), &ok ).toBool();
if ( ok && !enabled )
return;
}

QgsPaintEffect* effect = layer->paintEffect();
if ( effect && effect->enabled() )
{
@@ -1748,6 +1773,14 @@ void QgsFillSymbol::renderPolygon( const QPolygonF& points, QList<QPolygonF>* ri

void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer* layer, const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolRenderContext& context )
{
if ( layer->hasDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED ) )
{
bool ok = false;
bool enabled = layer->evaluateDataDefinedProperty( QgsSymbolLayer::EXPR_LAYER_ENABLED, context, QVariant(), &ok ).toBool();
if ( ok && !enabled )
return;
}

QgsSymbol::SymbolType layertype = layer->type();

QgsPaintEffect* effect = layer->paintEffect();
@@ -89,6 +89,7 @@ const QString QgsSymbolLayer::EXPR_INTERVAL( "interval" );
const QString QgsSymbolLayer::EXPR_OFFSET_ALONG_LINE( "offset_along_line" );
const QString QgsSymbolLayer::EXPR_HORIZONTAL_ANCHOR_POINT( "horizontal_anchor_point" );
const QString QgsSymbolLayer::EXPR_VERTICAL_ANCHOR_POINT( "vertical_anchor_point" );
const QString QgsSymbolLayer::EXPR_LAYER_ENABLED( "enabled" );

QgsDataDefined *QgsSymbolLayer::getDataDefinedProperty( const QString &property ) const
{
@@ -278,6 +278,16 @@ class CORE_EXPORT QgsSymbolLayer
*/
void setPaintEffect( QgsPaintEffect* effect );

/** Prepares all data defined property expressions for evaluation. This should
* be called prior to evaluating data defined properties.
* @param context symbol render context
* @note added in QGIS 2.12
*/
virtual void prepareExpressions( const QgsSymbolRenderContext& context );

//! Data defined layer enabled string
static const QString EXPR_LAYER_ENABLED;

protected:
QgsSymbolLayer( QgsSymbol::SymbolType type, bool locked = false );

@@ -299,13 +309,6 @@ class CORE_EXPORT QgsSymbolLayer
static const bool selectFillBorder = false; // Fill symbol layer also selects border symbology
static const bool selectFillStyle = false; // Fill symbol uses symbol layer style..

/** Prepares all data defined property expressions for evaluation. This should
* be called prior to evaluating data defined properties.
* @param context symbol render context
* @note added in QGIS 2.12
*/
virtual void prepareExpressions( const QgsSymbolRenderContext& context );

/** Saves all data defined properties to a string map.
* @param stringMap destination string map
* @see restoreDataDefinedProperties

0 comments on commit a6148de

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