Add some rendering checks for layout items

This commit is contained in:
Nyall Dawson 2017-07-10 12:36:47 +10:00
parent dd370373be
commit 498c4cda16
6 changed files with 116 additions and 1 deletions

View File

@ -36,7 +36,14 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
painter->save();
preparePainter( painter );
draw( painter, itemStyle, pWidget );
if ( shouldDrawDebugRect() )
{
drawDebugRect( painter );
}
else
{
draw( painter, itemStyle, pWidget );
}
painter->restore();
}
@ -62,4 +69,20 @@ void QgsLayoutItem::preparePainter( QPainter *painter )
{
return;
}
painter->setRenderHint( QPainter::Antialiasing, shouldDrawAntialiased() );
}
bool QgsLayoutItem::shouldDrawAntialiased() const
{
if ( !mLayout )
{
return true;
}
return mLayout->context().testFlag( QgsLayoutContext::FlagAntialiasing ) && !mLayout->context().testFlag( QgsLayoutContext::FlagDebug );
}
bool QgsLayoutItem::shouldDrawDebugRect() const
{
return mLayout && mLayout->context().testFlag( QgsLayoutContext::FlagDebug );
}

View File

@ -66,6 +66,8 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
//! Prepares a painter by setting rendering flags
void preparePainter( QPainter *painter );
bool shouldDrawAntialiased() const;
bool shouldDrawDebugRect() const;
friend class TestQgsLayoutItem;
};

View File

@ -37,6 +37,11 @@ class TestQgsLayoutItem: public QObject
void cleanup();// will be called after every testfunction.
void creation(); //test creation of QgsLayoutItem
void registry();
void shouldDrawDebug();
void shouldDrawAntialiased();
void preparePainter();
void debugRect();
void draw();
private:
@ -160,6 +165,90 @@ void TestQgsLayoutItem::registry()
QVERIFY( !reg2.populate() );
}
void TestQgsLayoutItem::shouldDrawDebug()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.context().setFlag( QgsLayoutContext::FlagDebug, true );
QVERIFY( item->shouldDrawDebugRect() );
l.context().setFlag( QgsLayoutContext::FlagDebug, false );
QVERIFY( !item->shouldDrawDebugRect() );
delete item;
}
void TestQgsLayoutItem::shouldDrawAntialiased()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false );
QVERIFY( !item->shouldDrawAntialiased() );
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, true );
QVERIFY( item->shouldDrawAntialiased() );
delete item;
}
void TestQgsLayoutItem::preparePainter()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
//test with no painter
item->preparePainter( nullptr );
//test antialiasing correctly set for painter
QImage image( QSize( 100, 100 ), QImage::Format_ARGB32 );
QPainter painter;
painter.begin( &image );
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false );
item->preparePainter( &painter );
QVERIFY( !( painter.renderHints() & QPainter::Antialiasing ) );
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, true );
item->preparePainter( &painter );
QVERIFY( painter.renderHints() & QPainter::Antialiasing );
delete item;
}
void TestQgsLayoutItem::debugRect()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.addItem( item );
item->setPos( 100, 100 );
item->setRect( 0, 0, 200, 200 );
l.setSceneRect( 0, 0, 400, 400 );
l.context().setFlag( QgsLayoutContext::FlagDebug, true );
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
image.fill( 0 );
QPainter painter( &image );
l.render( &painter );
painter.end();
bool result = renderCheck( "layoutitem_debugrect", image, 0 );
QVERIFY( result );
}
void TestQgsLayoutItem::draw()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.addItem( item );
item->setPos( 100, 100 );
item->setRect( 0, 0, 200, 200 );
l.setSceneRect( 0, 0, 400, 400 );
l.context().setFlag( QgsLayoutContext::FlagAntialiasing, false ); //disable antialiasing to limit cross platform differences
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
image.fill( 0 );
QPainter painter( &image );
l.render( &painter );
painter.end();
bool result = renderCheck( "layoutitem_draw", image, 0 );
QVERIFY( result );
}
bool TestQgsLayoutItem::renderCheck( QString testName, QImage &image, int mismatchCount )
{
mReport += "<h2>" + testName + "</h2>\n";
@ -167,6 +256,7 @@ bool TestQgsLayoutItem::renderCheck( QString testName, QImage &image, int mismat
QString myFileName = myTmpDir + testName + ".png";
image.save( myFileName, "PNG" );
QgsRenderChecker myChecker;
myChecker.setControlPathPrefix( "layouts" );
myChecker.setControlName( "expected_" + testName );
myChecker.setRenderedImage( myFileName );
bool myResultFlag = myChecker.compareImages( testName, mismatchCount );

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB