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