QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
83  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
94  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
95 
107  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
115  bool isKnownAnomaly( QString theDiffImageFile );
116 
117  QString expectedImageFile() { return mExpectedImageFile; };
118 
119  protected:
120  QString mReport;
121  unsigned int mMatchTarget;
125 
126  private:
127  QString mControlName;
128  unsigned int mMismatchCount;
129  unsigned int mColorTolerance;
130  int mElapsedTimeTarget;
131  QgsMapSettings mMapSettings;
132  QString mControlPathPrefix;
133 
134 }; // class QgsRenderChecker
135 
136 
144 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
145 {
146  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
147  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
148 
149  QString a0( a ), b0( b );
150  a0.replace( re, "#" );
151  b0.replace( re, "#" );
152 
153  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
154 
155  if ( a0 != b0 )
156  return false;
157 
158  QList<double> al, bl;
159 
160  int pos;
161  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
162  {
163  al << re.cap( 0 ).toDouble();
164  }
165  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
166  {
167  bl << re.cap( 0 ).toDouble();
168  }
169 
170  if ( al.size() != bl.size() )
171  return false;
172 
173  for ( int i = 0; i < al.size(); i++ )
174  {
175  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
176  return false;
177  }
178 
179  return true;
180 }
181 
182 #endif
void setColorTolerance(unsigned int theColorTolerance)
Set tolerance for color components used by runTest() and compareImages().
Definition: qgsrenderchecker.h:83
float matchPercent()
Definition: qgsrenderchecker.h:48
#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...
Definition: qgsrenderchecker.h:36
A non GUI class for rendering a map layer set onto a QPainter.
Definition: qgsmaprenderer.h:133
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:324
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:52
int elapsedTime()
Definition: qgsrenderchecker.h:56
~QgsRenderChecker()
Destructor.
Definition: qgsrenderchecker.h:43
void setControlPathPrefix(const QString theName)
Prefix where the control images are kept.
Definition: qgsrenderchecker.h:66
unsigned int mMatchTarget
Definition: qgsrenderchecker.h:121
unsigned int matchTarget()
Definition: qgsrenderchecker.h:54
QString mRenderedImageFile
Definition: qgsrenderchecker.h:123
void setRenderedImage(QString theImageFileName)
Definition: qgsrenderchecker.h:70
QString mExpectedImageFile
Definition: qgsrenderchecker.h:124
int mElapsedTime
Definition: qgsrenderchecker.h:122
unsigned int mismatchCount()
Definition: qgsrenderchecker.h:53
void setElapsedTimeTarget(int theTarget)
Definition: qgsrenderchecker.h:57
bool compareWkt(QString a, QString b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.
Definition: qgsrenderchecker.h:144
QString report()
Definition: qgsrenderchecker.h:47