Skip to content
Permalink
Browse files

Add some rendering checks for layout items

  • Loading branch information
nyalldawson committed Jul 10, 2017
1 parent dd37037 commit 498c4cda160e022cface3ab4b793bfad114f8ba8
@@ -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 );
}
@@ -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;
};
@@ -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,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 );
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 498c4cd

Please sign in to comment.
You can’t perform that action at this time.