Skip to content

Commit b32a719

Browse files
nirvnm-kuhn
authored andcommittedNov 16, 2016
[symbology] draw effect(s) when rendering preview icon
1 parent f5f2850 commit b32a719

File tree

5 files changed

+159
-31
lines changed

5 files changed

+159
-31
lines changed
 

‎python/core/effects/qgspainteffect.sip

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,36 @@ class QgsDrawSourceEffect : QgsPaintEffect
306306
virtual void draw( QgsRenderContext& context );
307307

308308
};
309+
310+
class QgsEffectPainter
311+
{
312+
%TypeHeaderCode
313+
#include <qgspainteffect.h>
314+
%End
315+
public:
316+
317+
/**
318+
* QgsEffectPainter constructor
319+
*
320+
* @param renderContext the QgsRenderContext object
321+
* @note Added in QGIS 3.0
322+
*/
323+
QgsEffectPainter( QgsRenderContext& renderContext );
324+
325+
/**
326+
* QgsEffectPainter constructor alternative if no painter translation is needed
327+
*
328+
* @param renderContext the QgsRenderContext object
329+
* @param effect the QgsPaintEffect object
330+
* @note Added in QGIS 3.0
331+
*/
332+
QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect );
333+
~QgsEffectPainter();
334+
335+
/**
336+
* Sets the effect to be painted
337+
*
338+
* @param effect the QgsPaintEffect object
339+
*/
340+
void setEffect( QgsPaintEffect* effect );
341+
};

‎src/core/effects/qgspainteffect.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,41 @@ void QgsDrawSourceEffect::readProperties( const QgsStringMap &props )
322322
mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
323323
mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
324324
}
325+
326+
327+
//
328+
// QgsEffectPainter
329+
//
330+
331+
QgsEffectPainter::QgsEffectPainter( QgsRenderContext& renderContext )
332+
: mRenderContext( renderContext )
333+
, mEffect( nullptr )
334+
{
335+
mPainter = renderContext.painter();
336+
mPainter->save();
337+
}
338+
339+
QgsEffectPainter::QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect )
340+
: mRenderContext( renderContext )
341+
, mEffect( effect )
342+
{
343+
mPainter = mRenderContext.painter();
344+
mPainter->save();
345+
mEffect->begin( mRenderContext );
346+
}
347+
348+
void QgsEffectPainter::setEffect( QgsPaintEffect* effect )
349+
{
350+
Q_ASSERT( !mEffect );
351+
352+
mEffect = effect;
353+
mEffect->begin( mRenderContext );
354+
}
355+
356+
QgsEffectPainter::~QgsEffectPainter()
357+
{
358+
Q_ASSERT( mEffect );
359+
360+
mEffect->end( mRenderContext );
361+
mPainter->restore();
362+
}

‎src/core/effects/qgspainteffect.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,56 @@ class CORE_EXPORT QgsDrawSourceEffect : public QgsPaintEffect
309309
QPainter::CompositionMode mBlendMode;
310310
};
311311

312+
/** \ingroup core
313+
* \class QgsEffectPainter
314+
* \brief A class to manager painter saving and restoring required for effect drawing
315+
*
316+
* \note Added in version 3.0
317+
*/
318+
class CORE_EXPORT QgsEffectPainter
319+
{
320+
public:
321+
322+
/**
323+
* QgsEffectPainter constructor
324+
*
325+
* @param renderContext the QgsRenderContext object
326+
* @note Added in QGIS 3.0
327+
*/
328+
QgsEffectPainter( QgsRenderContext& renderContext );
329+
330+
/**
331+
* QgsEffectPainter constructor alternative if no painter translation is needed
332+
*
333+
* @param renderContext the QgsRenderContext object
334+
* @param effect the QgsPaintEffect object
335+
* @note Added in QGIS 3.0
336+
*/
337+
QgsEffectPainter( QgsRenderContext& renderContext, QgsPaintEffect* effect );
338+
~QgsEffectPainter();
339+
340+
/**
341+
* Sets the effect to be painted
342+
*
343+
* @param effect the QgsPaintEffect object
344+
*/
345+
void setEffect( QgsPaintEffect* effect );
346+
347+
///@cond PRIVATE
348+
349+
/**
350+
* Access to the painter object
351+
*
352+
* @note Added in QGIS 3.0
353+
*/
354+
QPainter* operator->() { return mPainter; }
355+
///@endcond
356+
357+
private:
358+
QgsRenderContext& mRenderContext;
359+
QPainter* mPainter;
360+
QgsPaintEffect* mEffect;
361+
};
362+
312363
#endif // QGSPAINTEFFECT_H
313364

‎src/core/symbology-ng/qgssymbol.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,8 @@ void QgsSymbol::renderUsingLayer( QgsSymbolLayer* layer, QgsSymbolRenderContext&
624624
QgsPaintEffect* effect = generatorLayer->paintEffect();
625625
if ( effect && effect->enabled() )
626626
{
627-
QPainter* p = context.renderContext().painter();
628-
p->save();
629-
630-
effect->begin( context.renderContext() );
627+
QgsEffectPainter p( context.renderContext(), effect );
631628
generatorLayer->render( context );
632-
effect->end( context.renderContext() );
633-
634-
p->restore();
635629
}
636630
else
637631
{
@@ -1428,15 +1422,10 @@ void QgsMarkerSymbol::renderPointUsingLayer( QgsMarkerSymbolLayer* layer, QPoint
14281422
QgsPaintEffect* effect = layer->paintEffect();
14291423
if ( effect && effect->enabled() )
14301424
{
1431-
QPainter* p = context.renderContext().painter();
1432-
p->save();
1425+
QgsEffectPainter p( context.renderContext() );
14331426
p->translate( point );
1434-
1435-
effect->begin( context.renderContext() );
1427+
p.setEffect( effect );
14361428
layer->renderPoint( nullPoint, context );
1437-
effect->end( context.renderContext() );
1438-
1439-
p->restore();
14401429
}
14411430
else
14421431
{
@@ -1709,15 +1698,10 @@ void QgsLineSymbol::renderPolylineUsingLayer( QgsLineSymbolLayer *layer, const Q
17091698
QgsPaintEffect* effect = layer->paintEffect();
17101699
if ( effect && effect->enabled() )
17111700
{
1712-
QPainter* p = context.renderContext().painter();
1713-
p->save();
1701+
QgsEffectPainter p( context.renderContext() );
17141702
p->translate( points.boundingRect().topLeft() );
1715-
1716-
effect->begin( context.renderContext() );
1703+
p.setEffect( effect );
17171704
layer->renderPolyline( points.translated( -points.boundingRect().topLeft() ), context );
1718-
effect->end( context.renderContext() );
1719-
1720-
p->restore();
17211705
}
17221706
else
17231707
{
@@ -1794,11 +1778,9 @@ void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer* layer, const QPolyg
17941778
QRectF bounds = polygonBounds( points, rings );
17951779
QList<QPolygonF>* translatedRings = translateRings( rings, -bounds.left(), -bounds.top() );
17961780

1797-
QPainter* p = context.renderContext().painter();
1798-
p->save();
1781+
QgsEffectPainter p( context.renderContext() );
17991782
p->translate( bounds.topLeft() );
1800-
1801-
effect->begin( context.renderContext() );
1783+
p.setEffect( effect );
18021784
if ( layertype == QgsSymbol::Fill )
18031785
{
18041786
( static_cast<QgsFillSymbolLayer*>( layer ) )->renderPolygon( points.translated( -bounds.topLeft() ), translatedRings, context );
@@ -1808,9 +1790,6 @@ void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer* layer, const QPolyg
18081790
( static_cast<QgsLineSymbolLayer*>( layer ) )->renderPolygonOutline( points.translated( -bounds.topLeft() ), translatedRings, context );
18091791
}
18101792
delete translatedRings;
1811-
1812-
effect->end( context.renderContext() );
1813-
p->restore();
18141793
}
18151794
else
18161795
{

‎src/core/symbology-ng/qgssymbollayer.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,16 @@ void QgsMarkerSymbolLayer::startRender( QgsSymbolRenderContext& context )
421421
void QgsMarkerSymbolLayer::drawPreviewIcon( QgsSymbolRenderContext& context, QSize size )
422422
{
423423
startRender( context );
424-
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
424+
QgsPaintEffect* effect = paintEffect();
425+
if ( effect && effect->enabled() )
426+
{
427+
QgsEffectPainter p( context.renderContext(), effect );
428+
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
429+
}
430+
else
431+
{
432+
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
433+
}
425434
stopRender( context );
426435
}
427436

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

592601
startRender( context );
593-
renderPolyline( points, context );
602+
QgsPaintEffect* effect = paintEffect();
603+
if ( effect && effect->enabled() )
604+
{
605+
QgsEffectPainter p( context.renderContext(), effect );
606+
renderPolyline( points, context );
607+
}
608+
else
609+
{
610+
renderPolyline( points, context );
611+
}
594612
stopRender( context );
595613
}
596614

@@ -615,7 +633,16 @@ void QgsFillSymbolLayer::drawPreviewIcon( QgsSymbolRenderContext& context, QSize
615633
{
616634
QPolygonF poly = QRectF( QPointF( 0, 0 ), QPointF( size.width(), size.height() ) );
617635
startRender( context );
618-
renderPolygon( poly, nullptr, context );
636+
QgsPaintEffect* effect = paintEffect();
637+
if ( effect && effect->enabled() )
638+
{
639+
QgsEffectPainter p( context.renderContext(), effect );
640+
renderPolygon( poly, nullptr, context );
641+
}
642+
else
643+
{
644+
renderPolygon( poly, nullptr, context );
645+
}
619646
stopRender( context );
620647
}
621648

0 commit comments

Comments
 (0)
Please sign in to comment.