Skip to content

Commit 498c4cd

Browse files
committed
Add some rendering checks for layout items
1 parent dd37037 commit 498c4cd

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

src/core/layout/qgslayoutitem.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
3636
painter->save();
3737
preparePainter( painter );
3838

39-
draw( painter, itemStyle, pWidget );
39+
if ( shouldDrawDebugRect() )
40+
{
41+
drawDebugRect( painter );
42+
}
43+
else
44+
{
45+
draw( painter, itemStyle, pWidget );
46+
}
4047

4148
painter->restore();
4249
}
@@ -62,4 +69,20 @@ void QgsLayoutItem::preparePainter( QPainter *painter )
6269
{
6370
return;
6471
}
72+
73+
painter->setRenderHint( QPainter::Antialiasing, shouldDrawAntialiased() );
74+
}
75+
76+
bool QgsLayoutItem::shouldDrawAntialiased() const
77+
{
78+
if ( !mLayout )
79+
{
80+
return true;
81+
}
82+
return mLayout->context().testFlag( QgsLayoutContext::FlagAntialiasing ) && !mLayout->context().testFlag( QgsLayoutContext::FlagDebug );
83+
}
84+
85+
bool QgsLayoutItem::shouldDrawDebugRect() const
86+
{
87+
return mLayout && mLayout->context().testFlag( QgsLayoutContext::FlagDebug );
6588
}

src/core/layout/qgslayoutitem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
6666

6767
//! Prepares a painter by setting rendering flags
6868
void preparePainter( QPainter *painter );
69+
bool shouldDrawAntialiased() const;
70+
bool shouldDrawDebugRect() const;
6971

7072
friend class TestQgsLayoutItem;
7173
};

tests/src/core/testqgslayoutitem.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class TestQgsLayoutItem: public QObject
3737
void cleanup();// will be called after every testfunction.
3838
void creation(); //test creation of QgsLayoutItem
3939
void registry();
40+
void shouldDrawDebug();
41+
void shouldDrawAntialiased();
42+
void preparePainter();
43+
void debugRect();
44+
void draw();
4045

4146
private:
4247

@@ -160,13 +165,98 @@ void TestQgsLayoutItem::registry()
160165
QVERIFY( !reg2.populate() );
161166
}
162167

168+
void TestQgsLayoutItem::shouldDrawDebug()
169+
{
170+
QgsProject p;
171+
QgsLayout l( &p );
172+
TestItem *item = new TestItem( &l );
173+
l.context().setFlag( QgsLayoutContext::FlagDebug, true );
174+
QVERIFY( item->shouldDrawDebugRect() );
175+
l.context().setFlag( QgsLayoutContext::FlagDebug, false );
176+
QVERIFY( !item->shouldDrawDebugRect() );
177+
delete item;
178+
}
179+
180+
void TestQgsLayoutItem::shouldDrawAntialiased()
181+
{
182+
QgsProject p;
183+
QgsLayout l( &p );
184+
TestItem *item = new TestItem( &l );
185+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false );
186+
QVERIFY( !item->shouldDrawAntialiased() );
187+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, true );
188+
QVERIFY( item->shouldDrawAntialiased() );
189+
delete item;
190+
}
191+
192+
void TestQgsLayoutItem::preparePainter()
193+
{
194+
QgsProject p;
195+
QgsLayout l( &p );
196+
TestItem *item = new TestItem( &l );
197+
//test with no painter
198+
item->preparePainter( nullptr );
199+
200+
//test antialiasing correctly set for painter
201+
QImage image( QSize( 100, 100 ), QImage::Format_ARGB32 );
202+
QPainter painter;
203+
painter.begin( &image );
204+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false );
205+
item->preparePainter( &painter );
206+
QVERIFY( !( painter.renderHints() & QPainter::Antialiasing ) );
207+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, true );
208+
item->preparePainter( &painter );
209+
QVERIFY( painter.renderHints() & QPainter::Antialiasing );
210+
delete item;
211+
}
212+
213+
void TestQgsLayoutItem::debugRect()
214+
{
215+
QgsProject p;
216+
QgsLayout l( &p );
217+
TestItem *item = new TestItem( &l );
218+
l.addItem( item );
219+
item->setPos( 100, 100 );
220+
item->setRect( 0, 0, 200, 200 );
221+
l.setSceneRect( 0, 0, 400, 400 );
222+
l.context().setFlag( QgsLayoutContext::FlagDebug, true );
223+
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
224+
image.fill( 0 );
225+
QPainter painter( &image );
226+
l.render( &painter );
227+
painter.end();
228+
229+
bool result = renderCheck( "layoutitem_debugrect", image, 0 );
230+
QVERIFY( result );
231+
}
232+
233+
void TestQgsLayoutItem::draw()
234+
{
235+
QgsProject p;
236+
QgsLayout l( &p );
237+
TestItem *item = new TestItem( &l );
238+
l.addItem( item );
239+
item->setPos( 100, 100 );
240+
item->setRect( 0, 0, 200, 200 );
241+
l.setSceneRect( 0, 0, 400, 400 );
242+
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false ); //disable antialiasing to limit cross platform differences
243+
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
244+
image.fill( 0 );
245+
QPainter painter( &image );
246+
l.render( &painter );
247+
painter.end();
248+
bool result = renderCheck( "layoutitem_draw", image, 0 );
249+
QVERIFY( result );
250+
}
251+
163252
bool TestQgsLayoutItem::renderCheck( QString testName, QImage &image, int mismatchCount )
164253
{
165254
mReport += "<h2>" + testName + "</h2>\n";
166255
QString myTmpDir = QDir::tempPath() + QDir::separator();
167256
QString myFileName = myTmpDir + testName + ".png";
168257
image.save( myFileName, "PNG" );
169258
QgsRenderChecker myChecker;
259+
myChecker.setControlPathPrefix( "layouts" );
170260
myChecker.setControlName( "expected_" + testName );
171261
myChecker.setRenderedImage( myFileName );
172262
bool myResultFlag = myChecker.compareImages( testName, mismatchCount );
Loading

0 commit comments

Comments
 (0)