QGIS API Documentation  3.2.0-Bonn (bc43194)
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 * std::numeric_limits<double>::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 
310 namespace qgis
311 {
312  // as_const
313 
322  template <typename T> struct QgsAddConst { typedef const T Type; };
323 
324  template <typename T>
325  constexpr typename QgsAddConst<T>::Type &as_const( T &t ) noexcept { return t; }
326 
327  template <typename T>
328  void as_const( const T && ) = delete;
329 
330  // make_unique - from https://stackoverflow.com/a/17902439/1861260
331 
332  template<class T> struct _Unique_if
333  {
334  typedef std::unique_ptr<T> _Single_object;
335  };
336 
337  template<class T> struct _Unique_if<T[]>
338  {
339  typedef std::unique_ptr<T[]> _Unknown_bound;
340  };
341 
342  template<class T, size_t N> struct _Unique_if<T[N]>
343  {
344  typedef void _Known_bound;
345  };
346 
347  template<class T, class... Args>
348  typename _Unique_if<T>::_Single_object
349  make_unique( Args &&... args )
350  {
351  return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
352  }
353 
354  template<class T>
355  typename _Unique_if<T>::_Unknown_bound
356  make_unique( size_t n )
357  {
358  typedef typename std::remove_extent<T>::type U;
359  return std::unique_ptr<T>( new U[n]() );
360  }
361 
362  template<class T, class... Args>
363  typename _Unique_if<T>::_Known_bound
364  make_unique( Args &&... ) = delete;
365 
378  template<typename... Args> struct overload
379  {
380  template<typename C, typename R>
381  static constexpr auto of( R( C::*pmf )( Args... ) ) -> decltype( pmf )
382  {
383  return pmf;
384  }
385  };
386 }
388 #endif
389 
399 CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
400 
410 CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );
411 
418 CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
419 
426 CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
427 
428 CORE_EXPORT QString qgsVsiPrefix( const QString &path );
429 
435 void CORE_EXPORT *qgsMalloc( size_t size ) SIP_SKIP;
436 
444 void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size ) SIP_SKIP;
445 
450 void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
451 
456 extern CORE_EXPORT const QString GEOWKT;
457 extern CORE_EXPORT const QString PROJECT_SCALES;
458 
460 extern CORE_EXPORT const QString GEOPROJ4;
462 const long GEOSRID = 4326;
464 const long GEOCRS_ID = 3452;
466 const long GEO_EPSG_CRS_ID = 4326;
468 extern CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID;
469 
473 const int USER_CRS_START_ID = 100000;
474 
476 extern CORE_EXPORT const QString GEO_NONE;
477 
478 //
479 // Constants for point symbols
480 //
481 
483 const double DEFAULT_POINT_SIZE = 2.0;
484 const double DEFAULT_LINE_WIDTH = 0.26;
485 
487 const double DEFAULT_SEGMENT_EPSILON = 1e-8;
488 
490 #ifndef SIP_RUN
491 
493 const int PREVIEW_JOB_DELAY_MS = 250;
494 
496 const int MAXIMUM_LAYER_PREVIEW_TIME_MS = 250;
497 #endif
498 
500 
501 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
502 
510 typedef unsigned long long qgssize;
511 
512 #ifndef SIP_RUN
513 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
514 
515 #define Q_NOWARN_DEPRECATED_PUSH \
516  _Pragma("GCC diagnostic push") \
517  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
518 #define Q_NOWARN_DEPRECATED_POP \
519  _Pragma("GCC diagnostic pop");
520 #define Q_NOWARN_UNREACHABLE_PUSH
521 #define Q_NOWARN_UNREACHABLE_POP
522 
523 #elif defined(_MSC_VER)
524 
525 #define Q_NOWARN_DEPRECATED_PUSH \
526  __pragma(warning(push)) \
527  __pragma(warning(disable:4996))
528 #define Q_NOWARN_DEPRECATED_POP \
529  __pragma(warning(pop))
530 #define Q_NOWARN_UNREACHABLE_PUSH \
531  __pragma(warning(push)) \
532  __pragma(warning(disable:4702))
533 #define Q_NOWARN_UNREACHABLE_POP \
534  __pragma(warning(pop))
535 
536 #else
537 
538 #define Q_NOWARN_DEPRECATED_PUSH
539 #define Q_NOWARN_DEPRECATED_POP
540 #define Q_NOWARN_UNREACHABLE_PUSH
541 #define Q_NOWARN_UNREACHABLE_POP
542 
543 #endif
544 #endif
545 
546 #ifndef QGISEXTERN
547 #ifdef Q_OS_WIN
548 # define QGISEXTERN extern "C" __declspec( dllexport )
549 # ifdef _MSC_VER
550 // do not warn about C bindings returning QString
551 # pragma warning(disable:4190)
552 # endif
553 #else
554 # if defined(__GNUC__) || defined(__clang__)
555 # define QGISEXTERN extern "C" __attribute__ ((visibility ("default")))
556 # else
557 # define QGISEXTERN extern "C"
558 # endif
559 #endif
560 #endif
561 #endif
562 
563 #if __cplusplus >= 201500
564 #define FALLTHROUGH [[fallthrough]];
565 #elif defined(__clang__)
566 #define FALLTHROUGH //[[clang::fallthrough]]
567 #elif defined(__GNUC__) && __GNUC__ >= 7
568 #define FALLTHROUGH [[gnu::fallthrough]];
569 #else
570 #define FALLTHROUGH
571 #endif
572 
573 // see https://infektor.net/posts/2017-01-19-using-cpp17-attributes-today.html#using-the-nodiscard-attribute
574 #if __cplusplus >= 201703L
575 #define NODISCARD [[nodiscard]]
576 #elif defined(__clang__)
577 #define NODISCARD [[nodiscard]]
578 #elif defined(_MSC_VER)
579 #define NODISCARD // no support
580 #elif defined(__has_cpp_attribute)
581 #if __has_cpp_attribute(nodiscard)
582 #define NODISCARD [[nodiscard]]
583 #elif __has_cpp_attribute(gnu::warn_unused_result)
584 #define NODISCARD [[gnu::warn_unused_result]]
585 #else
586 #define NODISCARD Q_REQUIRED_RESULT
587 #endif
588 #else
589 #define NODISCARD Q_REQUIRED_RESULT
590 #endif
591 
592 #if __cplusplus >= 201703L
593 #define MAYBE_UNUSED [[maybe_unused]]
594 #elif defined(__clang__)
595 #define MAYBE_UNUSED [[maybe_unused]]
596 #elif defined(_MSC_VER)
597 #define MAYBE_UNUSED // no support
598 #elif defined(__has_cpp_attribute)
599 #if __has_cpp_attribute(gnu::unused)
600 #define MAYBE_UNUSED [[gnu::unused]]
601 #else
602 #define MAYBE_UNUSED
603 #endif
604 #else
605 #define MAYBE_UNUSED
606 #endif
607 
608 
609 
610 
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:251
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:501
~QgsSignalBlocker()
Definition: qgis.h:195
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:487
#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:464
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:510
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:483
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:473
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:462
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:484
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:466
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