Skip to content

Commit

Permalink
[layouts] Remove use of QStyleOptionGraphicsItem from QgsLayoutItem API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson committed Feb 19, 2018
1 parent 9911209 commit 0e3af9c
Show file tree
Hide file tree
Showing 54 changed files with 268 additions and 125 deletions.
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutframe.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Returns whether the frame is empty.


protected: protected:


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


virtual void drawFrame( QgsRenderContext &context ); virtual void drawFrame( QgsRenderContext &context );


Expand Down
61 changes: 58 additions & 3 deletions python/core/layout/qgslayoutitem.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,10 +9,64 @@







class QgsLayoutItemRenderContext
{
%Docstring
Contains settings and helpers relating to a render of a QgsLayoutItem.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitem.h"
%End
public:

QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor = 1.0 );
%Docstring
Constructor for QgsLayoutItemRenderContext.

The ``renderContext`` parameter specifies a QgsRenderContext for use within
the QgsLayoutItemRenderContext.

The ``viewScaleFactor`` gives the current view zoom (scale factor). It can be
used to scale render graphics so that they always appear a constant size,
regardless of the current view zoom.
%End



QgsRenderContext &renderContext();
%Docstring
Returns a reference to the context's render context.

Note that the context's painter has been scaled so that painter units are pixels.
Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
%End


double viewScaleFactor() const;
%Docstring
Returns the current view zoom (scale factor). It can be
used to scale render graphics so that they always appear a constant size,
regardless of the current view zoom.

E.g. a value of 0.5 indicates that the view is zoomed out to 50% size, so rendered
items must be scaled by 200% in order to have a constant visible size. A value
of 2.0 indicates that the view is zoomed in 200%, so rendered items must be
scaled by 50% in order to have a constant visible size.
%End

private:
QgsLayoutItemRenderContext( const QgsLayoutItemRenderContext &rh );
};


class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInterface class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInterface
{ {
%Docstring %Docstring
Base class for graphical items within a QgsLayout. Base class for graphical items within a :py:class:`QgsLayout`.


.. versionadded:: 3.0 .. versionadded:: 3.0
%End %End
Expand Down Expand Up @@ -939,9 +993,10 @@ painter.
:param painter: destination QPainter :param painter: destination QPainter
%End %End


virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; virtual void draw( QgsLayoutItemRenderContext &context ) = 0;
%Docstring %Docstring
Draws the item's contents using the specified render ``context``. Draws the item's contents using the specified item render ``context``.

Note that the context's painter has been scaled so that painter units are pixels. Note that the context's painter has been scaled so that painter units are pixels.
Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units. Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
%End %End
Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemgroup.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Returns a list of items contained by the group.




protected: protected:
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); virtual void draw( QgsLayoutItemRenderContext &context );


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


Expand Down
4 changes: 2 additions & 2 deletions python/core/layout/qgslayoutitemhtml.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ Returns whether user stylesheets are enabled for the HTML content.


virtual QSizeF totalSize() const; virtual QSizeF totalSize() const;


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


virtual double findNearbyPageBreak( double yPos ); virtual double findNearbyPageBreak( double yPos );


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemlabel.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Returns the label font color.




protected: protected:
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); virtual void draw( QgsLayoutItemRenderContext &context );


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemlegend.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ Returns the legend's renderer settings object.




protected: protected:
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); virtual void draw( QgsLayoutItemRenderContext &context );


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemmap.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ will be calculated. This can be expensive to calculate, so if they are not requi


protected: protected:


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


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


Expand Down
4 changes: 2 additions & 2 deletions python/core/layout/qgslayoutitemnodeitem.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Constructor for QgsLayoutNodesItem, attached to the specified ``layout``.
Constructor for a QgsLayoutNodesItem with the given ``polygon`` nodes, attached to the specified ``layout``. Constructor for a QgsLayoutNodesItem with the given ``polygon`` nodes, attached to the specified ``layout``.
%End %End


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




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


virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
%Docstring %Docstring
Method called in paint. Method called in paint.
%End %End
Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitempage.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ page orientation.


protected: protected:


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


virtual void drawFrame( QgsRenderContext &context ); virtual void drawFrame( QgsRenderContext &context );


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitempicture.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Is emitted on picture rotation change


protected: protected:


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


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitempolygon.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Ownership of ``symbol`` is not transferred.


virtual bool _removeNode( const int nodeIndex ); virtual bool _removeNode( const int nodeIndex );


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


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitempolyline.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Returns the pen width in millimeters for the stroke of the arrow head.


virtual bool _removeNode( const int nodeIndex ); virtual bool _removeNode( const int nodeIndex );


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


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemscalebar.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ Adjusts the scale bar box size and updates the item.


protected: protected:


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


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


Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemshape.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Returns the corner radius for rounded rectangle corners.


protected: protected:


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




virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const; virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
Expand Down
4 changes: 1 addition & 3 deletions python/core/layout/qgslayoutmultiframe.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ the frame size has no minimum in that direction.
.. seealso:: :py:func:`recalculateFrameRects` .. seealso:: :py:func:`recalculateFrameRects`
%End %End


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


:param context: destination render painter :param context: destination render painter
:param renderExtent: visible extent of content to render into the painter. :param renderExtent: visible extent of content to render into the painter.
:param frameIndex: frame number for content :param frameIndex: frame number for content
:param itemStyle: item style options for graphics item rendering
%End %End


virtual void addFrame( QgsLayoutFrame *frame /Transfer/, bool recalcFrameSizes = true ); virtual void addFrame( QgsLayoutFrame *frame /Transfer/, bool recalcFrameSizes = true );
Expand Down
4 changes: 2 additions & 2 deletions python/core/layout/qgslayouttable.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ Returns the current contents of the table. Excludes header cells.


virtual QSizeF totalSize() const; virtual QSizeF totalSize() const;


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


public slots: public slots:


Expand Down
4 changes: 4 additions & 0 deletions python/core/symbology/qgssymbol.sip.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ Constructor for QgsSymbolRenderContext




QgsRenderContext &renderContext(); QgsRenderContext &renderContext();
%Docstring
Returns a reference to the context's render context.
%End



void setOriginalValueVariable( const QVariant &value ); void setOriginalValueVariable( const QVariant &value );
%Docstring %Docstring
Expand Down
4 changes: 2 additions & 2 deletions src/core/layout/qgslayoutframe.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ void QgsLayoutFrame::cleanup()
QgsLayoutItem::cleanup(); QgsLayoutItem::cleanup();
} }


void QgsLayoutFrame::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle ) void QgsLayoutFrame::draw( QgsLayoutItemRenderContext &context )
{ {
if ( mMultiFrame ) if ( mMultiFrame )
{ {
//calculate index of frame //calculate index of frame
int frameIndex = mMultiFrame->frameIndex( this ); int frameIndex = mMultiFrame->frameIndex( this );
Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" ); Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
mMultiFrame->render( context, mSection, frameIndex, itemStyle ); mMultiFrame->render( context, mSection, frameIndex );
} }
} }


Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutframe.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem


protected: protected:


void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; void draw( QgsLayoutItemRenderContext &context ) override;
void drawFrame( QgsRenderContext &context ) override; void drawFrame( QgsRenderContext &context ) override;
void drawBackground( QgsRenderContext &context ) override; void drawBackground( QgsRenderContext &context ) override;
bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override;
Expand Down
17 changes: 15 additions & 2 deletions src/core/layout/qgslayoutitem.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@


#define CACHE_SIZE_LIMIT 5000 #define CACHE_SIZE_LIMIT 5000


QgsLayoutItemRenderContext::QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor )
: mRenderContext( context )
, mViewScaleFactor( viewScaleFactor )
{
}



QgsLayoutItem::QgsLayoutItem( QgsLayout *layout, bool manageZValue ) QgsLayoutItem::QgsLayoutItem( QgsLayout *layout, bool manageZValue )
: QgsLayoutObject( layout ) : QgsLayoutObject( layout )
, QGraphicsRectItem( nullptr ) , QGraphicsRectItem( nullptr )
Expand Down Expand Up @@ -312,7 +320,9 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
// need to translate so that item origin is at 0,0 in painter coordinates (not bounding rect origin) // need to translate so that item origin is at 0,0 in painter coordinates (not bounding rect origin)
p.translate( -boundingRect().x() * context.scaleFactor(), -boundingRect().y() * context.scaleFactor() ); p.translate( -boundingRect().x() * context.scaleFactor(), -boundingRect().y() * context.scaleFactor() );
drawBackground( context ); drawBackground( context );
draw( context, itemStyle ); double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
draw( itemRenderContext );
drawFrame( context ); drawFrame( context );
p.end(); p.end();


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


// scale painter from mm to dots // scale painter from mm to dots
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() ); painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
draw( context, itemStyle ); double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
draw( itemRenderContext );


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

Loading

0 comments on commit 0e3af9c

Please sign in to comment.