QGIS API Documentation  3.0.2-Girona (307d082)
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 <QString>
23 #include <QRegExp>
24 #include <QMetaType>
25 #include <QVariant>
26 #include <QDateTime>
27 #include <QDate>
28 #include <QTime>
29 #include <QHash>
30 #include <cstdlib>
31 #include <cfloat>
32 #include <memory>
33 #include <type_traits>
34 #include <cmath>
35 #include <qnumeric.h>
36 
37 #include "qgstolerance.h"
38 #include "qgswkbtypes.h"
39 #include "qgis_core.h"
40 #include "qgis_sip.h"
41 
42 #ifdef SIP_RUN
43 % ModuleHeaderCode
44 #include <qgis.h>
45 % End
46 
47 % ModuleCode
48 int QgisEvent = QEvent::User + 1;
49 % End
50 #endif
51 
52 
57 class CORE_EXPORT Qgis
58 {
59  public:
60  // Version constants
61  //
63  static const QString QGIS_VERSION;
65  static const int QGIS_VERSION_INT;
67  static const QString QGIS_RELEASE_NAME;
69  static const char *QGIS_DEV_VERSION;
70 
71  // Enumerations
72  //
73 
79  {
80  Info = 0,
81  Warning = 1,
82  Critical = 2,
83  Success = 3,
84  None = 4
85  };
86 
91  enum DataType
92  {
93  UnknownDataType = 0,
94  Byte = 1,
95  UInt16 = 2,
96  Int16 = 3,
97  UInt32 = 4,
98  Int32 = 5,
99  Float32 = 6,
100  Float64 = 7,
101  CInt16 = 8,
102  CInt32 = 9,
103  CFloat32 = 10,
104  CFloat64 = 11,
105  ARGB32 = 12,
106  ARGB32_Premultiplied = 13
107  };
108 
112  static const double DEFAULT_SEARCH_RADIUS_MM;
113 
115  static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
116 
121  static const QColor DEFAULT_HIGHLIGHT_COLOR;
122 
126  static const double DEFAULT_HIGHLIGHT_BUFFER_MM;
127 
131  static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM;
132 
138  static const double SCALE_PRECISION;
139 
144  static const double DEFAULT_Z_COORDINATE;
145 
151  static const double UI_SCALE_FACTOR;
152 
157  static const double DEFAULT_SNAP_TOLERANCE;
158 
164 };
165 
166 // hack to workaround warnings when casting void pointers
167 // retrieved from QLibrary::resolve to function pointers.
168 // It's assumed that this works on all systems supporting
169 // QLibrary
170 #define cast_to_fptr(f) f
171 
172 
181 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
182 template<class Object> class QgsSignalBlocker SIP_SKIP SIP_SKIP // clazy:exclude=rule-of-three
183 {
184  public:
185 
190  explicit QgsSignalBlocker( Object *object )
191  : mObject( object )
192  , mPreviousState( object->blockSignals( true ) )
193  {}
194 
196  {
197  mObject->blockSignals( mPreviousState );
198  }
199 
201  Object *operator->() { return mObject; }
202 
203  private:
204 
205  Object *mObject = nullptr;
206  bool mPreviousState;
207 
208 };
209 
223 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
224 template<class Object> inline QgsSignalBlocker<Object> whileBlocking( Object *object ) SIP_SKIP SIP_SKIP
225 {
226  return QgsSignalBlocker<Object>( object );
227 }
228 
230 CORE_EXPORT uint qHash( const QVariant &variant );
231 
237 inline QString qgsDoubleToString( double a, int precision = 17 )
238 {
239  if ( precision )
240  return QString::number( a, 'f', precision ).remove( QRegExp( "\\.?0+$" ) );
241  else
242  return QString::number( a, 'f', precision );
243 }
244 
251 inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * DBL_EPSILON )
252 {
253  const double diff = a - b;
254  return diff > -epsilon && diff <= epsilon;
255 }
256 
263 inline bool qgsFloatNear( float a, float b, float epsilon = 4 * FLT_EPSILON )
264 {
265  const float diff = a - b;
266  return diff > -epsilon && diff <= epsilon;
267 }
268 
270 inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
271 {
272  // The most simple would be to print numbers as %.xe and compare as strings
273  // but that is probably too costly
274  // Then the fastest would be to set some bits directly, but little/big endian
275  // has to be considered (maybe TODO)
276  // Is there a better way?
277  int aexp, bexp;
278  double ar = std::frexp( a, &aexp );
279  double br = std::frexp( b, &bexp );
280 
281  return aexp == bexp &&
282  std::round( ar * std::pow( 10.0, significantDigits ) ) == std::round( br * std::pow( 10.0, significantDigits ) );
283 }
284 
290 inline double qgsRound( double number, double places )
291 {
292  int scaleFactor = std::pow( 10, places );
293  return static_cast<double>( static_cast<qlonglong>( number * scaleFactor + 0.5 ) ) / scaleFactor;
294 }
295 
296 
297 #ifndef SIP_RUN
298 
300 
309 namespace qgis
310 {
311  // as_const
312 
321  template <typename T> struct QgsAddConst { typedef const T Type; };
322 
323  template <typename T>
324  constexpr typename QgsAddConst<T>::Type &as_const( T &t ) noexcept { return t; }
325 
326  template <typename T>
327  void as_const( const T && ) = delete;
328 
329  // make_unique - from https://stackoverflow.com/a/17902439/1861260
330 
331  template<class T> struct _Unique_if
332  {
333  typedef std::unique_ptr<T> _Single_object;
334  };
335 
336  template<class T> struct _Unique_if<T[]>
337  {
338  typedef std::unique_ptr<T[]> _Unknown_bound;
339  };
340 
341  template<class T, size_t N> struct _Unique_if<T[N]>
342  {
343  typedef void _Known_bound;
344  };
345 
346  template<class T, class... Args>
347  typename _Unique_if<T>::_Single_object
348  make_unique( Args &&... args )
349  {
350  return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
351  }
352 
353  template<class T>
354  typename _Unique_if<T>::_Unknown_bound
355  make_unique( size_t n )
356  {
357  typedef typename std::remove_extent<T>::type U;
358  return std::unique_ptr<T>( new U[n]() );
359  }
360 
361  template<class T, class... Args>
362  typename _Unique_if<T>::_Known_bound
363  make_unique( Args &&... ) = delete;
364 }
366 #endif
367 
377 CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
378 
388 CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );
389 
396 CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
397 
404 CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
405 
406 CORE_EXPORT QString qgsVsiPrefix( const QString &path );
407 
413 void CORE_EXPORT *qgsMalloc( size_t size ) SIP_SKIP;
414 
422 void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size ) SIP_SKIP;
423 
428 void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
429 
434 extern CORE_EXPORT const QString GEOWKT;
435 extern CORE_EXPORT const QString PROJECT_SCALES;
436 
438 extern CORE_EXPORT const QString GEOPROJ4;
440 const long GEOSRID = 4326;
442 const long GEOCRS_ID = 3452;
444 const long GEO_EPSG_CRS_ID = 4326;
446 extern CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID;
447 
451 const int USER_CRS_START_ID = 100000;
452 
454 extern CORE_EXPORT const QString GEO_NONE;
455 
456 //
457 // Constants for point symbols
458 //
459 
461 const double DEFAULT_POINT_SIZE = 2.0;
462 const double DEFAULT_LINE_WIDTH = 0.26;
463 
465 const double DEFAULT_SEGMENT_EPSILON = 1e-8;
466 
468 #ifndef SIP_RUN
469 
471 const int PREVIEW_JOB_DELAY_MS = 250;
472 
474 const int MAXIMUM_LAYER_PREVIEW_TIME_MS = 250;
475 #endif
476 
478 
479 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
480 
488 typedef unsigned long long qgssize;
489 
490 #ifndef SIP_RUN
491 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
492 
493 #define Q_NOWARN_DEPRECATED_PUSH \
494  _Pragma("GCC diagnostic push") \
495  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
496 #define Q_NOWARN_DEPRECATED_POP \
497  _Pragma("GCC diagnostic pop");
498 #define Q_NOWARN_UNREACHABLE_PUSH
499 #define Q_NOWARN_UNREACHABLE_POP
500 
501 #elif defined(_MSC_VER)
502 
503 #define Q_NOWARN_DEPRECATED_PUSH \
504  __pragma(warning(push)) \
505  __pragma(warning(disable:4996))
506 #define Q_NOWARN_DEPRECATED_POP \
507  __pragma(warning(pop))
508 #define Q_NOWARN_UNREACHABLE_PUSH \
509  __pragma(warning(push)) \
510  __pragma(warning(disable:4702))
511 #define Q_NOWARN_UNREACHABLE_POP \
512  __pragma(warning(pop))
513 
514 #else
515 
516 #define Q_NOWARN_DEPRECATED_PUSH
517 #define Q_NOWARN_DEPRECATED_POP
518 #define Q_NOWARN_UNREACHABLE_PUSH
519 #define Q_NOWARN_UNREACHABLE_POP
520 
521 #endif
522 #endif
523 
524 #ifndef QGISEXTERN
525 #ifdef Q_OS_WIN
526 # define QGISEXTERN extern "C" __declspec( dllexport )
527 # ifdef _MSC_VER
528 // do not warn about C bindings returning QString
529 # pragma warning(disable:4190)
530 # endif
531 #else
532 # if defined(__GNUC__) || defined(__clang__)
533 # define QGISEXTERN extern "C" __attribute__ ((visibility ("default")))
534 # else
535 # define QGISEXTERN extern "C"
536 # endif
537 #endif
538 #endif
539 #endif
540 
541 #if __cplusplus >= 201500
542 #define FALLTHROUGH [[fallthrough]];
543 #elif defined(__clang__)
544 #define FALLTHROUGH //[[clang::fallthrough]]
545 #elif defined(__GNUC__) && __GNUC__ >= 7
546 #define FALLTHROUGH [[gnu::fallthrough]];
547 #else
548 #define FALLTHROUGH
549 #endif
550 
551 
CORE_EXPORT QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:219
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:63
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:69
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:151
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference)
Definition: qgis.h:263
DataType
Raster data types.
Definition: qgis.h:91
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:40
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:121
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:126
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:65
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
~QgsSignalBlocker()
Definition: qgis.h:195
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
double qgsRound(double number, double places)
Returns a double number, rounded (as close as possible) to the specified number of places...
Definition: qgis.h:290
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:57
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:146
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:144
QgsSignalBlocker(Object *object)
Constructor for QgsSignalBlocker.
Definition: qgis.h:190
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:157
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:465
#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:237
const long GEOCRS_ID
Magic number for a geographic coord sys in QGIS srs.db tbl_srs.srs_id.
Definition: qgis.h:442
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:112
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:97
CORE_EXPORT const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:50
RAII signal blocking class.
Definition: qgis.h:182
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:163
void CORE_EXPORT qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:141
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:488
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:461
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:115
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/...
Definition: qgis.h:451
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:224
const long GEOSRID
Magic number for a geographic coord sys in POSTGIS SRID.
Definition: qgis.h:440
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:67
bool qgsDoubleNearSig(double a, double b, int significantDigits=10)
Compare two doubles using specified number of significant digits.
Definition: qgis.h:270
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:462
CORE_EXPORT const QString PROJECT_SCALES
Definition: qgis.cpp:65
CORE_EXPORT uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:236
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:131
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:126
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:104
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:214
void CORE_EXPORT * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:111
Object * operator->()
Returns pointer to blocked QObject.
Definition: qgis.h:201
const long GEO_EPSG_CRS_ID
Magic number for a geographic coord sys in EpsgCrsId ID format.
Definition: qgis.h:444
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:138
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