Skip to content
Permalink
Browse files

[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.
  • Loading branch information
nyalldawson committed Feb 19, 2018
1 parent 9911209 commit 0e3af9c6a94f48cc21e417b8e892f44ad565f0b7
Showing with 268 additions and 125 deletions.
  1. +1 −1 python/core/layout/qgslayoutframe.sip.in
  2. +58 −3 python/core/layout/qgslayoutitem.sip.in
  3. +1 −1 python/core/layout/qgslayoutitemgroup.sip.in
  4. +2 −2 python/core/layout/qgslayoutitemhtml.sip.in
  5. +1 −1 python/core/layout/qgslayoutitemlabel.sip.in
  6. +1 −1 python/core/layout/qgslayoutitemlegend.sip.in
  7. +1 −1 python/core/layout/qgslayoutitemmap.sip.in
  8. +2 −2 python/core/layout/qgslayoutitemnodeitem.sip.in
  9. +1 −1 python/core/layout/qgslayoutitempage.sip.in
  10. +1 −1 python/core/layout/qgslayoutitempicture.sip.in
  11. +1 −1 python/core/layout/qgslayoutitempolygon.sip.in
  12. +1 −1 python/core/layout/qgslayoutitempolyline.sip.in
  13. +1 −1 python/core/layout/qgslayoutitemscalebar.sip.in
  14. +1 −1 python/core/layout/qgslayoutitemshape.sip.in
  15. +1 −3 python/core/layout/qgslayoutmultiframe.sip.in
  16. +2 −2 python/core/layout/qgslayouttable.sip.in
  17. +4 −0 python/core/symbology/qgssymbol.sip.in
  18. +2 −2 src/core/layout/qgslayoutframe.cpp
  19. +1 −1 src/core/layout/qgslayoutframe.h
  20. +15 −2 src/core/layout/qgslayoutitem.cpp
  21. +73 −2 src/core/layout/qgslayoutitem.h
  22. +1 −1 src/core/layout/qgslayoutitemgroup.cpp
  23. +1 −1 src/core/layout/qgslayoutitemgroup.h
  24. +3 −4 src/core/layout/qgslayoutitemhtml.cpp
  25. +1 −2 src/core/layout/qgslayoutitemhtml.h
  26. +3 −3 src/core/layout/qgslayoutitemlabel.cpp
  27. +1 −1 src/core/layout/qgslayoutitemlabel.h
  28. +3 −3 src/core/layout/qgslayoutitemlegend.cpp
  29. +1 −1 src/core/layout/qgslayoutitemlegend.h
  30. +1 −1 src/core/layout/qgslayoutitemmap.cpp
  31. +1 −1 src/core/layout/qgslayoutitemmap.h
  32. +16 −16 src/core/layout/qgslayoutitemnodeitem.cpp
  33. +4 −4 src/core/layout/qgslayoutitemnodeitem.h
  34. +9 −9 src/core/layout/qgslayoutitempage.cpp
  35. +1 −1 src/core/layout/qgslayoutitempage.h
  36. +3 −3 src/core/layout/qgslayoutitempicture.cpp
  37. +1 −1 src/core/layout/qgslayoutitempicture.h
  38. +5 −5 src/core/layout/qgslayoutitempolygon.cpp
  39. +1 −1 src/core/layout/qgslayoutitempolygon.h
  40. +10 −10 src/core/layout/qgslayoutitempolyline.cpp
  41. +1 −1 src/core/layout/qgslayoutitempolyline.h
  42. +2 −2 src/core/layout/qgslayoutitemscalebar.cpp
  43. +1 −1 src/core/layout/qgslayoutitemscalebar.h
  44. +6 −6 src/core/layout/qgslayoutitemshape.cpp
  45. +1 −1 src/core/layout/qgslayoutitemshape.h
  46. +2 −3 src/core/layout/qgslayoutmultiframe.h
  47. +3 −4 src/core/layout/qgslayouttable.cpp
  48. +1 −2 src/core/layout/qgslayouttable.h
  49. +8 −0 src/core/symbology/qgssymbol.h
  50. +1 −1 src/plugins/georeferencer/qgsresidualplotitem.cpp
  51. +1 −1 src/plugins/georeferencer/qgsresidualplotitem.h
  52. +2 −2 tests/src/core/testqgslayoutitem.cpp
  53. +1 −2 tests/src/core/testqgslayoutmultiframe.cpp
  54. +1 −1 tests/src/gui/testqgslayoutview.cpp
@@ -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 );

@@ -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
@@ -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;

@@ -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 );

@@ -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;

@@ -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;

@@ -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;

@@ -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
@@ -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 );

@@ -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 );

@@ -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 );

@@ -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 );

@@ -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;

@@ -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;
@@ -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 );
@@ -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:

@@ -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
@@ -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 );
}
}

@@ -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;
@@ -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 );
}

0 comments on commit 0e3af9c

Please sign in to comment.
You can’t perform that action at this time.