Skip to content

Commit 436710a

Browse files
committed
Ensure that item painter is correctly scaled so that painter units are pixels
1 parent 56bb657 commit 436710a

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

python/core/layout/qgslayoutitem.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
173173
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
174174
%Docstring
175175
Draws the item's contents using the specified render ``context``.
176+
Note that the context's painter has been scaled so that painter units are pixels.
177+
Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
176178
%End
177179

178180
virtual void setFixedSize( const QgsLayoutSize &size );

src/core/layout/qgslayoutitem.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
#include "qgslayout.h"
1919
#include "qgslayoututils.h"
2020
#include <QPainter>
21+
#include <QStyleOptionGraphicsItem>
2122

2223
QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
2324
: QgsLayoutObject( layout )
2425
, QGraphicsRectItem( 0 )
2526
{
27+
// needed to access current view transform during paint operations
28+
setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
2629
setCacheMode( QGraphicsItem::DeviceCoordinateCache );
2730

2831
//record initial position
@@ -50,7 +53,10 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
5053
}
5154
else
5255
{
53-
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, painter );
56+
double destinationDpi = itemStyle->matrix.m11() * 25.4;
57+
QgsRenderContext context = QgsLayoutUtils::createRenderContextForMap( nullptr, painter, destinationDpi );
58+
// scale painter from mm to dots
59+
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
5460
draw( context, itemStyle );
5561
}
5662

src/core/layout/qgslayoutitem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
186186

187187
/**
188188
* Draws the item's contents using the specified render \a context.
189+
* Note that the context's painter has been scaled so that painter units are pixels.
190+
* Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.
189191
*/
190192
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) = 0;
191193

src/core/layout/qgslayoutitemregistry.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
***************************************************************************/
1616

1717
#include "qgslayoutitemregistry.h"
18+
#include "qgsgloweffect.h"
19+
#include "qgseffectstack.h"
1820
#include <QPainter>
1921

2022
QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent )
@@ -98,13 +100,24 @@ TestLayoutItem::TestLayoutItem( QgsLayout *layout )
98100
void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
99101
{
100102
Q_UNUSED( itemStyle );
103+
104+
QgsEffectStack stack;
105+
stack.appendEffect( new QgsDrawSourceEffect() );
106+
stack.appendEffect( new QgsInnerGlowEffect() );
107+
stack.begin( context );
108+
101109
QPainter *painter = context.painter();
102110

103111
painter->save();
104112
painter->setRenderHint( QPainter::Antialiasing, false );
105113
painter->setPen( Qt::NoPen );
106114
painter->setBrush( mColor );
107-
painter->drawRect( rect() );
115+
116+
double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
117+
QRectF r = QRectF( rect().left() * scale, rect().top() * scale,
118+
rect().width() * scale, rect().height() * scale );
119+
painter->drawRect( r );
108120
painter->restore();
121+
stack.end( context );
109122
}
110123
///@endcond

0 commit comments

Comments
 (0)