From 56bb65709d22ec01157cd1eb893ffb7cad89bd8f Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 14 Jul 2017 13:24:02 +1000 Subject: [PATCH] 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. --- python/core/layout/qgslayoutitem.sip | 4 ++-- src/core/layout/qgslayoutitem.cpp | 5 +++-- src/core/layout/qgslayoutitem.h | 5 +++-- src/core/layout/qgslayoutitemregistry.cpp | 5 +++-- src/core/layout/qgslayoutitemregistry.h | 2 +- tests/src/core/testqgslayoutitem.cpp | 7 ++++--- tests/src/gui/testqgslayoutview.cpp | 2 +- 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/python/core/layout/qgslayoutitem.sip b/python/core/layout/qgslayoutitem.sip index 0e8c763457d0..75df2681147e 100644 --- a/python/core/layout/qgslayoutitem.sip +++ b/python/core/layout/qgslayoutitem.sip @@ -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 ); diff --git a/src/core/layout/qgslayoutitem.cpp b/src/core/layout/qgslayoutitem.cpp index 74112ead3f78..f245f4373ff5 100644 --- a/src/core/layout/qgslayoutitem.cpp +++ b/src/core/layout/qgslayoutitem.cpp @@ -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(); diff --git a/src/core/layout/qgslayoutitem.h b/src/core/layout/qgslayoutitem.h index 3c7f0640cefc..9b07fea64cb5 100644 --- a/src/core/layout/qgslayoutitem.h +++ b/src/core/layout/qgslayoutitem.h @@ -21,6 +21,7 @@ #include "qgslayoutobject.h" #include "qgslayoutsize.h" #include "qgslayoutpoint.h" +#include "qgsrendercontext.h" #include 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 diff --git a/src/core/layout/qgslayoutitemregistry.cpp b/src/core/layout/qgslayoutitemregistry.cpp index 6bed127a26c8..8f7eed167e75 100644 --- a/src/core/layout/qgslayoutitemregistry.cpp +++ b/src/core/layout/qgslayoutitemregistry.cpp @@ -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 ); diff --git a/src/core/layout/qgslayoutitemregistry.h b/src/core/layout/qgslayoutitemregistry.h index 0d2eed98b315..a2cda2eb7e71 100644 --- a/src/core/layout/qgslayoutitemregistry.h +++ b/src/core/layout/qgslayoutitemregistry.h @@ -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; diff --git a/tests/src/core/testqgslayoutitem.cpp b/tests/src/core/testqgslayoutitem.cpp index 11442a261024..11aaf40d0d0b 100644 --- a/tests/src/core/testqgslayoutitem.cpp +++ b/tests/src/core/testqgslayoutitem.cpp @@ -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 ); diff --git a/tests/src/gui/testqgslayoutview.cpp b/tests/src/gui/testqgslayoutview.cpp index abc0d5bfa3cf..096dca3877b0 100644 --- a/tests/src/gui/testqgslayoutview.cpp +++ b/tests/src/gui/testqgslayoutview.cpp @@ -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 { } };