QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 <geos_c.h>
34 #include <ogr_api.h>
35 
36 #define xstr(x) str(x)
37 #define str(x) #x
38 
39 // Version constants
40 //
41 
42 // development version
43 const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
44 
45 const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
46 
47 const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
48 
49 const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
50 
51 const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
52 
53 const double Qgis::DEFAULT_HIGHLIGHT_MIN_WIDTH_MM = 1.0;
54 
55 const double Qgis::SCALE_PRECISION = 0.9999999999;
56 
57 const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
58 
59 const double Qgis::DEFAULT_M_COORDINATE = 0.0;
60 
61 const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
62 
64 
65 #ifdef Q_OS_WIN
66 const double Qgis::UI_SCALE_FACTOR = 1.5;
67 #else
68 const double Qgis::UI_SCALE_FACTOR = 1;
69 #endif
70 
71 double qgsPermissiveToDouble( QString string, bool &ok )
72 {
73  //remove any thousands separators
74  string.remove( QLocale().groupSeparator() );
75  return QLocale().toDouble( string, &ok );
76 }
77 
78 int qgsPermissiveToInt( QString string, bool &ok )
79 {
80  //remove any thousands separators
81  string.remove( QLocale().groupSeparator() );
82  return QLocale().toInt( string, &ok );
83 }
84 
85 qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
86 {
87  //remove any thousands separators
88  string.remove( QLocale().groupSeparator() );
89  return QLocale().toLongLong( string, &ok );
90 }
91 
92 void *qgsMalloc( size_t size )
93 {
94  if ( size == 0 )
95  {
96  QgsDebugMsg( QStringLiteral( "Zero size requested" ) );
97  return nullptr;
98  }
99 
100  if ( ( size >> ( 8 * sizeof( size ) - 1 ) ) != 0 )
101  {
102  QgsDebugMsg( QStringLiteral( "qgsMalloc - bad size requested: %1" ).arg( size ) );
103  return nullptr;
104  }
105 
106  void *p = malloc( size );
107  if ( !p )
108  {
109  QgsDebugMsg( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
110  }
111  return p;
112 }
113 
114 void qgsFree( void *ptr )
115 {
116  free( ptr );
117 }
118 
119 bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
120 {
121  // invalid < NULL < any value
122  if ( !lhs.isValid() )
123  return rhs.isValid();
124  else if ( lhs.isNull() )
125  return rhs.isValid() && !rhs.isNull();
126  else if ( !rhs.isValid() || rhs.isNull() )
127  return false;
128 
129  switch ( lhs.type() )
130  {
131  case QVariant::Int:
132  return lhs.toInt() < rhs.toInt();
133  case QVariant::UInt:
134  return lhs.toUInt() < rhs.toUInt();
135  case QVariant::LongLong:
136  return lhs.toLongLong() < rhs.toLongLong();
137  case QVariant::ULongLong:
138  return lhs.toULongLong() < rhs.toULongLong();
139  case QVariant::Double:
140  return lhs.toDouble() < rhs.toDouble();
141  case QVariant::Char:
142  return lhs.toChar() < rhs.toChar();
143  case QVariant::Date:
144  return lhs.toDate() < rhs.toDate();
145  case QVariant::Time:
146  return lhs.toTime() < rhs.toTime();
147  case QVariant::DateTime:
148  return lhs.toDateTime() < rhs.toDateTime();
149  case QVariant::Bool:
150  return lhs.toBool() < rhs.toBool();
151 
152  case QVariant::List:
153  {
154  const QList<QVariant> &lhsl = lhs.toList();
155  const QList<QVariant> &rhsl = rhs.toList();
156 
157  int i, n = std::min( lhsl.size(), rhsl.size() );
158  for ( i = 0; i < n && lhsl[i].type() == rhsl[i].type() && qgsVariantEqual( lhsl[i], rhsl[i] ); i++ )
159  ;
160 
161  if ( i == n )
162  return lhsl.size() < rhsl.size();
163  else
164  return qgsVariantLessThan( lhsl[i], rhsl[i] );
165  }
166 
167  case QVariant::StringList:
168  {
169  const QStringList &lhsl = lhs.toStringList();
170  const QStringList &rhsl = rhs.toStringList();
171 
172  int i, n = std::min( lhsl.size(), rhsl.size() );
173  for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
174  ;
175 
176  if ( i == n )
177  return lhsl.size() < rhsl.size();
178  else
179  return lhsl[i] < rhsl[i];
180  }
181 
182  default:
183  return QString::localeAwareCompare( lhs.toString(), rhs.toString() ) < 0;
184  }
185 }
186 
187 bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
188 {
189  return ! qgsVariantLessThan( lhs, rhs );
190 }
191 
192 QString qgsVsiPrefix( const QString &path )
193 {
194  if ( path.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) )
195  return QStringLiteral( "/vsizip/" );
196  else if ( path.endsWith( QLatin1String( ".shp.zip" ), Qt::CaseInsensitive ) )
197  {
198  // GDAL 3.1 Shapefile driver directly handles .shp.zip files
199  if ( GDALIdentifyDriverEx( path.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr ) )
200  return QString();
201  return QStringLiteral( "/vsizip/" );
202  }
203  else if ( path.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
204  return QStringLiteral( "/vsizip/" );
205  else if ( path.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) ||
206  path.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
207  path.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
208  path.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
209  return QStringLiteral( "/vsitar/" );
210  else if ( path.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) ||
211  path.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
212  return QStringLiteral( "/vsigzip/" );
213  else
214  return QString();
215 }
216 
217 uint qHash( const QVariant &variant )
218 {
219  if ( !variant.isValid() || variant.isNull() )
220  return std::numeric_limits<uint>::max();
221 
222  switch ( variant.type() )
223  {
224  case QVariant::Int:
225  return qHash( variant.toInt() );
226  case QVariant::UInt:
227  return qHash( variant.toUInt() );
228  case QVariant::Bool:
229  return qHash( variant.toBool() );
230  case QVariant::Double:
231  return qHash( variant.toDouble() );
232  case QVariant::LongLong:
233  return qHash( variant.toLongLong() );
234  case QVariant::ULongLong:
235  return qHash( variant.toULongLong() );
236  case QVariant::String:
237  return qHash( variant.toString() );
238  case QVariant::Char:
239  return qHash( variant.toChar() );
240  case QVariant::List:
241  return qHash( variant.toList() );
242  case QVariant::StringList:
243  return qHash( variant.toStringList() );
244  case QVariant::ByteArray:
245  return qHash( variant.toByteArray() );
246  case QVariant::Date:
247  return qHash( variant.toDate() );
248  case QVariant::Time:
249  return qHash( variant.toTime() );
250  case QVariant::DateTime:
251  return qHash( variant.toDateTime() );
252  case QVariant::Url:
253  case QVariant::Locale:
254  case QVariant::RegularExpression:
255 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
256  case QVariant::RegExp:
257 #endif
258  return qHash( variant.toString() );
259  default:
260  break;
261  }
262 
263  return std::numeric_limits<uint>::max();
264 }
265 
266 bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
267 {
268  return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
269 }
270 
272 {
273  return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
274  "1:10000,1:5000,1:2500,1:1000,1:500" );
275 }
276 
277 QString Qgis::version()
278 {
279  return QString::fromUtf8( VERSION );
280 }
281 
283 {
284  // Version number used for comparing versions using the
285  // "Check QGIS Version" function
286  return VERSION_INT;
287 }
288 
290 {
291  return QString::fromUtf8( RELEASE_NAME );
292 }
293 
295 {
296  return QString::fromUtf8( QGIS_DEV_VERSION );
297 }
298 
300 {
301  return GEOSversion();
302 }
303 
305 {
306  static const int version = QStringLiteral( "%1%2%3" )
307  .arg( GEOS_VERSION_MAJOR, 2, 10, QChar( '0' ) )
308  .arg( GEOS_VERSION_MINOR, 2, 10, QChar( '0' ) )
309  .arg( geosVersionPatch(), 2, 10, QChar( '0' ) ).toInt()
310  ;
311  return version;
312 }
313 
315 {
316  return GEOS_VERSION_MAJOR;
317 }
318 
320 {
321  return GEOS_VERSION_MINOR;
322 }
323 
325 {
326  static const int version = atoi( xstr( GEOS_VERSION_PATCH ) );
327  return version;
328 }
329 
330 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
331 template<>
332 bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
333 {
334  // qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
335  // this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
336  return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
337 }
338 #endif
339 
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition: qgis.h:1351
static QString version()
Version string.
Definition: qgis.cpp:277
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value.
Definition: qgis.h:1366
static const char * QGIS_DEV_VERSION
The development version.
Definition: qgis.h:90
static QString geosVersion()
GEOS string version linked.
Definition: qgis.cpp:299
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:1386
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:1392
static const double DEFAULT_M_COORDINATE
Default M coordinate value.
Definition: qgis.h:1373
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition: qgis.h:1345
static int geosVersionPatch()
GEOS Patch version number linked.
Definition: qgis.cpp:324
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition: qgis.h:1339
static const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition: qgis.h:1359
static QString devVersion()
The development version.
Definition: qgis.cpp:294
static QString releaseName()
Release name.
Definition: qgis.cpp:289
static QString defaultProjectScales()
A string with default project scales.
Definition: qgis.cpp:271
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition: qgis.h:1331
static int geosVersionMajor()
GEOS Major version number linked.
Definition: qgis.cpp:314
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition: qgis.h:1328
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition: qgis.cpp:282
static int geosVersionMinor()
GEOS Minor version number linked.
Definition: qgis.cpp:319
static int geosVersionInt()
GEOS version number linked.
Definition: qgis.cpp:304
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:1380
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
@ Pixels
Pixels unit of tolerance.
Definition: qgstolerance.h:46
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:85
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:266
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:217
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:71
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:78
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition: qgis.cpp:114
QString qgsVsiPrefix(const QString &path)
Definition: qgis.cpp:192
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:119
#define xstr(x)
Definition: qgis.cpp:36
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition: qgis.cpp:92
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:187
#define QgsDebugMsg(str)
Definition: qgslogger.h:38