Skip to content
Permalink
Browse files

Ensure that item painter is correctly scaled so that painter units ar…

…e pixels
  • Loading branch information
nyalldawson committed Jul 14, 2017
1 parent 56bb657 commit 436710a177dcac783a7d6bf904cb73fe232299e3
@@ -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 );
@@ -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
@@ -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 );
}

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

@@ -15,6 +15,8 @@
***************************************************************************/

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

QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent )
@@ -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.
You can’t perform that action at this time.