QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
29 class QImage;
30 
36 class CORE_EXPORT QgsRenderChecker
37 {
38  public:
39 
41 
44 
45  QString controlImagePath() const;
46 
47  QString report() { return mReport; };
48  float matchPercent()
49  {
50  return static_cast<float>( mMismatchCount ) /
51  static_cast<float>( mMatchTarget ) * 100;
52  }
53  unsigned int mismatchCount() { return mMismatchCount; }
54  unsigned int matchTarget() { return mMatchTarget; }
55  //only records time for actual render part
56  int elapsedTime() { return mElapsedTime; }
57  void setElapsedTimeTarget( int theTarget ) { mElapsedTimeTarget = theTarget; };
62  void setControlName( const QString theName );
66  void setControlPathPrefix( const QString theName ) { mControlPathPrefix = theName + QDir::separator(); }
68  QString imageToHash( QString theImageFile );
69 
70  void setRenderedImage( QString theImageFileName ) { mRenderedImageFile = theImageFileName; }
72  Q_DECL_DEPRECATED void setMapRenderer( QgsMapRenderer * thepMapRenderer );
73 
75  void setMapSettings( const QgsMapSettings& mapSettings );
76 
77 
84  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
95  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
96 
108  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
116  bool isKnownAnomaly( QString theDiffImageFile );
117 
118  QString expectedImageFile() { return mExpectedImageFile; };
119 
120  protected:
121 
122  QString mReport;
123  unsigned int mMatchTarget;
127 
128  private:
129 
130  QString mControlName;
131  unsigned int mMismatchCount;
132  unsigned int mColorTolerance;
136 
137 }; // class QgsRenderChecker
138 
139 
147 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
148 {
149  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
150  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
151 
152  QString a0( a ), b0( b );
153  a0.replace( re, "#" );
154  b0.replace( re, "#" );
155 
156  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
157 
158  if ( a0 != b0 )
159  return false;
160 
161  QList<double> al, bl;
162 
163  int pos;
164  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
165  {
166  al << re.cap( 0 ).toDouble();
167  }
168  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
169  {
170  bl << re.cap( 0 ).toDouble();
171  }
172 
173  if ( al.size() != bl.size() )
174  return false;
175 
176  for ( int i = 0; i < al.size(); i++ )
177  {
178  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
179  return false;
180  }
181 
182  return true;
183 }
184 
185 #endif
void setColorTolerance(unsigned int theColorTolerance)
Set tolerance for color components used by runTest() and compareImages().
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
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.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:324
unsigned int mMismatchCount
~QgsRenderChecker()
Destructor.
void setControlPathPrefix(const QString theName)
Prefix where the control images are kept.
unsigned int mMatchTarget
unsigned int matchTarget()
QgsMapSettings mMapSettings
unsigned int mColorTolerance
void setRenderedImage(QString theImageFileName)
unsigned int mismatchCount()
void setElapsedTimeTarget(int theTarget)
bool compareWkt(QString a, QString b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.