Skip to content
Permalink
Browse files

Change QgsLayoutItem::draw to use a renderContext instead of

direct QPainter argument

This will make use of other rendering code within layout items
much easier - since symbology/text renderer/diagrams/etc all
require QgsRenderContexts for use, it makes sense for
layout item rendering to also use this approach.

This also avoids lots of duplicate code which was scattered
throughout different composer item types to manually handle
creation of QgsRenderContexts when required.
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent c282024 commit 56bb65709d22ec01157cd1eb893ffb7cad89bd8f
@@ -170,9 +170,9 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
@param painter destination QPainter
%End

virtual void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) = 0;
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
%Docstring
Draws the item's contents on a specified ``painter``.
Draws the item's contents using the specified render ``context``.
%End

virtual void setFixedSize( const QgsLayoutSize &size );
@@ -33,7 +33,7 @@ QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
initConnectionsToLayout();
}

void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
{
if ( !painter || !painter->device() )
{
@@ -50,7 +50,8 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
}
else
{
draw( painter, itemStyle, pWidget );
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, painter );
draw( context, itemStyle );
}

painter->restore();
@@ -21,6 +21,7 @@
#include "qgslayoutobject.h"
#include "qgslayoutsize.h"
#include "qgslayoutpoint.h"
#include "qgsrendercontext.h"
#include <QGraphicsRectItem>

class QgsLayout;
@@ -184,9 +185,9 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
virtual void drawDebugRect( QPainter *painter );

/**
* Draws the item's contents on a specified \a painter.
* Draws the item's contents using the specified render \a context.
*/
virtual void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) = 0;
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0;

/**
* Sets a fixed \a size for the layout item, which prevents it from being freely
@@ -95,10 +95,11 @@ TestLayoutItem::TestLayoutItem( QgsLayout *layout )
mColor = QColor::fromHsv( h, s, v );
}

void TestLayoutItem::draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
QPainter *painter = context.painter();

painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->setPen( Qt::NoPen );
@@ -267,7 +267,7 @@ class TestLayoutItem : public QgsLayoutItem

//implement pure virtual methods
int type() const { return QgsLayoutItemRegistry::LayoutItem + 102; }
void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr );

private:
QColor mColor;
@@ -69,10 +69,11 @@ class TestQgsLayoutItem: public QObject

//implement pure virtual methods
int type() const { return QgsLayoutItemRegistry::LayoutItem + 101; }
void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )

protected:
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * = nullptr ) override
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
QPainter *painter = context.painter();
painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->setPen( Qt::NoPen );
@@ -242,7 +242,7 @@ class TestItem : public QgsLayoutItem

//implement pure virtual methods
int type() const override { return QgsLayoutItemRegistry::LayoutItem + 101; }
void draw( QPainter *, const QStyleOptionGraphicsItem *, QWidget * ) override
void draw( QgsRenderContext &, const QStyleOptionGraphicsItem * = nullptr ) override
{ }
};

0 comments on commit 56bb657

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