QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#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 xstr(x) str(x)
39#define 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
67#ifdef Q_OS_WIN
68const double Qgis::UI_SCALE_FACTOR = 1.5;
69#else
70const double Qgis::UI_SCALE_FACTOR = 1;
71#endif
72
73double qgsPermissiveToDouble( QString string, bool &ok )
74{
75 //remove any thousands separators
76 string.remove( QLocale().groupSeparator() );
77 return QLocale().toDouble( string, &ok );
78}
79
80int qgsPermissiveToInt( QString string, bool &ok )
81{
82 //remove any thousands separators
83 string.remove( QLocale().groupSeparator() );
84 return QLocale().toInt( string, &ok );
85}
86
87qlonglong qgsPermissiveToLongLong( QString string, bool &ok )
88{
89 //remove any thousands separators
90 string.remove( QLocale().groupSeparator() );
91 return QLocale().toLongLong( string, &ok );
92}
93
94void *qgsMalloc( size_t size )
95{
96 if ( size == 0 )
97 {
98 QgsDebugError( QStringLiteral( "Zero size requested" ) );
99 return nullptr;
100 }
101
102 if ( ( size >> ( 8 * sizeof( size ) - 1 ) ) != 0 )
103 {
104 QgsDebugError( QStringLiteral( "qgsMalloc - bad size requested: %1" ).arg( size ) );
105 return nullptr;
106 }
107
108 void *p = malloc( size );
109 if ( !p )
110 {
111 QgsDebugError( QStringLiteral( "Allocation of %1 bytes failed." ).arg( size ) );
112 }
113 return p;
114}
115
116void qgsFree( void *ptr )
117{
118 free( ptr );
119}
120
121bool 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.userType() )
132 {
133 case QMetaType::Type::Int:
134 return lhs.toInt() < rhs.toInt();
135 case QMetaType::Type::UInt:
136 return lhs.toUInt() < rhs.toUInt();
137 case QMetaType::Type::LongLong:
138 return lhs.toLongLong() < rhs.toLongLong();
139 case QMetaType::Type::ULongLong:
140 return lhs.toULongLong() < rhs.toULongLong();
141 case QMetaType::Type::Double:
142 return lhs.toDouble() < rhs.toDouble();
143 case QMetaType::Type::QChar:
144 return lhs.toChar() < rhs.toChar();
145 case QMetaType::Type::QDate:
146 return lhs.toDate() < rhs.toDate();
147 case QMetaType::Type::QTime:
148 return lhs.toTime() < rhs.toTime();
149 case QMetaType::Type::QDateTime:
150 return lhs.toDateTime() < rhs.toDateTime();
151 case QMetaType::Type::Bool:
152 return lhs.toBool() < rhs.toBool();
153
154 case QMetaType::Type::QVariantList:
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].userType() == rhsl[i].userType() && 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 QMetaType::Type::QStringList:
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
189bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs )
190{
191 return ! qgsVariantLessThan( lhs, rhs );
192}
193
194QString qgsVsiPrefix( const QString &path )
195{
196 return QgsGdalUtils::vsiPrefixForPath( path );
197}
198
199uint qHash( const QVariant &variant )
200{
201 if ( !variant.isValid() || variant.isNull() )
202 return std::numeric_limits<uint>::max();
203
204 switch ( variant.userType() )
205 {
206 case QMetaType::Type::Int:
207 return qHash( variant.toInt() );
208 case QMetaType::Type::UInt:
209 return qHash( variant.toUInt() );
210 case QMetaType::Type::Bool:
211 return qHash( variant.toBool() );
212 case QMetaType::Type::Double:
213 return qHash( variant.toDouble() );
214 case QMetaType::Type::LongLong:
215 return qHash( variant.toLongLong() );
216 case QMetaType::Type::ULongLong:
217 return qHash( variant.toULongLong() );
218 case QMetaType::Type::QString:
219 return qHash( variant.toString() );
220 case QMetaType::Type::QChar:
221 return qHash( variant.toChar() );
222 case QMetaType::Type::QVariantList:
223 return qHash( variant.toList() );
224 case QMetaType::Type::QStringList:
225 return qHash( variant.toStringList() );
226 case QMetaType::Type::QByteArray:
227 return qHash( variant.toByteArray() );
228 case QMetaType::Type::QDate:
229 return qHash( variant.toDate() );
230 case QMetaType::Type::QTime:
231 return qHash( variant.toTime() );
232 case QMetaType::Type::QDateTime:
233 return qHash( variant.toDateTime() );
234 case QMetaType::Type::QUrl:
235 case QMetaType::Type::QLocale:
236 case QMetaType::Type::QRegularExpression:
237#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
238 case QMetaType::Type::QRegExp:
239#endif
240 return qHash( variant.toString() );
241 default:
242 break;
243 }
244
245 return std::numeric_limits<uint>::max();
246}
247
248bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs )
249{
250 return ( lhs.isNull() == rhs.isNull() && lhs == rhs ) || ( lhs.isNull() && rhs.isNull() && lhs.isValid() && rhs.isValid() );
251}
252
254{
255 return QStringLiteral( "1:1000000,1:500000,1:250000,1:100000,1:50000,1:25000,"
256 "1:10000,1:5000,1:2500,1:1000,1:500" );
257}
258
260{
261 return QString::fromUtf8( VERSION );
262}
263
265{
266 // Version number used for comparing versions using the
267 // "Check QGIS Version" function
268 return VERSION_INT;
269}
270
272{
273 return QString::fromUtf8( RELEASE_NAME );
274}
275
277{
278 return QString::fromUtf8( QGIS_DEV_VERSION );
279}
280
282{
283 return GEOSversion();
284}
285
287{
288 static const int version = QStringLiteral( "%1%2%3" )
289 .arg( GEOS_VERSION_MAJOR, 2, 10, QChar( '0' ) )
290 .arg( GEOS_VERSION_MINOR, 2, 10, QChar( '0' ) )
291 .arg( geosVersionPatch(), 2, 10, QChar( '0' ) ).toInt()
292 ;
293 return version;
294}
295
297{
298 return GEOS_VERSION_MAJOR;
299}
300
302{
303 return GEOS_VERSION_MINOR;
304}
305
307{
308 static const int version = atoi( xstr( GEOS_VERSION_PATCH ) );
309 return version;
310}
311
312#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
313template<>
314bool qMapLessThanKey<QVariantList>( const QVariantList &key1, const QVariantList &key2 )
315{
316 // qt's built in qMapLessThanKey for QVariantList is broken and does a case-insensitive operation.
317 // this breaks QMap< QVariantList, ... >, where key matching incorrectly becomes case-insensitive..!!?!
318 return qgsVariantGreaterThan( key1, key2 ) && key1 != key2;
319}
320#endif
321
static const Qgis::MapToolUnit DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition qgis.h:5637
MapToolUnit
Type of unit of tolerance value from settings.
Definition qgis.h:4637
@ Pixels
Pixels unit of tolerance.
static const double DEFAULT_HIGHLIGHT_MIN_WIDTH_MM
Default highlight line/stroke minimum width in mm.
Definition qgis.h:5599
static QString version()
Version string.
Definition qgis.cpp:259
static const double DEFAULT_Z_COORDINATE
Default Z coordinate value.
Definition qgis.h:5614
static const char * QGIS_DEV_VERSION
The development version.
Definition qgis.h:82
static QString geosVersion()
GEOS string version linked.
Definition qgis.cpp:281
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition qgis.h:5632
static const double DEFAULT_M_COORDINATE
Default M coordinate value.
Definition qgis.h:5621
static const double DEFAULT_HIGHLIGHT_BUFFER_MM
Default highlight buffer in mm.
Definition qgis.h:5594
static int geosVersionPatch()
GEOS Patch version number linked.
Definition qgis.cpp:306
static const QColor DEFAULT_HIGHLIGHT_COLOR
Default highlight color.
Definition qgis.h:5589
static Q_DECL_DEPRECATED const double SCALE_PRECISION
Fudge factor used to compare two scales.
Definition qgis.h:5608
static QString devVersion()
The development version.
Definition qgis.cpp:276
static QString releaseName()
Release name.
Definition qgis.cpp:271
static QString defaultProjectScales()
A string with default project scales.
Definition qgis.cpp:253
static const float DEFAULT_MAPTOPIXEL_THRESHOLD
Default threshold between map coordinates and device coordinates for map2pixel simplification.
Definition qgis.h:5582
static int geosVersionMajor()
GEOS Major version number linked.
Definition qgis.cpp:296
static const double DEFAULT_SEARCH_RADIUS_MM
Identify search radius in mm.
Definition qgis.h:5579
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
Definition qgis.cpp:264
static int geosVersionMinor()
GEOS Minor version number linked.
Definition qgis.cpp:301
static int geosVersionInt()
GEOS version number linked.
Definition qgis.cpp:286
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:5627
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:94
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:87
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:248
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:199
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:73
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:80
void qgsFree(void *ptr)
Frees the memory space pointed to by ptr.
Definition qgis.cpp:116
QString qgsVsiPrefix(const QString &path)
Definition qgis.cpp:194
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
#define xstr(x)
Definition qgis.cpp:38
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
#define QgsDebugError(str)
Definition qgslogger.h:38