QGIS API Documentation  2.99.0-Master (f867b65)
qgsrenderchecker.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrenderchecker.h - check maprender output against an expected image
3  --------------------------------------
4  Date : 18 Jan 2008
5  Copyright : (C) 2008 by Tim Sutton
6  email : tim @ linfiniti.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSRENDERCHECKER_H
17 #define QGSRENDERCHECKER_H
18 
19 #include "qgis_core.h"
20 #include <qgis.h>
21 #include <QDir>
22 #include <QString>
23 #include <QRegExp>
24 #include <QList>
25 
26 #include <qgslogger.h>
27 #include <qgsmapsettings.h>
28 #include <qgsdartmeasurement.h>
29 
30 class QImage;
31 
37 class CORE_EXPORT QgsRenderChecker
38 {
39  public:
40 
42 
43  QString controlImagePath() const;
44 
45  QString report() { return mReport; }
46 
47  float matchPercent()
48  {
49  return static_cast<float>( mMismatchCount ) /
50  static_cast<float>( mMatchTarget ) * 100;
51  }
52  unsigned int mismatchCount() { return mMismatchCount; }
53  unsigned int matchTarget() { return mMatchTarget; }
54  //only records time for actual render part
55  int elapsedTime() { return mElapsedTime; }
56  void setElapsedTimeTarget( int target ) { mElapsedTimeTarget = target; }
57 
62  void setControlName( const QString &name );
63 
67  void setControlPathPrefix( const QString &name ) { mControlPathPrefix = name + '/'; }
68 
69  void setControlPathSuffix( const QString &name );
70 
72  QString imageToHash( const QString &imageFile );
73 
74  void setRenderedImage( const QString &imageFileName ) { mRenderedImageFile = imageFileName; }
75 
82  QString renderedImage() { return mRenderedImageFile; }
83 
85  void setMapSettings( const QgsMapSettings &mapSettings );
86 
93  void setColorTolerance( unsigned int colorTolerance ) { mColorTolerance = colorTolerance; }
94 
100  void setSizeTolerance( int xTolerance, int yTolerance ) { mMaxSizeDifferenceX = xTolerance; mMaxSizeDifferenceY = yTolerance; }
101 
112  bool runTest( const QString &testName, unsigned int mismatchCount = 0 );
113 
125  bool compareImages( const QString &testName, unsigned int mismatchCount = 0, const QString &renderedImageFile = "" );
126 
134  bool isKnownAnomaly( const QString &diffImageFile );
135 
139  static void drawBackground( QImage *image );
140 
146  QString expectedImageFile() const { return mExpectedImageFile; }
147 
155  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
156 
164  QVector<QgsDartMeasurement> dartMeasurements() const { return mDashMessages; }
165 
166  protected:
167  QString mReport;
168  unsigned int mMatchTarget;
172 
173  private:
174  void emitDashMessage( const QgsDartMeasurement &dashMessage );
175  void emitDashMessage( const QString &name, QgsDartMeasurement::Type type, const QString &value );
176 
177  QString mControlName;
178  unsigned int mMismatchCount;
179  unsigned int mColorTolerance;
180  int mMaxSizeDifferenceX;
181  int mMaxSizeDifferenceY;
182  int mElapsedTimeTarget;
183  QgsMapSettings mMapSettings;
184  QString mControlPathPrefix;
185  QString mControlPathSuffix;
186  QVector<QgsDartMeasurement> mDashMessages;
187  bool mBufferDashMessages;
188 }; // class QgsRenderChecker
189 
190 
198 inline bool compareWkt( const QString &a, const QString &b, double tolerance = 0.000001 )
199 {
200  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a, b ).arg( tolerance ) );
201  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
202 
203  QString a0( a ), b0( b );
204  a0.replace( re, QStringLiteral( "#" ) );
205  b0.replace( re, QStringLiteral( "#" ) );
206 
207  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0, b0 ) );
208 
209  if ( a0 != b0 )
210  return false;
211 
212  QList<double> al, bl;
213 
214  int pos;
215  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
216  {
217  al << re.cap( 0 ).toDouble();
218  }
219  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
220  {
221  bl << re.cap( 0 ).toDouble();
222  }
223 
224  if ( al.size() != bl.size() )
225  return false;
226 
227  for ( int i = 0; i < al.size(); i++ )
228  {
229  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
230  return false;
231  }
232 
233  return true;
234 }
235 
236 #endif
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
QString expectedImageFile() const
Returns the path to the expected image file.
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
void setRenderedImage(const QString &imageFileName)
QString renderedImage()
The path of the rendered image can be retrieved through that method.
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
void setSizeTolerance(int xTolerance, int yTolerance)
Sets the largest allowable difference in size between the rendered and the expected image...
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:210
The QgsMapSettings class contains configuration for rendering of the map.
void setControlPathPrefix(const QString &name)
Prefix where the control images are kept.
unsigned int mMatchTarget
bool compareWkt(const QString &a, const QString &b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.
unsigned int matchTarget()
QVector< QgsDartMeasurement > dartMeasurements() const
Get access to buffered dash messages.
void setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() and compareImages().
void setElapsedTimeTarget(int target)
unsigned int mismatchCount()