Skip to content

Commit

Permalink
Added optional argument to create reference layout images
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jan 9, 2018
1 parent 4dc5b56 commit a6199be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
5 changes: 4 additions & 1 deletion python/core/qgsmultirenderchecker.sip
Expand Up @@ -163,13 +163,16 @@ Constructor for QgsLayoutChecker.
Sets the output (reference) image ``size``.
%End

bool testLayout( QString &report, int page = 0, int pixelDiff = 0 );
bool testLayout( QString &report, int page = 0, int pixelDiff = 0, bool createReferenceImage = false );
%Docstring
Runs a render check on the layout, adding results to the specified ``report``.

The maximum number of allowable pixels differing from the reference image is
specified via the ``pixelDiff`` argument.

A reference image can be creates by setting ``createReferenceImage`` to true
in this case the test will always return true.

The page number is specified via ``page``, where 0 corresponds to the first
page in the layout.

Expand Down
40 changes: 23 additions & 17 deletions src/core/qgsmultirenderchecker.cpp
Expand Up @@ -187,7 +187,7 @@ QgsLayoutChecker::QgsLayoutChecker( const QString &testName, QgsLayout *layout )
setColorTolerance( 5 );
}

bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDiff )
bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDiff, bool createReferenceImage )
{
if ( !mLayout )
{
Expand All @@ -196,22 +196,28 @@ bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDi

setControlName( "expected_" + mTestName );

#if 0
//fake mode to generate expected image
//assume 96 dpi and size of the control image 1122 * 794
QImage newImage( QSize( 1122, 794 ), QImage::Format_RGB32 );
mComposition->setPlotStyle( QgsComposition::Print );
newImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
newImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
drawBackground( &newImage );
QPainter expectedPainter( &newImage );
//QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
//QRectF targetArea( 0, 0, 3507, 2480 );
mComposition->renderPage( &expectedPainter, page );
expectedPainter.end();
newImage.save( controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png", "PNG" );
return true;
#endif //0

if ( createReferenceImage )
{
//fake mode to generate expected image
//assume 96 dpi


QImage _outputImage( mSize, QImage::Format_RGB32 );
_outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
_outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
QPainter _p( &_outputImage );
mLayout->exporter().renderPage( &_p, page );
_p.end();

if ( ! QDir( controlImagePath() ).exists() )
{
QDir().mkdir( controlImagePath() );
}
_outputImage.save( controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png", "PNG" );
qDebug( ) << "Reference image saved to : " + controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png";

}

QImage outputImage( mSize, QImage::Format_RGB32 );
outputImage.setDotsPerMeterX( mDotsPerMeter );
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsmultirenderchecker.h
Expand Up @@ -207,12 +207,15 @@ class CORE_EXPORT QgsLayoutChecker : public QgsMultiRenderChecker
* The maximum number of allowable pixels differing from the reference image is
* specified via the \a pixelDiff argument.
*
* A reference image can be creates by setting \a createReferenceImage to true
* in this case the test will always return true.
*
* The page number is specified via \a page, where 0 corresponds to the first
* page in the layout.
*
* Returns false if the rendered layout differs from the expected reference image.
*/
bool testLayout( QString &report, int page = 0, int pixelDiff = 0 );
bool testLayout( QString &report, int page = 0, int pixelDiff = 0, bool createReferenceImage = false );

private:
QgsLayoutChecker() = delete;
Expand Down

0 comments on commit a6199be

Please sign in to comment.