QGIS API Documentation  2.99.0-Master (ef89a62)
qgis.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgis.h - QGIS namespace
3  -------------------
4  begin : Sat Jun 30 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGIS_H
19 #define QGIS_H
20 
21 #include <QEvent>
22 #include "qgis_sip.h"
23 #include <QString>
24 #include <QRegExp>
25 #include <QMetaType>
26 #include <QVariant>
27 #include <QDateTime>
28 #include <QDate>
29 #include <QTime>
30 #include <QHash>
31 #include <stdlib.h>
32 #include <cfloat>
33 #include <cmath>
34 #include <qnumeric.h>
35 
36 #include "qgswkbtypes.h"
37 #include "qgis_core.h"
38 #include "qgis_sip.h"
39 
40 #ifdef SIP_RUN
41 % ModuleHeaderCode
42 #include <qgis.h>
43 % End
44 
45 % ModuleCode
46 int QgisEvent = QEvent::User + 1;
47 % End
48 #endif
49 
50 
54 class CORE_EXPORT Qgis
55 {
56  public:
57  // Version constants
58  //
60  static const QString QGIS_VERSION;
62  static const int QGIS_VERSION_INT;
64  static const QString QGIS_RELEASE_NAME;
66  static const char *QGIS_DEV_VERSION;
67 
68  // Enumerations
69  //
70 
74  enum DataType
75  {
76  UnknownDataType = 0,
77  Byte = 1,
78  UInt16 = 2,
79  Int16 = 3,
80  UInt32 = 4,
81  Int32 = 5,
82  Float32 = 6,
83  Float64 = 7,
84  CInt16 = 8,
85  CInt32 = 9,
86  CFloat32 = 10,
87  CFloat64 = 11,
88  ARGB32 = 12,
89  ARGB32_Premultiplied = 13
90  };
91 
94  static const double DEFAULT_SEARCH_RADIUS_MM;
95 
97  static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
98 
102  static const QColor DEFAULT_HIGHLIGHT_COLOR;
103 
106  static const double DEFAULT_HIGHLIGHT_BUFFER_MM;
107 
110  static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM;
111 
116  static const double SCALE_PRECISION;
117 
121  static const double DEFAULT_Z_COORDINATE;
122 
128  static const double UI_SCALE_FACTOR;
129 
130 };
131 
132 // hack to workaround warnings when casting void pointers
133 // retrieved from QLibrary::resolve to function pointers.
134 // It's assumed that this works on all systems supporting
135 // QLibrary
136 #define cast_to_fptr(f) f
137 
138 
146 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
147 template<class Object> class QgsSignalBlocker SIP_SKIP SIP_SKIP // clazy:exclude=rule-of-three
148 {
149  public:
150 
154  explicit QgsSignalBlocker( Object *object )
155  : mObject( object )
156  , mPreviousState( object->blockSignals( true ) )
157  {}
158 
160  {
161  mObject->blockSignals( mPreviousState );
162  }
163 
165  Object *operator->() { return mObject; }
166 
167  private:
168 
169  Object *mObject = nullptr;
170  bool mPreviousState;
171 
172 };
173 
186 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
187 template<class Object> inline QgsSignalBlocker<Object> whileBlocking( Object *object ) SIP_SKIP SIP_SKIP
188 {
189  return QgsSignalBlocker<Object>( object );
190 }
191 
193 CORE_EXPORT uint qHash( const QVariant &variant );
194 
198 inline QString qgsDoubleToString( double a, int precision = 17 )
199 {
200  if ( precision )
201  return QString::number( a, 'f', precision ).remove( QRegExp( "\\.?0+$" ) );
202  else
203  return QString::number( a, 'f', precision );
204 }
205 
210 inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON )
211 {
212  const double diff = a - b;
213  return diff > -epsilon && diff <= epsilon;
214 }
215 
220 inline bool qgsFloatNear( float a, float b, float epsilon = 4 * FLT_EPSILON )
221 {
222  const float diff = a - b;
223  return diff > -epsilon && diff <= epsilon;
224 }
225 
227 inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
228 {
229  // The most simple would be to print numbers as %.xe and compare as strings
230  // but that is probably too costly
231  // Then the fastest would be to set some bits directly, but little/big endian
232  // has to be considered (maybe TODO)
233  // Is there a better way?
234  int aexp, bexp;
235  double ar = frexp( a, &aexp );
236  double br = frexp( b, &bexp );
237 
238  return aexp == bexp &&
239  qRound( ar * pow( 10.0, significantDigits ) ) == qRound( br * pow( 10.0, significantDigits ) );
240 }
241 
243 inline double qgsRound( double x )
244 {
245  return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 );
246 }
247 
253 inline double qgsRound( double number, double places )
254 {
255  int scaleFactor = pow( 10, places );
256  return static_cast<double>( static_cast<qlonglong>( number * scaleFactor + 0.5 ) ) / scaleFactor;
257 }
258 
267 CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
268 
277 CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );
278 
283 CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
284 
289 CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
290 
291 CORE_EXPORT QString qgsVsiPrefix( const QString &path );
292 
297 void CORE_EXPORT *qgsMalloc( size_t size ) SIP_SKIP;
298 
305 void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size ) SIP_SKIP;
306 
310 void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
311 
315 extern CORE_EXPORT const QString GEOWKT;
316 extern CORE_EXPORT const QString PROJECT_SCALES;
317 
319 extern CORE_EXPORT const QString GEOPROJ4;
321 const long GEOSRID = 4326;
323 const long GEOCRS_ID = 3452;
325 const long GEO_EPSG_CRS_ID = 4326;
327 extern CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID;
328 
331 const int USER_CRS_START_ID = 100000;
332 
334 extern CORE_EXPORT const QString GEO_NONE;
335 
336 //
337 // Constants for point symbols
338 //
339 
341 const double DEFAULT_POINT_SIZE = 2.0;
342 const double DEFAULT_LINE_WIDTH = 0.26;
343 
345 const double DEFAULT_SEGMENT_EPSILON = 1e-8;
346 
347 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
348 
355 typedef unsigned long long qgssize;
356 
357 #ifndef SIP_RUN
358 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
359 
360 #define Q_NOWARN_DEPRECATED_PUSH \
361  _Pragma("GCC diagnostic push") \
362  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
363 #define Q_NOWARN_DEPRECATED_POP \
364  _Pragma("GCC diagnostic pop");
365 #define Q_NOWARN_UNREACHABLE_PUSH
366 #define Q_NOWARN_UNREACHABLE_POP
367 
368 #elif defined(_MSC_VER)
369 
370 #define Q_NOWARN_DEPRECATED_PUSH \
371  __pragma(warning(push)) \
372  __pragma(warning(disable:4996))
373 #define Q_NOWARN_DEPRECATED_POP \
374  __pragma(warning(pop))
375 #define Q_NOWARN_UNREACHABLE_PUSH \
376  __pragma(warning(push)) \
377  __pragma(warning(disable:4702))
378 #define Q_NOWARN_UNREACHABLE_POP \
379  __pragma(warning(pop))
380 
381 #else
382 
383 #define Q_NOWARN_DEPRECATED_PUSH
384 #define Q_NOWARN_DEPRECATED_POP
385 #define Q_NOWARN_UNREACHABLE_PUSH
386 #define Q_NOWARN_UNREACHABLE_POP
387 
388 #endif
389 #endif
390 
391 #ifndef QGISEXTERN
392 #ifdef Q_OS_WIN
393 # define QGISEXTERN extern "C" __declspec( dllexport )
394 # ifdef _MSC_VER
395 // do not warn about C bindings returning QString
396 # pragma warning(disable:4190)
397 # endif
398 #else
399 # if defined(__GNUC__) || defined(__clang__)
400 # define QGISEXTERN extern "C" __attribute__ ((visibility ("default")))
401 # else
402 # define QGISEXTERN extern "C"
403 # endif
404 #endif
405 #endif
406 #endif
407 
408 #if __cplusplus >= 201500
409 #define FALLTHROUGH [[fallthrough]];
410 #elif defined(__clang__)
411 #define FALLTHROUGH //[[clang::fallthrough]]
412 #elif defined(__GNUC__) && __GNUC__ >= 7
413 #define FALLTHROUGH [[gnu::fallthrough]];
414 #else
415 #define FALLTHROUGH
416 #endif
417 
418 
CORE_EXPORT QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:215
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:60
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:66
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:128
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference)
Definition: qgis.h:220
DataType
Raster data types.
Definition: qgis.h:74
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:102
void CORE_EXPORT * qgsCalloc(size_t nmemb, size_t size)
Allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the alloc...
Definition: qgis.cpp:122
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:62
QMap< QString, QString > QgsStringMap
Definition: qgis.h:347
~QgsSignalBlocker()
Definition: qgis.h:159
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:210
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:54
CORE_EXPORT bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition: qgis.cpp:142
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value for 2.5d geometry This value have to be assigned to the Z coordinate for t...
Definition: qgis.h:121
double qgsRound(double x)
A round function which returns a double to guard against overflows.
Definition: qgis.h:243
QgsSignalBlocker(Object *object)
Constructor for QgsSignalBlocker.
Definition: qgis.h:154
CORE_EXPORT const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:71
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:345
#define SIP_SKIP
Definition: qgis_sip.h:119
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:198
const long GEOCRS_ID
Magic number for a geographic coord sys in QGIS srs.db tbl_srs.srs_id.
Definition: qgis.h:323
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:94
CORE_EXPORT double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition: qgis.cpp:93
CORE_EXPORT const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:50
RAII signal blocking class.
Definition: qgis.h:147
void CORE_EXPORT qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:137
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Definition: qgis.h:355
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:341
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:97
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/...
Definition: qgis.h:331
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:187
const long GEOSRID
Magic number for a geographic coord sys in POSTGIS SRID.
Definition: qgis.h:321
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:64
bool qgsDoubleNearSig(double a, double b, int significantDigits=10)
Compare two doubles using specified number of significant digits.
Definition: qgis.h:227
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:342
CORE_EXPORT const QString PROJECT_SCALES
Definition: qgis.cpp:65
CORE_EXPORT uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:232
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:110
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:106
CORE_EXPORT int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
Definition: qgis.cpp:100
CORE_EXPORT bool qgsVariantGreaterThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is greater than the second.
Definition: qgis.cpp:210
void CORE_EXPORT * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:107
Object * operator->()
Returns pointer to blocked QObject.
Definition: qgis.h:165
const long GEO_EPSG_CRS_ID
Magic number for a geographic coord sys in EpsgCrsId ID format.
Definition: qgis.h:325
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:116
CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:69
CORE_EXPORT const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:52