QGIS API Documentation  2.8.2-Wien
 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 #include "qgsdartmeasurement.h"
29 
30 class QImage;
31 
37 class CORE_EXPORT QgsRenderChecker
38 {
39  public:
40 
42 
45 
46  QString controlImagePath() const;
47 
48  QString report() { return mReport; };
49  float matchPercent()
50  {
51  return static_cast<float>( mMismatchCount ) /
52  static_cast<float>( mMatchTarget ) * 100;
53  }
54  unsigned int mismatchCount() { return mMismatchCount; }
55  unsigned int matchTarget() { return mMatchTarget; }
56  //only records time for actual render part
57  int elapsedTime() { return mElapsedTime; }
58  void setElapsedTimeTarget( int theTarget ) { mElapsedTimeTarget = theTarget; };
59 
64  void setControlName( const QString &theName );
65 
69  void setControlPathPrefix( const QString &theName ) { mControlPathPrefix = theName + QDir::separator(); }
70 
71  void setControlPathSuffix( const QString& theName ) { mControlPathSuffix = theName + QDir::separator(); }
72 
74  QString imageToHash( QString theImageFile );
75 
76  void setRenderedImage( QString theImageFileName ) { mRenderedImageFile = theImageFileName; }
77 
84  const QString& renderedImage() { return mRenderedImageFile; }
85 
87  Q_DECL_DEPRECATED void setMapRenderer( QgsMapRenderer * thepMapRenderer );
88 
90  void setMapSettings( const QgsMapSettings& mapSettings );
91 
98  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
109  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
110 
122  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
130  bool isKnownAnomaly( QString theDiffImageFile );
131 
135  static void drawBackground( QImage* image );
136 
142  const QString& expectedImageFile() const { return mExpectedImageFile; }
143 
151  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
152 
160  const QVector<QgsDartMeasurement>& dartMeasurements() const { return mDashMessages; }
161 
162  protected:
163  QString mReport;
164  unsigned int mMatchTarget;
168 
169  private:
170  void emitDashMessage( const QgsDartMeasurement& dashMessage );
171  void emitDashMessage( const QString& name, QgsDartMeasurement::Type type, const QString& value );
172 
173  QString mControlName;
174  unsigned int mMismatchCount;
175  unsigned int mColorTolerance;
176  int mElapsedTimeTarget;
177  QgsMapSettings mMapSettings;
178  QString mControlPathPrefix;
179  QString mControlPathSuffix;
180  QVector<QgsDartMeasurement> mDashMessages;
181  bool mBufferDashMessages;
182 }; // class QgsRenderChecker
183 
184 
192 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
193 {
194  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
195  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
196 
197  QString a0( a ), b0( b );
198  a0.replace( re, "#" );
199  b0.replace( re, "#" );
200 
201  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
202 
203  if ( a0 != b0 )
204  return false;
205 
206  QList<double> al, bl;
207 
208  int pos;
209  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
210  {
211  al << re.cap( 0 ).toDouble();
212  }
213  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
214  {
215  bl << re.cap( 0 ).toDouble();
216  }
217 
218  if ( al.size() != bl.size() )
219  return false;
220 
221  for ( int i = 0; i < al.size(); i++ )
222  {
223  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
224  return false;
225  }
226 
227  return true;
228 }
229 
230 #endif