QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgis.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgis.cpp
3 
4  -------------------
5  begin : 2007
6  copyright : (C) 2007 by Gary E. Sherman
7  email : [email protected]
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 #include "qgis.h"
19 #ifndef QGSVERSION
20 #include "qgsversion.h"
21 #endif
22 #include <QCoreApplication>
23 #include <QColor>
24 #include <QDate>
25 #include <QTime>
26 #include <QLocale>
27 #include <QDateTime>
28 #include "qgsconfig.h"
29 #include "qgslogger.h"
30 #include "qgswkbtypes.h"
31 
32 #include <gdal.h>
33 #include <ogr_api.h>
34 
35 // Version constants
36 //
37 
38 // development version
39 const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
40 
41 const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
42 
43 const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
44 
45 const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
46 
47 const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
48 
49 const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
50 
51 const double Qgis::SCALE_PRECISION = 0.9999999999;
52 
53 const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
54 
55 const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
56 
58 
59 #ifdef Q_OS_WIN
60 const double Qgis::UI_SCALE_FACTOR = 1.5;
61 #else
62 const double Qgis::UI_SCALE_FACTOR = 1;
63 #endif
64 
65 double qgsPermissiveToDouble( QString string, bool &ok )
66 {
67  //remove any thousands separators
68  string.remove( QLocale().groupSeparator() );
69  return QLocale().toDouble( string, &ok );
70 }
71 
72 int qgsPermissiveToInt( QString string, bool &ok )
73 {
74  //remove any thousands separators
75  string.remove( QLocale().groupSeparator() );
76  return QLocale().toInt( string, &ok );
77 }
78 
79 qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
80 {
81  //remove any thousands separators
82  string.remove( QLocale().groupSeparator() );
83  return QLocale().toLongLong( string, &ok );
84 }
85 
86 void *qgsMalloc( size_t size )
87 {
88  if ( size == 0 || long( size ) < 0 )
89  {
90  QgsDebugMsg( QStringLiteral( "Negative or zero size %1." ).arg( size ) );
91  return nullptr;
92  }
93  void *p = malloc( size );
94  if ( !p )
95  {
96  QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
97  }
98  return p;
99 }
100 
101 void *qgsCalloc( size_t nmemb, size_t size )
102 {
103  if ( nmemb == 0 || long( nmemb ) < 0 || size == 0 || long( size ) < 0 )
104  {
105  QgsDebugMsg( QStringLiteral( "Negative or zero nmemb %1 or size %2." ).arg( nmemb ).arg( size ) );
106  return nullptr;
107  }
108  void *p = qgsMalloc( nmemb * size );
109  if ( p )
110  {
111  memset( p, 0, nmemb * size );
112  }
113  return p;
114 }
115 
116 void qgsFree( void *ptr )
117 {
118  free( ptr );
119 }
120 
121 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
122 {
123  // invalid < NULL < any value
124  if ( !lhs.isValid() )
125  return rhs.isValid();
126  else if ( lhs.isNull() )
127  return rhs.isValid() && !rhs.isNull();
128  else if ( !rhs.isValid() || rhs.isNull() )
129  return false;
130 
131  switch ( lhs.type() )
132  {
133  case QVariant::Int:
134  return lhs.toInt() < rhs.toInt();
135  case QVariant::UInt:
136  return lhs.toUInt() < rhs.toUInt();
137  case QVariant::LongLong:
138  return lhs.toLongLong() < rhs.toLongLong();
139  case QVariant::ULongLong:
140  return lhs.toULongLong() < rhs.toULongLong();
141  case QVariant::Double:
142  return lhs.toDouble() < rhs.toDouble();
143  case QVariant::Char:
144  return lhs.toChar() < rhs.toChar();
145  case QVariant::Date:
146  return lhs.toDate() < rhs.toDate();
147  case QVariant::Time:
148  return lhs.toTime() < rhs.toTime();
149  case QVariant::DateTime:
150  return lhs.toDateTime() < rhs.toDateTime();
151  case QVariant::Bool:
152  return lhs.toBool() < rhs.toBool();
153 
154  case QVariant::List:
155  {
156  const QList<QVariant> &lhsl = lhs.toList();
157  const QList<QVariant> &rhsl = rhs.toList();
158 
159  int i, n = std::min( lhsl.size(), rhsl.size() );
160  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && qgsVariantEqual( lhsl[i], rhsl[i] ); i++ )
161  ;
162 
163  if ( i == n )
164  return lhsl.size() < rhsl.size();
165  else
166  return qgsVariantLessThan( lhsl[i], rhsl[i] );
167  }
168 
169  case QVariant::StringList:
170  {
171  const QStringList &lhsl = lhs.toStringList();
172  const QStringList &rhsl = rhs.toStringList();
173 
174  int i, n = std::min( lhsl.size(), rhsl.size() );
175  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
176  ;
177 
178  if ( i == n )
179  return lhsl.size() < rhsl.size();
180  else
181  return lhsl[i] < rhsl[i];
182  }
183 
184  default:
185  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
186  }
187 }
188 
189 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
190 {
191  return ! qgsVariantLessThan( lhs, rhs );
192 }
193 
194 QString qgsVsiPrefix( const QString &path )
195 {
196  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) )
197  return QStringLiteral( "/vsizip/" );
198  else if ( path.endsWith( QLatin1String( ".shp.zip" ), Qt::CaseInsensitive ) )
199  {
200  // GDAL 3.1 Shapefile driver directly handles .shp.zip files
201  if ( GDALIdentifyDriver( path.toUtf8().constData(), nullptr ) )
202  return QString();
203  return QStringLiteral( "/vsizip/" );
204  }
205  else if ( path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
206  return QStringLiteral( "/vsizip/" );
207  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
208  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
209  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
210  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
211  return QStringLiteral( "/vsitar/" );
212  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
213  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
214  return QStringLiteral( "/vsigzip/" );
215  else
216  return QString();
217 }
218 
219 uint qHash( const QVariant &variant )
220 {
221  if ( !variant.isValid() || variant.isNull() )
222  return std::numeric_limits<uint>::max();
223 
224  switch ( variant.type() )
225  {
226  case QVariant::Int:
227  return qHash( variant.toInt() );
228  case QVariant::UInt:
229  return qHash( variant.toUInt() );
230  case QVariant::Bool:
231  return qHash( variant.toBool() );
232  case QVariant::Double:
233  return qHash( variant.toDouble() );
234  case QVariant::LongLong:
235  return qHash( variant.toLongLong() );
236  case QVariant::ULongLong:
237  return qHash( variant.toULongLong() );
238  case QVariant::String:
239  return qHash( variant.toString() );
240  case QVariant::Char:
241  return qHash( variant.toChar() );
242  case QVariant::List:
243  return qHash( variant.toList() );
244  case QVariant::StringList:
245  return qHash( variant.toStringList() );
246  case QVariant::ByteArray:
247  return qHash( variant.toByteArray() );
248  case QVariant::Date:
249  return qHash( variant.toDate() );
250  case QVariant::Time:
251  return qHash( variant.toTime() );
252  case QVariant::DateTime:
253  return qHash( variant.toDateTime() );
254  case QVariant::Url:
255  case QVariant::Locale:
256  case QVariant::RegExp:
257  return qHash( variant.toString() );
258  default:
259  break;
260  }
261 
262  return std::numeric_limits<uint>::max();
263 }
264 
265 bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
266 {
267  return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
268 }
269 
271 {
272  return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
273  "1:10000,1:5000,1:2500,1:1000,1:500" );
274 }
275 
276 QString Qgis::version()
277 {
278  return QString::fromUtf8( VERSION );
279 }
280 
282 {
283  // Version number used for comparing versions using the
284  // "Check QGIS Version" function
285  return VERSION_INT;
286 }
287 
289 {
290  return QString::fromUtf8( RELEASE_NAME );
291 }
292 
294 {
295  return QString::fromUtf8( QGIS_DEV_VERSION );
296 }
297 
298 template<>
299 bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
300 {
301  // qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
302  // this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
303  return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
304 }
qgsPermissiveToDouble
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:65
qMapLessThanKey< QVariantList >
bool qMapLessThanKey< QVariantList >(const QVariantList &key1, const QVariantList &key2)
Compares two QVariantList values and returns whether the first is less than the second.
Definition: qgis.cpp:299
Qgis::version
static QString version()
Version string.
Definition: qgis.cpp:276
Qgis::DEFAULT_HIGHLIGHT_COLOR
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:148
Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:160
qgsCalloc
void * 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:101
qgsVariantLessThan
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:121
Qgis::DEFAULT_SNAP_TOLERANCE
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:188
Qgis::defaultProjectScales
static QString defaultProjectScales()
A string with default project scales.
Definition: qgis.cpp:270
qgis.h
qgsMalloc
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:86
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:140
qgsVariantGreaterThan
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:189
qgsPermissiveToInt
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:72
QgsTolerance::UnitType
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
qHash
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:219
QgsTolerance::Pixels
@ Pixels
Pixels unit of tolerance.
Definition: qgstolerance.h:46
qgsPermissiveToLongLong
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:79
Qgis::UI_SCALE_FACTOR
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:182
Qgis::DEFAULT_Z_COORDINATE
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:175
qgsVsiPrefix
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:194
Qgis::QGIS_DEV_VERSION
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:72
qgsVariantEqual
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:265
Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:154
qgsFree
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:116
Qgis::DEFAULT_SEARCH_RADIUS_MM
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:137
Qgis::SCALE_PRECISION
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:168
Qgis::releaseName
static QString releaseName()
Release name.
Definition: qgis.cpp:288
qgslogger.h
Qgis::DEFAULT_SNAP_UNITS
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:194
qgswkbtypes.h
Qgis::versionInt
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.cpp:281
Qgis::devVersion
static QString devVersion()
The development version.
Definition: qgis.cpp:293