Skip to content

Commit 0e3af9c

Browse files
committed
[layouts] Remove use of QStyleOptionGraphicsItem from QgsLayoutItem API
Turns out the useful methods in QStyleOptionGraphicsItem were deprecated in Qt 5.10. To future protect our API this replaces all public use of QStyleOptionGraphicsItem, so that we're free to revise the internals of how we use QStyleOptionGraphicsItem without breaking the public QGIS API.
1 parent 9911209 commit 0e3af9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+268
-125
lines changed

python/core/layout/qgslayoutframe.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Returns whether the frame is empty.
118118

119119
protected:
120120

121-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
121+
virtual void draw( QgsLayoutItemRenderContext &context );
122122

123123
virtual void drawFrame( QgsRenderContext &context );
124124

python/core/layout/qgslayoutitem.sip.in

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,64 @@
99

1010

1111

12+
13+
class QgsLayoutItemRenderContext
14+
{
15+
%Docstring
16+
Contains settings and helpers relating to a render of a QgsLayoutItem.
17+
18+
.. versionadded:: 3.0
19+
%End
20+
21+
%TypeHeaderCode
22+
#include "qgslayoutitem.h"
23+
%End
24+
public:
25+
26+
QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor = 1.0 );
27+
%Docstring
28+
Constructor for QgsLayoutItemRenderContext.
29+
30+
The ``renderContext`` parameter specifies a QgsRenderContext for use within
31+
the QgsLayoutItemRenderContext.
32+
33+
The ``viewScaleFactor`` gives the current view zoom (scale factor). It can be
34+
used to scale render graphics so that they always appear a constant size,
35+
regardless of the current view zoom.
36+
%End
37+
38+
39+
40+
QgsRenderContext &renderContext();
41+
%Docstring
42+
Returns a reference to the context's render context.
43+
44+
Note that the context's painter has been scaled so that painter units are pixels.
45+
Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
46+
%End
47+
48+
49+
double viewScaleFactor() const;
50+
%Docstring
51+
Returns the current view zoom (scale factor). It can be
52+
used to scale render graphics so that they always appear a constant size,
53+
regardless of the current view zoom.
54+
55+
E.g. a value of 0.5 indicates that the view is zoomed out to 50% size, so rendered
56+
items must be scaled by 200% in order to have a constant visible size. A value
57+
of 2.0 indicates that the view is zoomed in 200%, so rendered items must be
58+
scaled by 50% in order to have a constant visible size.
59+
%End
60+
61+
private:
62+
QgsLayoutItemRenderContext( const QgsLayoutItemRenderContext &rh );
63+
};
64+
65+
1266
class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInterface
1367
{
1468
%Docstring
15-
Base class for graphical items within a QgsLayout.
69+
Base class for graphical items within a :py:class:`QgsLayout`.
1670

1771
.. versionadded:: 3.0
1872
%End
@@ -939,9 +993,10 @@ painter.
939993
:param painter: destination QPainter
940994
%End
941995

942-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
996+
virtual void draw( QgsLayoutItemRenderContext &context ) = 0;
943997
%Docstring
944-
Draws the item's contents using the specified render ``context``.
998+
Draws the item's contents using the specified item render ``context``.
999+
9451000
Note that the context's painter has been scaled so that painter units are pixels.
9461001
Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
9471002
%End

python/core/layout/qgslayoutitemgroup.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Returns a list of items contained by the group.
7373

7474

7575
protected:
76-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
76+
virtual void draw( QgsLayoutItemRenderContext &context );
7777

7878
virtual bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const;
7979

python/core/layout/qgslayoutitemhtml.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ Returns whether user stylesheets are enabled for the HTML content.
222222

223223
virtual QSizeF totalSize() const;
224224

225-
virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex,
226-
const QStyleOptionGraphicsItem *itemStyle = 0 );
225+
virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex );
226+
227227

228228
virtual double findNearbyPageBreak( double yPos );
229229

python/core/layout/qgslayoutitemlabel.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Returns the label font color.
233233

234234

235235
protected:
236-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
236+
virtual void draw( QgsLayoutItemRenderContext &context );
237237

238238
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
239239

python/core/layout/qgslayoutitemlegend.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ Returns the legend's renderer settings object.
495495

496496

497497
protected:
498-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
498+
virtual void draw( QgsLayoutItemRenderContext &context );
499499

500500
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
501501

python/core/layout/qgslayoutitemmap.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ will be calculated. This can be expensive to calculate, so if they are not requi
472472

473473
protected:
474474

475-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
475+
virtual void draw( QgsLayoutItemRenderContext &context );
476476

477477
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
478478

python/core/layout/qgslayoutitemnodeitem.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Constructor for QgsLayoutNodesItem, attached to the specified ``layout``.
125125
Constructor for a QgsLayoutNodesItem with the given ``polygon`` nodes, attached to the specified ``layout``.
126126
%End
127127

128-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
128+
virtual void draw( QgsLayoutItemRenderContext &context );
129129

130130

131131
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
@@ -145,7 +145,7 @@ Method called in addNode.
145145
Method called in removeNode.
146146
%End
147147

148-
virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
148+
virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
149149
%Docstring
150150
Method called in paint.
151151
%End

python/core/layout/qgslayoutitempage.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ page orientation.
105105

106106
protected:
107107

108-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
108+
virtual void draw( QgsLayoutItemRenderContext &context );
109109

110110
virtual void drawFrame( QgsRenderContext &context );
111111

python/core/layout/qgslayoutitempicture.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ Is emitted on picture rotation change
299299

300300
protected:
301301

302-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
302+
virtual void draw( QgsLayoutItemRenderContext &context );
303303

304304
virtual QSizeF applyItemSizeConstraint( const QSizeF &targetSize );
305305

python/core/layout/qgslayoutitempolygon.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Ownership of ``symbol`` is not transferred.
6868

6969
virtual bool _removeNode( const int nodeIndex );
7070

71-
virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
71+
virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
7272

7373
virtual void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context );
7474

python/core/layout/qgslayoutitempolyline.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ Returns the pen width in millimeters for the stroke of the arrow head.
215215

216216
virtual bool _removeNode( const int nodeIndex );
217217

218-
virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
218+
virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
219219

220220
virtual void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context );
221221

python/core/layout/qgslayoutitemscalebar.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ Adjusts the scale bar box size and updates the item.
491491

492492
protected:
493493

494-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
494+
virtual void draw( QgsLayoutItemRenderContext &context );
495495

496496
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
497497

python/core/layout/qgslayoutitemshape.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Returns the corner radius for rounded rectangle corners.
101101

102102
protected:
103103

104-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
104+
virtual void draw( QgsLayoutItemRenderContext &context );
105105

106106

107107
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

python/core/layout/qgslayoutmultiframe.sip.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,13 @@ the frame size has no minimum in that direction.
144144
.. seealso:: :py:func:`recalculateFrameRects`
145145
%End
146146

147-
virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex,
148-
const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
147+
virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) = 0;
149148
%Docstring
150149
Renders a portion of the multiframe's content into a render ``context``.
151150

152151
:param context: destination render painter
153152
:param renderExtent: visible extent of content to render into the painter.
154153
:param frameIndex: frame number for content
155-
:param itemStyle: item style options for graphics item rendering
156154
%End
157155

158156
virtual void addFrame( QgsLayoutFrame *frame /Transfer/, bool recalcFrameSizes = true );

python/core/layout/qgslayouttable.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ Returns the current contents of the table. Excludes header cells.
515515

516516
virtual QSizeF totalSize() const;
517517

518-
virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex,
519-
const QStyleOptionGraphicsItem *itemStyle = 0 );
518+
virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex );
519+
520520

521521
public slots:
522522

python/core/symbology/qgssymbol.sip.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ Constructor for QgsSymbolRenderContext
413413

414414

415415
QgsRenderContext &renderContext();
416+
%Docstring
417+
Returns a reference to the context's render context.
418+
%End
419+
416420

417421
void setOriginalValueVariable( const QVariant &value );
418422
%Docstring

src/core/layout/qgslayoutframe.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ void QgsLayoutFrame::cleanup()
157157
QgsLayoutItem::cleanup();
158158
}
159159

160-
void QgsLayoutFrame::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
160+
void QgsLayoutFrame::draw( QgsLayoutItemRenderContext &context )
161161
{
162162
if ( mMultiFrame )
163163
{
164164
//calculate index of frame
165165
int frameIndex = mMultiFrame->frameIndex( this );
166166
Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
167-
mMultiFrame->render( context, mSection, frameIndex, itemStyle );
167+
mMultiFrame->render( context, mSection, frameIndex );
168168
}
169169
}
170170

src/core/layout/qgslayoutframe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem
113113

114114
protected:
115115

116-
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
116+
void draw( QgsLayoutItemRenderContext &context ) override;
117117
void drawFrame( QgsRenderContext &context ) override;
118118
void drawBackground( QgsRenderContext &context ) override;
119119
bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override;

src/core/layout/qgslayoutitem.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333

3434
#define CACHE_SIZE_LIMIT 5000
3535

36+
QgsLayoutItemRenderContext::QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor )
37+
: mRenderContext( context )
38+
, mViewScaleFactor( viewScaleFactor )
39+
{
40+
}
41+
42+
43+
3644
QgsLayoutItem::QgsLayoutItem( QgsLayout *layout, bool manageZValue )
3745
: QgsLayoutObject( layout )
3846
, QGraphicsRectItem( nullptr )
@@ -312,7 +320,9 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
312320
// need to translate so that item origin is at 0,0 in painter coordinates (not bounding rect origin)
313321
p.translate( -boundingRect().x() * context.scaleFactor(), -boundingRect().y() * context.scaleFactor() );
314322
drawBackground( context );
315-
draw( context, itemStyle );
323+
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
324+
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
325+
draw( itemRenderContext );
316326
drawFrame( context );
317327
p.end();
318328

@@ -341,7 +351,9 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
341351

342352
// scale painter from mm to dots
343353
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
344-
draw( context, itemStyle );
354+
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
355+
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
356+
draw( itemRenderContext );
345357

346358
painter->scale( context.scaleFactor(), context.scaleFactor() );
347359
drawFrame( context );
@@ -1427,3 +1439,4 @@ void QgsLayoutItem::refreshBlendMode()
14271439
// Update the item effect to use the new blend mode
14281440
mEffect->setCompositionMode( blendMode );
14291441
}
1442+

0 commit comments

Comments
 (0)