QGIS API Documentation  2.0.1-Dufour
 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 
28 class QImage;
29 
35 class CORE_EXPORT QgsRenderChecker
36 {
37  public:
38 
40 
43 
44  QString controlImagePath() const;
45 
46  QString report() { return mReport; };
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 theTarget ) { mElapsedTimeTarget = theTarget; };
61  void setControlName( const QString theName );
65  void setControlPathPrefix( const QString theName ) { mControlPathPrefix = theName + QDir::separator(); }
67  QString imageToHash( QString theImageFile );
68 
69  void setRenderedImage( QString theImageFileName ) { mRenderedImageFile = theImageFileName; };
70  void setMapRenderer( QgsMapRenderer * thepMapRenderer ) { mpMapRenderer = thepMapRenderer; };
81  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
82 
94  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
102  bool isKnownAnomaly( QString theDiffImageFile );
103 
104  private:
105 
106  QString mReport;
108  QString mControlName;
110  unsigned int mMismatchCount;
111  unsigned int mMatchTarget;
116 
117 }; // class QgsRenderChecker
118 
119 
127 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
128 {
129  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
130  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
131 
132  QString a0( a ), b0( b );
133  a0.replace( re, "#" );
134  b0.replace( re, "#" );
135 
136  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
137 
138  if ( a0 != b0 )
139  return false;
140 
141  QList<double> al, bl;
142 
143  int pos;
144  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
145  {
146  al << re.cap( 0 ).toDouble();
147  }
148  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
149  {
150  bl << re.cap( 0 ).toDouble();
151  }
152 
153  if ( al.size() != bl.size() )
154  return false;
155 
156  for ( int i = 0; i < al.size(); i++ )
157  {
158  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
159  return false;
160  }
161 
162  return true;
163 }
164 
165 #endif
QgsMapRenderer * mpMapRenderer
#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...
void setMapRenderer(QgsMapRenderer *thepMapRenderer)
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:258
unsigned int mMismatchCount
~QgsRenderChecker()
Destructor.
void setControlPathPrefix(const QString theName)
Prefix where the control images are kept.
unsigned int mMatchTarget
unsigned int matchTarget()
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.