QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 <QMetaEnum>
22 #include <cfloat>
23 #include <memory>
24 #include <cmath>
25 
26 #include "qgstolerance.h"
27 #include "qgis_core.h"
28 #include "qgis_sip.h"
29 
30 #ifdef SIP_RUN
31 % ModuleHeaderCode
32 #include <qgis.h>
33 % End
34 
35 % ModuleCode
36 int QgisEvent = QEvent::User + 1;
37 % End
38 #endif
39 
40 
45 class CORE_EXPORT Qgis
46 {
47  Q_GADGET
48  public:
49  // Version constants
50  //
52  static const QString QGIS_VERSION;
54  static const int QGIS_VERSION_INT;
56  static const QString QGIS_RELEASE_NAME;
58  static const char *QGIS_DEV_VERSION;
59 
60  // Enumerations
61  //
62 
68  {
69  Info = 0,
70  Warning = 1,
71  Critical = 2,
72  Success = 3,
73  None = 4
74  };
75 
80  enum DataType
81  {
82  UnknownDataType = 0,
83  Byte = 1,
84  UInt16 = 2,
85  Int16 = 3,
86  UInt32 = 4,
87  Int32 = 5,
88  Float32 = 6,
89  Float64 = 7,
90  CInt16 = 8,
91  CInt32 = 9,
92  CFloat32 = 10,
93  CFloat64 = 11,
94  ARGB32 = 12,
95  ARGB32_Premultiplied = 13
96  };
97 
103  {
104  Never = 0,
105  Ask = 1,
106  SessionOnly = 2,
107  Always = 3,
109  };
110  Q_ENUM( PythonMacroMode )
111 
112 
115  static const double DEFAULT_SEARCH_RADIUS_MM;
116 
118  static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
119 
124  static const QColor DEFAULT_HIGHLIGHT_COLOR;
125 
129  static const double DEFAULT_HIGHLIGHT_BUFFER_MM;
130 
134  static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM;
135 
141  static const double SCALE_PRECISION;
142 
147  static const double DEFAULT_Z_COORDINATE;
148 
154  static const double UI_SCALE_FACTOR;
155 
160  static const double DEFAULT_SNAP_TOLERANCE;
161 
166  static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS;
167 };
168 
169 // hack to workaround warnings when casting void pointers
170 // retrieved from QLibrary::resolve to function pointers.
171 // It's assumed that this works on all systems supporting
172 // QLibrary
173 #define cast_to_fptr(f) f
174 
175 
184 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
185 template<class Object> class QgsSignalBlocker SIP_SKIP SIP_SKIP // clazy:exclude=rule-of-three
186 {
187  public:
188 
193  explicit QgsSignalBlocker( Object *object )
194  : mObject( object )
195  , mPreviousState( object->blockSignals( true ) )
196  {}
197 
199  {
200  mObject->blockSignals( mPreviousState );
201  }
202 
204  Object *operator->() { return mObject; }
205 
206  private:
207 
208  Object *mObject = nullptr;
209  bool mPreviousState;
210 
211 };
212 
226 // based on Boojum's code from http://stackoverflow.com/questions/3556687/prevent-firing-signals-in-qt
227 template<class Object> inline QgsSignalBlocker<Object> whileBlocking( Object *object ) SIP_SKIP SIP_SKIP
228 {
229  return QgsSignalBlocker<Object>( object );
230 }
231 
233 CORE_EXPORT uint qHash( const QVariant &variant );
234 
240 inline QString qgsDoubleToString( double a, int precision = 17 )
241 {
242  if ( precision )
243  {
244  QString str = QString::number( a, 'f', precision );
245  if ( str.contains( QLatin1Char( '.' ) ) )
246  {
247  // remove ending 0s
248  int idx = str.length() - 1;
249  while ( str.at( idx ) == '0' && idx > 1 )
250  {
251  idx--;
252  }
253  if ( idx < str.length() - 1 )
254  str.truncate( str.at( idx ) == '.' ? idx : idx + 1 );
255  }
256  return str;
257  }
258  else
259  {
260  // avoid printing -0
261  // see https://bugreports.qt.io/browse/QTBUG-71439
262  const QString str( QString::number( a, 'f', precision ) );
263  if ( str == QLatin1String( "-0" ) )
264  {
265  return QLatin1String( "0" );
266  }
267  else
268  {
269  return str;
270  }
271  }
272 }
273 
280 inline bool qgsDoubleNear( double a, double b, double epsilon = 4 * std::numeric_limits<double>::epsilon() )
281 {
282  if ( std::isnan( a ) || std::isnan( b ) )
283  return std::isnan( a ) && std::isnan( b ) ;
284 
285  const double diff = a - b;
286  return diff > -epsilon && diff <= epsilon;
287 }
288 
295 inline bool qgsFloatNear( float a, float b, float epsilon = 4 * FLT_EPSILON )
296 {
297  if ( std::isnan( a ) || std::isnan( b ) )
298  return std::isnan( a ) && std::isnan( b ) ;
299 
300  const float diff = a - b;
301  return diff > -epsilon && diff <= epsilon;
302 }
303 
305 inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
306 {
307  if ( std::isnan( a ) || std::isnan( b ) )
308  return std::isnan( a ) && std::isnan( b ) ;
309 
310  // The most simple would be to print numbers as %.xe and compare as strings
311  // but that is probably too costly
312  // Then the fastest would be to set some bits directly, but little/big endian
313  // has to be considered (maybe TODO)
314  // Is there a better way?
315  int aexp, bexp;
316  double ar = std::frexp( a, &aexp );
317  double br = std::frexp( b, &bexp );
318 
319  return aexp == bexp &&
320  std::round( ar * std::pow( 10.0, significantDigits ) ) == std::round( br * std::pow( 10.0, significantDigits ) );
321 }
322 
328 inline double qgsRound( double number, int places )
329 {
330  double m = ( number < 0.0 ) ? -1.0 : 1.0;
331  double scaleFactor = std::pow( 10.0, places );
332  return ( std::round( number * m * scaleFactor ) / scaleFactor ) * m;
333 }
334 
335 
336 #ifndef SIP_RUN
337 
339 
349 namespace qgis
350 {
351  // as_const
352 
361  template <typename T> struct QgsAddConst { typedef const T Type; };
362 
363  template <typename T>
364  constexpr typename QgsAddConst<T>::Type &as_const( T &t ) noexcept { return t; }
365 
366  template <typename T>
367  void as_const( const T && ) = delete;
368 
369  // make_unique - from https://stackoverflow.com/a/17902439/1861260
370 
371  template<class T> struct _Unique_if
372  {
373  typedef std::unique_ptr<T> _Single_object;
374  };
375 
376  template<class T> struct _Unique_if<T[]>
377  {
378  typedef std::unique_ptr<T[]> _Unknown_bound;
379  };
380 
381  template<class T, size_t N> struct _Unique_if<T[N]>
382  {
383  typedef void _Known_bound;
384  };
385 
386  template<class T, class... Args>
387  typename _Unique_if<T>::_Single_object
388  make_unique( Args &&... args )
389  {
390  return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
391  }
392 
393  template<class T>
394  typename _Unique_if<T>::_Unknown_bound
395  make_unique( size_t n )
396  {
397  typedef typename std::remove_extent<T>::type U;
398  return std::unique_ptr<T>( new U[n]() );
399  }
400 
401  template<class T, class... Args>
402  typename _Unique_if<T>::_Known_bound
403  make_unique( Args &&... ) = delete;
404 
417  template<typename... Args> struct overload
418  {
419  template<typename C, typename R>
420  static constexpr auto of( R( C::*pmf )( Args... ) ) -> decltype( pmf )
421  {
422  return pmf;
423  }
424  };
425 }
427 #endif
428 
434 template<class T> const QMap<T, QString> qgsEnumMap() SIP_SKIP
435 {
436  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
437  Q_ASSERT( metaEnum.isValid() );
438  QMap<T, QString> enumMap;
439  for ( int idx = 0; idx < metaEnum.keyCount(); ++idx )
440  {
441  const char *enumKey = metaEnum.key( idx );
442  enumMap.insert( static_cast<T>( metaEnum.keyToValue( enumKey ) ), QString( enumKey ) );
443  }
444  return enumMap;
445 }
446 
451 template<class T> QString qgsEnumValueToKey( const T &value ) SIP_SKIP
452 {
453  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
454  Q_ASSERT( metaEnum.isValid() );
455  return QString::fromUtf8( metaEnum.valueToKey( static_cast<int>( value ) ) );
456 }
457 
463 template<class T> T qgsEnumKeyToValue( const QString &key, const T &defaultValue ) SIP_SKIP
464 {
465  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
466  Q_ASSERT( metaEnum.isValid() );
467  bool ok = false;
468  T v = static_cast<T>( metaEnum.keyToValue( key.toUtf8().data(), &ok ) );
469  if ( ok )
470  return v;
471  else
472  return defaultValue;
473 }
474 
475 
485 CORE_EXPORT double qgsPermissiveToDouble( QString string, bool &ok );
486 
496 CORE_EXPORT int qgsPermissiveToInt( QString string, bool &ok );
497 
507 CORE_EXPORT qlonglong qgsPermissiveToLongLong( QString string, bool &ok );
508 
518 CORE_EXPORT bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs );
519 
528 CORE_EXPORT bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs );
529 
530 
537 CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs );
538 
539 CORE_EXPORT QString qgsVsiPrefix( const QString &path );
540 
546 void CORE_EXPORT *qgsMalloc( size_t size ) SIP_SKIP;
547 
555 void CORE_EXPORT *qgsCalloc( size_t nmemb, size_t size ) SIP_SKIP;
556 
561 void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
562 
567 extern CORE_EXPORT const QString GEOWKT;
568 extern CORE_EXPORT const QString PROJECT_SCALES;
569 
571 extern CORE_EXPORT const QString GEOPROJ4;
573 const long GEOSRID = 4326;
575 const long GEOCRS_ID = 3452;
577 const long GEO_EPSG_CRS_ID = 4326;
579 extern CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID;
580 
584 const int USER_CRS_START_ID = 100000;
585 
587 extern CORE_EXPORT const QString GEO_NONE;
588 
589 //
590 // Constants for point symbols
591 //
592 
594 const double DEFAULT_POINT_SIZE = 2.0;
595 const double DEFAULT_LINE_WIDTH = 0.26;
596 
598 const double DEFAULT_SEGMENT_EPSILON = 1e-8;
599 
601 #ifndef SIP_RUN
602 
604 const int PREVIEW_JOB_DELAY_MS = 250;
605 
607 const int MAXIMUM_LAYER_PREVIEW_TIME_MS = 250;
608 #endif
609 
611 
612 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
613 
621 typedef unsigned long long qgssize;
622 
623 #ifndef SIP_RUN
624 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
625 
626 #define Q_NOWARN_DEPRECATED_PUSH \
627  _Pragma("GCC diagnostic push") \
628  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
629 #define Q_NOWARN_DEPRECATED_POP \
630  _Pragma("GCC diagnostic pop");
631 #define Q_NOWARN_UNREACHABLE_PUSH
632 #define Q_NOWARN_UNREACHABLE_POP
633 
634 #elif defined(_MSC_VER)
635 
636 #define Q_NOWARN_DEPRECATED_PUSH \
637  __pragma(warning(push)) \
638  __pragma(warning(disable:4996))
639 #define Q_NOWARN_DEPRECATED_POP \
640  __pragma(warning(pop))
641 #define Q_NOWARN_UNREACHABLE_PUSH \
642  __pragma(warning(push)) \
643  __pragma(warning(disable:4702))
644 #define Q_NOWARN_UNREACHABLE_POP \
645  __pragma(warning(pop))
646 
647 #else
648 
649 #define Q_NOWARN_DEPRECATED_PUSH
650 #define Q_NOWARN_DEPRECATED_POP
651 #define Q_NOWARN_UNREACHABLE_PUSH
652 #define Q_NOWARN_UNREACHABLE_POP
653 
654 #endif
655 #endif
656 
657 #ifndef QGISEXTERN
658 #ifdef Q_OS_WIN
659 # define QGISEXTERN extern "C" __declspec( dllexport )
660 # ifdef _MSC_VER
661 // do not warn about C bindings returning QString
662 # pragma warning(disable:4190)
663 # endif
664 #else
665 # if defined(__GNUC__) || defined(__clang__)
666 # define QGISEXTERN extern "C" __attribute__ ((visibility ("default")))
667 # else
668 # define QGISEXTERN extern "C"
669 # endif
670 #endif
671 #endif
672 #endif
673 
674 #if __cplusplus >= 201500
675 #define FALLTHROUGH [[fallthrough]];
676 #elif defined(__clang__)
677 #define FALLTHROUGH [[clang::fallthrough]];
678 #elif defined(__GNUC__) && __GNUC__ >= 7
679 #define FALLTHROUGH [[gnu::fallthrough]];
680 #else
681 #define FALLTHROUGH
682 #endif
683 
684 // see https://infektor.net/posts/2017-01-19-using-cpp17-attributes-today.html#using-the-nodiscard-attribute
685 #if __cplusplus >= 201703L
686 #define NODISCARD [[nodiscard]]
687 #elif defined(__clang__)
688 #define NODISCARD [[nodiscard]]
689 #elif defined(_MSC_VER)
690 #define NODISCARD // no support
691 #elif defined(__has_cpp_attribute)
692 #if __has_cpp_attribute(nodiscard)
693 #define NODISCARD [[nodiscard]]
694 #elif __has_cpp_attribute(gnu::warn_unused_result)
695 #define NODISCARD [[gnu::warn_unused_result]]
696 #else
697 #define NODISCARD Q_REQUIRED_RESULT
698 #endif
699 #else
700 #define NODISCARD Q_REQUIRED_RESULT
701 #endif
702 
703 #if __cplusplus >= 201703L
704 #define MAYBE_UNUSED [[maybe_unused]]
705 #elif defined(__clang__)
706 #define MAYBE_UNUSED [[maybe_unused]]
707 #elif defined(_MSC_VER)
708 #define MAYBE_UNUSED // no support
709 #elif defined(__has_cpp_attribute)
710 #if __has_cpp_attribute(gnu::unused)
711 #define MAYBE_UNUSED [[gnu::unused]]
712 #else
713 #define MAYBE_UNUSED
714 #endif
715 #else
716 #define MAYBE_UNUSED
717 #endif
718 
719 #ifndef FINAL
720 #define FINAL final
721 #endif
722 
723 
CORE_EXPORT QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:227
int precision
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:52
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:58
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference)
Definition: qgis.h:295
CORE_EXPORT bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, two NULL values are always treated a...
Definition: qgis.cpp:298
QString qgsEnumValueToKey(const T &value)
Returns the value for the given key of an enum.
Definition: qgis.h:451
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:280
DataType
Raster data types.
Definition: qgis.h:80
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:134
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:67
static const int QGIS_VERSION_INT
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.h:54
QMap< QString, QString > QgsStringMap
Definition: qgis.h:612
~QgsSignalBlocker()
Definition: qgis.h:198
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:45
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:154
QgsSignalBlocker(Object *object)
Constructor for QgsSignalBlocker.
Definition: qgis.h:193
T qgsEnumKeyToValue(const QString &key, const T &defaultValue)
Returns the value corresponding to the given key of an enum.
Definition: qgis.h:463
CORE_EXPORT const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
Definition: qgis.cpp:72
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:598
Macros will not be run for this session.
Definition: qgis.h:108
#define SIP_SKIP
Definition: qgis_sip.h:126
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:240
This is the class is providing tolerance value in map unit values.
Definition: qgstolerance.h:32
const long GEOCRS_ID
Magic number for a geographic coord sys in QGIS srs.db tbl_srs.srs_id.
Definition: qgis.h:575
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:98
CORE_EXPORT const QString GEOPROJ4
PROJ4 string that represents a geographic coord sys.
Definition: qgis.cpp:51
RAII signal blocking class.
Definition: qgis.h:185
void CORE_EXPORT qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:149
CORE_EXPORT qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
Definition: qgis.cpp:112
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:621
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:594
const int USER_CRS_START_ID
Magick number that determines whether a projection crsid is a system (srs.db) or user (~/...
Definition: qgis.h:584
const QMap< T, QString > qgsEnumMap()
Returns a map of all enum entries.
Definition: qgis.h:434
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:227
const long GEOSRID
Magic number for a geographic coord sys in POSTGIS SRID.
Definition: qgis.h:573
static const QString QGIS_RELEASE_NAME
Release name.
Definition: qgis.h:56
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places...
Definition: qgis.h:328
bool qgsDoubleNearSig(double a, double b, int significantDigits=10)
Compare two doubles using specified number of significant digits.
Definition: qgis.h:305
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:595
CORE_EXPORT const QString PROJECT_SCALES
Definition: qgis.cpp:66
PythonMacroMode
Authorisation to run Python Macros.
Definition: qgis.h:102
CORE_EXPORT uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:252
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:105
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:222
void CORE_EXPORT * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:119
Object * operator->()
Returns pointer to blocked QObject.
Definition: qgis.h:204
const long GEO_EPSG_CRS_ID
Magic number for a geographic coord sys in EpsgCrsId ID format.
Definition: qgis.h:577
CORE_EXPORT const QString GEO_EPSG_CRS_AUTHID
Geographic coord sys from EPSG authority.
Definition: qgis.cpp:70
CORE_EXPORT const QString GEOWKT
Wkt string that represents a geographic coord sys.
Definition: qgis.cpp:53