17
17
#include " qgslayoutitempage.h"
18
18
#include " qgslayout.h"
19
19
#include " qgslayoututils.h"
20
+ #include " qgssymbollayerutils.h"
20
21
#include < QPainter>
21
22
23
+ #define SHADOW_WIDTH_PIXELS 5
22
24
QgsLayoutItemPage::QgsLayoutItemPage ( QgsLayout *layout )
23
25
: QgsLayoutItem( layout )
24
26
{
25
27
26
28
}
27
29
28
- void QgsLayoutItemPage::draw ( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
30
+ void QgsLayoutItemPage::draw ( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
29
31
{
30
- #if 0
31
- Q_UNUSED( itemStyle );
32
- Q_UNUSED( pWidget );
33
- if ( !painter || !mComposition || !mComposition->pagesVisible() )
32
+ if ( !context.painter () || !mLayout /* || !mLayout->pagesVisible() */ )
34
33
{
35
34
return ;
36
35
}
37
36
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 );
44
38
45
39
QgsExpressionContext expressionContext = createExpressionContext ();
46
40
context.setExpressionContext ( expressionContext );
47
41
42
+ QPainter *painter = context.painter ();
48
43
painter->save ();
49
44
45
+ #if 0 //TODO
50
46
if ( mComposition->plotStyle() == QgsComposition::Preview )
47
+ #endif
51
48
{
52
49
// if in preview mode, draw page border and shadow so that it's
53
50
// still possible to tell where pages with a transparent style begin and end
@@ -56,28 +53,33 @@ void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraph
56
53
// shadow
57
54
painter->setBrush ( QBrush ( QColor ( 150 , 150 , 150 ) ) );
58
55
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 ) );
60
57
61
58
// page area
62
59
painter->setBrush ( QColor ( 215 , 215 , 215 ) );
63
60
QPen pagePen = QPen ( QColor ( 100 , 100 , 100 ), 0 );
64
61
pagePen.setCosmetic ( true );
65
62
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 () ) );
67
64
}
68
65
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 );
70
71
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--;
73
76
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 ) ) );
77
79
QList<QPolygonF> rings; // empty list
78
80
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
+
81
84
painter->restore ();
82
- #endif
83
85
}
0 commit comments