Skip to content
Permalink
Browse files

Added optional argument to create reference layout images

  • Loading branch information
elpaso committed Jan 9, 2018
1 parent 4dc5b56 commit a6199bec030ea248ff64ecda9be6eb21ecff3644
Showing with 31 additions and 19 deletions.
  1. +4 −1 python/core/qgsmultirenderchecker.sip
  2. +23 −17 src/core/qgsmultirenderchecker.cpp
  3. +4 −1 src/core/qgsmultirenderchecker.h
@@ -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.

@@ -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 )
{
@@ -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 );
@@ -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;

0 comments on commit a6199be

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