QGIS API Documentation  2.14.0-Essen
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.h>
20 #include <QDir>
21 #include <QString>
22 #include <QRegExp>
23 #include <QList>
24 
25 #include <qgsmaprenderer.h>
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 
45 
46  QString controlImagePath() const;
47 
48  QString report() { return mReport; }
49 
50  float matchPercent()
51  {
52  return static_cast<float>( mMismatchCount ) /
53  static_cast<float>( mMatchTarget ) * 100;
54  }
55  unsigned int mismatchCount() { return mMismatchCount; }
56  unsigned int matchTarget() { return mMatchTarget; }
57  //only records time for actual render part
58  int elapsedTime() { return mElapsedTime; }
59  void setElapsedTimeTarget( int theTarget ) { mElapsedTimeTarget = theTarget; }
60 
65  void setControlName( const QString &theName );
66 
70  void setControlPathPrefix( const QString &theName ) { mControlPathPrefix = theName + '/'; }
71 
72  void setControlPathSuffix( const QString& theName );
73 
75  QString imageToHash( const QString& theImageFile );
76 
77  void setRenderedImage( const QString& theImageFileName ) { mRenderedImageFile = theImageFileName; }
78 
85  QString renderedImage() { return mRenderedImageFile; }
86 
88  Q_DECL_DEPRECATED void setMapRenderer( QgsMapRenderer *thepMapRenderer );
89 
91  void setMapSettings( const QgsMapSettings& mapSettings );
92 
99  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
100 
106  void setSizeTolerance( int xTolerance, int yTolerance ) { mMaxSizeDifferenceX = xTolerance; mMaxSizeDifferenceY = yTolerance; }
107 
118  bool runTest( const QString& theTestName, unsigned int theMismatchCount = 0 );
119 
131  bool compareImages( const QString& theTestName, unsigned int theMismatchCount = 0, const QString& theRenderedImageFile = "" );
139  bool isKnownAnomaly( const QString& theDiffImageFile );
140 
144  static void drawBackground( QImage* image );
145 
151  QString expectedImageFile() const { return mExpectedImageFile; }
152 
160  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
161 
169  QVector<QgsDartMeasurement> dartMeasurements() const { return mDashMessages; }
170 
171  protected:
173  unsigned int mMatchTarget;
177 
178  private:
179  void emitDashMessage( const QgsDartMeasurement& dashMessage );
180  void emitDashMessage( const QString& name, QgsDartMeasurement::Type type, const QString& value );
181 
182  QString mControlName;
183  unsigned int mMismatchCount;
184  unsigned int mColorTolerance;
185  int mMaxSizeDifferenceX;
186  int mMaxSizeDifferenceY;
187  int mElapsedTimeTarget;
188  QgsMapSettings mMapSettings;
189  QString mControlPathPrefix;
190  QString mControlPathSuffix;
191  QVector<QgsDartMeasurement> mDashMessages;
192  bool mBufferDashMessages;
193 }; // class QgsRenderChecker
194 
195 
203 inline bool compareWkt( const QString& a, const QString& b, double tolerance = 0.000001 )
204 {
205  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a, b ).arg( tolerance ) );
206  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
207 
208  QString a0( a ), b0( b );
209  a0.replace( re, "#" );
210  b0.replace( re, "#" );
211 
212  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0, b0 ) );
213 
214  if ( a0 != b0 )
215  return false;
216 
217  QList<double> al, bl;
218 
219  int pos;
220  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
221  {
222  al << re.cap( 0 ).toDouble();
223  }
224  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
225  {
226  bl << re.cap( 0 ).toDouble();
227  }
228 
229  if ( al.size() != bl.size() )
230  return false;
231 
232  for ( int i = 0; i < al.size(); i++ )
233  {
234  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
235  return false;
236  }
237 
238  return true;
239 }
240 
241 #endif
void setColorTolerance(unsigned int theColorTolerance)
Set tolerance for color components used by runTest() and compareImages().
QString cap(int nth) const
QString expectedImageFile() const
Returns the path to the expected image file.
QVector< QgsDartMeasurement > dartMeasurements() const
Get access to buffered dash messages.
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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...
A non GUI class for rendering a map layer set onto a QPainter.
double toDouble(bool *ok) const
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)
Definition: qgis.h:285
int size() const
The QgsMapSettings class contains configuration for rendering of the map.
int matchedLength() const
int indexIn(const QString &str, int offset, CaretMode caretMode) const
void setRenderedImage(const QString &theImageFileName)
~QgsRenderChecker()
Destructor.
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()
QString & replace(int position, int n, QChar after)
void setControlPathPrefix(const QString &theName)
Prefix where the control images are kept.
unsigned int mismatchCount()
void setElapsedTimeTarget(int theTarget)