QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
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 : sherman@mrcc.com
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#include "moc_qgis.cpp"
20#ifndef QGSVERSION
21#include "qgsversion.h"
22#endif
23#include <QCoreApplication>
24#include <QColor>
25#include <QDate>
26#include <QTime>
27#include <QLocale>
28#include <QDateTime>
29#include "qgsconfig.h"
30#include "qgslogger.h"
31#include "qgsgdalutils.h"
32#include "qgswkbtypes.h"
33
34#include <gdal.h>
35#include <geos_c.h>
36#include <ogr_api.h>
37
38#define qgis_xstr(x) qgis_str(x)
39#define qgis_str(x) #x
40
41// Version constants
42//
43
44// development version
45const char *Qgis::QGIS_DEV_VERSION = QGSVERSION;
46
47const double Qgis::DEFAULT_SEARCH_RADIUS_MM = 2.;
48
49const float Qgis::DEFAULT_MAPTOPIXEL_THRESHOLD = 1.0f;
50
51const QColor Qgis::DEFAULT_HIGHLIGHT_COLOR = QColor( 255, 0, 0, 128 );
52
53const double Qgis::DEFAULT_HIGHLIGHT_BUFFER_MM = 0.5;
54
56
57const double Qgis::SCALE_PRECISION = 0.9999999999;
58
59const double Qgis::DEFAULT_Z_COORDINATE = 0.0;
60
61const double Qgis::DEFAULT_M_COORDINATE = 0.0;
62
63const double Qgis::DEFAULT_SNAP_TOLERANCE = 12.0;
64
66
67const int Qgis::USER_CRS_START_ID = 100000;
68const double Qgis::DEFAULT_POINT_SIZE = 2.0;
69const double Qgis::DEFAULT_LINE_WIDTH = 0.26;
70const double Qgis::DEFAULT_SEGMENT_EPSILON = 1e-8;
71
72const int Qgis::PREVIEW_JOB_DELAY_MS = 250;
74
75#ifdef Q_OS_WIN
76const double Qgis::UI_SCALE_FACTOR = 1.5;
77#else
78const double Qgis::UI_SCALE_FACTOR = 1;
79#endif
80
81double qgsPermissiveToDouble( QString string, bool &ok )
82{
83 //remove any thousands separators
84 string.remove( QLocale().groupSeparator() );
85 return QLocale().toDouble( string, &ok );
86}
87
88int qgsPermissiveToInt( QString string, bool &ok )
89{
90 //remove any thousands separators
91 string.remove( QLocale().groupSeparator() );
92 return QLocale().toInt( string, &ok );
93}
94
95qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
96{
97 //remove any thousands separators
98 string.remove( QLocale().groupSeparator() );
99 return QLocale().toLongLong( string, &ok );
100}
101
102void *qgsMalloc( size_t size )
103{
104 if ( size == 0 )
105 {
106 QgsDebugError( QStringLiteral( "Zero size requested" ) );
107 return nullptr;
108 }
109
110 if ( ( size >> ( 8 * sizeof( size ) - 1 ) ) != 0 )
111 {
112 QgsDebugError( QStringLiteral( "qgsMalloc - bad size requested: %1" ).arg( size ) );
113 return nullptr;
114 }
115
116 void *p = malloc( size );
117 if ( !p )
118 {
119 QgsDebugError( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
120 }
121 return p;
122}
123
124void qgsFree( void *ptr )
125{
126 free( ptr );
127}
128
129int qgsVariantCompare( const QVariant &lhs, const QVariant &rhs )
130{
131 // invalid < NULL < any value
132 if ( !lhs.isValid() )
133 {
134 return rhs.isValid() ? -1 : 0;
135 }
136 else if ( lhs.isNull() )
137 {
138 if ( !rhs.isValid() )
139 return 1;
140 if ( rhs.isNull() )
141 return 0;
142 return -1;
143 }
144 else if ( !rhs.isValid() || rhs.isNull() )
145 {
146 return 1;
147 }
148
149 // both valid
150 switch ( lhs.userType() )
151 {
152 case QMetaType::Type::Int:
153 case QMetaType::Type::Char:
154 case QMetaType::Type::Short:
155 {
156 const int lhsInt = lhs.toInt();
157 const int rhsInt = rhs.toInt();
158 return lhsInt < rhsInt ? -1 : ( lhsInt == rhsInt ? 0 : 1 );
159 }
160 case QMetaType::Type::UInt:
161 case QMetaType::Type::UChar:
162 case QMetaType::Type::UShort:
163 {
164 const uint lhsUInt = lhs.toUInt();
165 const uint rhsUInt = rhs.toUInt();
166 return lhsUInt < rhsUInt ? -1 : ( lhsUInt == rhsUInt ? 0 : 1 );
167 }
168 case QMetaType::Type::LongLong:
169 case QMetaType::Type::Long:
170 {
171 const qlonglong lhsLongLong = lhs.toLongLong();
172 const qlonglong rhsLongLong = rhs.toLongLong();
173 return lhsLongLong < rhsLongLong ? -1 : ( lhsLongLong == rhsLongLong ? 0 : 1 );
174 }
175 case QMetaType::Type::ULongLong:
176 case QMetaType::Type::ULong:
177 {
178 const qulonglong lhsULongLong = lhs.toULongLong();
179 const qulonglong rhsULongLong = rhs.toULongLong();
180 return lhsULongLong < rhsULongLong ? -1 : ( lhsULongLong == rhsULongLong ? 0 : 1 );
181 }
182 case QMetaType::Type::Double:
183 {
184 const double lhsDouble = lhs.toDouble();
185 const double rhsDouble = rhs.toDouble();
186
187 // consider NaN < any non-NaN
188 const bool lhsIsNan = std::isnan( lhsDouble );
189 const bool rhsIsNan = std::isnan( rhsDouble );
190 if ( lhsIsNan )
191 {
192 return rhsIsNan ? 0 : -1;
193 }
194 else if ( rhsIsNan )
195 {
196 return 1;
197 }
198
199 return lhsDouble < rhsDouble ? -1 : ( lhsDouble == rhsDouble ? 0 : 1 );
200 }
201 case QMetaType::Type::Float:
202 {
203 const float lhsFloat = lhs.toFloat();
204 const float rhsFloat = rhs.toFloat();
205
206 // consider NaN < any non-NaN
207 const bool lhsIsNan = std::isnan( lhsFloat );
208 const bool rhsIsNan = std::isnan( rhsFloat );
209 if ( lhsIsNan )
210 {
211 return rhsIsNan ? 0 : -1;
212 }
213 else if ( rhsIsNan )
214 {
215 return 1;
216 }
217
218 return lhsFloat < rhsFloat ? -1 : ( lhsFloat == rhsFloat ? 0 : 1 );
219 }
220 case QMetaType::Type::QChar:
221 {
222 const QChar lhsChar = lhs.toChar();
223 const QChar rhsChar = rhs.toChar();
224 return lhsChar < rhsChar ? -1 : ( lhsChar == rhsChar ? 0 : 1 );
225 }
226 case QMetaType::Type::QDate:
227 {
228 const QDate lhsDate = lhs.toDate();
229 const QDate rhsDate = rhs.toDate();
230 return lhsDate < rhsDate ? -1 : ( lhsDate == rhsDate ? 0 : 1 );
231 }
232 case QMetaType::Type::QTime:
233 {
234 const QTime lhsTime = lhs.toTime();
235 const QTime rhsTime = rhs.toTime();
236 return lhsTime < rhsTime ? -1 : ( lhsTime == rhsTime ? 0 : 1 );
237 }
238 case QMetaType::Type::QDateTime:
239 {
240 const QDateTime lhsTime = lhs.toDateTime();
241 const QDateTime rhsTime = rhs.toDateTime();
242 return lhsTime < rhsTime ? -1 : ( lhsTime == rhsTime ? 0 : 1 );
243 }
244 case QMetaType::Type::Bool:
245 {
246 const bool lhsBool = lhs.toBool();
247 const bool rhsBool = rhs.toBool();
248 return lhsBool == rhsBool ? 0 : ( lhsBool ? 1 : -1 );
249 }
250
251 case QMetaType::Type::QVariantList:
252 {
253 const QList<QVariant> &lhsl = lhs.toList();
254 const QList<QVariant> &rhsl = rhs.toList();
255
256 int i, n = std::min( lhsl.size(), rhsl.size() );
257 for ( i = 0; i < n && lhsl[i].userType() == rhsl[i].userType() && qgsVariantCompare( lhsl[i], rhsl[i] ) == 0; i++ )
258 ;
259
260 if ( i == n )
261 return lhsl.size() < rhsl.size() ? -1 : ( lhsl.size() > rhsl.size() ? 1 : 0 );
262 else
263 return qgsVariantCompare( lhsl[i], rhsl[i] );
264 }
265
266 case QMetaType::Type::QStringList:
267 {
268 const QStringList &lhsl = lhs.toStringList();
269 const QStringList &rhsl = rhs.toStringList();
270
271 int i, n = std::min( lhsl.size(), rhsl.size() );
272 for ( i = 0; i < n && lhsl[i] == rhsl[i]; i++ )
273 ;
274
275 if ( i == n )
276 return lhsl.size() < rhsl.size() ? -1 : ( lhsl.size() > rhsl.size() ? 1 : 0 );
277 else
278 return lhsl[i] < rhsl[i] ? -1 : ( lhsl[i] == rhsl[i] ? 0 : 1 );
279 }
280
281 default:
282 return QString::localeAwareCompare( lhs.toString(), rhs.toString() );
283 }
284}
285
286bool qgsVariantLessThan( const QVariant &lhs, const QVariant &rhs )
287{
288 return qgsVariantCompare( lhs, rhs ) < 0;
289}
290
291bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
292{
293 return qgsVariantCompare( lhs, rhs ) > 0;
294}
295
296
297QString qgsVsiPrefix( const QString &path )
298{
299 return QgsGdalUtils::vsiPrefixForPath( path );
300}
301
302uint qHash( const QVariant &variant )
303{
304 if ( !variant.isValid() || variant.isNull() )
305 return std::numeric_limits<uint>::max();
306
307 switch ( variant.userType() )
308 {
309 case QMetaType::Type::Int:
310 return qHash( variant.toInt() );
311 case QMetaType::Type::UInt:
312 return qHash( variant.toUInt() );
313 case QMetaType::Type::Bool:
314 return qHash( variant.toBool() );
315 case QMetaType::Type::Double:
316 return qHash( variant.toDouble() );
317 case QMetaType::Type::LongLong:
318 return qHash( variant.toLongLong() );
319 case QMetaType::Type::ULongLong:
320 return qHash( variant.toULongLong() );
321 case QMetaType::Type::QString:
322 return qHash( variant.toString() );
323 case QMetaType::Type::QChar:
324 return qHash( variant.toChar() );
325 case QMetaType::Type::QVariantList:
326 return qHash( variant.toList() );
327 case QMetaType::Type::QStringList:
328 return qHash( variant.toStringList() );
329 case QMetaType::Type::QByteArray:
330 return qHash( variant.toByteArray() );
331 case QMetaType::Type::QDate:
332 return qHash( variant.toDate() );
333 case QMetaType::Type::QTime:
334 return qHash( variant.toTime() );
335 case QMetaType::Type::QDateTime:
336 return qHash( variant.toDateTime() );
337 case QMetaType::Type::QUrl:
338 case QMetaType::Type::QLocale:
339 case QMetaType::Type::QRegularExpression:
340#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
341 case QMetaType::Type::QRegExp:
342#endif
343 return qHash( variant.toString() );
344 default:
345 break;
346 }
347
348 return std::numeric_limits<uint>::max();
349}
350
351bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
352{
353 return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
354}
355
357{
358 return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
359 "1:10000,1:5000,1:2500,1:1000,1:500" );
360}
361
363{
364 return QString::fromUtf8( VERSION );
365}
366
368{
369 // Version number used for comparing versions using the
370 // "Check QGIS Version" function
371 return VERSION_INT;
372}
373
375{
376 return QString::fromUtf8( RELEASE_NAME );
377}
378
380{
381 return QString::fromUtf8( QGIS_DEV_VERSION );
382}
383
385{
386 return GEOSversion();
387}
388
390{
391 static const int version = QStringLiteral( "%1%2%3" )
392 .arg( GEOS_VERSION_MAJOR, 2, 10, QChar( '0' ) )
393 .arg( GEOS_VERSION_MINOR, 2, 10, QChar( '0' ) )
394 .arg( geosVersionPatch(), 2, 10, QChar( '0' ) ).toInt()
395 ;
396 return version;
397}
398
400{
401 return GEOS_VERSION_MAJOR;
402}
403
405{
406 return GEOS_VERSION_MINOR;
407}
408
410{
411 static const int version = atoi( qgis_xstr( GEOS_VERSION_PATCH ) );
412 return version;
413}
414
415#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
416template<>
417bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
418{
419 // qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
420 // this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
421 return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
422}
423#endif
424
static const Qgis::MapToolUnit DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition qgis.h:5958
MapToolUnit
Type of unit of tolerance value from settings.
Definition qgis.h:4827
@ Pixels
Pixels unit of tolerance.
static const double DEFAULT_LINE_WIDTH
The default width (in millimeters) for line symbols.
Definition qgis.h:5969
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition qgis.h:5920
static QString version()
Version string.
Definition qgis.cpp:362
static const int PREVIEW_JOB_DELAY_MS
Delay between the scheduling of 2 preview jobs.
Definition qgis.h:5975
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value.
Definition qgis.h:5935
static const char * QGIS_DEV_VERSION
The development version.
Definition qgis.h:82
static QString geosVersion()
GEOS string version linked.
Definition qgis.cpp:384
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition qgis.h:5953
static const double DEFAULT_M_COORDINATE
Default M coordinate value.
Definition qgis.h:5942
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition qgis.h:5915
static int geosVersionPatch()
GEOS Patch version number linked.
Definition qgis.cpp:409
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition qgis.h:5910
static Q_DECL_DEPRECATED const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition qgis.h:5929
static const int MAXIMUM_LAYER_PREVIEW_TIME_MS
Maximum rendering time for a layer of a preview job.
Definition qgis.h:5978
static QString devVersion()
The development version.
Definition qgis.cpp:379
static QString releaseName()
Release name.
Definition qgis.cpp:374
static QString defaultProjectScales()
A string with default project scales.
Definition qgis.cpp:356
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition qgis.h:5903
static int geosVersionMajor()
GEOS Major version number linked.
Definition qgis.cpp:399
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:5900
static const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:5972
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition qgis.cpp:367
static const int USER_CRS_START_ID
Minimum ID number for a user-defined projection.
Definition qgis.h:5963
static int geosVersionMinor()
GEOS Minor version number linked.
Definition qgis.cpp:404
static int geosVersionInt()
GEOS version number linked.
Definition qgis.cpp:389
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:5948
static const double DEFAULT_POINT_SIZE
The default size (in millimeters) for point marker symbols.
Definition qgis.h:5966
static QString vsiPrefixForPath(const QString &path)
Returns a the vsi prefix which corresponds to a file path, or an empty string if the path is not asso...
#define qgis_xstr(x)
Definition qgis.cpp:38
void * qgsMalloc(size_t size)
Allocates size bytes and returns a pointer to the allocated memory.
Definition qgis.cpp:102
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:95
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:351
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:302
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:81
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:88
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition qgis.cpp:124
QString qgsVsiPrefix(const QString &path)
Returns a the vsi prefix which corresponds to a file path, or an empty string if the path is not asso...
Definition qgis.cpp:297
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:286
int qgsVariantCompare(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values.
Definition qgis.cpp:129
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:291
#define QgsDebugError(str)
Definition qgslogger.h:40