Skip to content

Commit 534f7ab

Browse files
committedJul 25, 2017
wip page rendering
1 parent 9df4a67 commit 534f7ab

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed
 

‎src/core/layout/qgslayout.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ QgsLayout::QgsLayout( QgsProject *project )
2121
: QGraphicsScene()
2222
, mProject( project )
2323
, mPageCollection( new QgsLayoutPageCollection( this ) )
24-
{}
24+
{
25+
setBackgroundBrush( QColor( 215, 215, 215 ) );
26+
}
2527

2628
QgsProject *QgsLayout::project() const
2729
{

‎src/core/layout/qgslayoutitem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
7878
}
7979

8080
double destinationDpi = itemStyle->matrix.m11() * 25.4;
81-
bool useImageCache = true;
81+
bool useImageCache = false;
8282

8383
if ( useImageCache )
8484
{

‎src/core/layout/qgslayoutitempage.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,34 @@
1717
#include "qgslayoutitempage.h"
1818
#include "qgslayout.h"
1919
#include "qgslayoututils.h"
20+
#include "qgssymbollayerutils.h"
2021
#include <QPainter>
2122

23+
#define SHADOW_WIDTH_PIXELS 5
2224
QgsLayoutItemPage::QgsLayoutItemPage( QgsLayout *layout )
2325
: QgsLayoutItem( layout )
2426
{
2527

2628
}
2729

28-
void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
30+
void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
2931
{
30-
#if 0
31-
Q_UNUSED( itemStyle );
32-
Q_UNUSED( pWidget );
33-
if ( !painter || !mComposition || !mComposition->pagesVisible() )
32+
if ( !context.painter() || !mLayout /*|| !mLayout->pagesVisible() */ )
3433
{
3534
return;
3635
}
3736

38-
//setup painter scaling to dots so that raster symbology is drawn to scale
39-
double dotsPerMM = painter->device()->logicalDpiX() / 25.4;
40-
41-
//setup render context
42-
QgsRenderContext context = QgsComposerUtils::createRenderContextForComposition( mComposition, painter );
43-
context.setForceVectorOutput( true );
37+
double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
4438

4539
QgsExpressionContext expressionContext = createExpressionContext();
4640
context.setExpressionContext( expressionContext );
4741

42+
QPainter *painter = context.painter();
4843
painter->save();
4944

45+
#if 0 //TODO
5046
if ( mComposition->plotStyle() == QgsComposition::Preview )
47+
#endif
5148
{
5249
//if in preview mode, draw page border and shadow so that it's
5350
//still possible to tell where pages with a transparent style begin and end
@@ -56,28 +53,33 @@ void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraph
5653
//shadow
5754
painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
5855
painter->setPen( Qt::NoPen );
59-
painter->drawRect( QRectF( 1, 1, rect().width() + 1, rect().height() + 1 ) );
56+
painter->drawRect( QRectF( SHADOW_WIDTH_PIXELS, SHADOW_WIDTH_PIXELS, rect().width() * scale + SHADOW_WIDTH_PIXELS, rect().height() * scale + SHADOW_WIDTH_PIXELS ) );
6057

6158
//page area
6259
painter->setBrush( QColor( 215, 215, 215 ) );
6360
QPen pagePen = QPen( QColor( 100, 100, 100 ), 0 );
6461
pagePen.setCosmetic( true );
6562
painter->setPen( pagePen );
66-
painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
63+
painter->drawRect( QRectF( 0, 0, scale * rect().width(), scale * rect().height() ) );
6764
}
6865

69-
painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
66+
std::unique_ptr< QgsFillSymbol > symbol( mLayout->pageCollection()->pageStyleSymbol()->clone() );
67+
symbol->startRender( context );
68+
69+
//get max bleed from symbol
70+
double maxBleedPixels = QgsSymbolLayerUtils::estimateMaxSymbolBleed( symbol.get(), context );
7071

71-
painter->setRenderHint( QPainter::Antialiasing );
72-
mComposition->pageStyleSymbol()->startRender( context );
72+
//Now subtract 1 pixel to prevent semi-transparent borders at edge of solid page caused by
73+
//anti-aliased painting. This may cause a pixel to be cropped from certain edge lines/symbols,
74+
//but that can be counteracted by adding a dummy transparent line symbol layer with a wider line width
75+
maxBleedPixels--;
7376

74-
calculatePageMargin();
75-
QPolygonF pagePolygon = QPolygonF( QRectF( mPageMargin * dotsPerMM, mPageMargin * dotsPerMM,
76-
( rect().width() - 2 * mPageMargin ) * dotsPerMM, ( rect().height() - 2 * mPageMargin ) * dotsPerMM ) );
77+
QPolygonF pagePolygon = QPolygonF( QRectF( maxBleedPixels, maxBleedPixels,
78+
( rect().width() * scale - 2 * maxBleedPixels ), ( rect().height() * scale - 2 * maxBleedPixels ) ) );
7779
QList<QPolygonF> rings; //empty list
7880

79-
mComposition->pageStyleSymbol()->renderPolygon( pagePolygon, &rings, nullptr, context );
80-
mComposition->pageStyleSymbol()->stopRender( context );
81+
symbol->renderPolygon( pagePolygon, &rings, nullptr, context );
82+
symbol->stopRender( context );
83+
8184
painter->restore();
82-
#endif
8385
}

0 commit comments

Comments
 (0)