QGIS API Documentation  2.5.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 
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;
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