Skip to content

Commit 56bb657

Browse files
committedJul 18, 2017
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.
1 parent c282024 commit 56bb657

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed
 

‎python/core/layout/qgslayoutitem.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
170170
@param painter destination QPainter
171171
%End
172172

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

178178
virtual void setFixedSize( const QgsLayoutSize &size );

‎src/core/layout/qgslayoutitem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
3333
initConnectionsToLayout();
3434
}
3535

36-
void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
36+
void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
3737
{
3838
if ( !painter || !painter->device() )
3939
{
@@ -50,7 +50,8 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
5050
}
5151
else
5252
{
53-
draw( painter, itemStyle, pWidget );
53+
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, painter );
54+
draw( context, itemStyle );
5455
}
5556

5657
painter->restore();

‎src/core/layout/qgslayoutitem.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgslayoutobject.h"
2222
#include "qgslayoutsize.h"
2323
#include "qgslayoutpoint.h"
24+
#include "qgsrendercontext.h"
2425
#include <QGraphicsRectItem>
2526

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

186187
/**
187-
* Draws the item's contents on a specified \a painter.
188+
* Draws the item's contents using the specified render \a context.
188189
*/
189-
virtual void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) = 0;
190+
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0;
190191

191192
/**
192193
* Sets a fixed \a size for the layout item, which prevents it from being freely

‎src/core/layout/qgslayoutitemregistry.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ TestLayoutItem::TestLayoutItem( QgsLayout *layout )
9595
mColor = QColor::fromHsv( h, s, v );
9696
}
9797

98-
void TestLayoutItem::draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
98+
void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
9999
{
100100
Q_UNUSED( itemStyle );
101-
Q_UNUSED( pWidget );
101+
QPainter *painter = context.painter();
102+
102103
painter->save();
103104
painter->setRenderHint( QPainter::Antialiasing, false );
104105
painter->setPen( Qt::NoPen );

‎src/core/layout/qgslayoutitemregistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class TestLayoutItem : public QgsLayoutItem
267267

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

272272
private:
273273
QColor mColor;

‎tests/src/core/testqgslayoutitem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ class TestQgsLayoutItem: public QObject
6969

7070
//implement pure virtual methods
7171
int type() const { return QgsLayoutItemRegistry::LayoutItem + 101; }
72-
void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
72+
73+
protected:
74+
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * = nullptr ) override
7375
{
74-
Q_UNUSED( itemStyle );
75-
Q_UNUSED( pWidget );
76+
QPainter *painter = context.painter();
7677
painter->save();
7778
painter->setRenderHint( QPainter::Antialiasing, false );
7879
painter->setPen( Qt::NoPen );

‎tests/src/gui/testqgslayoutview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class TestItem : public QgsLayoutItem
242242

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

0 commit comments

Comments
 (0)
Please sign in to comment.