QGIS API Documentation  2.7.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 #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; }
86  Q_DECL_DEPRECATED void setMapRenderer( QgsMapRenderer * thepMapRenderer );
87 
89  void setMapSettings( const QgsMapSettings& mapSettings );
90 
97  void setColorTolerance( unsigned int theColorTolerance ) { mColorTolerance = theColorTolerance; }
108  bool runTest( QString theTestName, unsigned int theMismatchCount = 0 );
109 
121  bool compareImages( QString theTestName, unsigned int theMismatchCount = 0, QString theRenderedImageFile = "" );
129  bool isKnownAnomaly( QString theDiffImageFile );
130 
134  static void drawBackround( QImage* image );
135 
141  const QString& expectedImageFile() const { return mExpectedImageFile; }
142 
150  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
151 
158  const QVector<QgsDartMeasurement>& dartMeasurements() const { return mDashMessages; }
159 
160  protected:
161  QString mReport;
162  unsigned int mMatchTarget;
166 
167  private:
168  void emitDashMessage( const QgsDartMeasurement& dashMessage );
169  void emitDashMessage( const QString& name, QgsDartMeasurement::Type type, const QString& value );
170 
171  QString mControlName;
172  unsigned int mMismatchCount;
173  unsigned int mColorTolerance;
174  int mElapsedTimeTarget;
175  QgsMapSettings mMapSettings;
176  QString mControlPathPrefix;
177  QString mControlPathSuffix;
178  QVector<QgsDartMeasurement> mDashMessages;
179  bool mBufferDashMessages;
180 }; // class QgsRenderChecker
181 
182 
190 inline bool compareWkt( QString a, QString b, double tolerance = 0.000001 )
191 {
192  QgsDebugMsg( QString( "a:%1 b:%2 tol:%3" ).arg( a ).arg( b ).arg( tolerance ) );
193  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
194 
195  QString a0( a ), b0( b );
196  a0.replace( re, "#" );
197  b0.replace( re, "#" );
198 
199  QgsDebugMsg( QString( "a0:%1 b0:%2" ).arg( a0 ).arg( b0 ) );
200 
201  if ( a0 != b0 )
202  return false;
203 
204  QList<double> al, bl;
205 
206  int pos;
207  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
208  {
209  al << re.cap( 0 ).toDouble();
210  }
211  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
212  {
213  bl << re.cap( 0 ).toDouble();
214  }
215 
216  if ( al.size() != bl.size() )
217  return false;
218 
219  for ( int i = 0; i < al.size(); i++ )
220  {
221  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
222  return false;
223  }
224 
225  return true;
226 }
227 
228 #endif
void setControlPathSuffix(const QString &theName)
void setColorTolerance(unsigned int theColorTolerance)
Set tolerance for color components used by runTest() and compareImages().
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
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:321
The QgsMapSettings class contains configuration for rendering of the map.
~QgsRenderChecker()
Destructor.
void setControlPathPrefix(const QString theName)
Prefix where the control images are kept.
unsigned int mMatchTarget
const QString & renderedImage()
The path of the rendered image can be retrieved through that method.
unsigned int matchTarget()
void setRenderedImage(QString theImageFileName)
unsigned int mismatchCount()
const QVector< QgsDartMeasurement > & dartMeasurements() const
Get access to buffered dash messages.
const QString & expectedImageFile() const
Returns the path to the expected image file.
void setElapsedTimeTarget(int theTarget)
bool compareWkt(QString a, QString b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.