Skip to content

Commit a6199be

Browse files
committedJan 9, 2018
Added optional argument to create reference layout images
1 parent 4dc5b56 commit a6199be

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed
 

‎python/core/qgsmultirenderchecker.sip

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ Constructor for QgsLayoutChecker.
163163
Sets the output (reference) image ``size``.
164164
%End
165165

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

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

173+
A reference image can be creates by setting ``createReferenceImage`` to true
174+
in this case the test will always return true.
175+
173176
The page number is specified via ``page``, where 0 corresponds to the first
174177
page in the layout.
175178

‎src/core/qgsmultirenderchecker.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ QgsLayoutChecker::QgsLayoutChecker( const QString &testName, QgsLayout *layout )
187187
setColorTolerance( 5 );
188188
}
189189

190-
bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDiff )
190+
bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDiff, bool createReferenceImage )
191191
{
192192
if ( !mLayout )
193193
{
@@ -196,22 +196,28 @@ bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDi
196196

197197
setControlName( "expected_" + mTestName );
198198

199-
#if 0
200-
//fake mode to generate expected image
201-
//assume 96 dpi and size of the control image 1122 * 794
202-
QImage newImage( QSize( 1122, 794 ), QImage::Format_RGB32 );
203-
mComposition->setPlotStyle( QgsComposition::Print );
204-
newImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
205-
newImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
206-
drawBackground( &newImage );
207-
QPainter expectedPainter( &newImage );
208-
//QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
209-
//QRectF targetArea( 0, 0, 3507, 2480 );
210-
mComposition->renderPage( &expectedPainter, page );
211-
expectedPainter.end();
212-
newImage.save( controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png", "PNG" );
213-
return true;
214-
#endif //0
199+
200+
if ( createReferenceImage )
201+
{
202+
//fake mode to generate expected image
203+
//assume 96 dpi
204+
205+
206+
QImage _outputImage( mSize, QImage::Format_RGB32 );
207+
_outputImage.setDotsPerMeterX( 96 / 25.4 * 1000 );
208+
_outputImage.setDotsPerMeterY( 96 / 25.4 * 1000 );
209+
QPainter _p( &_outputImage );
210+
mLayout->exporter().renderPage( &_p, page );
211+
_p.end();
212+
213+
if ( ! QDir( controlImagePath() ).exists() )
214+
{
215+
QDir().mkdir( controlImagePath() );
216+
}
217+
_outputImage.save( controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png", "PNG" );
218+
qDebug( ) << "Reference image saved to : " + controlImagePath() + QDir::separator() + "expected_" + mTestName + ".png";
219+
220+
}
215221

216222
QImage outputImage( mSize, QImage::Format_RGB32 );
217223
outputImage.setDotsPerMeterX( mDotsPerMeter );

‎src/core/qgsmultirenderchecker.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,15 @@ class CORE_EXPORT QgsLayoutChecker : public QgsMultiRenderChecker
207207
* The maximum number of allowable pixels differing from the reference image is
208208
* specified via the \a pixelDiff argument.
209209
*
210+
* A reference image can be creates by setting \a createReferenceImage to true
211+
* in this case the test will always return true.
212+
*
210213
* The page number is specified via \a page, where 0 corresponds to the first
211214
* page in the layout.
212215
*
213216
* Returns false if the rendered layout differs from the expected reference image.
214217
*/
215-
bool testLayout( QString &report, int page = 0, int pixelDiff = 0 );
218+
bool testLayout( QString &report, int page = 0, int pixelDiff = 0, bool createReferenceImage = false );
216219

217220
private:
218221
QgsLayoutChecker() = delete;

0 commit comments

Comments
 (0)
Please sign in to comment.