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.
This commit is contained in:
Nyall Dawson 2017-07-14 13:24:02 +10:00
parent c282024748
commit 56bb65709d
7 changed files with 17 additions and 13 deletions

View File

@ -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 );

View File

@ -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();

View File

@ -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

View File

@ -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 );

View File

@ -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;

View File

@ -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 );

View File

@ -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
{ }
};