Skip to content

Commit 213064a

Browse files
committed
Fix layout context flags not being respected when rendering items
1 parent 0bffff0 commit 213064a

File tree

9 files changed

+106
-11
lines changed

9 files changed

+106
-11
lines changed

python/core/layout/qgslayoutcontext.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class QgsLayoutContext
6767
:rtype: bool
6868
%End
6969

70+
QgsRenderContext::Flags renderContextFlags() const;
71+
%Docstring
72+
Returns the combination of render context flags matched to the layout context's settings.
73+
:rtype: QgsRenderContext.Flags
74+
%End
75+
7076
void setFeature( const QgsFeature &feature );
7177
%Docstring
7278
Sets the current ``feature`` for evaluating the layout. This feature may

python/core/layout/qgslayoututils.sip

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ class QgsLayoutUtils
3333
%Docstring
3434
Creates a render context suitable for the specified layout ``map`` and ``painter`` destination.
3535
This method returns a new QgsRenderContext which matches the scale and settings of the
36-
target map. If the ``dpi`` argument is not specified then the dpi will be taken from the destinatation
36+
target map. If the ``dpi`` argument is not specified then the dpi will be taken from the destination
3737
painter device.
3838
.. seealso:: createRenderContextForLayout()
3939
:rtype: QgsRenderContext
4040
%End
4141

42-
static QgsRenderContext createRenderContextForLayout( QgsLayout *layout, QPainter *painter );
42+
static QgsRenderContext createRenderContextForLayout( QgsLayout *layout, QPainter *painter, double dpi = -1 );
4343
%Docstring
4444
Creates a render context suitable for the specified ``layout`` and ``painter`` destination.
4545
This method returns a new QgsRenderContext which matches the scale and settings from the layout's
4646
QgsLayout.referenceMap().
47+
If the ``dpi`` argument is not specified then the dpi will be taken from the destination
48+
painter device.
4749
.. seealso:: createRenderContextForMap()
4850
:rtype: QgsRenderContext
4951
%End

src/core/layout/qgslayoutcontext.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ bool QgsLayoutContext::testFlag( const QgsLayoutContext::Flag flag ) const
4545
return mFlags.testFlag( flag );
4646
}
4747

48+
QgsRenderContext::Flags QgsLayoutContext::renderContextFlags() const
49+
{
50+
QgsRenderContext::Flags flags = 0;
51+
if ( mFlags & FlagAntialiasing )
52+
flags = flags | QgsRenderContext::Antialiasing;
53+
if ( mFlags & FlagUseAdvancedEffects )
54+
flags = flags | QgsRenderContext::UseAdvancedEffects;
55+
56+
// TODO - expose as layout context flag?
57+
flags |= QgsRenderContext::ForceVectorOutput;
58+
return flags;
59+
}
60+
4861
QgsVectorLayer *QgsLayoutContext::layer() const
4962
{
5063
return mLayer;

src/core/layout/qgslayoutcontext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class CORE_EXPORT QgsLayoutContext
8181
*/
8282
bool testFlag( const Flag flag ) const;
8383

84+
/**
85+
* Returns the combination of render context flags matched to the layout context's settings.
86+
*/
87+
QgsRenderContext::Flags renderContextFlags() const;
88+
8489
/**
8590
* Sets the current \a feature for evaluating the layout. This feature may
8691
* be used for altering an item's content and appearance for a report

src/core/layout/qgslayoutitem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
128128
QPainter p( &mItemCachedImage );
129129

130130
preparePainter( &p );
131-
QgsRenderContext context = QgsLayoutUtils::createRenderContextForMap( nullptr, &p, destinationDpi );
131+
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( nullptr, &p, destinationDpi );
132132
// painter is already scaled to dots
133133
// need to translate so that item origin is at 0,0 in painter coordinates (not bounding rect origin)
134134
p.translate( -boundingRect().x() * context.scaleFactor(), -boundingRect().y() * context.scaleFactor() );
@@ -147,7 +147,8 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
147147
{
148148
// no caching or flattening
149149
painter->save();
150-
QgsRenderContext context = QgsLayoutUtils::createRenderContextForMap( nullptr, painter, destinationDpi );
150+
preparePainter( painter );
151+
QgsRenderContext context = QgsLayoutUtils::createRenderContextForLayout( mLayout, painter, destinationDpi );
151152
// scale painter from mm to dots
152153
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
153154
draw( context, itemStyle );

src/core/layout/qgslayoututils.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ QgsRenderContext QgsLayoutUtils::createRenderContextForMap( QgsLayoutItemMap *ma
7474
QgsRenderContext context; // = QgsRenderContext::fromMapSettings( ms );
7575
if ( painter )
7676
context.setPainter( painter );
77+
78+
context.setFlags( map->layout()->context().renderContextFlags() );
7779
return context;
7880
}
7981
}
8082

81-
QgsRenderContext QgsLayoutUtils::createRenderContextForLayout( QgsLayout *layout, QPainter *painter )
83+
QgsRenderContext QgsLayoutUtils::createRenderContextForLayout( QgsLayout *layout, QPainter *painter, double dpi )
8284
{
8385
QgsLayoutItemMap *referenceMap = layout ? layout->referenceMap() : nullptr;
84-
return createRenderContextForMap( referenceMap, painter );
86+
QgsRenderContext context = createRenderContextForMap( referenceMap, painter, dpi );
87+
if ( layout )
88+
context.setFlags( layout->context().renderContextFlags() );
89+
return context;
8590
}

src/core/layout/qgslayoututils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CORE_EXPORT QgsLayoutUtils
4343
/**
4444
* Creates a render context suitable for the specified layout \a map and \a painter destination.
4545
* This method returns a new QgsRenderContext which matches the scale and settings of the
46-
* target map. If the \a dpi argument is not specified then the dpi will be taken from the destinatation
46+
* target map. If the \a dpi argument is not specified then the dpi will be taken from the destination
4747
* painter device.
4848
* \see createRenderContextForLayout()
4949
*/
@@ -53,9 +53,11 @@ class CORE_EXPORT QgsLayoutUtils
5353
* Creates a render context suitable for the specified \a layout and \a painter destination.
5454
* This method returns a new QgsRenderContext which matches the scale and settings from the layout's
5555
* QgsLayout::referenceMap().
56+
* If the \a dpi argument is not specified then the dpi will be taken from the destination
57+
* painter device.
5658
* \see createRenderContextForMap()
5759
*/
58-
static QgsRenderContext createRenderContextForLayout( QgsLayout *layout, QPainter *painter );
60+
static QgsRenderContext createRenderContextForLayout( QgsLayout *layout, QPainter *painter, double dpi = -1 );
5961

6062
};
6163

tests/src/core/testqgslayoutcontext.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsLayoutContext: public QObject
3636
void feature();
3737
void layer();
3838
void dpi();
39+
void renderContextFlags();
3940

4041
private:
4142
QString mReport;
@@ -135,5 +136,27 @@ void TestQgsLayoutContext::dpi()
135136
QCOMPARE( context.measurementConverter().dpi(), 600.0 );
136137
}
137138

139+
void TestQgsLayoutContext::renderContextFlags()
140+
{
141+
QgsLayoutContext context;
142+
context.setFlags( 0 );
143+
QgsRenderContext::Flags flags = context.renderContextFlags();
144+
QVERIFY( !( flags & QgsRenderContext::Antialiasing ) );
145+
QVERIFY( !( flags & QgsRenderContext::UseAdvancedEffects ) );
146+
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
147+
148+
context.setFlag( QgsLayoutContext::FlagAntialiasing );
149+
flags = context.renderContextFlags();
150+
QVERIFY( ( flags & QgsRenderContext::Antialiasing ) );
151+
QVERIFY( !( flags & QgsRenderContext::UseAdvancedEffects ) );
152+
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
153+
154+
context.setFlag( QgsLayoutContext::FlagUseAdvancedEffects );
155+
flags = context.renderContextFlags();
156+
QVERIFY( ( flags & QgsRenderContext::Antialiasing ) );
157+
QVERIFY( ( flags & QgsRenderContext::UseAdvancedEffects ) );
158+
QVERIFY( ( flags & QgsRenderContext::ForceVectorOutput ) );
159+
}
160+
138161
QGSTEST_MAIN( TestQgsLayoutContext )
139162
#include "testqgslayoutcontext.moc"

tests/src/core/testqgslayoututils.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,17 @@ void TestQgsLayoutUtils::createRenderContextFromLayout()
124124
testImage.setDotsPerMeterY( 150 / 25.4 * 1000 );
125125
QPainter p( &testImage );
126126

127-
// no composition
127+
// no layout
128128
QgsRenderContext rc = QgsLayoutUtils::createRenderContextForLayout( nullptr, &p );
129129
QGSCOMPARENEAR( rc.scaleFactor(), 150 / 25.4, 0.001 );
130130
QCOMPARE( rc.painter(), &p );
131131

132-
// no composition, no painter
132+
// no layout, no painter
133133
rc = QgsLayoutUtils::createRenderContextForLayout( nullptr, nullptr );
134134
QGSCOMPARENEAR( rc.scaleFactor(), 88 / 25.4, 0.001 );
135135
QVERIFY( !rc.painter() );
136136

137-
//create composition with no reference map
137+
//create layout with no reference map
138138
QgsRectangle extent( 2000, 2800, 2500, 2900 );
139139
QgsProject project;
140140
QgsLayout l( &project );
@@ -167,6 +167,25 @@ void TestQgsLayoutUtils::createRenderContextFromLayout()
167167
QGSCOMPARENEAR( rc.rendererScale(), map->scale(), 1000000 );
168168
QVERIFY( !rc.painter() );
169169

170+
// check render context flags are correctly set
171+
l.context().setFlags( 0 );
172+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
173+
QVERIFY( !( rc.flags() & QgsRenderContext::Antialiasing ) );
174+
QVERIFY( !( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
175+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
176+
177+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing );
178+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
179+
QVERIFY( ( rc.flags() & QgsRenderContext::Antialiasing ) );
180+
QVERIFY( !( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
181+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
182+
183+
l.context().setFlag( QgsLayoutContext::FlagUseAdvancedEffects );
184+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
185+
QVERIFY( ( rc.flags() & QgsRenderContext::Antialiasing ) );
186+
QVERIFY( ( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
187+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
188+
170189
p.end();
171190
}
172191

@@ -224,6 +243,25 @@ void TestQgsLayoutUtils::createRenderContextFromMap()
224243
QGSCOMPARENEAR( rc.scaleFactor(), 150 / 25.4, 0.001 );
225244
QGSCOMPARENEAR( rc.rendererScale(), map2->scale(), 1000000 );
226245
QVERIFY( rc.painter() );
246+
247+
// check render context flags are correctly set
248+
l.context().setFlags( 0 );
249+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
250+
QVERIFY( !( rc.flags() & QgsRenderContext::Antialiasing ) );
251+
QVERIFY( !( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
252+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
253+
254+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing );
255+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
256+
QVERIFY( ( rc.flags() & QgsRenderContext::Antialiasing ) );
257+
QVERIFY( !( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
258+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
259+
260+
l.context().setFlag( QgsLayoutContext::FlagUseAdvancedEffects );
261+
rc = QgsLayoutUtils::createRenderContextForLayout( &l, nullptr );
262+
QVERIFY( ( rc.flags() & QgsRenderContext::Antialiasing ) );
263+
QVERIFY( ( rc.flags() & QgsRenderContext::UseAdvancedEffects ) );
264+
QVERIFY( ( rc.flags() & QgsRenderContext::ForceVectorOutput ) );
227265
#endif
228266
p.end();
229267
}

0 commit comments

Comments
 (0)