QGIS API Documentation  2.2.0-Valmiera
 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; }
71 
78  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
89  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
90 
102  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
110  bool isKnownAnomaly( QString theDiffImageFile );
111 
112  QString expectedImageFile() { return mExpectedImageFile; };
113 
114  protected:
115 
116  QString mReport;
117  unsigned int mMatchTarget;
122 
123  private:
124 
125  QString mControlName;
126  unsigned int mMismatchCount;
127  unsigned int mColorTolerance;
130 
131 }; // class QgsRenderChecker
132 
133 
141 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
142 {
143  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
144  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
145 
146  QString a0( a ), b0( b );
147  a0.replace( re, "#" );
148  b0.replace( re, "#" );
149 
150  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
151 
152  if ( a0 != b0 )
153  return false;
154 
155  QList<double> al, bl;
156 
157  int pos;
158  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
159  {
160  al << re.cap( 0 ).toDouble();
161  }
162  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
163  {
164  bl << re.cap( 0 ).toDouble();
165  }
166 
167  if ( al.size() != bl.size() )
168  return false;
169 
170  for ( int i = 0; i < al.size(); i++ )
171  {
172  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
173  return false;
174  }
175 
176  return true;
177 }
178 
179 #endif