Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure that item painter is correctly scaled so that painter units ar…
…e pixels
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent 56bb657 commit 436710a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -173,6 +173,8 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ) = 0;
%Docstring
Draws the item's contents using the specified 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

virtual void setFixedSize( const QgsLayoutSize &size );
Expand Down
8 changes: 7 additions & 1 deletion src/core/layout/qgslayoutitem.cpp
Expand Up @@ -18,11 +18,14 @@
#include "qgslayout.h"
#include "qgslayoututils.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>

QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
: QgsLayoutObject( layout )
, QGraphicsRectItem( 0 )
{
// needed to access current view transform during paint operations
setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
setCacheMode( QGraphicsItem::DeviceCoordinateCache );

//record initial position
Expand Down Expand Up @@ -50,7 +53,10 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
}
else
{
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, painter );
double destinationDpi = itemStyle->matrix.m11() * 25.4;
QgsRenderContext context = QgsLayoutUtils::createRenderContextForMap( nullptr, painter, destinationDpi );
// scale painter from mm to dots
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
draw( context, itemStyle );
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -186,6 +186,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt

/**
* Draws the item's contents using the specified 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;

Expand Down
15 changes: 14 additions & 1 deletion src/core/layout/qgslayoutitemregistry.cpp
Expand Up @@ -15,6 +15,8 @@
***************************************************************************/

#include "qgslayoutitemregistry.h"
#include "qgsgloweffect.h"
#include "qgseffectstack.h"
#include <QPainter>

QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent )
Expand Down Expand Up @@ -98,13 +100,24 @@ TestLayoutItem::TestLayoutItem( QgsLayout *layout )
void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
{
Q_UNUSED( itemStyle );

QgsEffectStack stack;
stack.appendEffect( new QgsDrawSourceEffect() );
stack.appendEffect( new QgsInnerGlowEffect() );
stack.begin( context );

QPainter *painter = context.painter();

painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->setPen( Qt::NoPen );
painter->setBrush( mColor );
painter->drawRect( rect() );

double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
QRectF r = QRectF( rect().left() * scale, rect().top() * scale,
rect().width() * scale, rect().height() * scale );
painter->drawRect( r );
painter->restore();
stack.end( context );
}
///@endcond

0 comments on commit 436710a

Please sign in to comment.