Skip to content

Commit

Permalink
[symbology] draw effect(s) when rendering preview icon
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and m-kuhn committed Nov 16, 2016
1 parent f5f2850 commit b32a719
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 31 deletions.
33 changes: 33 additions & 0 deletions python/core/effects/qgspainteffect.sip
Expand Up @@ -306,3 +306,36 @@ class QgsDrawSourceEffect : QgsPaintEffect
virtual void draw( QgsRenderContext& context );

};

class QgsEffectPainter
{
%TypeHeaderCode
#include <qgspainteffect.h>
%End
public:

/**
* QgsEffectPainter constructor
*
* @param renderContext the QgsRenderContext object
* @note Added in QGIS 3.0
*/
QgsEffectPainter( QgsRenderContext& renderContext );

/**
* QgsEffectPainter constructor alternative if no painter translation is needed
*
* @param renderContext the QgsRenderContext object
* @param effect the QgsPaintEffect object
* @note Added in QGIS 3.0
*/
QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect );
~QgsEffectPainter();

/**
* Sets the effect to be painted
*
* @param effect the QgsPaintEffect object
*/
void setEffect( QgsPaintEffect* effect );
};
38 changes: 38 additions & 0 deletions src/core/effects/qgspainteffect.cpp
Expand Up @@ -322,3 +322,41 @@ void QgsDrawSourceEffect::readProperties( const QgsStringMap &props )
mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
}


//
// QgsEffectPainter
//

QgsEffectPainter::QgsEffectPainter( QgsRenderContext& renderContext )
: mRenderContext( renderContext )
, mEffect( nullptr )
{
mPainter = renderContext.painter();
mPainter->save();
}

QgsEffectPainter::QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect )
: mRenderContext( renderContext )
, mEffect( effect )
{
mPainter = mRenderContext.painter();
mPainter->save();
mEffect->begin( mRenderContext );
}

void QgsEffectPainter::setEffect( QgsPaintEffect* effect )
{
Q_ASSERT( !mEffect );

mEffect = effect;
mEffect->begin( mRenderContext );
}

QgsEffectPainter::~QgsEffectPainter()
{
Q_ASSERT( mEffect );

mEffect->end( mRenderContext );
mPainter->restore();
}
51 changes: 51 additions & 0 deletions src/core/effects/qgspainteffect.h
Expand Up @@ -309,5 +309,56 @@ class CORE_EXPORT QgsDrawSourceEffect : public QgsPaintEffect
QPainter::CompositionMode mBlendMode;
};

/** \ingroup core
* \class QgsEffectPainter
* \brief A class to manager painter saving and restoring required for effect drawing
*
* \note Added in version 3.0
*/
class CORE_EXPORT QgsEffectPainter
{
public:

/**
* QgsEffectPainter constructor
*
* @param renderContext the QgsRenderContext object
* @note Added in QGIS 3.0
*/
QgsEffectPainter( QgsRenderContext& renderContext );

/**
* QgsEffectPainter constructor alternative if no painter translation is needed
*
* @param renderContext the QgsRenderContext object
* @param effect the QgsPaintEffect object
* @note Added in QGIS 3.0
*/
QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect );
~QgsEffectPainter();

/**
* Sets the effect to be painted
*
* @param effect the QgsPaintEffect object
*/
void setEffect( QgsPaintEffect* effect );

///@cond PRIVATE

/**
* Access to the painter object
*
* @note Added in QGIS 3.0
*/
QPainter* operator->() { return mPainter; }
///@endcond

private:
QgsRenderContext& mRenderContext;
QPainter* mPainter;
QgsPaintEffect* mEffect;
};

#endif // QGSPAINTEFFECT_H

35 changes: 7 additions & 28 deletions src/core/symbology-ng/qgssymbol.cpp
Expand Up @@ -624,14 +624,8 @@ void QgsSymbol::renderUsingLayer( QgsSymbolLayer* layer, QgsSymbolRenderContext&
QgsPaintEffect* effect = generatorLayer->paintEffect();
if ( effect && effect->enabled() )
{
QPainter* p = context.renderContext().painter();
p->save();

effect->begin( context.renderContext() );
QgsEffectPainter p( context.renderContext(), effect );
generatorLayer->render( context );
effect->end( context.renderContext() );

p->restore();
}
else
{
Expand Down Expand Up @@ -1428,15 +1422,10 @@ void QgsMarkerSymbol::renderPointUsingLayer( QgsMarkerSymbolLayer* layer, QPoint
QgsPaintEffect* effect = layer->paintEffect();
if ( effect && effect->enabled() )
{
QPainter* p = context.renderContext().painter();
p->save();
QgsEffectPainter p( context.renderContext() );
p->translate( point );

effect->begin( context.renderContext() );
p.setEffect( effect );
layer->renderPoint( nullPoint, context );
effect->end( context.renderContext() );

p->restore();
}
else
{
Expand Down Expand Up @@ -1709,15 +1698,10 @@ void QgsLineSymbol::renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const Q
QgsPaintEffect* effect = layer->paintEffect();
if ( effect && effect->enabled() )
{
QPainter* p = context.renderContext().painter();
p->save();
QgsEffectPainter p( context.renderContext() );
p->translate( points.boundingRect().topLeft() );

effect->begin( context.renderContext() );
p.setEffect( effect );
layer->renderPolyline( points.translated( -points.boundingRect().topLeft() ), context );
effect->end( context.renderContext() );

p->restore();
}
else
{
Expand Down Expand Up @@ -1794,11 +1778,9 @@ void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer* layer, const QPolyg
QRectF bounds = polygonBounds( points, rings );
QList<QPolygonF>* translatedRings = translateRings( rings, -bounds.left(), -bounds.top() );

QPainter* p = context.renderContext().painter();
p->save();
QgsEffectPainter p( context.renderContext() );
p->translate( bounds.topLeft() );

effect->begin( context.renderContext() );
p.setEffect( effect );
if ( layertype == QgsSymbol::Fill )
{
( static_cast<QgsFillSymbolLayer*>( layer ) )->renderPolygon( points.translated( -bounds.topLeft() ), translatedRings, context );
Expand All @@ -1808,9 +1790,6 @@ void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer* layer, const QPolyg
( static_cast<QgsLineSymbolLayer*>( layer ) )->renderPolygonOutline( points.translated( -bounds.topLeft() ), translatedRings, context );
}
delete translatedRings;

effect->end( context.renderContext() );
p->restore();
}
else
{
Expand Down
33 changes: 30 additions & 3 deletions src/core/symbology-ng/qgssymbollayer.cpp
Expand Up @@ -421,7 +421,16 @@ void QgsMarkerSymbolLayer::startRender( QgsSymbolRenderContext& context )
void QgsMarkerSymbolLayer::drawPreviewIcon( QgsSymbolRenderContext& context, QSize size )
{
startRender( context );
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
QgsPaintEffect* effect = paintEffect();
if ( effect && effect->enabled() )
{
QgsEffectPainter p( context.renderContext(), effect );
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
}
else
{
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
}
stopRender( context );
}

Expand Down Expand Up @@ -590,7 +599,16 @@ void QgsLineSymbolLayer::drawPreviewIcon( QgsSymbolRenderContext& context, QSize
points << QPointF( 0, int( size.height() / 2 ) + 0.5 ) << QPointF( size.width(), int( size.height() / 2 ) + 0.5 );

startRender( context );
renderPolyline( points, context );
QgsPaintEffect* effect = paintEffect();
if ( effect && effect->enabled() )
{
QgsEffectPainter p( context.renderContext(), effect );
renderPolyline( points, context );
}
else
{
renderPolyline( points, context );
}
stopRender( context );
}

Expand All @@ -615,7 +633,16 @@ void QgsFillSymbolLayer::drawPreviewIcon( QgsSymbolRenderContext& context, QSize
{
QPolygonF poly = QRectF( QPointF( 0, 0 ), QPointF( size.width(), size.height() ) );
startRender( context );
renderPolygon( poly, nullptr, context );
QgsPaintEffect* effect = paintEffect();
if ( effect && effect->enabled() )
{
QgsEffectPainter p( context.renderContext(), effect );
renderPolygon( poly, nullptr, context );
}
else
{
renderPolygon( poly, nullptr, context );
}
stopRender( context );
}

Expand Down

0 comments on commit b32a719

Please sign in to comment.