From 0e3af9c6a94f48cc21e417b8e892f44ad565f0b7 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 19 Feb 2018 16:04:46 +1000 Subject: [PATCH] [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. --- python/core/layout/qgslayoutframe.sip.in | 2 +- python/core/layout/qgslayoutitem.sip.in | 61 ++++++++++++++- python/core/layout/qgslayoutitemgroup.sip.in | 2 +- python/core/layout/qgslayoutitemhtml.sip.in | 4 +- python/core/layout/qgslayoutitemlabel.sip.in | 2 +- python/core/layout/qgslayoutitemlegend.sip.in | 2 +- python/core/layout/qgslayoutitemmap.sip.in | 2 +- .../core/layout/qgslayoutitemnodeitem.sip.in | 4 +- python/core/layout/qgslayoutitempage.sip.in | 2 +- .../core/layout/qgslayoutitempicture.sip.in | 2 +- .../core/layout/qgslayoutitempolygon.sip.in | 2 +- .../core/layout/qgslayoutitempolyline.sip.in | 2 +- .../core/layout/qgslayoutitemscalebar.sip.in | 2 +- python/core/layout/qgslayoutitemshape.sip.in | 2 +- python/core/layout/qgslayoutmultiframe.sip.in | 4 +- python/core/layout/qgslayouttable.sip.in | 4 +- python/core/symbology/qgssymbol.sip.in | 4 + src/core/layout/qgslayoutframe.cpp | 4 +- src/core/layout/qgslayoutframe.h | 2 +- src/core/layout/qgslayoutitem.cpp | 17 ++++- src/core/layout/qgslayoutitem.h | 75 ++++++++++++++++++- src/core/layout/qgslayoutitemgroup.cpp | 2 +- src/core/layout/qgslayoutitemgroup.h | 2 +- src/core/layout/qgslayoutitemhtml.cpp | 7 +- src/core/layout/qgslayoutitemhtml.h | 3 +- src/core/layout/qgslayoutitemlabel.cpp | 6 +- src/core/layout/qgslayoutitemlabel.h | 2 +- src/core/layout/qgslayoutitemlegend.cpp | 6 +- src/core/layout/qgslayoutitemlegend.h | 2 +- src/core/layout/qgslayoutitemmap.cpp | 2 +- src/core/layout/qgslayoutitemmap.h | 2 +- src/core/layout/qgslayoutitemnodeitem.cpp | 32 ++++---- src/core/layout/qgslayoutitemnodeitem.h | 8 +- src/core/layout/qgslayoutitempage.cpp | 18 ++--- src/core/layout/qgslayoutitempage.h | 2 +- src/core/layout/qgslayoutitempicture.cpp | 6 +- src/core/layout/qgslayoutitempicture.h | 2 +- src/core/layout/qgslayoutitempolygon.cpp | 10 +-- src/core/layout/qgslayoutitempolygon.h | 2 +- src/core/layout/qgslayoutitempolyline.cpp | 20 ++--- src/core/layout/qgslayoutitempolyline.h | 2 +- src/core/layout/qgslayoutitemscalebar.cpp | 4 +- src/core/layout/qgslayoutitemscalebar.h | 2 +- src/core/layout/qgslayoutitemshape.cpp | 12 +-- src/core/layout/qgslayoutitemshape.h | 2 +- src/core/layout/qgslayoutmultiframe.h | 5 +- src/core/layout/qgslayouttable.cpp | 7 +- src/core/layout/qgslayouttable.h | 3 +- src/core/symbology/qgssymbol.h | 8 ++ .../georeferencer/qgsresidualplotitem.cpp | 2 +- .../georeferencer/qgsresidualplotitem.h | 2 +- tests/src/core/testqgslayoutitem.cpp | 4 +- tests/src/core/testqgslayoutmultiframe.cpp | 3 +- tests/src/gui/testqgslayoutview.cpp | 2 +- 54 files changed, 268 insertions(+), 125 deletions(-) diff --git a/python/core/layout/qgslayoutframe.sip.in b/python/core/layout/qgslayoutframe.sip.in index 7190475c333..b34f5132bb7 100644 --- a/python/core/layout/qgslayoutframe.sip.in +++ b/python/core/layout/qgslayoutframe.sip.in @@ -118,7 +118,7 @@ Returns whether the frame is empty. protected: - virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); + virtual void draw( QgsLayoutItemRenderContext &context ); virtual void drawFrame( QgsRenderContext &context ); diff --git a/python/core/layout/qgslayoutitem.sip.in b/python/core/layout/qgslayoutitem.sip.in index 3712cf9bb54..4e1e8e09735 100644 --- a/python/core/layout/qgslayoutitem.sip.in +++ b/python/core/layout/qgslayoutitem.sip.in @@ -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 { %Docstring - Base class for graphical items within a QgsLayout. + Base class for graphical items within a :py:class:`QgsLayout`. .. versionadded:: 3.0 %End @@ -939,9 +993,10 @@ painter. :param painter: destination QPainter %End - virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; + virtual void draw( QgsLayoutItemRenderContext &context ) = 0; %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. Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units. %End diff --git a/python/core/layout/qgslayoutitemgroup.sip.in b/python/core/layout/qgslayoutitemgroup.sip.in index cecd22c8435..2424049d3e0 100644 --- a/python/core/layout/qgslayoutitemgroup.sip.in +++ b/python/core/layout/qgslayoutitemgroup.sip.in @@ -73,7 +73,7 @@ Returns a list of items contained by the group. 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; diff --git a/python/core/layout/qgslayoutitemhtml.sip.in b/python/core/layout/qgslayoutitemhtml.sip.in index 432eafb53a2..e8d483cc32b 100644 --- a/python/core/layout/qgslayoutitemhtml.sip.in +++ b/python/core/layout/qgslayoutitemhtml.sip.in @@ -222,8 +222,8 @@ Returns whether user stylesheets are enabled for the HTML content. virtual QSizeF totalSize() const; - virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = 0 ); + virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ); + virtual double findNearbyPageBreak( double yPos ); diff --git a/python/core/layout/qgslayoutitemlabel.sip.in b/python/core/layout/qgslayoutitemlabel.sip.in index 4c0113c991d..6e92767ebd7 100644 --- a/python/core/layout/qgslayoutitemlabel.sip.in +++ b/python/core/layout/qgslayoutitemlabel.sip.in @@ -233,7 +233,7 @@ Returns the label font color. 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; diff --git a/python/core/layout/qgslayoutitemlegend.sip.in b/python/core/layout/qgslayoutitemlegend.sip.in index 10062722e95..3dd0c10a686 100644 --- a/python/core/layout/qgslayoutitemlegend.sip.in +++ b/python/core/layout/qgslayoutitemlegend.sip.in @@ -495,7 +495,7 @@ Returns the legend's renderer settings object. 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; diff --git a/python/core/layout/qgslayoutitemmap.sip.in b/python/core/layout/qgslayoutitemmap.sip.in index 3a6a5a1f565..73e3479ff43 100644 --- a/python/core/layout/qgslayoutitemmap.sip.in +++ b/python/core/layout/qgslayoutitemmap.sip.in @@ -472,7 +472,7 @@ will be calculated. This can be expensive to calculate, so if they are not requi 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; diff --git a/python/core/layout/qgslayoutitemnodeitem.sip.in b/python/core/layout/qgslayoutitemnodeitem.sip.in index abec3742bf1..17ef1feb08f 100644 --- a/python/core/layout/qgslayoutitemnodeitem.sip.in +++ b/python/core/layout/qgslayoutitemnodeitem.sip.in @@ -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``. %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; @@ -145,7 +145,7 @@ Method called in addNode. Method called in removeNode. %End - virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; + virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; %Docstring Method called in paint. %End diff --git a/python/core/layout/qgslayoutitempage.sip.in b/python/core/layout/qgslayoutitempage.sip.in index e199465e72f..b826b0a1a5a 100644 --- a/python/core/layout/qgslayoutitempage.sip.in +++ b/python/core/layout/qgslayoutitempage.sip.in @@ -105,7 +105,7 @@ page orientation. protected: - virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); + virtual void draw( QgsLayoutItemRenderContext &context ); virtual void drawFrame( QgsRenderContext &context ); diff --git a/python/core/layout/qgslayoutitempicture.sip.in b/python/core/layout/qgslayoutitempicture.sip.in index aa7476868a1..5816740811b 100644 --- a/python/core/layout/qgslayoutitempicture.sip.in +++ b/python/core/layout/qgslayoutitempicture.sip.in @@ -299,7 +299,7 @@ Is emitted on picture rotation change protected: - virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); + virtual void draw( QgsLayoutItemRenderContext &context ); virtual QSizeF applyItemSizeConstraint( const QSizeF &targetSize ); diff --git a/python/core/layout/qgslayoutitempolygon.sip.in b/python/core/layout/qgslayoutitempolygon.sip.in index cc4508e25c2..5996070476d 100644 --- a/python/core/layout/qgslayoutitempolygon.sip.in +++ b/python/core/layout/qgslayoutitempolygon.sip.in @@ -68,7 +68,7 @@ Ownership of ``symbol`` is not transferred. 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 ); diff --git a/python/core/layout/qgslayoutitempolyline.sip.in b/python/core/layout/qgslayoutitempolyline.sip.in index 4b0035e4e88..c8fb832d3a2 100644 --- a/python/core/layout/qgslayoutitempolyline.sip.in +++ b/python/core/layout/qgslayoutitempolyline.sip.in @@ -215,7 +215,7 @@ Returns the pen width in millimeters for the stroke of the arrow head. 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 ); diff --git a/python/core/layout/qgslayoutitemscalebar.sip.in b/python/core/layout/qgslayoutitemscalebar.sip.in index 6932c17633a..98204f90a0d 100644 --- a/python/core/layout/qgslayoutitemscalebar.sip.in +++ b/python/core/layout/qgslayoutitemscalebar.sip.in @@ -491,7 +491,7 @@ Adjusts the scale bar box size and updates the item. 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; diff --git a/python/core/layout/qgslayoutitemshape.sip.in b/python/core/layout/qgslayoutitemshape.sip.in index 86f02359696..b96ddf4ac33 100644 --- a/python/core/layout/qgslayoutitemshape.sip.in +++ b/python/core/layout/qgslayoutitemshape.sip.in @@ -101,7 +101,7 @@ Returns the corner radius for rounded rectangle corners. 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; diff --git a/python/core/layout/qgslayoutmultiframe.sip.in b/python/core/layout/qgslayoutmultiframe.sip.in index 751ac4ad96e..d5c76cfd875 100644 --- a/python/core/layout/qgslayoutmultiframe.sip.in +++ b/python/core/layout/qgslayoutmultiframe.sip.in @@ -144,15 +144,13 @@ the frame size has no minimum in that direction. .. seealso:: :py:func:`recalculateFrameRects` %End - virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0; + virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) = 0; %Docstring Renders a portion of the multiframe's content into a render ``context``. :param context: destination render painter :param renderExtent: visible extent of content to render into the painter. :param frameIndex: frame number for content -:param itemStyle: item style options for graphics item rendering %End virtual void addFrame( QgsLayoutFrame *frame /Transfer/, bool recalcFrameSizes = true ); diff --git a/python/core/layout/qgslayouttable.sip.in b/python/core/layout/qgslayouttable.sip.in index 94920667ff1..03cdfe498a4 100644 --- a/python/core/layout/qgslayouttable.sip.in +++ b/python/core/layout/qgslayouttable.sip.in @@ -515,8 +515,8 @@ Returns the current contents of the table. Excludes header cells. virtual QSizeF totalSize() const; - virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = 0 ); + virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ); + public slots: diff --git a/python/core/symbology/qgssymbol.sip.in b/python/core/symbology/qgssymbol.sip.in index 4ba821ee553..0bb2351d17b 100644 --- a/python/core/symbology/qgssymbol.sip.in +++ b/python/core/symbology/qgssymbol.sip.in @@ -413,6 +413,10 @@ Constructor for QgsSymbolRenderContext QgsRenderContext &renderContext(); +%Docstring +Returns a reference to the context's render context. +%End + void setOriginalValueVariable( const QVariant &value ); %Docstring diff --git a/src/core/layout/qgslayoutframe.cpp b/src/core/layout/qgslayoutframe.cpp index d9246fc743b..3dbde126582 100644 --- a/src/core/layout/qgslayoutframe.cpp +++ b/src/core/layout/qgslayoutframe.cpp @@ -157,14 +157,14 @@ void QgsLayoutFrame::cleanup() QgsLayoutItem::cleanup(); } -void QgsLayoutFrame::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle ) +void QgsLayoutFrame::draw( QgsLayoutItemRenderContext &context ) { if ( mMultiFrame ) { //calculate index of frame int frameIndex = mMultiFrame->frameIndex( this ); Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" ); - mMultiFrame->render( context, mSection, frameIndex, itemStyle ); + mMultiFrame->render( context, mSection, frameIndex ); } } diff --git a/src/core/layout/qgslayoutframe.h b/src/core/layout/qgslayoutframe.h index 68644bee0af..b2ceedbf010 100644 --- a/src/core/layout/qgslayoutframe.h +++ b/src/core/layout/qgslayoutframe.h @@ -113,7 +113,7 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; void drawFrame( QgsRenderContext &context ) override; void drawBackground( QgsRenderContext &context ) override; bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override; diff --git a/src/core/layout/qgslayoutitem.cpp b/src/core/layout/qgslayoutitem.cpp index a25b6613b22..e79479c78e5 100644 --- a/src/core/layout/qgslayoutitem.cpp +++ b/src/core/layout/qgslayoutitem.cpp @@ -33,6 +33,14 @@ #define CACHE_SIZE_LIMIT 5000 +QgsLayoutItemRenderContext::QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor ) + : mRenderContext( context ) + , mViewScaleFactor( viewScaleFactor ) +{ +} + + + QgsLayoutItem::QgsLayoutItem( QgsLayout *layout, bool manageZValue ) : QgsLayoutObject( layout ) , QGraphicsRectItem( nullptr ) @@ -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) p.translate( -boundingRect().x() * context.scaleFactor(), -boundingRect().y() * context.scaleFactor() ); drawBackground( context ); - draw( context, itemStyle ); + double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ); + QgsLayoutItemRenderContext itemRenderContext( context, viewScale ); + draw( itemRenderContext ); drawFrame( context ); p.end(); @@ -341,7 +351,9 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it // scale painter from mm to dots 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() ); drawFrame( context ); @@ -1427,3 +1439,4 @@ void QgsLayoutItem::refreshBlendMode() // Update the item effect to use the new blend mode mEffect->setCompositionMode( blendMode ); } + diff --git a/src/core/layout/qgslayoutitem.h b/src/core/layout/qgslayoutitem.h index 752de8c4ddf..ae5b868154c 100644 --- a/src/core/layout/qgslayoutitem.h +++ b/src/core/layout/qgslayoutitem.h @@ -33,6 +33,76 @@ class QPainter; class QgsLayoutItemGroup; class QgsLayoutEffect; + +/** + * \ingroup core + * \class QgsLayoutItemRenderContext + * Contains settings and helpers relating to a render of a QgsLayoutItem. + * \since QGIS 3.0 + */ +class CORE_EXPORT QgsLayoutItemRenderContext +{ + public: + + /** + * Constructor for QgsLayoutItemRenderContext. + * + * The \a renderContext parameter specifies a QgsRenderContext for use within + * the QgsLayoutItemRenderContext. + * + * The \a 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. + */ + QgsLayoutItemRenderContext( QgsRenderContext &context, double viewScaleFactor = 1.0 ); + + //! QgsLayoutItemRenderContext cannot be copied. + QgsLayoutItemRenderContext( const QgsLayoutItemRenderContext &other ) = delete; + + //! QgsLayoutItemRenderContext cannot be copied. + QgsLayoutItemRenderContext &operator=( const QgsLayoutItemRenderContext &other ) = delete; + + /** + * 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. + */ + QgsRenderContext &renderContext() { return mRenderContext; } + + /** + * 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. + * + * \note Not available in Python bindings. + */ + const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP + + /** + * 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. + */ + double viewScaleFactor() const { return mViewScaleFactor; } + + private: + +#ifdef SIP_RUN + QgsLayoutItemRenderContext( const QgsLayoutItemRenderContext &rh ) SIP_FORCE; +#endif + + QgsRenderContext &mRenderContext; + double mViewScaleFactor = 1.0; +}; + + /** * \ingroup core * \class QgsLayoutItem @@ -845,11 +915,12 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt virtual void drawDebugRect( QPainter *painter ); /** - * Draws the item's contents using the specified render \a context. + * Draws the item's contents using the specified item render \a 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. */ - virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0; + virtual void draw( QgsLayoutItemRenderContext &context ) = 0; /** * Draws the frame around the item. diff --git a/src/core/layout/qgslayoutitemgroup.cpp b/src/core/layout/qgslayoutitemgroup.cpp index 57416e46ded..aeba7181950 100644 --- a/src/core/layout/qgslayoutitemgroup.cpp +++ b/src/core/layout/qgslayoutitemgroup.cpp @@ -276,7 +276,7 @@ void QgsLayoutItemGroup::paint( QPainter *, const QStyleOptionGraphicsItem *, QW { } -void QgsLayoutItemGroup::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemGroup::draw( QgsLayoutItemRenderContext & ) { // nothing to draw here! } diff --git a/src/core/layout/qgslayoutitemgroup.h b/src/core/layout/qgslayoutitemgroup.h index 0c3d7367da3..3bf0c237c1c 100644 --- a/src/core/layout/qgslayoutitemgroup.h +++ b/src/core/layout/qgslayoutitemgroup.h @@ -77,7 +77,7 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem void finalizeRestoreFromXml() override; protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitemhtml.cpp b/src/core/layout/qgslayoutitemhtml.cpp index fc5e11bc0d2..3a3f7388d20 100644 --- a/src/core/layout/qgslayoutitemhtml.cpp +++ b/src/core/layout/qgslayoutitemhtml.cpp @@ -279,16 +279,15 @@ QSizeF QgsLayoutItemHtml::totalSize() const return mSize; } -void QgsLayoutItemHtml::render( QgsRenderContext &context, const QRectF &renderExtent, const int, - const QStyleOptionGraphicsItem * ) +void QgsLayoutItemHtml::render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, const int ) { if ( !mWebPage ) return; - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); // painter is scaled to dots, so scale back to layout units - painter->scale( context.scaleFactor() / mHtmlUnitsToLayoutUnits, context.scaleFactor() / mHtmlUnitsToLayoutUnits ); + painter->scale( context.renderContext().scaleFactor() / mHtmlUnitsToLayoutUnits, context.renderContext().scaleFactor() / mHtmlUnitsToLayoutUnits ); painter->translate( 0.0, -renderExtent.top() * mHtmlUnitsToLayoutUnits ); mWebPage->mainFrame()->render( painter, QRegion( renderExtent.left(), renderExtent.top() * mHtmlUnitsToLayoutUnits, renderExtent.width() * mHtmlUnitsToLayoutUnits, renderExtent.height() * mHtmlUnitsToLayoutUnits ) ); painter->restore(); diff --git a/src/core/layout/qgslayoutitemhtml.h b/src/core/layout/qgslayoutitemhtml.h index 51b00211a17..96efade3501 100644 --- a/src/core/layout/qgslayoutitemhtml.h +++ b/src/core/layout/qgslayoutitemhtml.h @@ -204,8 +204,7 @@ class CORE_EXPORT QgsLayoutItemHtml: public QgsLayoutMultiFrame QString displayName() const override; QSizeF totalSize() const override; - void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override; //overridden to break frames without dividing lines of text double findNearbyPageBreak( double yPos ) override; diff --git a/src/core/layout/qgslayoutitemlabel.cpp b/src/core/layout/qgslayoutitemlabel.cpp index b5416a9a509..2d525bac6a6 100644 --- a/src/core/layout/qgslayoutitemlabel.cpp +++ b/src/core/layout/qgslayoutitemlabel.cpp @@ -97,13 +97,13 @@ QIcon QgsLayoutItemLabel::icon() const return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemLabel.svg" ) ); } -void QgsLayoutItemLabel::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemLabel::draw( QgsLayoutItemRenderContext &context ) { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); // painter is scaled to dots, so scale back to layout units - painter->scale( context.scaleFactor(), context.scaleFactor() ); + painter->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0; double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth; diff --git a/src/core/layout/qgslayoutitemlabel.h b/src/core/layout/qgslayoutitemlabel.h index c04610bba04..4e2f32a9281 100644 --- a/src/core/layout/qgslayoutitemlabel.h +++ b/src/core/layout/qgslayoutitemlabel.h @@ -218,7 +218,7 @@ class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem void refresh() override; protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitemlegend.cpp b/src/core/layout/qgslayoutitemlegend.cpp index 16f90ba6c14..cba617d29e7 100644 --- a/src/core/layout/qgslayoutitemlegend.cpp +++ b/src/core/layout/qgslayoutitemlegend.cpp @@ -138,13 +138,13 @@ void QgsLayoutItemLegend::refresh() onAtlasFeature(); } -void QgsLayoutItemLegend::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemLegend::draw( QgsLayoutItemRenderContext &context ) { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); // painter is scaled to dots, so scale back to layout units - painter->scale( context.scaleFactor(), context.scaleFactor() ); + painter->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); diff --git a/src/core/layout/qgslayoutitemlegend.h b/src/core/layout/qgslayoutitemlegend.h index 06fc61e50e0..030b0d512bc 100644 --- a/src/core/layout/qgslayoutitemlegend.h +++ b/src/core/layout/qgslayoutitemlegend.h @@ -442,7 +442,7 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem void refreshDataDefinedProperty( const QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override; protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitemmap.cpp b/src/core/layout/qgslayoutitemmap.cpp index ad3157cdc01..d53c8423c87 100644 --- a/src/core/layout/qgslayoutitemmap.cpp +++ b/src/core/layout/qgslayoutitemmap.cpp @@ -509,7 +509,7 @@ QgsLayoutItemMapOverview *QgsLayoutItemMap::overview() return mOverviewStack->overview( 0 ); } -void QgsLayoutItemMap::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemMap::draw( QgsLayoutItemRenderContext & ) { } diff --git a/src/core/layout/qgslayoutitemmap.h b/src/core/layout/qgslayoutitemmap.h index 80ab523db99..11c37198bae 100644 --- a/src/core/layout/qgslayoutitemmap.h +++ b/src/core/layout/qgslayoutitemmap.h @@ -419,7 +419,7 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitemnodeitem.cpp b/src/core/layout/qgslayoutitemnodeitem.cpp index ecc191e0c8f..4041bcdd7cf 100644 --- a/src/core/layout/qgslayoutitemnodeitem.cpp +++ b/src/core/layout/qgslayoutitemnodeitem.cpp @@ -70,18 +70,18 @@ void QgsLayoutNodesItem::init() connect( this, &QgsLayoutNodesItem::sizePositionChanged, this, &QgsLayoutNodesItem::updateBoundingRect ); } -void QgsLayoutNodesItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *style ) +void QgsLayoutNodesItem::draw( QgsLayoutItemRenderContext &context ) { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->setPen( Qt::NoPen ); painter->setBrush( Qt::NoBrush ); - context.setForceVectorOutput( true ); + context.renderContext().setForceVectorOutput( true ); rescaleToFitBoundingBox(); _draw( context ); if ( mDrawNodes && layout()->renderContext().isPreviewRender() ) - drawNodes( context, style ); + drawNodes( context ); } double QgsLayoutNodesItem::computeDistance( QPointF pt1, @@ -161,11 +161,11 @@ bool QgsLayoutNodesItem::addNode( QPointF pt, return rc; } -void QgsLayoutNodesItem::drawNodes( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle ) const +void QgsLayoutNodesItem::drawNodes( QgsLayoutItemRenderContext &context ) const { - context.painter()->setRenderHint( QPainter::Antialiasing, false ); + context.renderContext().painter()->setRenderHint( QPainter::Antialiasing, false ); - double rectSize = 9.0 / QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ); + double rectSize = 9.0 / context.viewScaleFactor(); QgsStringMap properties; properties.insert( QStringLiteral( "name" ), QStringLiteral( "cross" ) ); @@ -176,18 +176,18 @@ void QgsLayoutNodesItem::drawNodes( QgsRenderContext &context, const QStyleOptio symbol->setSize( rectSize ); symbol->setAngle( 45 ); - symbol->startRender( context ); + symbol->startRender( context.renderContext() ); for ( QPointF pt : mPolygon ) - symbol->renderPoint( pt * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ), nullptr, context ); - symbol->stopRender( context ); + symbol->renderPoint( pt * context.viewScaleFactor(), nullptr, context.renderContext() ); + symbol->stopRender( context.renderContext() ); if ( mSelectedNode >= 0 && mSelectedNode < mPolygon.size() ) - drawSelectedNode( context, itemStyle ); + drawSelectedNode( context ); } -void QgsLayoutNodesItem::drawSelectedNode( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle ) const +void QgsLayoutNodesItem::drawSelectedNode( QgsLayoutItemRenderContext &context ) const { - double rectSize = 9.0 / QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ); + double rectSize = 9.0 / context.viewScaleFactor(); QgsStringMap properties; properties.insert( QStringLiteral( "name" ), QStringLiteral( "square" ) ); @@ -199,9 +199,9 @@ void QgsLayoutNodesItem::drawSelectedNode( QgsRenderContext &context, const QSty symbol.reset( QgsMarkerSymbol::createSimple( properties ) ); symbol->setSize( rectSize ); - symbol->startRender( context ); - symbol->renderPoint( mPolygon.at( mSelectedNode ) * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ), nullptr, context ); - symbol->stopRender( context ); + symbol->startRender( context.renderContext() ); + symbol->renderPoint( mPolygon.at( mSelectedNode ) * context.viewScaleFactor(), nullptr, context.renderContext() ); + symbol->stopRender( context.renderContext() ); } int QgsLayoutNodesItem::nodeAtPosition( QPointF node, diff --git a/src/core/layout/qgslayoutitemnodeitem.h b/src/core/layout/qgslayoutitemnodeitem.h index c76a67c872f..9a58417741a 100644 --- a/src/core/layout/qgslayoutitemnodeitem.h +++ b/src/core/layout/qgslayoutitemnodeitem.h @@ -126,7 +126,7 @@ class CORE_EXPORT QgsLayoutNodesItem: public QgsLayoutItem */ QgsLayoutNodesItem( const QPolygonF &polygon, QgsLayout *layout ); - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; @@ -144,7 +144,7 @@ class CORE_EXPORT QgsLayoutNodesItem: public QgsLayoutItem virtual bool _removeNode( const int nodeIndex ) = 0; //! Method called in paint. - virtual void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0; + virtual void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0; //! Method called in readXml. virtual void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) = 0; @@ -188,8 +188,8 @@ class CORE_EXPORT QgsLayoutNodesItem: public QgsLayoutItem bool mDrawNodes = false; //! Draw nodes - void drawNodes( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) const; - void drawSelectedNode( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) const; + void drawNodes( QgsLayoutItemRenderContext &context ) const; + void drawSelectedNode( QgsLayoutItemRenderContext &context ) const; }; diff --git a/src/core/layout/qgslayoutitempage.cpp b/src/core/layout/qgslayoutitempage.cpp index cfdac2933da..55d781ff522 100644 --- a/src/core/layout/qgslayoutitempage.cpp +++ b/src/core/layout/qgslayoutitempage.cpp @@ -181,19 +181,19 @@ void QgsLayoutItemPage::redraw() mGrid->update(); } -void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemPage::draw( QgsLayoutItemRenderContext &context ) { - if ( !context.painter() || !mLayout || !mLayout->renderContext().pagesVisible() ) + if ( !context.renderContext().painter() || !mLayout || !mLayout->renderContext().pagesVisible() ) { return; } - double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); + double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); QgsExpressionContext expressionContext = createExpressionContext(); - context.setExpressionContext( expressionContext ); + context.renderContext().setExpressionContext( expressionContext ); - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); if ( mLayout->renderContext().isPreviewRender() ) @@ -220,10 +220,10 @@ void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraph } std::unique_ptr< QgsFillSymbol > symbol( mLayout->pageCollection()->pageStyleSymbol()->clone() ); - symbol->startRender( context ); + symbol->startRender( context.renderContext() ); //get max bleed from symbol - double maxBleedPixels = QgsSymbolLayerUtils::estimateMaxSymbolBleed( symbol.get(), context ); + double maxBleedPixels = QgsSymbolLayerUtils::estimateMaxSymbolBleed( symbol.get(), context.renderContext() ); //Now subtract 1 pixel to prevent semi-transparent borders at edge of solid page caused by //anti-aliased painting. This may cause a pixel to be cropped from certain edge lines/symbols, @@ -238,8 +238,8 @@ void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraph std::ceil( rect().width() * scale ) - 2 * maxBleedPixels, std::ceil( rect().height() * scale ) - 2 * maxBleedPixels ) ); QList rings; //empty list - symbol->renderPolygon( pagePolygon, &rings, nullptr, context ); - symbol->stopRender( context ); + symbol->renderPolygon( pagePolygon, &rings, nullptr, context.renderContext() ); + symbol->stopRender( context.renderContext() ); painter->restore(); } diff --git a/src/core/layout/qgslayoutitempage.h b/src/core/layout/qgslayoutitempage.h index 86bf7aeb3e1..349231e65af 100644 --- a/src/core/layout/qgslayoutitempage.h +++ b/src/core/layout/qgslayoutitempage.h @@ -131,7 +131,7 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; void drawFrame( QgsRenderContext &context ) override; void drawBackground( QgsRenderContext &context ) override; diff --git a/src/core/layout/qgslayoutitempicture.cpp b/src/core/layout/qgslayoutitempicture.cpp index 474b15232fa..d7c6bebb842 100644 --- a/src/core/layout/qgslayoutitempicture.cpp +++ b/src/core/layout/qgslayoutitempicture.cpp @@ -80,12 +80,12 @@ QgsLayoutItemPicture *QgsLayoutItemPicture::create( QgsLayout *layout ) return new QgsLayoutItemPicture( layout ); } -void QgsLayoutItemPicture::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemPicture::draw( QgsLayoutItemRenderContext &context ) { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); // painter is scaled to dots, so scale back to layout units - painter->scale( context.scaleFactor(), context.scaleFactor() ); + painter->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); //picture resizing if ( mMode != FormatUnknown ) diff --git a/src/core/layout/qgslayoutitempicture.h b/src/core/layout/qgslayoutitempicture.h index c310553771d..970b273d0f4 100644 --- a/src/core/layout/qgslayoutitempicture.h +++ b/src/core/layout/qgslayoutitempicture.h @@ -268,7 +268,7 @@ class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; QSizeF applyItemSizeConstraint( const QSizeF &targetSize ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitempolygon.cpp b/src/core/layout/qgslayoutitempolygon.cpp index 3cee18d164e..a2fd092fe42 100644 --- a/src/core/layout/qgslayoutitempolygon.cpp +++ b/src/core/layout/qgslayoutitempolygon.cpp @@ -97,20 +97,20 @@ QString QgsLayoutItemPolygon::displayName() const return tr( "" ); } -void QgsLayoutItemPolygon::_draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemPolygon::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * ) { //setup painter scaling to dots so that raster symbology is drawn to scale - double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); + double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); QTransform t = QTransform::fromScale( scale, scale ); QList rings; //empty QPainterPath polygonPath; polygonPath.addPolygon( mPolygon ); - mPolygonStyleSymbol->startRender( context ); + mPolygonStyleSymbol->startRender( context.renderContext() ); mPolygonStyleSymbol->renderPolygon( polygonPath.toFillPolygon( t ), &rings, - nullptr, context ); - mPolygonStyleSymbol->stopRender( context ); + nullptr, context.renderContext() ); + mPolygonStyleSymbol->stopRender( context.renderContext() ); } void QgsLayoutItemPolygon::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) diff --git a/src/core/layout/qgslayoutitempolygon.h b/src/core/layout/qgslayoutitempolygon.h index cd1ea6af338..f7bdca635eb 100644 --- a/src/core/layout/qgslayoutitempolygon.h +++ b/src/core/layout/qgslayoutitempolygon.h @@ -71,7 +71,7 @@ class CORE_EXPORT QgsLayoutItemPolygon: public QgsLayoutNodesItem protected: bool _addNode( const int indexPoint, QPointF newPoint, const double radius ) override; bool _removeNode( const int nodeIndex ) override; - void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) override; void _writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const override; diff --git a/src/core/layout/qgslayoutitempolyline.cpp b/src/core/layout/qgslayoutitempolyline.cpp index f72516bde5b..7d74b893cf6 100644 --- a/src/core/layout/qgslayoutitempolyline.cpp +++ b/src/core/layout/qgslayoutitempolyline.cpp @@ -263,23 +263,23 @@ QString QgsLayoutItemPolyline::displayName() const return tr( "" ); } -void QgsLayoutItemPolyline::_draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemPolyline::_draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem * ) { - context.painter()->save(); + context.renderContext().painter()->save(); //setup painter scaling to dots so that raster symbology is drawn to scale - double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); + double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); QTransform t = QTransform::fromScale( scale, scale ); - mPolylineStyleSymbol->startRender( context ); - mPolylineStyleSymbol->renderPolyline( t.map( mPolygon ), nullptr, context ); - mPolylineStyleSymbol->stopRender( context ); + mPolylineStyleSymbol->startRender( context.renderContext() ); + mPolylineStyleSymbol->renderPolyline( t.map( mPolygon ), nullptr, context.renderContext() ); + mPolylineStyleSymbol->stopRender( context.renderContext() ); // painter is scaled to dots, so scale back to layout units - context.painter()->scale( context.scaleFactor(), context.scaleFactor() ); + context.renderContext().painter()->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); - drawStartMarker( context.painter() ); - drawEndMarker( context.painter() ); - context.painter()->restore(); + drawStartMarker( context.renderContext().painter() ); + drawEndMarker( context.renderContext().painter() ); + context.renderContext().painter()->restore(); } void QgsLayoutItemPolyline::_readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) diff --git a/src/core/layout/qgslayoutitempolyline.h b/src/core/layout/qgslayoutitempolyline.h index 31141bc1166..ce409fbf1e6 100644 --- a/src/core/layout/qgslayoutitempolyline.h +++ b/src/core/layout/qgslayoutitempolyline.h @@ -189,7 +189,7 @@ class CORE_EXPORT QgsLayoutItemPolyline: public QgsLayoutNodesItem bool _addNode( const int indexPoint, QPointF newPoint, const double radius ) override; bool _removeNode( const int nodeIndex ) override; - void _draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void _draw( QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; void _readXmlStyle( const QDomElement &elmt, const QgsReadWriteContext &context ) override; void _writeXmlStyle( QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context ) const override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; diff --git a/src/core/layout/qgslayoutitemscalebar.cpp b/src/core/layout/qgslayoutitemscalebar.cpp index 033890580f4..db5401925d3 100644 --- a/src/core/layout/qgslayoutitemscalebar.cpp +++ b/src/core/layout/qgslayoutitemscalebar.cpp @@ -67,12 +67,12 @@ QgsLayoutSize QgsLayoutItemScaleBar::minimumSize() const return QgsLayoutSize( mStyle->calculateBoxSize( mSettings, createScaleContext() ), QgsUnitTypes::LayoutMillimeters ); } -void QgsLayoutItemScaleBar::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemScaleBar::draw( QgsLayoutItemRenderContext &context ) { if ( !mStyle ) return; - mStyle->draw( context, mSettings, createScaleContext() ); + mStyle->draw( context.renderContext(), mSettings, createScaleContext() ); } void QgsLayoutItemScaleBar::setNumberOfSegments( int nSegments ) diff --git a/src/core/layout/qgslayoutitemscalebar.h b/src/core/layout/qgslayoutitemscalebar.h index d4dccdcd621..7a6fd4fd26b 100644 --- a/src/core/layout/qgslayoutitemscalebar.h +++ b/src/core/layout/qgslayoutitemscalebar.h @@ -425,7 +425,7 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem void finalizeRestoreFromXml() override; protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutitemshape.cpp b/src/core/layout/qgslayoutitemshape.cpp index 12540d73933..b190362b03f 100644 --- a/src/core/layout/qgslayoutitemshape.cpp +++ b/src/core/layout/qgslayoutitemshape.cpp @@ -150,13 +150,13 @@ double QgsLayoutItemShape::estimatedFrameBleed() const return mMaxSymbolBleed; } -void QgsLayoutItemShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) +void QgsLayoutItemShape::draw( QgsLayoutItemRenderContext &context ) { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->setPen( Qt::NoPen ); painter->setBrush( Qt::NoBrush ); - double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); + double scale = context.renderContext().convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters ); QPolygonF shapePolygon; @@ -206,9 +206,9 @@ void QgsLayoutItemShape::draw( QgsRenderContext &context, const QStyleOptionGrap QList rings; //empty list - symbol()->startRender( context ); - symbol()->renderPolygon( shapePolygon, &rings, nullptr, context ); - symbol()->stopRender( context ); + symbol()->startRender( context.renderContext() ); + symbol()->renderPolygon( shapePolygon, &rings, nullptr, context.renderContext() ); + symbol()->stopRender( context.renderContext() ); } bool QgsLayoutItemShape::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const diff --git a/src/core/layout/qgslayoutitemshape.h b/src/core/layout/qgslayoutitemshape.h index 6fee0487cd7..86a7fe66258 100644 --- a/src/core/layout/qgslayoutitemshape.h +++ b/src/core/layout/qgslayoutitemshape.h @@ -109,7 +109,7 @@ class CORE_EXPORT QgsLayoutItemShape : public QgsLayoutItem protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override; diff --git a/src/core/layout/qgslayoutmultiframe.h b/src/core/layout/qgslayoutmultiframe.h index e5192843e08..d680bb8701c 100644 --- a/src/core/layout/qgslayoutmultiframe.h +++ b/src/core/layout/qgslayoutmultiframe.h @@ -34,6 +34,7 @@ class QRectF; class QPainter; class QStyleOptionGraphicsItem; class QgsRenderContext; +class QgsLayoutItemRenderContext; /** * \ingroup core @@ -175,10 +176,8 @@ class CORE_EXPORT QgsLayoutMultiFrame: public QgsLayoutObject, public QgsLayoutU * \param context destination render painter * \param renderExtent visible extent of content to render into the painter. * \param frameIndex frame number for content - * \param itemStyle item style options for graphics item rendering */ - virtual void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0; + virtual void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) = 0; /** * Adds a \a frame to the multiframe. diff --git a/src/core/layout/qgslayouttable.cpp b/src/core/layout/qgslayouttable.cpp index 727db54b4e8..7ec1d92fb8c 100644 --- a/src/core/layout/qgslayouttable.cpp +++ b/src/core/layout/qgslayouttable.cpp @@ -279,8 +279,7 @@ QPair QgsLayoutTable::rowRange( const int frameIndex ) const return qMakePair( firstVisible, lastVisible ); } -void QgsLayoutTable::render( QgsRenderContext &context, const QRectF &, const int frameIndex, - const QStyleOptionGraphicsItem * ) +void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &, const int frameIndex ) { bool emptyTable = mTableContents.length() == 0; if ( emptyTable && mEmptyTableMode == QgsLayoutTable::HideTable ) @@ -327,10 +326,10 @@ void QgsLayoutTable::render( QgsRenderContext &context, const QRectF &, const in mergeCells = true; } - QPainter *p = context.painter(); + QPainter *p = context.renderContext().painter(); p->save(); // painter is scaled to dots, so scale back to layout units - p->scale( context.scaleFactor(), context.scaleFactor() ); + p->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); //draw the text p->setPen( Qt::SolidLine ); diff --git a/src/core/layout/qgslayouttable.h b/src/core/layout/qgslayouttable.h index 328297ec11a..a6b4e5f0dce 100644 --- a/src/core/layout/qgslayouttable.h +++ b/src/core/layout/qgslayouttable.h @@ -477,8 +477,7 @@ class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override; bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override; QSizeF totalSize() const override; - void render( QgsRenderContext &context, const QRectF &renderExtent, int frameIndex, - const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override; public slots: diff --git a/src/core/symbology/qgssymbol.h b/src/core/symbology/qgssymbol.h index 8159696969e..493b3a6bc4a 100644 --- a/src/core/symbology/qgssymbol.h +++ b/src/core/symbology/qgssymbol.h @@ -447,7 +447,15 @@ class CORE_EXPORT QgsSymbolRenderContext //! QgsSymbolRenderContext cannot be copied. QgsSymbolRenderContext( const QgsSymbolRenderContext &rh ) = delete; + /** + * Returns a reference to the context's render context. + */ QgsRenderContext &renderContext() { return mRenderContext; } + + /** + * Returns a reference to the context's render context. + * \note Not available in Python bindings. + */ const QgsRenderContext &renderContext() const { return mRenderContext; } SIP_SKIP /** diff --git a/src/plugins/georeferencer/qgsresidualplotitem.cpp b/src/plugins/georeferencer/qgsresidualplotitem.cpp index 13daab1ec33..86cdc059c66 100644 --- a/src/plugins/georeferencer/qgsresidualplotitem.cpp +++ b/src/plugins/georeferencer/qgsresidualplotitem.cpp @@ -150,7 +150,7 @@ void QgsResidualPlotItem::paint( QPainter *painter, const QStyleOptionGraphicsIt } } -void QgsResidualPlotItem::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * ) +void QgsResidualPlotItem::draw( QgsLayoutItemRenderContext & ) { } diff --git a/src/plugins/georeferencer/qgsresidualplotitem.h b/src/plugins/georeferencer/qgsresidualplotitem.h index c0b41e619d0..c28b171e6ee 100644 --- a/src/plugins/georeferencer/qgsresidualplotitem.h +++ b/src/plugins/georeferencer/qgsresidualplotitem.h @@ -42,7 +42,7 @@ class QgsResidualPlotItem: public QgsLayoutItem void setConvertScaleToMapUnits( bool convert ) { mConvertScaleToMapUnits = convert; } bool convertScaleToMapUnits() const { return mConvertScaleToMapUnits; } - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; + void draw( QgsLayoutItemRenderContext &context ) override; private: //gcp list QgsGCPList mGCPList; diff --git a/tests/src/core/testqgslayoutitem.cpp b/tests/src/core/testqgslayoutitem.cpp index 3aeffac957b..667b7def390 100644 --- a/tests/src/core/testqgslayoutitem.cpp +++ b/tests/src/core/testqgslayoutitem.cpp @@ -54,9 +54,9 @@ class TestItem : public QgsLayoutItem bool forceResize = false; protected: - void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * = nullptr ) override + void draw( QgsLayoutItemRenderContext &context ) override { - QPainter *painter = context.painter(); + QPainter *painter = context.renderContext().painter(); painter->save(); painter->setRenderHint( QPainter::Antialiasing, false ); painter->setPen( Qt::NoPen ); diff --git a/tests/src/core/testqgslayoutmultiframe.cpp b/tests/src/core/testqgslayoutmultiframe.cpp index a1e1ed03a69..348c1fbe7db 100644 --- a/tests/src/core/testqgslayoutmultiframe.cpp +++ b/tests/src/core/testqgslayoutmultiframe.cpp @@ -74,8 +74,7 @@ class TestMultiFrame : public QgsLayoutMultiFrame return QgsLayoutItemRegistry::PluginItem + 1; } - void render( QgsRenderContext &, const QRectF &, int, - const QStyleOptionGraphicsItem * ) override + void render( QgsLayoutItemRenderContext &, const QRectF &, int ) override { } diff --git a/tests/src/gui/testqgslayoutview.cpp b/tests/src/gui/testqgslayoutview.cpp index 1c27351f774..6e34c0f30d3 100644 --- a/tests/src/gui/testqgslayoutview.cpp +++ b/tests/src/gui/testqgslayoutview.cpp @@ -247,7 +247,7 @@ class TestItem : public QgsLayoutItem //implement pure virtual methods int type() const override { return QgsLayoutItemRegistry::LayoutItem + 101; } - void draw( QgsRenderContext &, const QStyleOptionGraphicsItem * = nullptr ) override + void draw( QgsLayoutItemRenderContext & ) override { } };