Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 56bb657
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -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 );
Expand Down
5 changes: 3 additions & 2 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -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() )
{
Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -21,6 +21,7 @@
#include "qgslayoutobject.h"
#include "qgslayoutsize.h"
#include "qgslayoutpoint.h"
#include "qgsrendercontext.h"
#include <QGraphicsRectItem>

class QgsLayout;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/core/layout/qgslayoutitemregistry.cpp
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemregistry.h
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/gui/testqgslayoutview.cpp
Expand Up @@ -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
{ }
};

Expand Down

0 comments on commit 56bb657

Please sign in to comment.