QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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_core.h"
20 #include "qgis_sip.h"
21 #include <QDir>
22 #include <QString>
23 #include <QRegExp>
24 #include <QList>
25 
26 #include "qgslogger.h"
27 #include "qgsmapsettings.h"
28 #include "qgsdartmeasurement.h"
29 
30 class QImage;
31 
38 class CORE_EXPORT QgsRenderChecker
39 {
40  public:
41 
45  QgsRenderChecker() = default;
46 
47  QString controlImagePath() const;
48 
49  QString report() { return mReport; }
50 
51  float matchPercent()
52  {
53  return static_cast<float>( mMismatchCount ) /
54  static_cast<float>( mMatchTarget ) * 100;
55  }
56  unsigned int mismatchCount() { return mMismatchCount; }
57  unsigned int matchTarget() { return mMatchTarget; }
58  //only records time for actual render part
59  int elapsedTime() { return mElapsedTime; }
60  void setElapsedTimeTarget( int target ) { mElapsedTimeTarget = target; }
61 
67  void setControlName( const QString &name );
68 
73  void setControlPathPrefix( const QString &name ) { mControlPathPrefix = name + '/'; }
74 
75  void setControlPathSuffix( const QString &name );
76 
78  QString imageToHash( const QString &imageFile );
79 
80  void setRenderedImage( const QString &imageFileName ) { mRenderedImageFile = imageFileName; }
81 
88  QString renderedImage() { return mRenderedImageFile; }
89 
91  void setMapSettings( const QgsMapSettings &mapSettings );
92 
100  void setColorTolerance( unsigned int colorTolerance ) { mColorTolerance = colorTolerance; }
101 
108  void setSizeTolerance( int xTolerance, int yTolerance ) { mMaxSizeDifferenceX = xTolerance; mMaxSizeDifferenceY = yTolerance; }
109 
120  bool runTest( const QString &testName, unsigned int mismatchCount = 0 );
121 
133  bool compareImages( const QString &testName, unsigned int mismatchCount = 0, const QString &renderedImageFile = QString() );
134 
143  bool isKnownAnomaly( const QString &diffImageFile );
144 
149  static void drawBackground( QImage *image );
150 
156  QString expectedImageFile() const { return mExpectedImageFile; }
157 
165  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
166 
173  QVector<QgsDartMeasurement> dartMeasurements() const { return mDashMessages; }
174 
175  protected:
176  QString mReport;
177  unsigned int mMatchTarget = 0;
178  int mElapsedTime = 0;
181 
182  private:
183  void emitDashMessage( const QgsDartMeasurement &dashMessage );
184  void emitDashMessage( const QString &name, QgsDartMeasurement::Type type, const QString &value );
185 
186  QString mControlName;
187  unsigned int mMismatchCount = 0;
188  unsigned int mColorTolerance = 0;
189  int mMaxSizeDifferenceX = 0;
190  int mMaxSizeDifferenceY = 0;
191  int mElapsedTimeTarget = 0;
192  QgsMapSettings mMapSettings;
193  QString mControlPathPrefix;
194  QString mControlPathSuffix;
195  QVector<QgsDartMeasurement> mDashMessages;
196  bool mBufferDashMessages = false;
197 }; // class QgsRenderChecker
198 
199 
208 inline bool compareWkt( const QString &a, const QString &b, double tolerance = 0.000001 )
209 {
210  QgsDebugMsg( QStringLiteral( "a:%1 b:%2 tol:%3" ).arg( a, b ).arg( tolerance ) );
211  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
212 
213  QString a0( a ), b0( b );
214  a0.replace( re, QStringLiteral( "#" ) );
215  b0.replace( re, QStringLiteral( "#" ) );
216 
217  QgsDebugMsg( QStringLiteral( "a0:%1 b0:%2" ).arg( a0, b0 ) );
218 
219  if ( a0 != b0 )
220  return false;
221 
222  QList<double> al, bl;
223 
224  int pos;
225  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
226  {
227  al << re.cap( 0 ).toDouble();
228  }
229  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
230  {
231  bl << re.cap( 0 ).toDouble();
232  }
233 
234  if ( al.size() != bl.size() )
235  return false;
236 
237  for ( int i = 0; i < al.size(); i++ )
238  {
239  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
240  return false;
241  }
242 
243  return true;
244 }
245 
246 #endif
QgsRenderChecker::mExpectedImageFile
QString mExpectedImageFile
Definition: qgsrenderchecker.h:180
QgsRenderChecker::setElapsedTimeTarget
void setElapsedTimeTarget(int target)
Definition: qgsrenderchecker.h:60
QgsRenderChecker::elapsedTime
int elapsedTime()
Definition: qgsrenderchecker.h:59
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
qgsmapsettings.h
QgsRenderChecker::QgsRenderChecker
QgsRenderChecker()=default
Constructor for QgsRenderChecker.
QgsRenderChecker::matchPercent
float matchPercent()
Definition: qgsrenderchecker.h:51
QgsRenderChecker::report
QString report()
Definition: qgsrenderchecker.h:49
QgsRenderChecker::mReport
QString mReport
Definition: qgsrenderchecker.h:176
QgsRenderChecker::matchTarget
unsigned int matchTarget()
Definition: qgsrenderchecker.h:57
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsRenderChecker::expectedImageFile
QString expectedImageFile() const
Returns the path to the expected image file.
Definition: qgsrenderchecker.h:156
QgsRenderChecker::setColorTolerance
void setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() and compareImages().
Definition: qgsrenderchecker.h:100
qgis_sip.h
qgsdartmeasurement.h
QgsDartMeasurement::Type
Type
Definition: qgsdartmeasurement.h:31
QgsRenderChecker::renderedImage
QString renderedImage()
The path of the rendered image can be retrieved through that method.
Definition: qgsrenderchecker.h:88
QgsRenderChecker::setSizeTolerance
void setSizeTolerance(int xTolerance, int yTolerance)
Sets the largest allowable difference in size between the rendered and the expected image.
Definition: qgsrenderchecker.h:108
QgsRenderChecker::mismatchCount
unsigned int mismatchCount()
Definition: qgsrenderchecker.h:56
QgsDartMeasurement
Definition: qgsdartmeasurement.h:28
QgsRenderChecker::enableDashBuffering
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
Definition: qgsrenderchecker.h:165
compareWkt
bool compareWkt(const QString &a, const QString &b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.
Definition: qgsrenderchecker.h:208
QgsRenderChecker::setControlPathPrefix
void setControlPathPrefix(const QString &name)
Prefix where the control images are kept.
Definition: qgsrenderchecker.h:73
QgsRenderChecker::setRenderedImage
void setRenderedImage(const QString &imageFileName)
Definition: qgsrenderchecker.h:80
QgsRenderChecker
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
Definition: qgsrenderchecker.h:39
qgslogger.h
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsRenderChecker::mRenderedImageFile
QString mRenderedImageFile
Definition: qgsrenderchecker.h:179
QgsRenderChecker::dartMeasurements
QVector< QgsDartMeasurement > dartMeasurements() const
Gets access to buffered dash messages.
Definition: qgsrenderchecker.h:173