Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add some rendering checks for layout items
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent dd37037 commit 498c4cd
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/core/layout/qgslayoutitem.cpp
Expand Up @@ -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();
}
Expand All @@ -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 );
}
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -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;
};
Expand Down
90 changes: 90 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -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:

Expand Down Expand Up @@ -160,13 +165,98 @@ 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";
QString myTmpDir = QDir::tempPath() + QDir::separator();
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 );
Expand Down
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 498c4cd

Please sign in to comment.